-
-
Notifications
You must be signed in to change notification settings - Fork 757
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
Refactored src/components/ChangeLanguageDropdown/ChangeLanguageDropdown.test.tsx from Jest to Vitest #2588
base: develop-postgres
Are you sure you want to change the base?
Refactored src/components/ChangeLanguageDropdown/ChangeLanguageDropdown.test.tsx from Jest to Vitest #2588
Changes from all commits
d3243f6
1b6d7a6
76c76b5
9ad3b98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,11 @@ | ||
import React, { act } from 'react'; | ||
/** | ||
* Unit tests for the ChangeLanguageDropdown component. | ||
* | ||
* This file contains tests for the ChangeLanguageDropdown component to ensure it behaves as expected | ||
* under various scenarios. | ||
*/ | ||
import React from 'react'; | ||
import { describe, test, expect } from 'vitest'; | ||
import { render } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { I18nextProvider } from 'react-i18next'; | ||
|
@@ -16,10 +23,8 @@ import useLocalStorage from 'utils/useLocalstorage'; | |
const { setItem } = useLocalStorage(); | ||
|
||
async function wait(ms = 100): Promise<void> { | ||
await act(() => { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
}); | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
}); | ||
} | ||
|
||
|
@@ -51,6 +56,7 @@ const MOCKS = [ | |
]; | ||
|
||
const link = new StaticMockLink(MOCKS, true); | ||
|
||
describe('Testing Change Language Dropdown', () => { | ||
test('Component Should be rendered properly', async () => { | ||
const { getByTestId } = render( | ||
|
@@ -71,7 +77,7 @@ describe('Testing Change Language Dropdown', () => { | |
getByTestId('language-dropdown-btn').className.includes(''); | ||
getByTestId('dropdown-btn-0').className.includes(''); | ||
|
||
userEvent.click(getByTestId('dropdown-btn-0')); | ||
await userEvent.click(getByTestId('dropdown-btn-0')); | ||
await wait(); | ||
|
||
languages.map((language) => { | ||
|
@@ -136,23 +142,23 @@ describe('Testing Change Language Dropdown', () => { | |
</MockedProvider>, | ||
); | ||
|
||
userEvent.click(getByTestId('language-dropdown-btn')); | ||
await userEvent.click(getByTestId('language-dropdown-btn')); | ||
await wait(); | ||
const changeLanguageBtn = getByTestId(`change-language-btn-fr`); | ||
await wait(); | ||
expect(changeLanguageBtn).toBeInTheDocument(); | ||
await wait(); | ||
userEvent.click(changeLanguageBtn); | ||
await userEvent.click(changeLanguageBtn); | ||
await wait(); | ||
expect(cookies.get('i18next')).toBe('fr'); | ||
await wait(); | ||
userEvent.click(getByTestId('language-dropdown-btn')); | ||
await userEvent.click(getByTestId('language-dropdown-btn')); | ||
await wait(); | ||
const changeLanguageBtnHi = getByTestId(`change-language-btn-hi`); | ||
await wait(); | ||
expect(changeLanguageBtnHi).toBeInTheDocument(); | ||
await wait(); | ||
userEvent.click(changeLanguageBtnHi); | ||
await userEvent.click(changeLanguageBtnHi); | ||
await wait(); | ||
}); | ||
Comment on lines
+155
to
162
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add assertions for error handling The test case for the Hindi language selection triggers an error (as per the mock), but doesn't verify how the error is handled. Consider adding assertions to validate the error state. await userEvent.click(getByTestId('language-dropdown-btn'));
- await wait();
const changeLanguageBtnHi = getByTestId(`change-language-btn-hi`);
- await wait();
expect(changeLanguageBtnHi).toBeInTheDocument();
- await wait();
await userEvent.click(changeLanguageBtnHi);
- await wait();
+ await wait(200);
+ // Add assertions for error handling
+ expect(cookies.get('i18next')).not.toBe('hi');
+ // Verify error message is displayed (add appropriate test ID)
+ expect(getByTestId('error-message')).toBeInTheDocument();
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello reviewer this changes are outside the scope of this issue, Do you still want me to add these changes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bitbard3, understood. Since these changes are outside the scope of this issue, it's acceptable to leave them as is. Would you like me to open a new GitHub issue to track adding these error handling assertions in the future? 🧠 Learnings used
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error handling has not been implemented in the component, so the test case does not cover error scenarios There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bitbard3, understood. Thank you for the clarification. Since error handling is not implemented in the component, it's acceptable that the test case does not cover error scenarios. ✏️ Learnings added
🧠 Learnings used
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here too.... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}); |
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.
🛠️ Refactor suggestion
Optimize test structure by reducing unnecessary waits
The current implementation has multiple consecutive
wait()
calls that could make the tests flaky and slower than necessary. Consider consolidating the waits and using more reliable ways to wait for state changes.📝 Committable suggestion
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.
Not relevant to this issue, This PR only refactors the test case to vitest
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.
Understood @bitbard3, thank you for the clarification. I'll keep that in mind for future reviews.
✏️ Learnings added
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.
@bitbard3 Multiple wait calls make redundancy. Please apply the coderabbitai changes
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.