test: WebUI add test for settings account
import { render, screen } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
import React, { useCallback, useEffect } from 'react';
import StoreProvider, { StoreAction, useStoreDispatch } from 'contexts/Store';
+import { NEW_PASSWORD_LABEL } from 'hooks/useModal/UserSettings/useModalPasswordChange';
+import { PatchUserParams } from 'services/types';
import { DetailedUser } from 'types';
import SettingsAccount, { CHANGE_PASSWORD_TEXT } from './SettingsAccount';
@@ -17,7 +20,23 @@ const currentUser: DetailedUser = {
username: USERNAME,
};
+const mockPatchUser = jest.fn();
+
+jest.mock('services/api', () => ({
+ patchUser: (params: PatchUserParams) => {
+ mockPatchUser(params);
+ return Promise.resolve({
+ displayName: params.userParams.displayName,
+ id: 1,
+ isActive: true,
+ isAdmin: false,
+ username: USERNAME,
+ });
+ },
+}));
+
const users: Array<DetailedUser> = [ currentUser ];
+const user = userEvent.setup();
const Container: React.FC = () => {
const storeDispatch = useStoreDispatch();
@@ -49,4 +68,18 @@ describe('SettingsAccount', () => {
const editor = container.querySelector(`[data-value="${DISPLAY_NAME}"]`);
expect(editor).toBeInTheDocument();
});
+ it('should be able to change display name', async () => {
+ setup();
+ await user.type(screen.getByPlaceholderText('Add display name'), 'a');
+ await user.keyboard('{enter}');
+ expect(mockPatchUser).toHaveBeenCalledWith({
+ userId: 1,
+ userParams: { displayName: `${DISPLAY_NAME}a` },
+ });
+ });
+ it('should be able to view change password modal when click', async () => {
+ setup();
+ await user.click(screen.getByText(CHANGE_PASSWORD_TEXT));
+ expect(screen.getByText(NEW_PASSWORD_LABEL)).toBeInTheDocument();
+ });
});
