Skip to content

Commit

Permalink
Refactored: src/screens/UserPortal/Campaigns from Jest to Vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
rafidoth committed Dec 11, 2024
1 parent 0630cff commit c9b2ead
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,34 @@ import {
USER_FUND_CAMPAIGNS_ERROR,
} from './CampaignsMocks';

jest.mock('react-toastify', () => ({
vi.mock('react-toastify', () => ({
toast: {
success: jest.fn(),
error: jest.fn(),
success: vi.fn(),
error: vi.fn(),
},
}));
jest.mock('@mui/x-date-pickers/DateTimePicker', () => {

vi.mock('@mui/x-date-pickers/DateTimePicker', async () => {
const actual = await vi.importActual(
'@mui/x-date-pickers/DesktopDateTimePicker',
);
return {
DateTimePicker: jest.requireActual(
'@mui/x-date-pickers/DesktopDateTimePicker',
).DesktopDateTimePicker,
DateTimePicker: actual.DesktopDateTimePicker,
};
});

const { setItem } = useLocalStorage();

const link1 = new StaticMockLink(MOCKS);
const link2 = new StaticMockLink(USER_FUND_CAMPAIGNS_ERROR);
const link3 = new StaticMockLink(EMPTY_MOCKS);

const cTranslations = JSON.parse(
JSON.stringify(
i18nForTest.getDataByLanguage('en')?.translation.userCampaigns,
),
);

const pTranslations = JSON.parse(
JSON.stringify(i18nForTest.getDataByLanguage('en')?.translation.pledges),
);
Expand Down Expand Up @@ -85,14 +90,17 @@ describe('Testing User Campaigns Screen', () => {
});

beforeAll(() => {
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({ orgId: 'orgId' }),
}));
vi.mock('react-router-dom', async () => {
const actual = await vi.importActual('react-router-dom');
return {
...actual,
useParams: vi.fn(() => ({ orgId: 'orgId' })), // Mock `useParams`
};
});
});

afterAll(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

afterEach(() => {
Expand All @@ -117,16 +125,19 @@ describe('Testing User Campaigns Screen', () => {
});

it('should redirect to fallback URL if URL params are undefined', async () => {
vi.unmock('react-router-dom');
render(
<MockedProvider addTypename={false} link={link1}>
<MemoryRouter initialEntries={['/user/campaigns/']}>
<MemoryRouter initialEntries={['/user/campaigns']}>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<Routes>
<Route path="/user/campaigns/" element={<Campaigns />} />
<Route path="/user/campaigns" element={<Campaigns />} />
<Route
path="/"
element={<div data-testid="paramsError"></div>}
element={
<div data-testid="paramsError">Error : orgId not found</div>
}
/>
</Routes>
</I18nextProvider>
Expand Down
7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{
"compilerOptions": {
"types": ["vite/client", "vite-plugin-svgr/client", "node"],
"types": [
"vite/client",
"vite-plugin-svgr/client",
"node",
"vitest/globals"
],
"baseUrl": "src",
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
Expand Down

0 comments on commit c9b2ead

Please sign in to comment.