Skip to content

Commit

Permalink
Increased code coverage of src/components/UserPortal/EventCard/EventC…
Browse files Browse the repository at this point in the history
…ard.tsx (#3267)

* increased code coverage of src/components/UserPortal/EventCard/EventCard.tsx

* resolved coderabbit conversation

* resolved coderabbit conversation
  • Loading branch information
syedali237 authored Jan 14, 2025
1 parent 224219d commit a23d95b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
39 changes: 38 additions & 1 deletion src/components/UserPortal/EventCard/EventCard.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { MockedProvider } from '@apollo/react-testing';
import { I18nextProvider } from 'react-i18next';
import { BrowserRouter } from 'react-router-dom';
import { ToastContainer } from 'react-toastify';
import { toast, ToastContainer } from 'react-toastify';
import i18nForTest from 'utils/i18nForTest';
import EventCard from './EventCard';
import { render, screen, waitFor } from '@testing-library/react';
Expand All @@ -12,6 +12,7 @@ import { store } from 'state/store';
import { StaticMockLink } from 'utils/StaticMockLink';
import userEvent from '@testing-library/user-event';
import useLocalStorage from 'utils/useLocalstorage';
import { vi } from 'vitest';

const { setItem } = useLocalStorage();

Expand Down Expand Up @@ -143,6 +144,42 @@ describe('Testing Event Card In User portal', () => {
).toBeInTheDocument(),
);
});

it('should display an error toast when the register mutation fails', async () => {
const toastErrorSpy = vi.spyOn(toast, 'error');
const errorMocks = [
{
request: {
query: REGISTER_EVENT,
variables: { eventId: '123' },
},
error: new Error('Failed to register for the event'),
},
];

const errorLink = new StaticMockLink(errorMocks, true);

render(
<MockedProvider addTypename={false} link={errorLink}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<ToastContainer />
<EventCard {...props} />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>,
);

userEvent.click(screen.getByText('Register'));

await waitFor(() => {
expect(toastErrorSpy).toHaveBeenCalledWith(
`Failed to register for the event`,
);
});
});
});

describe('Event card when start and end time are not given', () => {
Expand Down
5 changes: 2 additions & 3 deletions src/components/UserPortal/EventCard/EventCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,12 @@ function eventCard(props: InterfaceEventCardProps): JSX.Element {
eventId: props.id,
},
});
/* istanbul ignore next */
if (data) {
setIsRegistered(true);
toast.success(`Successfully registered for ${props.title}`);
}
} catch (error: unknown) {
/* istanbul ignore next */
} catch (error) {
toast.error(`Failed to register for the event`);
toast.error(error as string);
}
}
Expand Down

0 comments on commit a23d95b

Please sign in to comment.