Added unit tests for components (#9790)

-ActivityFeelList
-CustomPropertyTable
-Users
This commit is contained in:
Aniket Katkar 2023-01-19 11:30:36 +05:30 committed by GitHub
parent be8daeba6d
commit ea9ef88af5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 98 additions and 6 deletions

View File

@ -11,7 +11,7 @@
* limitations under the License.
*/
import { findByTestId, render } from '@testing-library/react';
import { findByTestId, queryByTestId, render } from '@testing-library/react';
import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import ActivityFeedList from './ActivityFeedList';
@ -85,6 +85,10 @@ jest.mock('../NoFeedPlaceholder/NoFeedPlaceholder', () => {
return jest.fn().mockReturnValue(<p>NoFeedPlaceholder</p>);
});
jest.mock('../../common/error-with-placeholder/ErrorPlaceHolder', () => {
return jest.fn().mockReturnValue(<p>ErrorPlaceHolder</p>);
});
jest.mock('./FeedListBody', () => {
return jest.fn().mockReturnValue(<p>FeedListBody</p>);
});
@ -115,4 +119,40 @@ describe('Test FeedList Component', () => {
expect(feed1).toBeInTheDocument();
expect(feed2).toBeInTheDocument();
});
it('No data placeholders should not displayed if feedList is empty but feeds are loading', async () => {
const { container } = render(
<ActivityFeedList {...mockFeedListProp} isFeedLoading feedList={[]} />,
{
wrapper: MemoryRouter,
}
);
const noFeedPlaceholderContainer = queryByTestId(
container,
'no-data-placeholder-container'
);
expect(noFeedPlaceholderContainer).toBeNull();
});
it('No data placeholders should be displayed if feedList is empty and feeds are not loading', async () => {
const { container } = render(
<ActivityFeedList
{...mockFeedListProp}
feedList={[]}
isFeedLoading={false}
/>,
{
wrapper: MemoryRouter,
}
);
const noFeedPlaceholderContainer = await findByTestId(
container,
'no-data-placeholder-container'
);
expect(noFeedPlaceholderContainer).toBeInTheDocument();
});
});

View File

@ -297,7 +297,7 @@ const ActivityFeedList: FC<ActivityFeedListProp> = ({
</>
) : (
!isFeedLoading && (
<div className="h-min-50">
<div className="h-min-50" data-testid="no-data-placeholder-container">
{entityName && feedFilter === FeedFilter.ALL && !threadType ? (
<NoFeedPlaceholder entityName={entityName} />
) : !refreshFeedCount ? (

View File

@ -235,4 +235,33 @@ describe('Test User Component', () => {
expect(inheritedRoles).toBeInTheDocument();
});
it('MyData tab should show loader if the data is loading', async () => {
const { container } = render(
<Users
userData={mockUserData}
{...mockProp}
isUserEntitiesLoading
tab="mydata"
/>,
{
wrapper: MemoryRouter,
}
);
const inheritedRoles = await findByTestId(container, 'loader');
expect(inheritedRoles).toBeInTheDocument();
});
it('Following tab should show loader if the data is loading', async () => {
const { container } = render(
<Users userData={mockUserData} {...mockProp} isUserEntitiesLoading />,
{
wrapper: MemoryRouter,
}
);
const inheritedRoles = await findByTestId(container, 'loader');
expect(inheritedRoles).toBeInTheDocument();
});
});

View File

@ -11,7 +11,12 @@
* limitations under the License.
*/
import { render, screen } from '@testing-library/react';
import {
act,
render,
screen,
waitForElementToBeRemoved,
} from '@testing-library/react';
import React from 'react';
import { getTypeByFQN } from 'rest/metadataTypeAPI';
import { EntityType } from '../../../enums/entity.enum';
@ -54,6 +59,10 @@ jest.mock('../error-with-placeholder/ErrorPlaceHolder', () => {
return jest.fn().mockReturnValue(<div>ErrorPlaceHolder.component</div>);
});
jest.mock('components/Loader/Loader', () => {
return jest.fn().mockReturnValue(<div data-testid="loader">Loader</div>);
});
jest.mock('rest/metadataTypeAPI', () => ({
getTypeByFQN: jest.fn().mockImplementation(() =>
Promise.resolve({
@ -73,7 +82,9 @@ const mockProp = {
describe('Test CustomProperty Table Component', () => {
it('Should render table component', async () => {
render(<CustomPropertyTable {...mockProp} />);
await act(async () => {
render(<CustomPropertyTable {...mockProp} />);
});
const table = await screen.findByTestId('custom-properties-table');
expect(table).toBeInTheDocument();
@ -91,9 +102,21 @@ describe('Test CustomProperty Table Component', () => {
(getTypeByFQN as jest.Mock).mockImplementationOnce(() =>
Promise.resolve({ customProperties: [] })
);
const { findByText } = render(<CustomPropertyTable {...mockProp} />);
const noDataPlaceHolder = await findByText('ErrorPlaceHolder.component');
await act(async () => {
render(<CustomPropertyTable {...mockProp} />);
});
const noDataPlaceHolder = await screen.findByText(
'ErrorPlaceHolder.component'
);
expect(noDataPlaceHolder).toBeInTheDocument();
});
it('Loader should be shown while loading the custom properties', async () => {
(getTypeByFQN as jest.Mock).mockResolvedValueOnce(Promise.resolve({}));
render(<CustomPropertyTable {...mockProp} />);
// To check if loader was rendered when the loading state was true and then removed after loading is false
await waitForElementToBeRemoved(() => screen.getByTestId('loader'));
});
});