Skip to content

Commit

Permalink
Added Test for PromotedPost.tsx (#1093)
Browse files Browse the repository at this point in the history
  • Loading branch information
aarishshahmohsin authored Nov 26, 2023
1 parent c9087d9 commit 4cbd14b
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import Events from 'screens/UserPortal/Events/Events';
import Tasks from 'screens/UserPortal/Tasks/Tasks';
// import Chat from 'screens/UserPortal/Chat/Chat';
import Advertisements from 'components/Advertisements/Advertisements';

function app(): JSX.Element {
/*const { updatePluginLinks, updateInstalled } = bindActionCreators(
actionCreators,
Expand Down
127 changes: 127 additions & 0 deletions src/components/UserPortal/PromotedPost/PromotedPost.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import React from 'react';
import { act, render, waitFor } from '@testing-library/react';
import { MockedProvider } from '@apollo/react-testing';
import { I18nextProvider } from 'react-i18next';

import { BrowserRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
import { store } from 'state/store';
import i18nForTest from 'utils/i18nForTest';
import { StaticMockLink } from 'utils/StaticMockLink';
import PromotedPost from './PromotedPost';

const link = new StaticMockLink([], true);

async function wait(ms = 100): Promise<void> {
await act(() => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
});
}

let props = {
id: '1',
image: '',
title: 'Test Post',
};

describe('Testing PromotedPost Test', () => {
test('Component should be rendered properly', async () => {
render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<PromotedPost {...props} />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);

await wait();
});

test('Component should be rendered properly if prop image is not undefined', async () => {
props = {
...props,
image: 'promotedPostImage',
};

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

await wait();
});
});

test('Component should display the icon correctly', async () => {
const { queryByTestId } = render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<PromotedPost {...props} />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);

await waitFor(() => {
const icon = queryByTestId('StarPurple500Icon');
expect(icon).toBeInTheDocument();
});
});

test('Component should display the text correctly', async () => {
const { queryAllByText } = render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<PromotedPost {...props} />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);

await waitFor(() => {
const title = queryAllByText('Test Post') as HTMLElement[];
expect(title[0]).toBeInTheDocument();
});
});

test('Component should display the image correctly', async () => {
props = {
...props,
image: 'promotedPostImage',
};
const { queryByRole } = render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<PromotedPost {...props} />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);

await waitFor(() => {
const image = queryByRole('img');
expect(image).toHaveAttribute('src', 'promotedPostImage');
});
});

0 comments on commit 4cbd14b

Please sign in to comment.