Skip to content

Commit

Permalink
test: webui user management unit test [DET-7968] (#4809)
Browse files Browse the repository at this point in the history
  • Loading branch information
gt2345 authored Aug 19, 2022
1 parent 0f4bf86 commit 2cb0c9b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
62 changes: 62 additions & 0 deletions webui/react/src/pages/Settings/UserManagement.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import { HelmetProvider } from 'react-helmet-async';
import { Router } from 'react-router-dom';

import StoreProvider from 'contexts/Store';
import history from 'shared/routes/history';
import { DetailedUser } from 'types';

import UserManagement, { CREAT_USER_LABEL, CREATE_USER, USER_TITLE } from './UserManagement';

const DISPLAY_NAME = 'Test Name';
const USERNAME = 'test_username1';

const user = userEvent.setup();

jest.mock('services/api', () => ({
getUsers: () => {
const currentUser: DetailedUser = {
displayName: DISPLAY_NAME,
id: 1,
isActive: true,
isAdmin: false,
username: USERNAME,
};
const users: Array<DetailedUser> = [ currentUser ];
return Promise.resolve({ pagination: { total: 1 }, users });
},
}));

const setup = () => render(
<StoreProvider>
<DndProvider backend={HTML5Backend}>
<HelmetProvider>
<Router history={history}>
<UserManagement />
</Router>
</HelmetProvider>
</DndProvider>
</StoreProvider>,
);

describe('UserManagement', () => {
it('should render table/button correct values', async () => {
await waitFor(() => setup());

expect(screen.getByText(CREATE_USER)).toBeInTheDocument();
expect(screen.getByText(USER_TITLE)).toBeInTheDocument();
expect(screen.getByText(DISPLAY_NAME)).toBeInTheDocument();
expect(screen.getByText(USERNAME)).toBeInTheDocument();
});

it('should render modal for create user when click the button', async () => {
await waitFor(() => setup());
await user.click(screen.getByLabelText(CREAT_USER_LABEL));

expect(screen.getAllByText('Create User')).toHaveLength(2);
});
});
8 changes: 6 additions & 2 deletions webui/react/src/pages/Settings/UserManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import css from './UserManagement.module.scss';
import settingsConfig, { DEFAULT_COLUMN_WIDTHS,
UserManagementSettings } from './UserManagement.settings';

export const USER_TITLE = 'Users';
export const CREATE_USER = 'New User';
export const CREAT_USER_LABEL = 'new_user';

const UserManagement: React.FC = () => {
const [ users, setUsers ] = useState<DetailedUser[]>([]);
const [ isLoading, setIsLoading ] = useState(true);
Expand Down Expand Up @@ -185,10 +189,10 @@ const UserManagement: React.FC = () => {
containerRef={pageRef}
options={(
<Space>
<Button onClick={onClickCreateUser}>New User</Button>
<Button aria-label={CREAT_USER_LABEL} onClick={onClickCreateUser}>{CREATE_USER}</Button>
</Space>
)}
title="Users">
title={USER_TITLE}>
<div className={css.usersTable}>{table}</div>
{modalCreateUserContextHolder}
</Page>
Expand Down

0 comments on commit 2cb0c9b

Please sign in to comment.