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

Create tests for OrganizationDashboard.tsx #1549

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
62 changes: 61 additions & 1 deletion src/screens/OrganizationDashboard/OrganizationDashboard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import { BrowserRouter } from 'react-router-dom';
import { store } from 'state/store';
import { StaticMockLink } from 'utils/StaticMockLink';
import OrganizationDashboard from './OrganizationDashboard';
import { EMPTY_MOCKS, ERROR_MOCKS, MOCKS } from './OrganizationDashboardMocks';
import {
EMPTY_MOCKS,
ERROR_MOCKS,
MOCKS,
linkMocked,
mockEventData,
} from './OrganizationDashboardMocks';
import i18nForTest from 'utils/i18nForTest';
import { toast } from 'react-toastify';
import userEvent from '@testing-library/user-event';
Expand Down Expand Up @@ -147,4 +153,58 @@ describe('Organisation Dashboard Page', () => {
await wait();
expect(window.location).toBeAt('/orglist');
});

test('Testing useEffect hook and error redirection', async () => {
// Render the component with mock data
await act(async () => {
render(
<MockedProvider addTypename={false} link={linkMocked}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<OrganizationDashboard />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);
});

// Wait for useEffect to be executed
await wait();

// Check if the hook updates the state with upcoming events
expect(screen.getByText('Upcoming Events')).toBeInTheDocument();
expect(screen.getByText('Latest Posts')).toBeInTheDocument();

// Check if tempUpcomingEvents logic is executed correctly
const startDate = new Date(
mockEventData.eventsByOrganizationConnection[0].startDate
);
const now = new Date();
const tempUpcomingEvents = [];
if (startDate > now) {
tempUpcomingEvents.push(mockEventData.eventsByOrganizationConnection[0]);
}
expect(tempUpcomingEvents).toHaveLength(0);

// Test conditional redirection when there's an error
await act(async () => {
render(
<MockedProvider addTypename={false} link={link3}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<OrganizationDashboard />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);
});

await wait();

expect(window.location.pathname).toBe('/orglist');
});
});
21 changes: 21 additions & 0 deletions src/screens/OrganizationDashboard/OrganizationDashboardMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ORGANIZATION_POST_CONNECTION_LIST,
} from 'GraphQl/Queries/Queries';
import dayjs from 'dayjs';
import { StaticMockLink } from 'utils/StaticMockLink';

export const MOCKS = [
{
Expand Down Expand Up @@ -298,3 +299,23 @@ export const ERROR_MOCKS = [
error: new Error('Mock Graphql ORGANIZATION_EVENT_LIST Error'),
},
];

export const mockEventData = {
Sauradip07 marked this conversation as resolved.
Show resolved Hide resolved
eventsByOrganizationConnection: [
{ startDate: new Date().toISOString() }, // Assuming an event is upcoming
],
};

// Mocking the response for the organization event query
export const linkMocked = new StaticMockLink(
[
{
request: {
query: ORGANIZATION_EVENT_CONNECTION_LIST,
variables: { organization_id: 'your_organization_id' },
},
result: { data: mockEventData },
},
],
true
);
Sauradip07 marked this conversation as resolved.
Show resolved Hide resolved
Loading