From 374a87db1f969b73d5f902095335f04c1b3d5c80 Mon Sep 17 00:00:00 2001 From: IITI-tushar <019saxenatushar@gmail.com> Date: Wed, 18 Dec 2024 19:11:52 +0530 Subject: [PATCH 01/12] Add AdvertisementEntry.spec.tsx for Vitest migration --- .../AdvertisementEntry.spec.tsx | 648 ++++++++++++++++++ 1 file changed, 648 insertions(+) create mode 100644 src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx diff --git a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx new file mode 100644 index 0000000000..50af27ad34 --- /dev/null +++ b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx @@ -0,0 +1,648 @@ +import React from 'react'; +import { render, fireEvent, waitFor, screen } from '@testing-library/react'; +import { + ApolloClient, + ApolloProvider, + InMemoryCache, + ApolloLink, + HttpLink, +} from '@apollo/client'; +import type { NormalizedCacheObject } from '@apollo/client'; +import { BrowserRouter } from 'react-router-dom'; +import AdvertisementEntry from './AdvertisementEntry'; +import AdvertisementRegister from '../AdvertisementRegister/AdvertisementRegister'; +import { Provider } from 'react-redux'; +import { store } from 'state/store'; +import { BACKEND_URL } from 'Constant/constant'; +import i18nForTest from 'utils/i18nForTest'; +import { I18nextProvider } from 'react-i18next'; +import dayjs from 'dayjs'; +import useLocalStorage from 'utils/useLocalstorage'; +import { MockedProvider } from '@apollo/client/testing'; +import { ORGANIZATION_ADVERTISEMENT_LIST } from 'GraphQl/Queries/OrganizationQueries'; +import { DELETE_ADVERTISEMENT_BY_ID } from 'GraphQl/Mutations/mutations'; +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import '@testing-library/jest-dom'; + +const { getItem } = useLocalStorage(); + +const httpLink = new HttpLink({ + uri: BACKEND_URL, + headers: { + authorization: 'Bearer ' + getItem('token') || '', + }, +}); + +const translations = JSON.parse( + JSON.stringify( + i18nForTest.getDataByLanguage('en')?.translation?.advertisement ?? null, + ), +); + +const client: ApolloClient = new ApolloClient({ + cache: new InMemoryCache(), + link: ApolloLink.from([httpLink]), +}); + +const mockUseMutation = vi.fn(); +vi.mock('@apollo/client', async () => { + const actual = await vi.importActual('@apollo/client'); + return { + ...actual, + useMutation: () => mockUseMutation(), + }; +}); + +vi.mock('react-router-dom', async () => { + const actual = await vi.importActual('react-router-dom'); + return { + ...actual, + useParams: () => ({ orgId: '1' }), + }; +}); + +describe('Testing Advertisement Entry Component', () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + + it('Testing rendering and deleting of advertisement', async () => { + const deleteAdByIdMock = vi.fn(); + mockUseMutation.mockReturnValue([deleteAdByIdMock]); + const { getByTestId, getAllByText } = render( + + + + + + + + + , + ); + + //Testing rendering + expect(getByTestId('AdEntry')).toBeInTheDocument(); + expect(getAllByText('POPUP')[0]).toBeInTheDocument(); + expect(getAllByText('Advert1')[0]).toBeInTheDocument(); + expect(screen.getByTestId('media')).toBeInTheDocument(); + + //Testing successful deletion + fireEvent.click(getByTestId('moreiconbtn')); + fireEvent.click(getByTestId('deletebtn')); + + await waitFor(() => { + expect(screen.getByTestId('delete_title')).toBeInTheDocument(); + expect(screen.getByTestId('delete_body')).toBeInTheDocument(); + }); + + fireEvent.click(getByTestId('delete_yes')); + + await waitFor(() => { + expect(deleteAdByIdMock).toHaveBeenCalledWith({ + variables: { + id: '1', + }, + }); + const deletedMessage = screen.queryByText('Advertisement Deleted'); + expect(deletedMessage).toBeNull(); + }); + + //Testing unsuccessful deletion + deleteAdByIdMock.mockRejectedValueOnce(new Error('Deletion Failed')); + + fireEvent.click(getByTestId('moreiconbtn')); + + fireEvent.click(getByTestId('delete_yes')); + + await waitFor(() => { + expect(deleteAdByIdMock).toHaveBeenCalledWith({ + variables: { + id: '1', + }, + }); + const deletionFailedText = screen.queryByText((content, element) => { + return ( + element?.textContent === 'Deletion Failed' && + element.tagName.toLowerCase() === 'div' + ); + }); + expect(deletionFailedText).toBeNull(); + }); + }); + + it('should use default props when none are provided', () => { + render( + , + : void { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + throw new Error('Function not implemented.'); + }} + />, + ); + + //Check if component renders with default ''(empty string) + const elements = screen.getAllByText(''); // This will return an array of matching elements + elements.forEach((element) => expect(element).toBeInTheDocument()); + + // Check that the component renders with default `mediaUrl` (empty string) + const mediaElement = screen.getByTestId('media'); + expect(mediaElement).toHaveAttribute('src', ''); + + // Check that the component renders with default `endDate` + const defaultEndDate = new Date().toDateString(); + expect(screen.getByText(`Ends on ${defaultEndDate}`)).toBeInTheDocument(); + + // Check that the component renders with default `startDate` + const defaultStartDate = new Date().toDateString(); + expect(screen.getByText(`Ends on ${defaultStartDate}`)).toBeInTheDocument(); //fix text "Ends on"? + }); + + it('should correctly override default props when values are provided', () => { + const mockName = 'Test Ad'; + const mockType = 'Banner'; + const mockMediaUrl = 'https://example.com/media.png'; + const mockEndDate = new Date(2025, 11, 31); + const mockStartDate = new Date(2024, 0, 1); + const mockOrganizationId = 'org123'; + + const { getByText } = render( + , + : void { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + throw new Error('Function not implemented.'); + }} + />, + ); + + // Check that the component renders with provided values + expect(getByText(mockName)).toBeInTheDocument(); + // Add more checks based on how each prop affects rendering + }); + + it('should open and close the dropdown when options button is clicked', () => { + const { getByTestId, queryByText, getAllByText } = render( + + + + + + + + + , + ); + + // Test initial rendering + expect(getByTestId('AdEntry')).toBeInTheDocument(); + expect(getAllByText('POPUP')[0]).toBeInTheDocument(); + expect(getAllByText('Advert1')[0]).toBeInTheDocument(); + + // Test dropdown functionality + const optionsButton = getByTestId('moreiconbtn'); + + // Initially, the dropdown should not be visible + expect(queryByText('Edit')).toBeNull(); + + // Click to open the dropdown + fireEvent.click(optionsButton); + + // After clicking the button, the dropdown should be visible + expect(queryByText('Edit')).toBeInTheDocument(); + + // Click again to close the dropdown + fireEvent.click(optionsButton); + + // After the second click, the dropdown should be hidden again + expect(queryByText('Edit')).toBeNull(); + }); + + it('Updates the advertisement and shows success toast on successful update', async () => { + const updateAdByIdMock = vi.fn().mockResolvedValue({ + data: { + updateAdvertisement: { + advertisement: { + _id: '1', + name: 'Updated Advertisement', + mediaUrl: '', + startDate: dayjs(new Date()).add(1, 'day').format('YYYY-MM-DD'), + endDate: dayjs(new Date()).add(2, 'days').format('YYYY-MM-DD'), + type: 'BANNER', + }, + }, + }, + }); + + mockUseMutation.mockReturnValue([updateAdByIdMock]); + + render( + + + + + + + + + , + ); + + const optionsButton = screen.getByTestId('moreiconbtn'); + fireEvent.click(optionsButton); + fireEvent.click(screen.getByTestId('editBtn')); + + fireEvent.change(screen.getByLabelText('Enter name of Advertisement'), { + target: { value: 'Updated Advertisement' }, + }); + + expect(screen.getByLabelText('Enter name of Advertisement')).toHaveValue( + 'Updated Advertisement', + ); + + fireEvent.change(screen.getByLabelText(translations.Rtype), { + target: { value: 'BANNER' }, + }); + expect(screen.getByLabelText(translations.Rtype)).toHaveValue('BANNER'); + + fireEvent.change(screen.getByLabelText(translations.RstartDate), { + target: { value: dayjs().add(1, 'day').format('YYYY-MM-DD') }, + }); + + fireEvent.change(screen.getByLabelText(translations.RendDate), { + target: { value: dayjs().add(2, 'days').format('YYYY-MM-DD') }, + }); + + fireEvent.click(screen.getByTestId('addonupdate')); + + expect(updateAdByIdMock).toHaveBeenCalledWith({ + variables: { + id: '1', + name: 'Updated Advertisement', + type: 'BANNER', + startDate: dayjs().add(1, 'day').format('YYYY-MM-DD'), + endDate: dayjs().add(2, 'days').format('YYYY-MM-DD'), + }, + }); + }); + + it('Simulating if the mutation doesnt have data variable while updating', async () => { + const updateAdByIdMock = vi.fn().mockResolvedValue({ + updateAdvertisement: { + _id: '1', + name: 'Updated Advertisement', + type: 'BANNER', + }, + }); + + mockUseMutation.mockReturnValue([updateAdByIdMock]); + + render( + + + + + + + + + , + ); + + const optionsButton = screen.getByTestId('moreiconbtn'); + fireEvent.click(optionsButton); + fireEvent.click(screen.getByTestId('editBtn')); + + fireEvent.change(screen.getByLabelText('Enter name of Advertisement'), { + target: { value: 'Updated Advertisement' }, + }); + + expect(screen.getByLabelText('Enter name of Advertisement')).toHaveValue( + 'Updated Advertisement', + ); + + fireEvent.change(screen.getByLabelText(translations.Rtype), { + target: { value: 'BANNER' }, + }); + expect(screen.getByLabelText(translations.Rtype)).toHaveValue('BANNER'); + + fireEvent.click(screen.getByTestId('addonupdate')); + + expect(updateAdByIdMock).toHaveBeenCalledWith({ + variables: { + id: '1', + name: 'Updated Advertisement', + type: 'BANNER', + }, + }); + }); + + it('Simulating if the mutation does not have data variable while registering', async () => { + vi.stubGlobal('location', { + reload: vi.fn(), + href: 'https://example.com/page/id=1', + }); + + const createAdByIdMock = vi.fn().mockResolvedValue({ + data1: { + createAdvertisement: { + _id: '1', + }, + }, + }); + + mockUseMutation.mockReturnValue([createAdByIdMock]); + + render( + + + + + { + + } + + + + , + ); + + fireEvent.click(screen.getByTestId('createAdvertisement')); + + fireEvent.change(screen.getByLabelText('Enter name of Advertisement'), { + target: { value: 'Updated Advertisement' }, + }); + + expect(screen.getByLabelText('Enter name of Advertisement')).toHaveValue( + 'Updated Advertisement', + ); + + fireEvent.change(screen.getByLabelText(translations.Rtype), { + target: { value: 'BANNER' }, + }); + expect(screen.getByLabelText(translations.Rtype)).toHaveValue('BANNER'); + + fireEvent.change(screen.getByLabelText(translations.RstartDate), { + target: { value: '2023-01-01' }, + }); + expect(screen.getByLabelText(translations.RstartDate)).toHaveValue( + '2023-01-01', + ); + + fireEvent.change(screen.getByLabelText(translations.RendDate), { + target: { value: '2023-02-01' }, + }); + expect(screen.getByLabelText(translations.RendDate)).toHaveValue( + '2023-02-01', + ); + + fireEvent.click(screen.getByTestId('addonregister')); + + expect(createAdByIdMock).toHaveBeenCalledWith({ + variables: { + organizationId: '1', + name: 'Updated Advertisement', + file: '', + type: 'BANNER', + startDate: dayjs(new Date('2023-01-01')).format('YYYY-MM-DD'), + endDate: dayjs(new Date('2023-02-01')).format('YYYY-MM-DD'), + }, + }); + }); + + it('delete advertisement', async () => { + const deleteAdByIdMock = vi.fn(); + const mocks = [ + { + request: { + query: ORGANIZATION_ADVERTISEMENT_LIST, + variables: { + id: '1', + first: 2, + after: null, + last: null, + before: null, + }, + }, + result: { + data: { + organizations: [ + { + _id: '1', + advertisements: { + edges: [ + { + node: { + _id: '1', + name: 'Advertisement1', + startDate: '2022-01-01', + endDate: '2023-01-01', + mediaUrl: 'http://example1.com', + }, + cursor: 'cursor1', + }, + { + node: { + _id: '2', + name: 'Advertisement2', + startDate: '2024-02-01', + endDate: '2025-02-01', + mediaUrl: 'http://example2.com', + }, + cursor: 'cursor2', + }, + { + node: { + _id: '3', + name: 'Advertisement1', + startDate: '2022-01-01', + endDate: '2023-01-01', + mediaUrl: 'http://example1.com', + }, + cursor: 'cursor3', + }, + { + node: { + _id: '4', + name: 'Advertisement2', + startDate: '2024-02-01', + endDate: '2025-02-01', + mediaUrl: 'http://example2.com', + }, + cursor: 'cursor4', + }, + { + node: { + _id: '5', + name: 'Advertisement1', + startDate: '2022-01-01', + endDate: '2023-01-01', + mediaUrl: 'http://example1.com', + }, + cursor: 'cursor5', + }, + { + node: { + _id: '6', + name: 'Advertisement2', + startDate: '2024-02-01', + endDate: '2025-02-01', + mediaUrl: 'http://example2.com', + }, + cursor: 'cursor6', + }, + ], + pageInfo: { + startCursor: 'cursor1', + endCursor: 'cursor6', + hasNextPage: true, + hasPreviousPage: false, + }, + totalCount: 8, + }, + }, + ], + }, + }, + }, + { + request: { + query: DELETE_ADVERTISEMENT_BY_ID, + variables: { + id: '1', + }, + }, + result: { + data: { + advertisements: { + _id: null, + }, + }, + }, + }, + ]; + mockUseMutation.mockReturnValue([deleteAdByIdMock]); + const { getByTestId, getAllByText } = render( + + + + + + + + + + + , + ); + + //Testing rendering + expect(getByTestId('AdEntry')).toBeInTheDocument(); + expect(getAllByText('POPUP')[0]).toBeInTheDocument(); + expect(getAllByText('Advert1')[0]).toBeInTheDocument(); + expect(screen.getByTestId('media')).toBeInTheDocument(); + + //Testing successful deletion + fireEvent.click(getByTestId('moreiconbtn')); + fireEvent.click(getByTestId('deletebtn')); + + await waitFor(() => { + expect(screen.getByTestId('delete_title')).toBeInTheDocument(); + expect(screen.getByTestId('delete_body')).toBeInTheDocument(); + }); + + fireEvent.click(getByTestId('delete_yes')); + + await waitFor(() => { + expect(deleteAdByIdMock).toHaveBeenCalledWith({ + variables: { + id: '1', + }, + }); + const deletedMessage = screen.queryByText('Advertisement Deleted'); + expect(deletedMessage).toBeNull(); + }); + + //Testing unsuccessful deletion + deleteAdByIdMock.mockRejectedValueOnce(new Error('Deletion Failed')); + + fireEvent.click(getByTestId('moreiconbtn')); + + fireEvent.click(getByTestId('delete_yes')); + + await waitFor(() => { + expect(deleteAdByIdMock).toHaveBeenCalledWith({ + variables: { + id: '1', + }, + }); + const deletionFailedText = screen.queryByText((content, element) => { + return ( + element?.textContent === 'Deletion Failed' && + element.tagName.toLowerCase() === 'div' + ); + }); + expect(deletionFailedText).toBeNull(); + }); + }); +}); From 47ad3d05344aa4e4cf5a2af3a64cce789e369b97 Mon Sep 17 00:00:00 2001 From: IITI-tushar <165766280+IITI-tushar@users.noreply.github.com> Date: Wed, 18 Dec 2024 21:54:04 +0530 Subject: [PATCH 02/12] Update AdvertisementEntry.spec.tsx removes eslint-disable --- .../core/AdvertisementEntry/AdvertisementEntry.spec.tsx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx index 50af27ad34..ae4f176a20 100644 --- a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx +++ b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx @@ -145,9 +145,8 @@ describe('Testing Advertisement Entry Component', () => { render( , + setAfter={function () _value: React.SetStateAction, : void { - // eslint-disable-next-line @typescript-eslint/no-unused-vars throw new Error('Function not implemented.'); }} />, @@ -187,9 +186,8 @@ describe('Testing Advertisement Entry Component', () => { startDate={mockStartDate} organizationId={mockOrganizationId} id={''} - setAfter={function () // _value: React.SetStateAction, + setAfter={function () _value: React.SetStateAction, : void { - // eslint-disable-next-line @typescript-eslint/no-unused-vars throw new Error('Function not implemented.'); }} />, From e0fe891516a733f7a820dc63bd5555daff835710 Mon Sep 17 00:00:00 2001 From: IITI-tushar <165766280+IITI-tushar@users.noreply.github.com> Date: Wed, 18 Dec 2024 22:00:52 +0530 Subject: [PATCH 03/12] Update AdvertisementEntry.spec.tsx --- .../AdvertisementEntry/AdvertisementEntry.spec.tsx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx index ae4f176a20..6bf3272c31 100644 --- a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx +++ b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx @@ -145,8 +145,9 @@ describe('Testing Advertisement Entry Component', () => { render( , - : void { + setAfter={function ( + _value: React.SetStateAction , + ): void { throw new Error('Function not implemented.'); }} />, @@ -186,8 +187,9 @@ describe('Testing Advertisement Entry Component', () => { startDate={mockStartDate} organizationId={mockOrganizationId} id={''} - setAfter={function () _value: React.SetStateAction, - : void { + setAfter={function ( + _value: React.SetStateAction , + ) : void { throw new Error('Function not implemented.'); }} />, From 666ae795327389b114433aa133c2d70e787ebf29 Mon Sep 17 00:00:00 2001 From: IITI-tushar <165766280+IITI-tushar@users.noreply.github.com> Date: Wed, 18 Dec 2024 22:14:24 +0530 Subject: [PATCH 04/12] Update AdvertisementEntry.spec.tsx --- .../core/AdvertisementEntry/AdvertisementEntry.spec.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx index 6bf3272c31..c24a5b3c3f 100644 --- a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx +++ b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx @@ -146,7 +146,7 @@ describe('Testing Advertisement Entry Component', () => { , + _value: React.SetStateAction, ): void { throw new Error('Function not implemented.'); }} @@ -188,8 +188,8 @@ describe('Testing Advertisement Entry Component', () => { organizationId={mockOrganizationId} id={''} setAfter={function ( - _value: React.SetStateAction , - ) : void { + _value: React.SetStateAction, + ): void { throw new Error('Function not implemented.'); }} />, From 858dbd46814e4e2eb461222f7430e625d6c32555 Mon Sep 17 00:00:00 2001 From: IITI-tushar <165766280+IITI-tushar@users.noreply.github.com> Date: Fri, 20 Dec 2024 10:32:10 +0530 Subject: [PATCH 05/12] Update AdvertisementEntry.spec.tsx --- .../core/AdvertisementEntry/AdvertisementEntry.spec.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx index c24a5b3c3f..9fd946b55f 100644 --- a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx +++ b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx @@ -146,7 +146,7 @@ describe('Testing Advertisement Entry Component', () => { , + // _value: React.SetStateAction, ): void { throw new Error('Function not implemented.'); }} @@ -188,7 +188,7 @@ describe('Testing Advertisement Entry Component', () => { organizationId={mockOrganizationId} id={''} setAfter={function ( - _value: React.SetStateAction, + // _value: React.SetStateAction, ): void { throw new Error('Function not implemented.'); }} From 83834724555530d47075e93d7311ec220d9a3938 Mon Sep 17 00:00:00 2001 From: IITI-tushar <165766280+IITI-tushar@users.noreply.github.com> Date: Fri, 20 Dec 2024 11:34:59 +0530 Subject: [PATCH 06/12] Update AdvertisementEntry.spec.tsx --- .../AdvertisementEntry/AdvertisementEntry.spec.tsx | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx index 9fd946b55f..8315c083eb 100644 --- a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx +++ b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx @@ -145,9 +145,8 @@ describe('Testing Advertisement Entry Component', () => { render( , - ): void { + setAfter={function () // _value: React.SetStateAction, + : void { throw new Error('Function not implemented.'); }} />, @@ -187,9 +186,8 @@ describe('Testing Advertisement Entry Component', () => { startDate={mockStartDate} organizationId={mockOrganizationId} id={''} - setAfter={function ( - // _value: React.SetStateAction, - ): void { + setAfter={function () // _value: React.SetStateAction, + : void { throw new Error('Function not implemented.'); }} />, From 3377370d3ba0dcac1c6e89d90e48f3177be57376 Mon Sep 17 00:00:00 2001 From: IITI-tushar <165766280+IITI-tushar@users.noreply.github.com> Date: Sat, 21 Dec 2024 16:07:04 +0530 Subject: [PATCH 07/12] Update AdvertisementEntry.spec.tsx --- .../core/AdvertisementEntry/AdvertisementEntry.spec.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx index 8315c083eb..c0db702c1f 100644 --- a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx +++ b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx @@ -9,8 +9,8 @@ import { } from '@apollo/client'; import type { NormalizedCacheObject } from '@apollo/client'; import { BrowserRouter } from 'react-router-dom'; -import AdvertisementEntry from './AdvertisementEntry'; -import AdvertisementRegister from '../AdvertisementRegister/AdvertisementRegister'; +import AdvertisementEntry from './core/AdvertisementEntry/AdvertisementEntry'; +import AdvertisementRegister from './core/AdvertisementRegister/AdvertisementRegister'; import { Provider } from 'react-redux'; import { store } from 'state/store'; import { BACKEND_URL } from 'Constant/constant'; From 2aba5a6afb9a34aa5616084f671ac7c64c15e4dc Mon Sep 17 00:00:00 2001 From: IITI-tushar <165766280+IITI-tushar@users.noreply.github.com> Date: Sat, 21 Dec 2024 16:16:57 +0530 Subject: [PATCH 08/12] Update AdvertisementEntry.spec.tsx --- .../core/AdvertisementEntry/AdvertisementEntry.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx index c0db702c1f..3937e41616 100644 --- a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx +++ b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx @@ -9,7 +9,7 @@ import { } from '@apollo/client'; import type { NormalizedCacheObject } from '@apollo/client'; import { BrowserRouter } from 'react-router-dom'; -import AdvertisementEntry from './core/AdvertisementEntry/AdvertisementEntry'; +import AdvertisementEntry from '/AdvertisementEntry'; import AdvertisementRegister from './core/AdvertisementRegister/AdvertisementRegister'; import { Provider } from 'react-redux'; import { store } from 'state/store'; From c239dc02e1299bc45791f029399d94cbb3141f38 Mon Sep 17 00:00:00 2001 From: IITI-tushar <165766280+IITI-tushar@users.noreply.github.com> Date: Sat, 21 Dec 2024 16:38:06 +0530 Subject: [PATCH 09/12] Update AdvertisementEntry.spec.tsx --- .../core/AdvertisementEntry/AdvertisementEntry.spec.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx index 3937e41616..8315c083eb 100644 --- a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx +++ b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx @@ -9,8 +9,8 @@ import { } from '@apollo/client'; import type { NormalizedCacheObject } from '@apollo/client'; import { BrowserRouter } from 'react-router-dom'; -import AdvertisementEntry from '/AdvertisementEntry'; -import AdvertisementRegister from './core/AdvertisementRegister/AdvertisementRegister'; +import AdvertisementEntry from './AdvertisementEntry'; +import AdvertisementRegister from '../AdvertisementRegister/AdvertisementRegister'; import { Provider } from 'react-redux'; import { store } from 'state/store'; import { BACKEND_URL } from 'Constant/constant'; From 700eff110b2c736e6c2b8925e3751b6eb885b263 Mon Sep 17 00:00:00 2001 From: IITI-tushar <165766280+IITI-tushar@users.noreply.github.com> Date: Sun, 22 Dec 2024 12:53:48 +0530 Subject: [PATCH 10/12] Update AdvertisementEntry.spec.tsx --- .../AdvertisementEntry.spec.tsx | 85 +++++++++++++++++-- 1 file changed, 77 insertions(+), 8 deletions(-) diff --git a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx index 8315c083eb..394f9e5d95 100644 --- a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx +++ b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx @@ -27,12 +27,11 @@ import '@testing-library/jest-dom'; const { getItem } = useLocalStorage(); const httpLink = new HttpLink({ - uri: BACKEND_URL, - headers: { - authorization: 'Bearer ' + getItem('token') || '', - }, -}); - + uri: BACKEND_URL, + headers: { + authorization: 'Bearer ' + getItem('token') || '', + }, + }); const translations = JSON.parse( JSON.stringify( i18nForTest.getDataByLanguage('en')?.translation?.advertisement ?? null, @@ -166,7 +165,9 @@ describe('Testing Advertisement Entry Component', () => { // Check that the component renders with default `startDate` const defaultStartDate = new Date().toDateString(); - expect(screen.getByText(`Ends on ${defaultStartDate}`)).toBeInTheDocument(); //fix text "Ends on"? + expect( + screen.getByText(`Ends on ${defaultStartDate}`), + ).toBeInTheDocument(); //fix text "Ends on"? }); it('should correctly override default props when values are provided', () => { @@ -195,7 +196,11 @@ describe('Testing Advertisement Entry Component', () => { // Check that the component renders with provided values expect(getByText(mockName)).toBeInTheDocument(); - // Add more checks based on how each prop affects rendering + expect(getByText(mockType)).toBeInTheDocument(); + expect(screen.getByTestId('media')).toHaveAttribute('src', mockMediaUrl); + expect( + getByText(`Ends on ${mockEndDate.toDateString()}`), + ).toBeInTheDocument(); }); it('should open and close the dropdown when options button is clicked', () => { @@ -322,6 +327,70 @@ describe('Testing Advertisement Entry Component', () => { }); }); + // it('Shows error toast on failed update', async () => { + // const updateAdByIdMock = vi + // .fn() + // .mockRejectedValue(new Error('Update failed')); + // mockUseMutation.mockReturnValue([updateAdByIdMock]); + + // render( + // + // + // + // + // + // + // + // + // , + // ); + + // const optionsButton = screen.getByTestId('moreiconbtn'); + // fireEvent.click(optionsButton); + // fireEvent.click(screen.getByTestId('editBtn')); + + // fireEvent.change(screen.getByLabelText('Enter name of Advertisement'), { + // target: { value: 'Updated Advertisement' }, + // }); + + // fireEvent.change(screen.getByLabelText(translations.Rtype), { + // target: { value: 'BANNER' }, + // }); + + // fireEvent.change(screen.getByLabelText(translations.RstartDate), { + // target: { value: dayjs().add(1, 'day').format('YYYY-MM-DD') }, + // }); + + // fireEvent.change(screen.getByLabelText(translations.RendDate), { + // target: { value: dayjs().add(2, 'days').format('YYYY-MM-DD') }, + // }); + + // fireEvent.click(screen.getByTestId('addonupdate')); + + // await waitFor(() => { + // expect(screen.getByText('Update failed')).toBeInTheDocument(); + // }, { timeout: 3000 }); + + // expect(updateAdByIdMock).toHaveBeenCalledWith({ + // variables: { + // id: '1', + // name: 'Updated Advertisement', + // type: 'BANNER', + // startDate: dayjs().add(1, 'day').format('YYYY-MM-DD'), + // endDate: dayjs().add(2, 'days').format('YYYY-MM-DD'), + // }, + // }); + // }); + it('Simulating if the mutation doesnt have data variable while updating', async () => { const updateAdByIdMock = vi.fn().mockResolvedValue({ updateAdvertisement: { From 01d2494584821ba1ea8224bfcbe80eb1617ef207 Mon Sep 17 00:00:00 2001 From: IITI-tushar <165766280+IITI-tushar@users.noreply.github.com> Date: Sun, 22 Dec 2024 12:59:10 +0530 Subject: [PATCH 11/12] Update AdvertisementEntry.spec.tsx --- .../AdvertisementEntry.spec.tsx | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx index 394f9e5d95..463dba0c91 100644 --- a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx +++ b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx @@ -27,11 +27,11 @@ import '@testing-library/jest-dom'; const { getItem } = useLocalStorage(); const httpLink = new HttpLink({ - uri: BACKEND_URL, - headers: { - authorization: 'Bearer ' + getItem('token') || '', - }, - }); + uri: BACKEND_URL, + headers: { + authorization: 'Bearer ' + getItem('token') || '', + }, +}); const translations = JSON.parse( JSON.stringify( i18nForTest.getDataByLanguage('en')?.translation?.advertisement ?? null, @@ -165,9 +165,7 @@ describe('Testing Advertisement Entry Component', () => { // Check that the component renders with default `startDate` const defaultStartDate = new Date().toDateString(); - expect( - screen.getByText(`Ends on ${defaultStartDate}`), - ).toBeInTheDocument(); //fix text "Ends on"? + expect(screen.getByText(`Ends on ${defaultStartDate}`)).toBeInTheDocument(); //fix text "Ends on"? }); it('should correctly override default props when values are provided', () => { @@ -372,7 +370,7 @@ describe('Testing Advertisement Entry Component', () => { // fireEvent.change(screen.getByLabelText(translations.RendDate), { // target: { value: dayjs().add(2, 'days').format('YYYY-MM-DD') }, - // }); + // }); // fireEvent.click(screen.getByTestId('addonupdate')); From dcfeff1458365c1b6a2a5efcf1f680250479decc Mon Sep 17 00:00:00 2001 From: IITI-tushar <165766280+IITI-tushar@users.noreply.github.com> Date: Sun, 22 Dec 2024 22:49:37 +0530 Subject: [PATCH 12/12] Update AdvertisementEntry.spec.tsx --- .../AdvertisementEntry.spec.tsx | 64 ------------------- 1 file changed, 64 deletions(-) diff --git a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx index 463dba0c91..4d27df6e22 100644 --- a/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx +++ b/src/components/Advertisements/core/AdvertisementEntry/AdvertisementEntry.spec.tsx @@ -325,70 +325,6 @@ describe('Testing Advertisement Entry Component', () => { }); }); - // it('Shows error toast on failed update', async () => { - // const updateAdByIdMock = vi - // .fn() - // .mockRejectedValue(new Error('Update failed')); - // mockUseMutation.mockReturnValue([updateAdByIdMock]); - - // render( - // - // - // - // - // - // - // - // - // , - // ); - - // const optionsButton = screen.getByTestId('moreiconbtn'); - // fireEvent.click(optionsButton); - // fireEvent.click(screen.getByTestId('editBtn')); - - // fireEvent.change(screen.getByLabelText('Enter name of Advertisement'), { - // target: { value: 'Updated Advertisement' }, - // }); - - // fireEvent.change(screen.getByLabelText(translations.Rtype), { - // target: { value: 'BANNER' }, - // }); - - // fireEvent.change(screen.getByLabelText(translations.RstartDate), { - // target: { value: dayjs().add(1, 'day').format('YYYY-MM-DD') }, - // }); - - // fireEvent.change(screen.getByLabelText(translations.RendDate), { - // target: { value: dayjs().add(2, 'days').format('YYYY-MM-DD') }, - // }); - - // fireEvent.click(screen.getByTestId('addonupdate')); - - // await waitFor(() => { - // expect(screen.getByText('Update failed')).toBeInTheDocument(); - // }, { timeout: 3000 }); - - // expect(updateAdByIdMock).toHaveBeenCalledWith({ - // variables: { - // id: '1', - // name: 'Updated Advertisement', - // type: 'BANNER', - // startDate: dayjs().add(1, 'day').format('YYYY-MM-DD'), - // endDate: dayjs().add(2, 'days').format('YYYY-MM-DD'), - // }, - // }); - // }); - it('Simulating if the mutation doesnt have data variable while updating', async () => { const updateAdByIdMock = vi.fn().mockResolvedValue({ updateAdvertisement: {