Skip to content

Commit

Permalink
Added test for entry file
Browse files Browse the repository at this point in the history
  • Loading branch information
SiddheshKukade committed Nov 17, 2023
1 parent f91b130 commit acc9067
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 7 deletions.
95 changes: 95 additions & 0 deletions src/components/Advertisements/Advertisement.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import React from 'react';
import { getByTestId, getByText, render, screen } from '@testing-library/react';
import { MockedProvider } from '@apollo/client/testing';
import {
ApolloClient,
ApolloProvider,
InMemoryCache,
ApolloLink,
HttpLink,
} from '@apollo/client';

import type { NormalizedCacheObject } from '@apollo/client';
import { BrowserRouter } from 'react-router-dom';
import Advertisement from './Advertisements';
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 { ADVERTISEMENTS_GET } from 'GraphQl/Queries/Queries';

const httpLink = new HttpLink({
uri: BACKEND_URL,
headers: {
authorization: 'Bearer ' + localStorage.getItem('token') || '',
},
});

const client: ApolloClient<NormalizedCacheObject> = new ApolloClient({
cache: new InMemoryCache(),
link: ApolloLink.from([httpLink]),
});
describe('Testing Advertisement Component', () => {
test('Temporary test for Advertisement', () => {
expect(true).toBe(true);
const { getByTestId } = render(
<ApolloProvider client={client}>
<Provider store={store}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
{<Advertisement />}
</I18nextProvider>
</BrowserRouter>
</Provider>
</ApolloProvider>
);
expect(getByTestId('AdEntryStore')).toBeInTheDocument();
});

test('renders advertisement data', async () => {
const mocks = [
{
request: {
query: ADVERTISEMENTS_GET,
variables: {
name: 'Test',
},
},
result: {
data: {
getAdvertisements: [
{
_id: '1',
name: 'Advertisement',
type: 'POPUP',
orgId: 'org1',
link: 'http://example.com',
endDate: new Date(),
startDate: new Date(),
},
// Add more mock data if needed
],
},
loading: false,
},
},
];

const { getByTestId } = render(
<ApolloProvider client={client}>
<Provider store={store}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<MockedProvider mocks={mocks} addTypename={false}>
<Advertisement />
</MockedProvider>
</I18nextProvider>
</BrowserRouter>
</Provider>
</ApolloProvider>
);

expect(getByTestId('AdEntryStore')).toBeInTheDocument();
});
});
10 changes: 7 additions & 3 deletions src/components/Advertisements/Advertisements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,18 @@ export default function advertisements(): JSX.Element {
if (loading) {
return (
<>
<div data-testid="AddOnEntryStore" className={styles.loader}></div>
<div data-testid="AdEntryStore" className={styles.loader}></div>
</>
);
}
console.log('Advertisement data is ', data2);

return (
<>
<OrganizationScreen screenName="Advertisement Store" title={t('title')}>
<OrganizationScreen
data-testid="AdEntryStore"
screenName="Advertisement Store"
title={t('title')}
>
<Row>
<Col col={8}>
<div className={styles.justifysp}>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from 'react';
import { getByTestId, getByText, render, 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 { 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';

const httpLink = new HttpLink({
uri: BACKEND_URL,
headers: {
authorization: 'Bearer ' + localStorage.getItem('token') || '',
},
});

const client: ApolloClient<NormalizedCacheObject> = new ApolloClient({
cache: new InMemoryCache(),
link: ApolloLink.from([httpLink]),
});
describe('Testing Advertisement Entry Component', () => {
test('Temporary test for Advertisement Entry', () => {
const { getByTestId, getByText, getAllByText } = render(
<ApolloProvider client={client}>
<Provider store={store}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
{
<AdvertisementEntry
endDate={new Date()}
startDate={new Date()}
id="1"
key={1}
link="google.com"
name="Advert1"
orgId="1"
type="POPUP"
/>
}
</I18nextProvider>
</BrowserRouter>
</Provider>
</ApolloProvider>
);
expect(getByTestId('AdEntry')).toBeInTheDocument();
expect(getAllByText('POPUP')[0]).toBeInTheDocument();
expect(getAllByText('Advert1')[0]).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ function advertisementEntry({
});
setButtonLoading(false);
};
const s = new Date(endDate);
console.log(s > new Date());
return (
<>
<Row xs={1} md={2} className="g-4">
<Row data-testid="AdEntry" xs={1} md={2} className="g-4">
{Array.from({ length: 4 }).map((_, idx) => (
<Col key={idx}>
<Card>
Expand All @@ -54,7 +52,7 @@ function advertisementEntry({
/>
<Card.Body>
<Card.Title>{name}</Card.Title>
<Card.Text>Ends on {endDate}</Card.Text>
<Card.Text>Ends on {endDate?.toDateString()}</Card.Text>
<Card.Subtitle className="mb-2 text-muted author">
{type}
</Card.Subtitle>
Expand Down

0 comments on commit acc9067

Please sign in to comment.