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 #1577

Closed
Closed
Show file tree
Hide file tree
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
59 changes: 59 additions & 0 deletions src/screens/OrganizationDashboard/OrganizationDashboard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,63 @@ describe('Organisation Dashboard Page', () => {
await wait();
expect(window.location).toBeAt('/orglist');
});
test('Testing useEffect hook and error redirection', async () => {
// eslint-disable-next-line jest/no-export
const mockEventData = {
eventsByOrganizationConnection: [
{ startDate: new Date().toISOString() }, // Assuming an event is upcoming
],
};
// Render the component with mock data
await act(async () => {
render(
<MockedProvider addTypename={false} link={link1}>
<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');
});
});
13 changes: 13 additions & 0 deletions src/screens/OrganizationDashboard/OrganizationDashboardMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ export const MOCKS = [
},
},
},
{
request: {
query: ORGANIZATION_EVENT_CONNECTION_LIST,
variables: { organization_id: 'your_organization_id' },
},
result: {
data: {
eventsByOrganizationConnection: [
{ startDate: new Date().toISOString() }, // Assuming an event is upcoming
],
},
},
},
{
request: {
query: ORGANIZATION_POST_CONNECTION_LIST,
Expand Down
Loading