Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Test for PromotedPost.tsx #1093

Merged
merged 1 commit into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});
Loading