From a23d95baa0fcbc078b886a40f1f646e33c0c6ae9 Mon Sep 17 00:00:00 2001 From: Syed Ali Ul Hasan Date: Tue, 14 Jan 2025 20:49:13 +0530 Subject: [PATCH] Increased code coverage of src/components/UserPortal/EventCard/EventCard.tsx (#3267) * increased code coverage of src/components/UserPortal/EventCard/EventCard.tsx * resolved coderabbit conversation * resolved coderabbit conversation --- .../UserPortal/EventCard/EventCard.spec.tsx | 39 ++++++++++++++++++- .../UserPortal/EventCard/EventCard.tsx | 5 +-- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/components/UserPortal/EventCard/EventCard.spec.tsx b/src/components/UserPortal/EventCard/EventCard.spec.tsx index 150f676447..1a4806ee18 100644 --- a/src/components/UserPortal/EventCard/EventCard.spec.tsx +++ b/src/components/UserPortal/EventCard/EventCard.spec.tsx @@ -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'; @@ -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(); @@ -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( + + + + + + + + + + , + ); + + 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', () => { diff --git a/src/components/UserPortal/EventCard/EventCard.tsx b/src/components/UserPortal/EventCard/EventCard.tsx index e93138ce42..7996104b05 100644 --- a/src/components/UserPortal/EventCard/EventCard.tsx +++ b/src/components/UserPortal/EventCard/EventCard.tsx @@ -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); } }