UI: unit test for users page component (#6449)

* added users and admin page to global settings menu

* addressing comment

* added unit test for userListPage with 96% statement coverage

* addressing comment
This commit is contained in:
Shailesh Parmar 2022-08-04 14:16:01 +05:30 committed by GitHub
parent 5754b1424a
commit dbc4e7fe36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 246 additions and 0 deletions

View File

@ -0,0 +1,189 @@
/*
* Copyright 2022 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,
cleanup,
fireEvent,
render,
screen,
waitForDomChange,
} from '@testing-library/react';
import React from 'react';
import { searchData } from '../../axiosAPIs/miscAPI';
import { getUsers } from '../../axiosAPIs/userAPI';
import { GlobalSettingOptions } from '../../constants/globalSettings.constants';
import { MOCK_USER_DATA } from './mockUserData';
import UserListPageV1 from './UserListPageV1';
const mockParam = {
tab: GlobalSettingOptions.USERS,
};
jest.mock('react-router-dom', () => ({
useParams: jest.fn().mockImplementation(() => mockParam),
}));
jest.mock('../../axiosAPIs/userAPI', () => ({
getUsers: jest.fn().mockImplementation(() =>
Promise.resolve({
data: MOCK_USER_DATA,
})
),
}));
jest.mock('../../axiosAPIs/miscAPI', () => ({
searchData: jest.fn().mockImplementation(() =>
Promise.resolve({
data: MOCK_USER_DATA,
})
),
}));
jest.mock('../../components/UserList/UserListV1', () => {
return jest.fn().mockImplementation((prop) => (
<div>
<p>UserList.component</p>
<button onClick={prop.afterDeleteAction}>afterDeleteAction</button>
<button onClick={() => prop.onPagingChange('next', 2)}>
onPagingChange
</button>
<input
data-testid="search-input"
type="text"
onChange={(e) => prop.onSearch(e.target.value)}
/>
<input
data-testid="show-deleted-toggle"
type="checkbox"
onChange={(e) => prop.onShowDeletedUserChange(e.target.checked)}
/>
</div>
));
});
jest.mock('../../components/Loader/Loader', () => {
return jest.fn().mockImplementation(() => <div>Loader.component</div>);
});
describe('Test UserListPage component', () => {
beforeEach(() => {
cleanup();
});
it('should render without crashing', async () => {
render(<UserListPageV1 />);
const userlist = await screen.findByText('UserList.component');
expect(userlist).toBeInTheDocument();
});
it('getUsers API should not call if its not user/admin page', async () => {
mockParam.tab = GlobalSettingOptions.WEBHOOK;
const userAPI = getUsers as jest.Mock;
render(<UserListPageV1 />);
const userlist = await screen.findByText('UserList.component');
expect(userlist).toBeInTheDocument();
expect(userAPI).not.toHaveBeenCalled();
});
it('Component should render without crashing, even if getUsers api send empty response', async () => {
(getUsers as jest.Mock).mockImplementationOnce(() =>
Promise.resolve({ data: '' })
);
render(<UserListPageV1 />);
const userlist = await screen.findByText('UserList.component');
expect(userlist).toBeInTheDocument();
});
it('handleFetch function should work properly', async () => {
const userAPI = getUsers as jest.Mock;
render(<UserListPageV1 />);
const afterDeleteAction = await screen.findByText('afterDeleteAction');
const userlist = await screen.findByText('UserList.component');
expect(afterDeleteAction).toBeInTheDocument();
await act(async () => {
fireEvent.click(afterDeleteAction);
});
expect(userAPI).toHaveBeenCalled();
expect(userlist).toBeInTheDocument();
});
it('handleSearch function should work properly', async () => {
mockParam.tab = GlobalSettingOptions.ADMINS;
const userAPI = getUsers as jest.Mock;
const searchAPI = searchData as jest.Mock;
render(<UserListPageV1 />);
const searchBox = await screen.findByTestId('search-input');
const userlist = await screen.findByText('UserList.component');
expect(searchBox).toBeInTheDocument();
await act(async () => {
fireEvent.change(searchBox, { target: { value: 'test' } });
});
expect(searchBox).toHaveValue('test');
expect(searchAPI).toHaveBeenCalled();
await act(async () => {
fireEvent.change(searchBox, { target: { value: '' } });
});
waitForDomChange();
expect(searchBox).toHaveValue('');
expect(userAPI).toHaveBeenCalled();
expect(userlist).toBeInTheDocument();
});
it('handleShowDeletedUserChange function should work properly', async () => {
const userAPI = getUsers as jest.Mock;
render(<UserListPageV1 />);
const toggle = await screen.findByTestId('show-deleted-toggle');
const userlist = await screen.findByText('UserList.component');
expect(toggle).toBeInTheDocument();
await act(async () => {
fireEvent.click(toggle);
});
expect(toggle).toBeChecked();
expect(userAPI).toHaveBeenCalled();
expect(userlist).toBeInTheDocument();
});
it('handlePagingChange function should work properly', async () => {
const userAPI = getUsers as jest.Mock;
render(<UserListPageV1 />);
const userlist = await screen.findByText('UserList.component');
const pagin = await screen.findByText('onPagingChange');
expect(pagin).toBeInTheDocument();
await act(async () => {
fireEvent.click(pagin);
});
expect(userAPI).toHaveBeenCalled();
expect(userlist).toBeInTheDocument();
});
});

View File

@ -0,0 +1,57 @@
/*
* Copyright 2022 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.
*/
export const MOCK_USER_DATA = {
data: [
{
id: '4efd3518-16ae-483c-8c99-d622bcbfbfab',
name: 'aaron_johnson0',
fullyQualifiedName: 'aaron_johnson0',
displayName: 'Aaron Johnson',
version: 0.4,
updatedAt: 1659077698874,
updatedBy: 'anonymous',
email: 'aaron_johnson0@gmail.com',
href: 'http://localhost:8585/api/v1/users/4efd3518-16ae-483c-8c99-d622bcbfbfab',
isAdmin: false,
teams: [
{
id: '71b6a498-6a48-4529-af1c-3bb28d3cd67e',
type: 'team',
name: 'Cloud_Infra',
fullyQualifiedName: 'Cloud_Infra',
description: 'This is Cloud_Infra description.',
displayName: 'Cloud_Infra',
deleted: false,
href: 'http://localhost:8585/api/v1/teams/71b6a498-6a48-4529-af1c-3bb28d3cd67e',
},
],
deleted: false,
roles: [],
inheritedRoles: [
{
id: '8ca4fb74-ee36-453c-91ec-7bd5fc361e00',
type: 'role',
name: 'DataConsumer',
fullyQualifiedName: 'DataConsumer',
displayName: 'Data Consumer',
deleted: false,
href: 'http://localhost:8585/api/v1/roles/8ca4fb74-ee36-453c-91ec-7bd5fc361e00',
},
],
},
],
paging: {
total: 1,
},
};