Skip to content

Commit

Permalink
fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
disha1202 committed Jan 23, 2024
1 parent a2ce0d0 commit 37cbc7d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,18 @@ describe('Testing Advertisement Entry Component', () => {
<Provider store={store}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<AdvertisementEntry
endDate={new Date()}
startDate={new Date()}
id="1"
key={1}
mediaUrl="data:videos"
name="Advert1"
organizationId="1"
type="POPUP"
/>
{
<AdvertisementEntry
endDate={new Date()}
startDate={new Date()}
id="1"
key={1}
mediaUrl="data:videos"
name="Advert1"
organizationId="1"
type="POPUP"
/>
}
</I18nextProvider>
</BrowserRouter>
</Provider>
Expand Down Expand Up @@ -131,7 +133,7 @@ describe('Testing Advertisement Entry Component', () => {
startDate={new Date()}
id="1"
key={1}
mediaUrl="google.com"
mediaUrl=""
name="Advert1"
organizationId="1"
type="POPUP"
Expand Down Expand Up @@ -173,7 +175,7 @@ describe('Testing Advertisement Entry Component', () => {
advertisement: {
_id: '1',
name: 'Updated Advertisement',
mediaUrl: 'google.com',
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',
Expand All @@ -196,7 +198,7 @@ describe('Testing Advertisement Entry Component', () => {
type="POPUP"
name="Advert1"
organizationId="1"
mediaUrl="google.com"
mediaUrl=""
id="1"
/>
}
Expand All @@ -218,13 +220,6 @@ describe('Testing Advertisement Entry Component', () => {
'Updated Advertisement'
);

fireEvent.change(screen.getByLabelText(translations.Rlink), {
target: { value: 'http://example.com' },
});
expect(screen.getByLabelText(translations.Rlink)).toHaveValue(
'http://example.com'
);

fireEvent.change(screen.getByLabelText(translations.Rtype), {
target: { value: 'BANNER' },
});
Expand All @@ -244,7 +239,6 @@ describe('Testing Advertisement Entry Component', () => {
variables: {
id: '1',
name: 'Updated Advertisement',
link: 'http://example.com',
type: 'BANNER',
startDate: dayjs().add(1, 'day').format('YYYY-MM-DD'),
endDate: dayjs().add(2, 'days').format('YYYY-MM-DD'),
Expand Down Expand Up @@ -275,7 +269,7 @@ describe('Testing Advertisement Entry Component', () => {
type="POPUP"
name="Advert1"
organizationId="1"
mediaUrl="google.com"
mediaUrl=""
id="1"
/>
}
Expand Down Expand Up @@ -332,7 +326,7 @@ describe('Testing Advertisement Entry Component', () => {
startDateEdit={new Date()}
typeEdit="POPUP"
organizationId="1"
advertisementMediaEdit="google.com"
advertisementMediaEdit=""
/>
}
</I18nextProvider>
Expand All @@ -343,19 +337,11 @@ describe('Testing Advertisement Entry Component', () => {

fireEvent.click(screen.getByTestId('editBtn'));

fireEvent.change(screen.getByLabelText(translations.Rlink), {
target: { value: 'http://example.com' },
});
expect(screen.getByLabelText(translations.Rlink)).toHaveValue(
'http://example.com'
);

fireEvent.click(screen.getByTestId('addonupdate'));

expect(updateAdByIdMock).toHaveBeenCalledWith({
variables: {
id: '-100',
link: 'http://example.com',
},
});
});
Expand Down Expand Up @@ -400,13 +386,6 @@ describe('Testing Advertisement Entry Component', () => {
'Updated Advertisement'
);

fireEvent.change(screen.getByLabelText(translations.Rlink), {
target: { value: 'http://example.com' },
});
expect(screen.getByLabelText(translations.Rlink)).toHaveValue(
'http://example.com'
);

fireEvent.change(screen.getByLabelText(translations.Rtype), {
target: { value: 'BANNER' },
});
Expand All @@ -432,7 +411,7 @@ describe('Testing Advertisement Entry Component', () => {
variables: {
organizationId: '1',
name: 'Updated Advertisement',
link: 'http://example.com',
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'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, fireEvent, waitFor, screen } from '@testing-library/react';
import { render, fireEvent, waitFor, screen, getByTestId } from '@testing-library/react';

import {
ApolloClient,
Expand All @@ -24,6 +24,7 @@ import { ADD_ADVERTISEMENT_MUTATION } from 'GraphQl/Mutations/mutations';
import dayjs from 'dayjs';
import { StaticMockLink } from 'utils/StaticMockLink';
import userEvent from '@testing-library/user-event';
import AdvertisementEntry from '../AdvertisementEntry/AdvertisementEntry';

jest.mock('react-toastify', () => ({
toast: {
Expand Down Expand Up @@ -253,6 +254,81 @@ describe('Testing Advertisement Register Component', () => {
expect(queryByText(translations.close)).not.toBeInTheDocument();
});

test('advertisement update', async () => {
const setTimeoutSpy = jest.spyOn(global, 'setTimeout');

const { getByLabelText, queryByText } = render(
<MockedProvider addTypename={false} link={link}>
<Provider store={store}>
<BrowserRouter>
<I18nextProvider i18n={i18n}>
{
<AdvertisementEntry
endDate={new Date()}
startDate={new Date()}
type="POPUP"
name="Advert1"
organizationId="1"
mediaUrl=""
id="1"
/>
}
</I18nextProvider>
</BrowserRouter>
</Provider>
</MockedProvider>
);

await waitFor(async () => {
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(getByLabelText(translations.Rname)).toHaveValue(
'Updated Advertisement'
);

const mediaFile = new File(['media content'], 'test.png', {
type: 'image/png',
});

const mediaInput = getByLabelText(translations.Rmedia);
fireEvent.change(mediaInput, {
target: {
files: [mediaFile],
},
});

const mediaPreview = await screen.findByTestId('mediaPreview');
expect(mediaPreview).toBeInTheDocument();

fireEvent.change(getByLabelText(translations.Rtype), {
target: { value: 'BANNER' },
});
expect(getByLabelText(translations.Rtype)).toHaveValue('BANNER');

fireEvent.change(getByLabelText(translations.RstartDate), {
target: { value: '2023-01-01' },
});
expect(getByLabelText(translations.RstartDate)).toHaveValue('2023-01-01');

fireEvent.change(getByLabelText(translations.RendDate), {
target: { value: '2023-02-01' },
});
expect(getByLabelText(translations.RendDate)).toHaveValue('2023-02-01');

fireEvent.click(screen.getByTestId('addonupdate'));

expect(setTimeoutSpy).toHaveBeenCalled();
});

expect(queryByText(translations.close)).not.toBeInTheDocument();
});

test('Logs error to the console and shows error toast when advertisement creation fails', async () => {
const { getByText, queryByText } = render(
<MockedProvider addTypename={false} link={link}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,13 @@ interface InterfaceFormStateTypes {
endDate: Date;
organizationId: string;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function advertisementRegister({
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
createdBy,
formStatus,
idEdit,
nameEdit,
typeEdit,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
orgIdEdit,
advertisementMediaEdit,
endDateEdit,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
startDateEdit,
}: InterfaceAddOnRegisterProps): JSX.Element {
const { t } = useTranslation('translation', { keyPrefix: 'advertisement' });
Expand Down

0 comments on commit 37cbc7d

Please sign in to comment.