Skip to content
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

Fixes #2640 refactor src/screens/UserPortal/Campaigns #2643

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,57 @@ import i18nForTest from 'utils/i18nForTest';
import type { ApolloLink } from '@apollo/client';
import useLocalStorage from 'utils/useLocalstorage';
import Campaigns from './Campaigns';
import { vi, it, expect, describe } from 'vitest';
import {
EMPTY_MOCKS,
MOCKS,
USER_FUND_CAMPAIGNS_ERROR,
} from './CampaignsMocks';

jest.mock('react-toastify', () => ({
/* Mocking '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', () => {

/* Mocking `@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();

/**
* Creates a mocked Apollo link for testing.
*/
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),
);

/*
* Renders the `Campaigns` component for testing.
*
* @param link - The mocked Apollo link used for testing.
* @returns The rendered result of the `Campaigns` component.
*/

const renderCampaigns = (link: ApolloLink): RenderResult => {
return render(
<MockedProvider addTypename={false} link={link}>
Expand All @@ -79,26 +97,38 @@ const renderCampaigns = (link: ApolloLink): RenderResult => {
);
};

/**
* Test suite for the User Campaigns screen.
*/
describe('Testing User Campaigns Screen', () => {
beforeEach(() => {
setItem('userId', 'userId');
});

beforeAll(() => {
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({ orgId: 'orgId' }),
}));
/**
* Mocks the `useParams` function from `react-router-dom` to simulate URL parameters.
*/
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(() => {
cleanup();
});

/**
* Verifies that the User Campaigns screen renders correctly with mock data.
*/
it('should render the User Campaigns screen', async () => {
renderCampaigns(link1);
await waitFor(() => {
Expand All @@ -108,6 +138,9 @@ describe('Testing User Campaigns Screen', () => {
});
});

/**
* Ensures the app redirects to the fallback URL if `userId` is null in LocalStorage.
*/
it('should redirect to fallback URL if userId is null in LocalStorage', async () => {
setItem('userId', null);
renderCampaigns(link1);
Expand All @@ -116,7 +149,11 @@ describe('Testing User Campaigns Screen', () => {
});
});

/**
* Ensures the app redirects to the fallback URL if URL parameters are undefined.
*/
it('should redirect to fallback URL if URL params are undefined', async () => {
vi.unmock('react-router-dom'); // unmocking to get real behavior from useParams
render(
<MockedProvider addTypename={false} link={link1}>
<MemoryRouter initialEntries={['/user/campaigns/']}>
Expand Down
Loading