Skip to content

Commit

Permalink
Fixed the failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vasharma05 committed Jul 26, 2023
1 parent 3e0c27a commit 516594b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export const AddressComponent: React.FC = () => {
const [selected, setSelected] = useState('');
const { addressTemplate } = useContext(ResourcesContext);
const addressLayout = useMemo(() => {
if (!addressTemplate) {
if (!addressTemplate?.lines) {
return [];
}
const allFields = addressTemplate.lines.flat();
const allFields = addressTemplate?.lines?.flat();
const fields = allFields?.filter(({ isToken }) => isToken === 'IS_ADDR_TOKEN');

return fields.map(({ displayText, codeName }) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Resources, ResourcesContext } from '../../../../offline.resources';
import { PatientRegistrationContext } from '../../../patient-registration-context';
import { useConfig } from '@openmrs/esm-framework';
import { useOrderedAddressHierarchyLevels } from '../address-hierarchy.resource';
import { mockResponse, mockedOrderedFields } from './mocks';
import { mockedAddressTemplate, mockedOrderedFields } from './mocks';

// Mocking the AddressSearchComponent
jest.mock('../address-search.component', () => jest.fn(() => <div data-testid="address-search-bar" />));
Expand All @@ -29,7 +29,7 @@ jest.mock('../address-hierarchy.resource', () => ({
useOrderedAddressHierarchyLevels: jest.fn(),
}));

async function renderAddressHierarchy(addressTemplate = mockResponse) {
async function renderAddressHierarchy(addressTemplate = mockedAddressTemplate) {
await render(
<ResourcesContext.Provider value={{ addressTemplate } as Resources}>
<Formik initialValues={{}} onSubmit={null}>
Expand Down Expand Up @@ -131,7 +131,7 @@ describe('Testing address hierarchy', () => {
errorFetchingFieldOrder: null,
}));
renderAddressHierarchy();
const allFields = mockResponse.lines.flat().filter(({ isToken }) => isToken === 'IS_ADDR_TOKEN');
const allFields = mockedAddressTemplate.lines.flat().filter(({ isToken }) => isToken === 'IS_ADDR_TOKEN');
allFields.forEach((field) => {
const textFieldInput = screen.getByLabelText(`${field.displayText} (optional)`);
expect(textFieldInput).toBeInTheDocument();
Expand All @@ -156,10 +156,11 @@ describe('Testing address hierarchy', () => {
errorFetchingFieldOrder: null,
}));
renderAddressHierarchy();
const allFields = mockResponse.lines.flat().filter(({ isToken }) => isToken === 'IS_ADDR_TOKEN');
const allFields = mockedAddressTemplate.lines.flat().filter(({ isToken }) => isToken === 'IS_ADDR_TOKEN');
const orderMap = Object.fromEntries(mockedOrderedFields.map((field, indx) => [field, indx]));
allFields.sort(
(existingField1, existingField2) => orderMap[existingField1.codeName] - orderMap[existingField2.codeName],
(existingField1, existingField2) =>
orderMap[existingField1.codeName ?? 0] - orderMap[existingField2.codeName ?? 0],
);
allFields.forEach((field) => {
const textFieldInput = screen.getByLabelText(`${field.displayText} (optional)`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Resources, ResourcesContext } from '../../../../offline.resources';
import { PatientRegistrationContext } from '../../../patient-registration-context';
import { useConfig } from '@openmrs/esm-framework';
import { useAddressHierarchy, useOrderedAddressHierarchyLevels } from '../address-hierarchy.resource';
import { mockResponse, mockedAddressOptions, mockedOrderedFields } from './mocks';
import { mockedAddressTemplate, mockedAddressOptions, mockedOrderedFields } from './mocks';
import AddressSearchComponent from '../address-search.component';
import userEvent from '@testing-library/user-event';

Expand All @@ -31,7 +31,7 @@ jest.mock('formik', () => ({
useFormikContext: jest.fn(() => ({})),
}));

const allFields = mockResponse.lines
const allFields = mockedAddressTemplate.lines
.flat()
.filter((field) => field.isToken === 'IS_ADDR_TOKEN')
.map(({ codeName, displayText }) => ({
Expand All @@ -42,7 +42,7 @@ const allFields = mockResponse.lines
const orderMap = Object.fromEntries(mockedOrderedFields.map((field, indx) => [field, indx]));
allFields.sort((existingField1, existingField2) => orderMap[existingField1.name] - orderMap[existingField2.name]);

async function renderAddressHierarchy(addressTemplate = mockResponse) {
async function renderAddressHierarchy(addressTemplate = mockedAddressTemplate) {
await render(
<ResourcesContext.Provider value={{ addressTemplate } as Resources}>
<Formik initialValues={{}} onSubmit={null}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AddressTemplate } from '../../../patient-registration-types';

export const mockResponse: AddressTemplate = {
export const mockedAddressTemplate: AddressTemplate = {
displayName: null,
codeName: 'default',
country: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Encounter } from './patient-registration-types';
import { Resources, ResourcesContext } from '../offline.resources';
import { PatientRegistration } from './patient-registration.component';
import { RegistrationConfig } from '../config-schema';
import { mockedAddressTemplate } from './field/address/tests/mocks';

const mockedUseConfig = useConfig as jest.Mock;
const mockedUsePatient = usePatient as jest.Mock;
Expand Down Expand Up @@ -56,17 +57,8 @@ jest.mock('@openmrs/esm-framework', () => {
};
});

const predefinedAddressTemplate = {
results: [
{
value:
'<org.openmrs.layout.address.AddressTemplate>\r\n <nameMappings class="properties">\r\n <property name="postalCode" value="Location.postalCode"/>\r\n <property name="address2" value="Location.address2"/>\r\n <property name="address1" value="Location.address1"/>\r\n <property name="country" value="Location.country"/>\r\n <property name="stateProvince" value="Location.stateProvince"/>\r\n <property name="cityVillage" value="Location.cityVillage"/>\r\n </nameMappings>\r\n <sizeMappings class="properties">\r\n <property name="postalCode" value="4"/>\r\n <property name="address1" value="40"/>\r\n <property name="address2" value="40"/>\r\n <property name="country" value="10"/>\r\n <property name="stateProvince" value="10"/>\r\n <property name="cityVillage" value="10"/>\r\n <asset name="cityVillage" value="10"/>\r\n </sizeMappings>\r\n <lineByLineFormat>\r\n <string>address1 address2</string>\r\n <string>cityVillage stateProvince postalCode</string>\r\n <string>country</string>\r\n </lineByLineFormat>\r\n <elementDefaults class="properties">\r\n <property name="country" value=""/>\r\n </elementDefaults>\r\n <elementRegex class="properties">\r\n <property name="address1" value="[a-zA-Z]+$"/>\r\n </elementRegex>\r\n <elementRegexFormats class="properties">\r\n <property name="address1" value="Countries can only be letters"/>\r\n </elementRegexFormats>\r\n </org.openmrs.layout.address.AddressTemplate>',
},
],
};

const mockResourcesContextValue = {
addressTemplate: predefinedAddressTemplate,
addressTemplate: mockedAddressTemplate,
currentSession: {
authenticated: true,
sessionId: 'JSESSION',
Expand All @@ -87,9 +79,11 @@ let mockOpenmrsConfig: RegistrationConfig = {
fieldConfigurations: {
name: {
displayMiddleName: true,
unidentifiedPatient: true,
allowUnidentifiedPatients: true,
defaultUnknownGivenName: 'UNKNOWN',
defaultUnknownFamilyName: 'UNKNOWN',
displayReverseFieldOrder: false,
displayCapturePhoto: true,
},
gender: [
{
Expand Down

0 comments on commit 516594b

Please sign in to comment.