-
Notifications
You must be signed in to change notification settings - Fork 575
test: migrate FilteredList tests from Enzyme to React Testing Library #2875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Vishvamsinh Vaghela <[email protected]>
How to use the Graphite Merge QueueAdd the label merge-queue to this PR to add it to the merge queue. You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. |
1 similar comment
How to use the Graphite Merge QueueAdd the label merge-queue to this PR to add it to the merge queue. You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. |
@yurishkuro This test file currently has around 25 test cases. I can reduce it to 10–15 tests while maintaining 100% coverage, if it feels too long or overly detailed. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2875 +/- ##
=======================================
Coverage 97.56% 97.56%
=======================================
Files 256 256
Lines 7975 7975
Branches 2086 2011 -75
=======================================
Hits 7781 7781
Misses 194 194 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
it('clears focused item on scroll', async () => { | ||
jest.useFakeTimers(); | ||
|
||
it('enter selects the current focus index', () => { | ||
const focusedIndex = 0; | ||
expect(props.setValue.mock.calls.length).toBe(0); | ||
wrapper.setState({ focusedIndex }); | ||
keyDown(EKey.Enter); | ||
expect(props.setValue.mock.calls).toEqual([[props.options[focusedIndex]]]); | ||
}); | ||
render(<FilteredList {...props} />); | ||
|
||
it('enter selects the filteredOption if there is only one option', () => { | ||
const value = words[1]; | ||
wrapper.find('input').simulate('change', { target: { value } }); | ||
expect(props.setValue.mock.calls.length).toBe(0); | ||
keyDown(EKey.Enter); | ||
expect(props.setValue.mock.calls).toEqual([[value]]); | ||
}); | ||
const input = getFilterInput(); | ||
input.focus(); | ||
|
||
fireEvent.keyDown(input, { key: EKey.ArrowDown }); | ||
|
||
await waitFor(() => { | ||
expect(getListItems()[0]).toHaveAttribute('data-focused', 'true'); | ||
}); | ||
|
||
it('enter is ignored when an item is not focused', () => { | ||
expect(props.setValue.mock.calls.length).toBe(0); | ||
keyDown(EKey.Enter); | ||
expect(props.setValue.mock.calls.length).toBe(0); | ||
fireEvent.scroll(getVirtualList()); | ||
jest.runAllTimers(); | ||
|
||
await waitFor(() => { | ||
const items = getListItems(); | ||
items.forEach(item => { | ||
expect(item).toHaveAttribute('data-focused', 'false'); | ||
}); | ||
}); | ||
|
||
jest.useRealTimers(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The timer cleanup in this test should be moved to ensure it always runs, even if the test fails. Consider adding jest.useRealTimers()
in an afterEach
block or wrapping the test body in a try/finally:
it('clears focused item on scroll', async () => {
jest.useFakeTimers();
try {
// Test implementation...
} finally {
jest.useRealTimers();
}
});
This prevents timer mocking from leaking into subsequent tests, which could cause unpredictable behavior in the test suite.
it('clears focused item on scroll', async () => { | |
jest.useFakeTimers(); | |
it('enter selects the current focus index', () => { | |
const focusedIndex = 0; | |
expect(props.setValue.mock.calls.length).toBe(0); | |
wrapper.setState({ focusedIndex }); | |
keyDown(EKey.Enter); | |
expect(props.setValue.mock.calls).toEqual([[props.options[focusedIndex]]]); | |
}); | |
render(<FilteredList {...props} />); | |
it('enter selects the filteredOption if there is only one option', () => { | |
const value = words[1]; | |
wrapper.find('input').simulate('change', { target: { value } }); | |
expect(props.setValue.mock.calls.length).toBe(0); | |
keyDown(EKey.Enter); | |
expect(props.setValue.mock.calls).toEqual([[value]]); | |
}); | |
const input = getFilterInput(); | |
input.focus(); | |
fireEvent.keyDown(input, { key: EKey.ArrowDown }); | |
await waitFor(() => { | |
expect(getListItems()[0]).toHaveAttribute('data-focused', 'true'); | |
}); | |
it('enter is ignored when an item is not focused', () => { | |
expect(props.setValue.mock.calls.length).toBe(0); | |
keyDown(EKey.Enter); | |
expect(props.setValue.mock.calls.length).toBe(0); | |
fireEvent.scroll(getVirtualList()); | |
jest.runAllTimers(); | |
await waitFor(() => { | |
const items = getListItems(); | |
items.forEach(item => { | |
expect(item).toHaveAttribute('data-focused', 'false'); | |
}); | |
}); | |
jest.useRealTimers(); | |
it('clears focused item on scroll', async () => { | |
jest.useFakeTimers(); | |
try { | |
render(<FilteredList {...props} />); | |
const input = getFilterInput(); | |
input.focus(); | |
fireEvent.keyDown(input, { key: EKey.ArrowDown }); | |
await waitFor(() => { | |
expect(getListItems()[0]).toHaveAttribute('data-focused', 'true'); | |
}); | |
fireEvent.scroll(getVirtualList()); | |
jest.runAllTimers(); | |
await waitFor(() => { | |
const items = getListItems(); | |
items.forEach(item => { | |
expect(item).toHaveAttribute('data-focused', 'false'); | |
}); | |
}); | |
} finally { | |
jest.useRealTimers(); | |
} | |
}); |
Spotted by Diamond
Is this helpful? React 👍 or 👎 to let us know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense
Signed-off-by: Vishvamsinh Vaghela <[email protected]>
@yurishkuro please review |
Rewrote all tests for using React Testing Library. Improved test structure, assertions, and removed legacy Enzyme usage.