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
Changes from 1 commit
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
75 changes: 75 additions & 0 deletions src/screens/OrganizationDashboard/OrganizationDashboard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { EMPTY_MOCKS, ERROR_MOCKS, MOCKS } from './OrganizationDashboardMocks';
import i18nForTest from 'utils/i18nForTest';
import { toast } from 'react-toastify';
import userEvent from '@testing-library/user-event';
import { ORGANIZATION_EVENT_CONNECTION_LIST } from 'GraphQl/Queries/Queries';

async function wait(ms = 100): Promise<void> {
await act(() => {
Expand Down Expand Up @@ -147,4 +148,78 @@ describe('Organisation Dashboard Page', () => {
await wait();
expect(window.location).toBeAt('/orglist');
});

test('Testing useEffect hook and error redirection', async () => {
const mockEventData = {
eventsByOrganizationConnection: [
{ startDate: new Date().toISOString() }, // Assuming an event is upcoming
],
};

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

// 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');
});
});
Loading