Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(test) O3-2216: Add tests for id-field.component.tsx #748

Merged
merged 6 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
export const mockedIdentifierTypes = [
{
fieldName: 'openMrsId',
format: null,
identifierSources: [
{
uuid: '8549f706-7e85-4c1d-9424-217d50a2988b',
name: 'Generator for OpenMRS ID',
description: 'Generator for OpenMRS ID',
baseCharacterSet: '0123456789ACDEFGHJKLMNPRTUVWXY',
prefix: '',
autoGenerationOption: {
manualEntryEnabled: false,
automaticGenerationEnabled: true,
},
},
{
uuid: '01af8526-cea4-4175-aa90-340acb411771',
name: 'Generator 2 for OpenMRS ID',
description: 'Generator 2 for OpenMRS ID',
baseCharacterSet: '0123456789ACDEFGHJKLMNPRTUVWXY',
prefix: '',
autoGenerationOption: {
manualEntryEnabled: true,
automaticGenerationEnabled: true,
},
},
],
isPrimary: true,
name: 'OpenMRS ID',
required: true,
uniquenessBehavior: 'UNIQUE',
uuid: '05a29f94-c0ed-11e2-94be-8c13b969e334',
autoGenerationSource: null,
},
{
fieldName: 'idCard',
format: null,
identifierSources: [],
isPrimary: false,
name: 'ID Card',
required: false,
uniquenessBehavior: 'UNIQUE',
uuid: 'b4143563-16cd-4439-b288-f83d61670fc8',
},
{
fieldName: 'legacyId',
format: null,
identifierSources: [],
isPrimary: false,
name: 'Legacy ID',
required: false,
uniquenessBehavior: null,
uuid: '22348099-3873-459e-a32e-d93b17eda533',
},
{
fieldName: 'oldIdentificationNumber',
format: '',
identifierSources: [],
isPrimary: false,
name: 'Old Identification Number',
required: false,
uniquenessBehavior: null,
uuid: '8d79403a-c2cc-11de-8d13-0010c6dffd0f',
},
{
fieldName: 'openMrsIdentificationNumber',
format: '',
identifierSources: [],
isPrimary: false,
name: 'OpenMRS Identification Number',
required: false,
uniquenessBehavior: null,
uuid: '8d793bee-c2cc-11de-8d13-0010c6dffd0f',
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export const openmrsID = {
Piumal1999 marked this conversation as resolved.
Show resolved Hide resolved
denniskigen marked this conversation as resolved.
Show resolved Hide resolved
name: 'OpenMRS ID',
fieldName: 'openMrsId',
required: true,
uuid: '05a29f94-c0ed-11e2-94be-8c13b969e334',
format: null,
isPrimary: true,
identifierSources: [
{
uuid: '691eed12-c0f1-11e2-94be-8c13b969e334',
name: 'Generator 1 for OpenMRS ID',
autoGenerationOption: {
manualEntryEnabled: false,
automaticGenerationEnabled: true,
},
},
{
uuid: '01af8526-cea4-4175-aa90-340acb411771',
name: 'Generator 2 for OpenMRS ID',
autoGenerationOption: {
manualEntryEnabled: true,
automaticGenerationEnabled: true,
},
},
],
autoGenerationSource: null,
};
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const Identifiers: React.FC = () => {

if (isLoading) {
return (
<div className={styles.halfWidthInDesktopView}>
<div data-testid="loading-skeleton" className={styles.halfWidthInDesktopView}>
<div className={styles.identifierLabelText}>
<h4 className={styles.productiveHeading02Light}>{t('idFieldLabelText', 'Identifiers')}</h4>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { Identifiers } from './id-field.component';
import { Resources, ResourcesContext } from '../../../offline.resources';
import { Form, Formik } from 'formik';
import { PatientRegistrationContext } from '../../patient-registration-context';
import { openmrsID } from '../__mocks__/identifiers.mock';
import { mockedIdentifierTypes } from '../__mocks__/identifier-types.mock';

jest.mock('@openmrs/esm-framework', () => ({
...jest.requireActual('@openmrs/esm-framework'),
useConfig: jest.fn().mockImplementation(() => ({
defaultPatientIdentifierTypes: ['OpenMRS ID'],
})),
}));

describe('Identifiers', () => {
const mockResourcesContextValue = {
addressTemplate: [],
currentSession: {
authenticated: true,
sessionId: 'JSESSION',
currentProvider: { uuid: 'provider-uuid', identifier: 'PRO-123' },
},
relationshipTypes: [],
identifierTypes: [...mockedIdentifierTypes],
} as Resources;

it('should render loading skeleton when identifier types are loading', () => {
render(
<ResourcesContext.Provider value={[]}>
<Formik initialValues={{}} onSubmit={null}>
<Form>
<PatientRegistrationContext.Provider
value={{
setFieldValue: jest.fn(),
initialFormValues: { identifiers: { ...mockedIdentifierTypes[0] } },
setInitialFormValues: jest.fn(),
values: {
identifiers: { openmrsID },
},
}}>
<Identifiers />
</PatientRegistrationContext.Provider>
</Form>
</Formik>
</ResourcesContext.Provider>,
);
expect(screen.getByTestId('loading-skeleton')).toBeInTheDocument();
});

it('should render identifier inputs when identifier types are loaded', () => {
render(
<ResourcesContext.Provider value={mockResourcesContextValue}>
<Formik initialValues={{}} onSubmit={null}>
<Form>
<PatientRegistrationContext.Provider
value={{
setFieldValue: jest.fn(),
initialFormValues: { identifiers: { ...mockedIdentifierTypes[0] } },
setInitialFormValues: jest.fn(),
values: {
identifiers: { openmrsID },
Piumal1999 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
identifiers: { openmrsID },
identifiers: { mockedIdentifierTypes[0] },

I meant some thing like this. Check my comment on identifier-types.mock.ts as well. If it is not working, let's keep this as it is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated it as per your suggestions @Piumal1999. Please check

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requested change is not added. Could you please recheck? @ayush-AI

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not working like the way you are suggesting. The test is failing because of syntax error

},
}}>
<Identifiers />
</PatientRegistrationContext.Provider>
</Form>
</Formik>
</ResourcesContext.Provider>,
);

expect(screen.getByText('Identifiers')).toBeInTheDocument();
const configureButton = screen.getByRole('button', { name: 'Configure' });
expect(configureButton).toBeInTheDocument();
expect(configureButton).toBeEnabled();
});

it('should open identifier selection overlay when "Configure" button is clicked', () => {
render(
<ResourcesContext.Provider value={mockResourcesContextValue}>
<Formik initialValues={{}} onSubmit={null}>
<Form>
<PatientRegistrationContext.Provider
value={{
setFieldValue: jest.fn(),
initialFormValues: { identifiers: { ...mockedIdentifierTypes[0] } },
setInitialFormValues: jest.fn(),
values: {
identifiers: { openmrsID },
},
}}>
<Identifiers />
</PatientRegistrationContext.Provider>
</Form>
</Formik>
</ResourcesContext.Provider>,
);

const configureButton = screen.getByRole('button', { name: 'Configure' });
fireEvent.click(configureButton);

expect(screen.getByRole('button', { name: 'Close overlay' })).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const Overlay: React.FC<OverlayProps> = ({ close, children, header, buttonsGroup
/>
</div>
) : (
<Header className={styles.tabletOverlayHeader}>
<Header className={styles.tabletOverlayHeader} aria-label="Overlay header">
<Button
kind="ghost"
onClick={close}
Expand Down
Loading