Skip to content

Commit

Permalink
Improved the code coverage in OrgAdminListCard.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
hars-21 committed Jan 11, 2025
1 parent e3a2bdf commit 5a21fff
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
98 changes: 98 additions & 0 deletions src/components/OrgAdminListCard/OrgAdminListCard.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import i18nForTest from 'utils/i18nForTest';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import { StaticMockLink } from 'utils/StaticMockLink';
import { vi, beforeEach, afterEach, expect, it, describe } from 'vitest';
import { errorHandler } from 'utils/errorHandler'; // Make sure this import is available

const MOCKS = [
{
Expand All @@ -26,6 +27,7 @@ const MOCKS = [
},
},
];

const link = new StaticMockLink(MOCKS, true);
async function wait(ms = 100): Promise<void> {
await act(() => {
Expand Down Expand Up @@ -58,13 +60,20 @@ const renderOrgAdminListCard = (props: {
</MockedProvider>,
);
};

vi.mock('i18next-browser-languagedetector', async () => ({
...(await vi.importActual('i18next-browser-languagedetector')),
init: vi.fn(),
type: 'languageDetector',
detect: vi.fn(() => 'en'),
cacheUserLanguage: vi.fn(),
}));

// Add the mock for errorHandler
vi.mock('utils/errorHandler', () => ({
errorHandler: vi.fn(),
}));

describe('Testing Organization Admin List Card', () => {
global.alert = vi.fn();

Expand Down Expand Up @@ -107,4 +116,93 @@ describe('Testing Organization Admin List Card', () => {
expect(orgListScreen).toBeInTheDocument();
});
});

it('should not call toast or reload if no data is returned from mutation', async () => {
// Simulate a failure or empty response from the mutation
const noDataMocks = [
{
request: {
query: REMOVE_ADMIN_MUTATION,
variables: { userid: '456', orgid: '987' },
},
result: {
data: null, // Simulating no data returned
},
},
];

const noDataLink = new StaticMockLink(noDataMocks, true);

const props = {
toggleRemoveModal: vi.fn(),
id: '456',
};

render(
<MockedProvider addTypename={false} link={noDataLink}>
<MemoryRouter initialEntries={['/orgpeople/987']}>
<Routes>
<Route
path="/orgpeople/:orgId"
element={<OrgAdminListCard {...props} />}
/>
</Routes>
</MemoryRouter>
</MockedProvider>,
);

// Simulate user click on "Yes"
userEvent.click(screen.getByTestId('removeAdminBtn'));

await waitFor(() => {
// Verify that neither toast.success nor window.location.reload are called
expect(global.alert).not.toHaveBeenCalled();
expect(window.location.reload).not.toHaveBeenCalled();
});
});

it('should call errorHandler when mutation fails', async () => {
// Override the mock to simulate a failure
const failingMocks = [
{
request: {
query: REMOVE_ADMIN_MUTATION,
variables: { userid: '456', orgid: '987' },
},
error: new Error('Failed to remove admin'),
},
];

const failingLink = new StaticMockLink(failingMocks, true);

const props = {
toggleRemoveModal: vi.fn(),
id: '456',
};

render(
<MockedProvider addTypename={false} link={failingLink}>
<MemoryRouter initialEntries={['/orgpeople/987']}>
<Routes>
<Route
path="/orgpeople/:orgId"
element={<OrgAdminListCard {...props} />}
/>
</Routes>
</MemoryRouter>
</MockedProvider>,
);

// Simulate user click on "Yes"
userEvent.click(screen.getByTestId('removeAdminBtn'));

// Wait for the errorHandler to be called
await waitFor(() => {
// Verify that errorHandler was called with the expected arguments
expect(errorHandler).toHaveBeenCalledWith(
expect.any(Function),
expect.any(Error),
);
});
});
});
1 change: 0 additions & 1 deletion src/components/OrgAdminListCard/OrgAdminListCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function orgAdminListCard(props: InterfaceOrgPeopleListCardProps): JSX.Element {
}, 2000);
}
} catch (error: unknown) {
/* istanbul ignore next */
errorHandler(t, error);
}
};
Expand Down

0 comments on commit 5a21fff

Please sign in to comment.