Skip to content

Commit

Permalink
GraphQL error scenarios to test cases Added
Browse files Browse the repository at this point in the history
  • Loading branch information
raggettii committed Dec 10, 2024
1 parent 1228beb commit b806e2d
Showing 1 changed file with 65 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { BrowserRouter } from 'react-router-dom';
import LeaveOrganization from './LeaveOrganization';
import { ORGANIZATIONS_LIST } from 'GraphQl/Queries/Queries';
import { REMOVE_MEMBER_MUTATION } from 'GraphQl/Mutations/mutations';
// import useLocalStorage from 'utils/useLocalstorage';

// Mock the custom hook
jest.mock('utils/useLocalStorage', () => {
Expand Down Expand Up @@ -50,12 +49,28 @@ const mocks = [
},
},
},
// Mock for query error
{
request: {
query: ORGANIZATIONS_LIST,
variables: { id: 'test-org-id' },
},
error: new Error('Failed to load organization details'),
},
// Mock for mutation error
{
request: {
query: REMOVE_MEMBER_MUTATION,
variables: { orgid: 'test-org-id', userid: 'test-user-id' },
},
error: new Error('Failed to leave organization'),
},
];

describe('LeaveOrganization Component', () => {
test('renders organization details', async () => {
render(
<MockedProvider mocks={mocks} addTypename={false}>
<MockedProvider mocks={mocks.slice(0, 2)} addTypename={false}>
<BrowserRouter>
<LeaveOrganization />
</BrowserRouter>
Expand All @@ -72,9 +87,26 @@ describe('LeaveOrganization Component', () => {
});
});

test('shows error message when query fails', async () => {
render(
<MockedProvider mocks={mocks.slice(2, 3)} addTypename={false}>
<BrowserRouter>
<LeaveOrganization />
</BrowserRouter>
</MockedProvider>,
);

await waitFor(() => screen.getByText(/Loading/i));
await waitFor(() => {
expect(
screen.getByText(/Failed to load organization details/i),
).toBeInTheDocument();
});
});

test('opens modal on leave button click', async () => {
render(
<MockedProvider mocks={mocks} addTypename={false}>
<MockedProvider mocks={mocks.slice(0, 2)} addTypename={false}>
<BrowserRouter>
<LeaveOrganization />
</BrowserRouter>
Expand All @@ -91,7 +123,7 @@ describe('LeaveOrganization Component', () => {

test('verifies email and submits', async () => {
render(
<MockedProvider mocks={mocks} addTypename={false}>
<MockedProvider mocks={mocks.slice(0, 2)} addTypename={false}>
<BrowserRouter>
<LeaveOrganization />
</BrowserRouter>
Expand All @@ -117,7 +149,7 @@ describe('LeaveOrganization Component', () => {

test('shows error for incorrect email', async () => {
render(
<MockedProvider mocks={mocks} addTypename={false}>
<MockedProvider mocks={mocks.slice(0, 2)} addTypename={false}>
<BrowserRouter>
<LeaveOrganization />
</BrowserRouter>
Expand All @@ -140,4 +172,32 @@ describe('LeaveOrganization Component', () => {
expect(screen.getByText(/Verification failed/i)).toBeInTheDocument(),
);
});

test('shows error message when mutation fails', async () => {
render(
<MockedProvider mocks={mocks.slice(3, 4)} addTypename={false}>
<BrowserRouter>
<LeaveOrganization />
</BrowserRouter>
</MockedProvider>,
);

await waitFor(() => screen.getByText('Leave Organization'));
fireEvent.click(screen.getByText('Leave Organization'));

await waitFor(() => screen.getByText('Continue'));
fireEvent.click(screen.getByText('Continue'));

fireEvent.change(screen.getByPlaceholderText(/Enter your email/i), {
target: { value: '[email protected]' },
});

fireEvent.click(screen.getByText('Confirm'));

await waitFor(() =>
expect(
screen.getByText(/Failed to leave organization/i),
).toBeInTheDocument(),
);
});
});

0 comments on commit b806e2d

Please sign in to comment.