Skip to content

Commit

Permalink
added unit test for ApplicationPage component (open-metadata#15124)
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishek332 authored and sushi30 committed Feb 12, 2024
1 parent 94397b2 commit 057ca22
Showing 1 changed file with 167 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* Copyright 2024 Collate.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { act, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import { ROUTES } from '../../constants/constants';
import { CursorType } from '../../enums/pagination.enum';
import ApplicationPage from './ApplicationPage';

const mockPush = jest.fn();

jest.mock('react-router-dom', () => ({
useHistory: jest.fn().mockImplementation(() => ({
push: mockPush,
})),
}));

jest.mock(
'../../components/Applications/ApplicationCard/ApplicationCard.component',
() =>
jest.fn(({ onClick }) => <button onClick={onClick}>ApplicationCard</button>)
);

jest.mock('../../components/common/ErrorWithPlaceholder/ErrorPlaceHolder', () =>
jest.fn().mockImplementation(() => <div>ErrorPlaceHolder</div>)
);

jest.mock('../../components/common/NextPrevious/NextPrevious', () => {
return jest.fn().mockImplementation(({ pagingHandler }) => (
<button
onClick={() =>
pagingHandler({
currentPage: 5,
cursorType: CursorType.AFTER,
})
}>
NextPrevious
</button>
));
});

jest.mock(
'../../components/common/TitleBreadcrumb/TitleBreadcrumb.component',
() => jest.fn(() => <div>TitleBreadcrumb</div>)
);

jest.mock('../../components/PageHeader/PageHeader.component', () =>
jest.fn(() => <div>PageHeader</div>)
);

jest.mock('../../components/PageLayoutV1/PageLayoutV1', () => {
return jest.fn().mockImplementation(({ children }) => <div>{children}</div>);
});

jest.mock('../../hooks/paging/usePaging', () => ({
usePaging: jest.fn().mockReturnValue({
currentPage: 1,
paging: {},
pageSize: 10,
showPagination: true,
handlePageChange: jest.fn(),
handlePagingChange: jest.fn(),
handlePageSizeChange: jest.fn(),
}),
}));

const mockGetApplicationList = jest
.fn()
.mockResolvedValue({ data: [{}], paging: {} });

jest.mock('../../rest/applicationAPI', () => ({
getApplicationList: jest.fn(() => mockGetApplicationList()),
}));

jest.mock('../../utils/EntityUtils', () => ({
getEntityName: jest.fn().mockReturnValue('Test'),
}));

jest.mock('../../utils/GlobalSettingsUtils', () => ({
getSettingPageEntityBreadCrumb: jest.fn().mockImplementation(() => [
{
name: 'setting',
url: ROUTES.SETTINGS,
},
]),
}));

jest.mock('../../utils/RouterUtils', () => ({
getApplicationDetailsPath: jest.fn(),
}));

const mockShowErrorToast = jest.fn();

jest.mock('../../utils/ToastUtils', () => ({
showErrorToast: jest.fn((...args) => mockShowErrorToast(...args)),
}));

describe('ApplicationPage', () => {
it('should render all necessary elements', async () => {
await act(async () => {
render(<ApplicationPage />);
});

expect(screen.getByText('TitleBreadcrumb')).toBeInTheDocument();
expect(screen.getByText('PageHeader')).toBeInTheDocument();
expect(screen.getByText('label.disabled')).toBeInTheDocument();
expect(screen.getByText('ApplicationCard')).toBeInTheDocument();
expect(screen.getByText('NextPrevious')).toBeInTheDocument();
expect(screen.getByText('label.add-entity')).toBeInTheDocument();
});

it('actions check', async () => {
await act(async () => {
render(<ApplicationPage />);
});

await act(async () => {
userEvent.click(screen.getByTestId('show-disabled'));
userEvent.click(
screen.getByRole('button', {
name: 'NextPrevious',
})
);
});

// 1 time call when render component, 2 time call for above actions
expect(mockGetApplicationList).toHaveBeenCalledTimes(3);

// add application
userEvent.click(
screen.getByRole('button', {
name: 'label.add-entity',
})
);

// view app details
userEvent.click(
screen.getByRole('button', {
name: 'ApplicationCard',
})
);

// add application + view app details
expect(mockPush).toHaveBeenCalledTimes(2);
});

it('error while fetching application list', async () => {
mockGetApplicationList.mockRejectedValueOnce('ERROR');

await act(async () => {
render(<ApplicationPage />);
});

expect(mockShowErrorToast).toHaveBeenCalledWith('ERROR');
expect(screen.getByText('ErrorPlaceHolder')).toBeInTheDocument();
});
});

0 comments on commit 057ca22

Please sign in to comment.