Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Commit

Permalink
text: ResourceCard unit tests & factories
Browse files Browse the repository at this point in the history
  • Loading branch information
BPierrick committed Dec 2, 2021
1 parent 45cdf45 commit e92e202
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 1 deletion.
111 changes: 111 additions & 0 deletions src/features/resources/__tests__/ResourceCard.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import React from "react";

import { DateTime } from "luxon";

import { render, screen } from "common/tests/test-utils";
import {
carePlanFactory,
conditionFactory,
encounterFactory,
} from "services/api/factory";

import { TERMINOLOGY_SYSTEM_URL } from "../../../constants";
import ResourceCard from "../ResourceCard";

const condition = conditionFactory.build();
const encounter = encounterFactory.build();
const carePlan = carePlanFactory.build();

describe("ResourceCard", () => {
it("Displays ConditionCard", async () => {
render(<ResourceCard resource={condition} />);

await screen.findByText(new RegExp(`${condition.resourceType}`, "i"));

await screen.findByText(
new RegExp(`${condition.code?.coding?.[0]?.display}`, "i")
);
await screen.findByText(
new RegExp(`${condition.code?.coding?.[0]?.system}`, "i")
);
await screen.findByText(
new RegExp(`${condition.code?.coding?.[0]?.code}`, "i")
);

expect(condition.onsetDateTime).not.toBeUndefined();
await screen.findByText(
new RegExp(
`${DateTime.fromISO(condition.onsetDateTime ?? "").toLocaleString(
{
day: "numeric",
month: "long",
year: "numeric",
},
{ locale: navigator.language }
)}`,
"i"
)
);

const tagValue = condition.meta?.tag?.find(
({ system }) => system === TERMINOLOGY_SYSTEM_URL
);
expect(tagValue).not.toBeUndefined();
await screen.findByText(new RegExp(`${tagValue?.display}`, "i"));
});

it("Displays EncounterCard", async () => {
render(<ResourceCard resource={encounter} />);
await screen.findByText(new RegExp(`${encounter.resourceType}`, "i"));

await screen.findByText(
new RegExp(`${encounter.location?.[0]?.location?.display}`, "i")
);

expect(encounter.period?.start).not.toBeUndefined();
await screen.findByText(
new RegExp(
`${DateTime.fromISO(encounter.period?.start ?? "").toLocaleString(
{
day: "numeric",
month: "long",
year: "numeric",
},
{
locale: navigator.language,
}
)}`,
"i"
)
);

expect(encounter.period?.end).not.toBeUndefined();
await screen.findByText(
new RegExp(
`${DateTime.fromISO(encounter.period?.end ?? "").toLocaleString(
{
day: "numeric",
month: "long",
year: "numeric",
},
{
locale: navigator.language,
}
)}`,
"i"
)
);

const tagValue = encounter.meta?.tag?.find(
({ system }) => system === TERMINOLOGY_SYSTEM_URL
);
expect(tagValue).not.toBeUndefined();
await screen.findByText(new RegExp(`${tagValue?.display}`, "i"));
});

it("Displays Default card", async () => {
render(<ResourceCard resource={carePlan} />);
await screen.findByText(new RegExp(`^${carePlan.resourceType}$`, "i"));
await screen.findByText(new RegExp(`${JSON.stringify(carePlan)}`, "i"));
});
});
62 changes: 61 additions & 1 deletion src/services/api/factory.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {
ICarePlan,
ICondition,
IEncounter,
IPatient,
IResourceList,
PatientGenderKind,
} from "@ahryman40k/ts-fhir-types/lib/R4";
import * as faker from "faker";
import { Factory } from "fishery";

import { TERMINOLOGY_SYSTEM_URL } from "../../constants";
import { Bundle } from "./api";

export const bundleFactory = <ResourceType extends IResourceList>(): Factory<
Expand All @@ -22,7 +26,63 @@ export const bundleFactory = <ResourceType extends IResourceList>(): Factory<
export const patientFactory = Factory.define<IPatient>(({ sequence }) => ({
id: sequence.toString(),
resourceType: "Patient",
birthDate: faker.date.past().toString(),
birthDate: faker.date.past().toISOString(),
name: [{ given: [faker.name.firstName()], family: faker.name.lastName() }],
gender: faker.name.gender() as PatientGenderKind,
}));

export const conditionFactory = Factory.define<ICondition>(
({ sequence, associations }) => ({
id: sequence.toString(),
resourceType: "Condition",
code: associations.code || {
coding: [
{
system: faker.internet.url(),
code: faker.lorem.word(),
display: faker.lorem.words(4),
},
],
},
meta: associations.meta || {
tag: [{ system: TERMINOLOGY_SYSTEM_URL, display: faker.lorem.word() }],
},
onsetDateTime:
associations.onsetDateTime || faker.date.past().toISOString(),
subject: associations.subject || {},
})
);

export const encounterFactory = Factory.define<IEncounter>(
({ sequence, associations }) => {
const periodEnd =
associations.period?.end || faker.date.past().toISOString();
const periodStart =
associations.period?.start ||
faker.date.past(undefined, periodEnd).toISOString();
return {
id: sequence.toString(),
resourceType: "Encounter",
location: associations.location || [
{ location: { display: faker.lorem.word() } },
],
meta: associations.meta || {
tag: [{ system: TERMINOLOGY_SYSTEM_URL, display: faker.lorem.word() }],
},
period: {
...associations.period,
start: periodStart,
end: periodEnd,
},
class: associations.subject || {},
};
}
);

export const carePlanFactory = Factory.define<ICarePlan>(
({ sequence, associations }) => ({
id: sequence.toString(),
resourceType: "CarePlan",
subject: associations.subject || {},
})
);

0 comments on commit e92e202

Please sign in to comment.