Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
peterMuriuki committed Nov 13, 2024
1 parent 814817e commit 671ae18
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 327 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Provider } from 'react-redux';
import { store } from '@opensrp/store';
import nock from 'nock';
import { cleanup, fireEvent, render } from '@testing-library/react';
import { waitFor } from '@testing-library/dom';
import { waitFor, within } from '@testing-library/dom';
import { createMemoryHistory } from 'history';
import { authenticateUser } from '@onaio/session-reducer';
import fetch from 'jest-fetch-mock';
Expand All @@ -35,6 +35,9 @@ import { practitionerResourceType, practitionerRoleResourceType } from '../../..
import { fetchKeycloakUsers } from '@opensrp/user-management';
import { history } from '@onaio/connected-reducer-registry';
import { opensrpI18nInstance } from '@opensrp/i18n';
import { setConfig } from '@opensrp/pkg-config';

setConfig('projectCode', 'giz');

jest.mock('fhirclient', () => {
return jest.requireActual('fhirclient/lib/entry/browser');
Expand Down Expand Up @@ -245,11 +248,11 @@ test('renders correctly for edit user', async () => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
userEvent.type(usernameInput!, 'flopo');

// change mark as practitioner to tue
const yesMarkPractitioner = document.querySelectorAll('input[name="active"]')[0];
const enabledFieldGroup = document.querySelector('#active') as HTMLElement;
const yesMarkPractitioner = within(enabledFieldGroup).getByRole('radio', { name: /yes/i });
userEvent.click(yesMarkPractitioner);

const markSupervisor = document.querySelectorAll('input[name="userType"]')[1];
const markSupervisor = document.querySelector('input[value="supervisor"]') as HTMLElement;
userEvent.click(markSupervisor);

const submitButton = document.querySelector('button[type="submit"]');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const newPractitioner = {
name: [{ use: 'official', family: 'plotus', given: ['flotus', ''] }],
telecom: [{ system: 'email', value: '[email protected]' }],
};

export const newPractitionerRole = {
resourceType: 'PractitionerRole',
id: 'acb9d47e-7247-448f-be93-7a193a5312da',
Expand All @@ -88,7 +89,11 @@ export const newPractitionerRole = {
code: [
{
coding: [
{ system: 'http://snomed.info/sct', code: '236321002', display: 'Supervisor (occupation)' },
{
system: 'http://snomed.info/sct',
code: '405623001',
display: 'Assigned practitioner',
},
],
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ test('renders correctly for edit user', async () => {
const yesMarkPractitioner = document.querySelectorAll('input[name="active"]')[0];
userEvent.click(yesMarkPractitioner);

const markSupervisor = document.querySelectorAll('input[name="userType"]')[1];
const markSupervisor = document.querySelector('input[value="supervisor"]') as HTMLElement;
userEvent.click(markSupervisor);

const submitButton = document.querySelector('button[type="submit"]');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,6 @@ test('renders correctly for new user', async () => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
userEvent.type(usernameInput!, 'flopo');

const markSupervisor = document.querySelectorAll('input[name="userType"]')[1];
userEvent.click(markSupervisor);

const submitButton = document.querySelector('button[type="submit"]');

// find antd Select with id 'fhirCoreAppId' in the 'Form' component
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`components/CreateEditUser loads edit user page without breaking if user is not a practitioner 1`] = `"Edit User | opensrpFirst NameLast NameEmailUsernameUser TypePractitionerSupervisorEnable userYesNoMark as PractitionerYesNoKeycloak User GroupAdminAdmin 2New Group Application IDSaveCancel"`;

exports[`components/CreateEditUser renders correctly 1`] = `
Object {
"children": <Col
span={24}
>
<UserForm
extraData={Object {}}
extraFormFields={Array []}
initialValues={
Object {
"active": true,
"contact": undefined,
"enabled": true,
"fhirCoreAppId": undefined,
"firstName": "",
Expand All @@ -25,9 +25,21 @@ Object {
"username": "",
}
}
isFHIRInstance={false}
keycloakBaseURL="https://keycloak-stage.smartregister.org/auth/admin/realms/opensrp-web-stage"
practitionerUpdaterFactory={[Function]}
renderFields={
Array [
"firstName",
"lastName",
"email",
"username",
"userType",
"enabled",
"active",
"userGroups",
"fhirCoreAppId",
]
}
userGroups={
Array [
Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
removeKeycloakUsers,
} from '../../../ducks/user';
import { authenticateUser } from '@onaio/session-reducer';
import { QueryClientProvider, QueryClient } from 'react-query';

import {
keycloakUser,
Expand All @@ -32,6 +33,12 @@ import { defaultUserFormInitialValues } from '../../forms/UserForm';
import { getFormValues } from '../../forms/UserForm/utils';
import { Dictionary } from '@onaio/utils/dist/types/types';

const queryClient = new QueryClient();

const QueryWrapper = ({ children }: { children: JSX.Element }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

jest.mock('@opensrp/store', () => {
const actualStore = jest.requireActual('@opensrp/store');
return {
Expand Down Expand Up @@ -119,11 +126,13 @@ describe('components/CreateEditUser', () => {
fetch.mockResponseOnce(JSON.stringify(requiredActions));

const wrapper = mount(
<Provider store={store}>
<Router history={history}>
<CreateEditUser {...propsCreate} />
</Router>
</Provider>
<QueryWrapper>
<Provider store={store}>
<Router history={history}>
<CreateEditUser {...propsCreate} />
</Router>
</Provider>
</QueryWrapper>
);

await act(async () => {
Expand Down Expand Up @@ -159,11 +168,13 @@ describe('components/CreateEditUser', () => {
.once(JSON.stringify(practitioner1));

const wrapper = mount(
<Provider store={store}>
<Router history={history}>
<ConnectedCreateEditUser {...props} />
</Router>
</Provider>
<QueryWrapper>
<Provider store={store}>
<Router history={history}>
<ConnectedCreateEditUser {...props} />
</Router>
</Provider>
</QueryWrapper>
);

await act(async () => {
Expand Down Expand Up @@ -193,9 +204,11 @@ describe('components/CreateEditUser', () => {
fetch.mockResponseOnce(JSON.stringify(keycloakUser));

const wrapper = mount(
<Router history={history}>
<CreateEditUser {...propsCreate} />
</Router>
<QueryWrapper>
<Router history={history}>
<CreateEditUser {...propsCreate} />
</Router>
</QueryWrapper>
);

expect(wrapper.exists('.ant-spin')).toBeTruthy();
Expand Down Expand Up @@ -239,11 +252,13 @@ describe('components/CreateEditUser', () => {
};

const wrapper = mount(
<Provider store={store}>
<Router history={history}>
<ConnectedCreateEditUser {...propsPageRefreshed} />
</Router>
</Provider>
<QueryWrapper>
<Provider store={store}>
<Router history={history}>
<ConnectedCreateEditUser {...propsPageRefreshed} />
</Router>
</Provider>
</QueryWrapper>
);

// Loader should be displayed
Expand Down Expand Up @@ -330,9 +345,11 @@ describe('components/CreateEditUser', () => {

const wrapper = mount(
<Provider store={store}>
<Router history={history}>
<ConnectedCreateEditUser {...userProps} />
</Router>
<QueryWrapper>
<Router history={history}>
<ConnectedCreateEditUser {...userProps} />
</Router>
</QueryWrapper>
</Provider>
);

Expand All @@ -345,8 +362,7 @@ describe('components/CreateEditUser', () => {
});
expect(toJson(wrapper.find('.ant-spin'))).toBeFalsy();
// eslint-disable-next-line no-irregular-whitespace
const newLocal = `"Edit User | opensrpFirst NameLast NameEmailUsernameEnable userYesNoMark as PractitionerYesNoKeycloak User GroupAdminAdmin 2New Group SaveCancel"`;
expect(wrapper.text()).toMatchInlineSnapshot(newLocal);
expect(wrapper.text()).toMatchSnapshot();
wrapper.unmount();
});

Expand Down Expand Up @@ -376,11 +392,13 @@ describe('components/CreateEditUser', () => {
};

const wrapper = mount(
<Provider store={store}>
<Router history={history}>
<ConnectedCreateEditUser {...propsPageRefreshed} />
</Router>
</Provider>
<QueryWrapper>
<Provider store={store}>
<Router history={history}>
<ConnectedCreateEditUser {...propsPageRefreshed} />
</Router>
</Provider>
</QueryWrapper>
);

await act(async () => {
Expand Down Expand Up @@ -408,11 +426,13 @@ describe('components/CreateEditUser', () => {
);

const wrapper = mount(
<Provider store={store}>
<Router history={history}>
<ConnectedCreateEditUser {...props} />
</Router>
</Provider>
<QueryWrapper>
<Provider store={store}>
<Router history={history}>
<ConnectedCreateEditUser {...props} />
</Router>
</Provider>
</QueryWrapper>
);

await act(async () => {
Expand All @@ -438,11 +458,13 @@ describe('components/CreateEditUser', () => {
.once(JSON.stringify(requiredActions));

const wrapper = mount(
<Provider store={store}>
<Router history={history}>
<ConnectedCreateEditUser {...props} />
</Router>
</Provider>
<QueryWrapper>
<Provider store={store}>
<Router history={history}>
<ConnectedCreateEditUser {...props} />
</Router>
</Provider>
</QueryWrapper>
);

expect(wrapper.exists('.ant-spin')).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,6 @@ export const defaultUserFormInitialValues: FormFields = {
fhirCoreAppId: undefined,
};

UserForm.defaultProps = {
initialValues: defaultUserFormInitialValues,
practitionerUpdaterFactory: postPutPractitioner,
};

export const commonFhirFields: FormFieldsKey[] = [
firstNameField,
lastNameField,
Expand All @@ -369,4 +364,10 @@ export const commonFhirFields: FormFieldsKey[] = [
fhirCoreAppIdField,
];

UserForm.defaultProps = {
initialValues: defaultUserFormInitialValues,
practitionerUpdaterFactory: postPutPractitioner,
renderFields: commonFhirFields,
};

export { UserForm };
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,6 @@ exports[`components/forms/UserForm edits user: readonly username edit 1`] = `
/>
`;

exports[`components/forms/UserForm form validation works for contact field: contact input 1`] = `
<input
aria-required="true"
className="ant-input css-dev-only-do-not-override-k7429z"
disabled={false}
id="contact"
onBlur={[Function]}
onChange={[Function]}
onFocus={[Function]}
onKeyDown={[Function]}
style={Object {}}
type="text"
value=""
/>
`;

exports[`components/forms/UserForm form validation works for contact field: contact label 1`] = `
<label
className="ant-form-item-required"
htmlFor="contact"
title="Contact"
>
Contact
</label>
`;

exports[`components/forms/UserForm renders correctly: email input 1`] = `
<input
className="ant-input css-dev-only-do-not-override-k7429z"
Expand Down Expand Up @@ -73,7 +47,6 @@ Array [
checked={true}
className="ant-radio-input"
disabled={false}
name="enabled"
onChange={[Function]}
type="radio"
value={true}
Expand All @@ -82,7 +55,6 @@ Array [
checked={false}
className="ant-radio-input"
disabled={false}
name="enabled"
onChange={[Function]}
type="radio"
value={false}
Expand Down Expand Up @@ -204,6 +176,7 @@ Object {
No
</Radio>
</Memo>,
"hidden": false,
"id": "practitionerToggle",
"label": "Mark as Practitioner",
"name": "active",
Expand All @@ -230,6 +203,7 @@ Object {
No
</Radio>
</Memo>,
"hidden": false,
"id": "practitionerToggle",
"label": "Mark as Practitioner",
"name": "active",
Expand Down
Loading

0 comments on commit 671ae18

Please sign in to comment.