@photonhealth/react 2.0.0-rc.1 → 2.0.0-rc.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -9
- package/dist/adapters/tailwind.d.ts.map +1 -1
- package/dist/adapters/tailwind.js +7 -4
- package/dist/adapters/tailwind.js.map +1 -1
- package/dist/analytics/PhotonAnalyticsProvider.d.ts +19 -0
- package/dist/analytics/PhotonAnalyticsProvider.d.ts.map +1 -0
- package/dist/analytics/PhotonAnalyticsProvider.js +76 -0
- package/dist/analytics/PhotonAnalyticsProvider.js.map +1 -0
- package/dist/analytics/cardLifecycle.d.ts +10 -0
- package/dist/analytics/cardLifecycle.d.ts.map +1 -0
- package/dist/analytics/cardLifecycle.js +43 -0
- package/dist/analytics/cardLifecycle.js.map +1 -0
- package/dist/analytics/fieldInteraction.d.ts +18 -0
- package/dist/analytics/fieldInteraction.d.ts.map +1 -0
- package/dist/analytics/fieldInteraction.js +102 -0
- package/dist/analytics/fieldInteraction.js.map +1 -0
- package/dist/analytics/lifecycleOutcomes.d.ts +8 -0
- package/dist/analytics/lifecycleOutcomes.d.ts.map +1 -0
- package/dist/analytics/lifecycleOutcomes.js +16 -0
- package/dist/analytics/lifecycleOutcomes.js.map +1 -0
- package/dist/analytics/tracker.d.ts +19 -0
- package/dist/analytics/tracker.d.ts.map +1 -0
- package/dist/analytics/tracker.js +41 -0
- package/dist/analytics/tracker.js.map +1 -0
- package/dist/analytics/viewed.d.ts +11 -0
- package/dist/analytics/viewed.d.ts.map +1 -0
- package/dist/analytics/viewed.js +16 -0
- package/dist/analytics/viewed.js.map +1 -0
- package/dist/components/PhotonOrder.d.ts +18 -18
- package/dist/components/PhotonOrder.d.ts.map +1 -1
- package/dist/components/PhotonOrder.js +34 -19
- package/dist/components/PhotonOrder.js.map +1 -1
- package/dist/components/PhotonPatient.d.ts +7 -35
- package/dist/components/PhotonPatient.d.ts.map +1 -1
- package/dist/components/PhotonPatient.js +6 -78
- package/dist/components/PhotonPatient.js.map +1 -1
- package/dist/components/PhotonPrescription.d.ts +15 -26
- package/dist/components/PhotonPrescription.d.ts.map +1 -1
- package/dist/components/PhotonPrescription.js +6 -360
- package/dist/components/PhotonPrescription.js.map +1 -1
- package/dist/components/format.d.ts +2 -2
- package/dist/components/format.js +2 -2
- package/dist/components/index.d.ts +1 -1
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/index.js +1 -1
- package/dist/components/index.js.map +1 -1
- package/dist/components/open.d.ts +11 -13
- package/dist/components/open.d.ts.map +1 -1
- package/dist/components/open.js +7 -2
- package/dist/components/open.js.map +1 -1
- package/dist/components/order/DraftOrderPrompt.d.ts +18 -0
- package/dist/components/order/DraftOrderPrompt.d.ts.map +1 -0
- package/dist/components/order/DraftOrderPrompt.js +26 -0
- package/dist/components/order/DraftOrderPrompt.js.map +1 -0
- package/dist/components/patient/PatientCard/PatientCard.d.ts +30 -0
- package/dist/components/patient/PatientCard/PatientCard.d.ts.map +1 -0
- package/dist/components/patient/PatientCard/PatientCard.js +101 -0
- package/dist/components/patient/PatientCard/PatientCard.js.map +1 -0
- package/dist/components/patient/PatientCard/usePatientToShow.d.ts +22 -0
- package/dist/components/patient/PatientCard/usePatientToShow.d.ts.map +1 -0
- package/dist/components/patient/PatientCard/usePatientToShow.js +40 -0
- package/dist/components/patient/PatientCard/usePatientToShow.js.map +1 -0
- package/dist/components/patient/PatientIntakeForm.d.ts +16 -0
- package/dist/components/patient/PatientIntakeForm.d.ts.map +1 -0
- package/dist/components/patient/PatientIntakeForm.js +114 -0
- package/dist/components/patient/PatientIntakeForm.js.map +1 -0
- package/dist/components/patient/PatientSearch.d.ts +12 -0
- package/dist/components/patient/PatientSearch.d.ts.map +1 -0
- package/dist/components/patient/PatientSearch.js +148 -0
- package/dist/components/patient/PatientSearch.js.map +1 -0
- package/dist/components/patient/{Benefits.d.ts → PatientSummary/BenefitsInputForm.d.ts} +2 -2
- package/dist/components/patient/PatientSummary/BenefitsInputForm.d.ts.map +1 -0
- package/dist/components/patient/{Benefits.js → PatientSummary/BenefitsInputForm.js} +6 -5
- package/dist/components/patient/PatientSummary/BenefitsInputForm.js.map +1 -0
- package/dist/components/patient/{Clinical.d.ts → PatientSummary/ClinicalInputForm.d.ts} +15 -16
- package/dist/components/patient/PatientSummary/ClinicalInputForm.d.ts.map +1 -0
- package/dist/components/patient/{Clinical.js → PatientSummary/ClinicalInputForm.js} +29 -28
- package/dist/components/patient/PatientSummary/ClinicalInputForm.js.map +1 -0
- package/dist/components/patient/PatientSummary/CollapsedSummary.d.ts +5 -0
- package/dist/components/patient/PatientSummary/CollapsedSummary.d.ts.map +1 -0
- package/dist/components/patient/PatientSummary/CollapsedSummary.js +30 -0
- package/dist/components/patient/PatientSummary/CollapsedSummary.js.map +1 -0
- package/dist/components/patient/PatientSummary/PatientDetailsForm.d.ts +10 -0
- package/dist/components/patient/PatientSummary/PatientDetailsForm.d.ts.map +1 -0
- package/dist/components/patient/PatientSummary/PatientDetailsForm.js +65 -0
- package/dist/components/patient/PatientSummary/PatientDetailsForm.js.map +1 -0
- package/dist/components/patient/PatientSummary/PatientDetailsSchema.d.ts +53 -0
- package/dist/components/patient/PatientSummary/PatientDetailsSchema.d.ts.map +1 -0
- package/dist/components/patient/PatientSummary/PatientDetailsSchema.js +78 -0
- package/dist/components/patient/PatientSummary/PatientDetailsSchema.js.map +1 -0
- package/dist/components/patient/PatientSummary/PatientSummary.d.ts +46 -0
- package/dist/components/patient/PatientSummary/PatientSummary.d.ts.map +1 -0
- package/dist/components/patient/PatientSummary/PatientSummary.js +57 -0
- package/dist/components/patient/PatientSummary/PatientSummary.js.map +1 -0
- package/dist/components/patient/PatientSummary/SummaryHeader.d.ts +19 -0
- package/dist/components/patient/PatientSummary/SummaryHeader.d.ts.map +1 -0
- package/dist/components/patient/PatientSummary/SummaryHeader.js +22 -0
- package/dist/components/patient/PatientSummary/SummaryHeader.js.map +1 -0
- package/dist/components/patient/fields.d.ts +7 -26
- package/dist/components/patient/fields.d.ts.map +1 -1
- package/dist/components/patient/fields.js +11 -52
- package/dist/components/patient/fields.js.map +1 -1
- package/dist/components/patient/parsePatientSearchQuery.d.ts +16 -0
- package/dist/components/patient/parsePatientSearchQuery.d.ts.map +1 -0
- package/dist/components/patient/{query.js → parsePatientSearchQuery.js} +3 -10
- package/dist/components/patient/parsePatientSearchQuery.js.map +1 -0
- package/dist/components/prescription/PrescriptionCard.d.ts +51 -0
- package/dist/components/prescription/PrescriptionCard.d.ts.map +1 -0
- package/dist/components/prescription/PrescriptionCard.js +186 -0
- package/dist/components/prescription/PrescriptionCard.js.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.d.ts +15 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.d.ts.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.js +18 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.js.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.d.ts +23 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.d.ts.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.js +42 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.js.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts +27 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js +169 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.d.ts +8 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.d.ts.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.js +12 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.js.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.d.ts +11 -0
- package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.d.ts.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.js +25 -0
- package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.js.map +1 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentPicker.d.ts +50 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentPicker.d.ts.map +1 -0
- package/dist/components/{TreatmentPicker.js → prescription/TreatmentSearch/TreatmentPicker.js} +113 -105
- package/dist/components/prescription/TreatmentSearch/TreatmentPicker.js.map +1 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentSearch.d.ts +28 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentSearch.d.ts.map +1 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentSearch.js +146 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentSearch.js.map +1 -0
- package/dist/components/schemaErrors.d.ts +13 -0
- package/dist/components/schemaErrors.d.ts.map +1 -0
- package/dist/components/schemaErrors.js +25 -0
- package/dist/components/schemaErrors.js.map +1 -0
- package/dist/components/search.d.ts +1 -1
- package/dist/components/search.js +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/meta/version.d.ts +5 -0
- package/dist/meta/version.d.ts.map +1 -0
- package/dist/meta/version.js +5 -0
- package/dist/meta/version.js.map +1 -0
- package/dist/primitives/PhotonProvider.d.ts +3 -1
- package/dist/primitives/PhotonProvider.d.ts.map +1 -1
- package/dist/primitives/PhotonProvider.js +23 -11
- package/dist/primitives/PhotonProvider.js.map +1 -1
- package/dist/primitives/defaults.d.ts.map +1 -1
- package/dist/primitives/defaults.js +6 -3
- package/dist/primitives/defaults.js.map +1 -1
- package/dist/primitives/photonContext.d.ts +3 -0
- package/dist/primitives/photonContext.d.ts.map +1 -0
- package/dist/primitives/photonContext.js +3 -0
- package/dist/primitives/photonContext.js.map +1 -0
- package/dist/primitives/types.d.ts +15 -3
- package/dist/primitives/types.d.ts.map +1 -1
- package/dist/primitives/types.js.map +1 -1
- package/dist/primitives/useProvidedPhoton.d.ts +8 -0
- package/dist/primitives/useProvidedPhoton.d.ts.map +1 -0
- package/dist/primitives/useProvidedPhoton.js +8 -0
- package/dist/primitives/useProvidedPhoton.js.map +1 -0
- package/dist/styles.css +45 -3
- package/dist/test-utils/contract.d.ts.map +1 -1
- package/dist/test-utils/contract.js +23 -2
- package/dist/test-utils/contract.js.map +1 -1
- package/package.json +8 -3
- package/src/adapters/tailwind.tsx +52 -24
- package/src/analytics/PhotonAnalyticsProvider.test.tsx +190 -0
- package/src/analytics/PhotonAnalyticsProvider.tsx +151 -0
- package/src/analytics/cardLifecycle.test.tsx +272 -0
- package/src/analytics/cardLifecycle.ts +44 -0
- package/src/analytics/fieldInteraction.test.tsx +186 -0
- package/src/analytics/fieldInteraction.tsx +148 -0
- package/src/analytics/lifecycleOutcomes.test.ts +78 -0
- package/src/analytics/lifecycleOutcomes.ts +29 -0
- package/src/analytics/tracker.test.ts +52 -0
- package/src/analytics/tracker.ts +49 -0
- package/src/analytics/usePhotonAnalytics.test.tsx +106 -0
- package/src/analytics/viewed.ts +27 -0
- package/src/components/PhotonOrder.test.tsx +394 -0
- package/src/components/PhotonOrder.tsx +79 -53
- package/src/components/PhotonPatient.tsx +15 -166
- package/src/components/PhotonPrescription.test.tsx +120 -0
- package/src/components/PhotonPrescription.tsx +23 -583
- package/src/components/ScreeningAlerts.test.tsx +81 -0
- package/src/components/format.ts +2 -2
- package/src/components/index.ts +1 -1
- package/src/components/open.ts +13 -16
- package/src/components/order/DraftOrderPrompt.test.tsx +77 -0
- package/src/components/order/DraftOrderPrompt.tsx +81 -0
- package/src/components/patient/PatientCard/PatientCard.test.tsx +255 -0
- package/src/components/patient/PatientCard/PatientCard.tsx +177 -0
- package/src/components/patient/PatientCard/usePatientToShow.ts +44 -0
- package/src/components/patient/PatientIntakeForm.test.tsx +262 -0
- package/src/components/patient/PatientIntakeForm.tsx +222 -0
- package/src/components/patient/PatientSearch.test.tsx +204 -0
- package/src/components/patient/PatientSearch.tsx +221 -0
- package/src/components/patient/PatientSummary/BenefitsInputForm.test.tsx +164 -0
- package/src/components/patient/{Benefits.tsx → PatientSummary/BenefitsInputForm.tsx} +24 -16
- package/src/components/patient/PatientSummary/ClinicalInputForm.test.tsx +247 -0
- package/src/components/patient/{Clinical.tsx → PatientSummary/ClinicalInputForm.tsx} +53 -48
- package/src/components/patient/PatientSummary/CollapsedSummary.test.tsx +90 -0
- package/src/components/patient/PatientSummary/CollapsedSummary.tsx +46 -0
- package/src/components/patient/PatientSummary/PatientDetailsForm.test.tsx +201 -0
- package/src/components/patient/PatientSummary/PatientDetailsForm.tsx +320 -0
- package/src/components/patient/PatientSummary/PatientDetailsSchema.test.ts +296 -0
- package/src/components/patient/PatientSummary/PatientDetailsSchema.ts +95 -0
- package/src/components/patient/PatientSummary/PatientSummary.test.tsx +194 -0
- package/src/components/patient/PatientSummary/PatientSummary.tsx +225 -0
- package/src/components/patient/PatientSummary/SummaryHeader.test.tsx +183 -0
- package/src/components/patient/PatientSummary/SummaryHeader.tsx +85 -0
- package/src/components/patient/fields.tsx +12 -124
- package/src/components/patient/parsePatientSearchQuery.test.ts +107 -0
- package/src/components/patient/{query.ts → parsePatientSearchQuery.ts} +4 -15
- package/src/components/prescription/PrescriptionCard.test.tsx +247 -0
- package/src/components/prescription/PrescriptionCard.tsx +361 -0
- package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.test.tsx +82 -0
- package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.tsx +41 -0
- package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.test.tsx +206 -0
- package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.tsx +99 -0
- package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.test.tsx +453 -0
- package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.tsx +238 -0
- package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.test.ts +63 -0
- package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.ts +15 -0
- package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.test.tsx +72 -0
- package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.tsx +41 -0
- package/src/components/prescription/TreatmentSearch/TreatmentPicker.tsx +375 -0
- package/src/components/prescription/TreatmentSearch/TreatmentSearch.test.tsx +695 -0
- package/src/components/prescription/TreatmentSearch/TreatmentSearch.tsx +220 -0
- package/src/components/schemaErrors.test.ts +49 -0
- package/src/components/schemaErrors.ts +34 -0
- package/src/components/search.tsx +1 -1
- package/src/index.ts +2 -0
- package/src/meta/version.test.ts +11 -0
- package/src/meta/version.ts +4 -0
- package/src/primitives/PhotonProvider.tsx +30 -15
- package/src/primitives/defaults.tsx +44 -21
- package/src/primitives/photonContext.ts +4 -0
- package/src/primitives/types.ts +15 -3
- package/src/primitives/useProvidedPhoton.ts +21 -0
- package/src/styles.css +45 -3
- package/src/test-utils/contract.tsx +45 -2
- package/dist/components/TreatmentPicker.d.ts +0 -45
- package/dist/components/TreatmentPicker.d.ts.map +0 -1
- package/dist/components/TreatmentPicker.js.map +0 -1
- package/dist/components/patient/Benefits.d.ts.map +0 -1
- package/dist/components/patient/Benefits.js.map +0 -1
- package/dist/components/patient/Clinical.d.ts.map +0 -1
- package/dist/components/patient/Clinical.js.map +0 -1
- package/dist/components/patient/IntakeForm.d.ts +0 -10
- package/dist/components/patient/IntakeForm.d.ts.map +0 -1
- package/dist/components/patient/IntakeForm.js +0 -324
- package/dist/components/patient/IntakeForm.js.map +0 -1
- package/dist/components/patient/Summary.d.ts +0 -48
- package/dist/components/patient/Summary.d.ts.map +0 -1
- package/dist/components/patient/Summary.js +0 -80
- package/dist/components/patient/Summary.js.map +0 -1
- package/dist/components/patient/query.d.ts +0 -23
- package/dist/components/patient/query.d.ts.map +0 -1
- package/dist/components/patient/query.js.map +0 -1
- package/src/components/TreatmentPicker.tsx +0 -350
- package/src/components/patient/IntakeForm.tsx +0 -532
- package/src/components/patient/Summary.tsx +0 -310
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
|
|
2
|
+
import userEvent from "@testing-library/user-event";
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
4
|
+
import { alert, change, fakeNetworkApiClient, noMatch, payload, rx } from "@photonhealth/client/testing";
|
|
5
|
+
import { PhotonOrder, PhotonPatient, PhotonPrescription } from "../index.ts";
|
|
6
|
+
import { draftOrder, fakeNetworkApi, order, patient, signed } from "../../test/cards.fixtures.tsx";
|
|
7
|
+
import {
|
|
8
|
+
meAsPrescriber,
|
|
9
|
+
recordAnalytics,
|
|
10
|
+
type RecordedAnalytics,
|
|
11
|
+
typeInto,
|
|
12
|
+
unmountAndRestoreFetch,
|
|
13
|
+
withAnalytics,
|
|
14
|
+
} from "../../test/analytics.fixtures.tsx";
|
|
15
|
+
|
|
16
|
+
type NetworkScript = Parameters<typeof fakeNetworkApiClient>[0];
|
|
17
|
+
|
|
18
|
+
const TYPED_BY_THE_PRESCRIBER = ["Grace", "Hopper", "1906-12-09", "5555550100"];
|
|
19
|
+
|
|
20
|
+
const grace = {
|
|
21
|
+
...patient,
|
|
22
|
+
id: "pat_grace",
|
|
23
|
+
demographic: {
|
|
24
|
+
...patient.demographic,
|
|
25
|
+
name: { ...patient.demographic.name, first: "Grace", last: "Hopper" },
|
|
26
|
+
dateOfBirth: "1906-12-09",
|
|
27
|
+
sex: "FEMALE",
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const firstNameAdded = change({ key: "demographic.name.first", label: "First name", operation: "ADDED" });
|
|
32
|
+
const phoneAdded = change({ key: "contact.phone", label: "Phone", operation: "ADDED" });
|
|
33
|
+
|
|
34
|
+
let analytics: RecordedAnalytics;
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
analytics = recordAnalytics();
|
|
37
|
+
});
|
|
38
|
+
afterEach(unmountAndRestoreFetch);
|
|
39
|
+
|
|
40
|
+
describe("PhotonPatient's lifecycle events", () => {
|
|
41
|
+
it("Save patient that creates the record sends Patient Created once, with which fields are filled and nothing typed", async () => {
|
|
42
|
+
renderCard(<PhotonPatient />, {
|
|
43
|
+
SyncPatient: (vars: { demographic?: { sex?: string } }) =>
|
|
44
|
+
vars.demographic?.sex
|
|
45
|
+
? payload("patient", "PatientPayload", grace, { changes: [firstNameAdded] } as never)
|
|
46
|
+
: noMatch(),
|
|
47
|
+
});
|
|
48
|
+
await addGraceHopper();
|
|
49
|
+
|
|
50
|
+
await expectLifecycleEvents(["Patient Created"]);
|
|
51
|
+
expect(lifecycleEvent("Patient Created")).toMatchObject({
|
|
52
|
+
patientId: "pat_grace",
|
|
53
|
+
snap_first_name: true,
|
|
54
|
+
snap_last_name: true,
|
|
55
|
+
snap_date_of_birth: true,
|
|
56
|
+
snap_sex: true,
|
|
57
|
+
snap_email: false,
|
|
58
|
+
category: "ctaClicked",
|
|
59
|
+
integration: "react",
|
|
60
|
+
});
|
|
61
|
+
expectNothingTypedWasSent();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("Save patient that finds an existing patient sends no lifecycle event", async () => {
|
|
65
|
+
renderCard(<PhotonPatient />, {
|
|
66
|
+
SyncPatient: (vars: { demographic?: { sex?: string } }) =>
|
|
67
|
+
vars.demographic?.sex ? payload("patient", "PatientPayload", grace) : noMatch(),
|
|
68
|
+
});
|
|
69
|
+
await addGraceHopper();
|
|
70
|
+
await screen.findByRole("heading", { name: /Grace Hopper/ });
|
|
71
|
+
|
|
72
|
+
await expectLifecycleEvents([]);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("saving an edit to the patient sends Patient Updated", async () => {
|
|
76
|
+
// Save needs every required field, sex included.
|
|
77
|
+
const withSex = { ...patient, demographic: { ...patient.demographic, sex: "FEMALE" } };
|
|
78
|
+
renderCard(<PhotonPatient patient="pat_1" />, {
|
|
79
|
+
GetPatient: () => payload("patient", "PatientPayload", withSex),
|
|
80
|
+
UpdatePatient: () => payload("patient", "PatientPayload", withSex, { changes: [phoneAdded] } as never),
|
|
81
|
+
});
|
|
82
|
+
await userEvent.click(await screen.findByRole("button", { name: "Edit patient" }));
|
|
83
|
+
await typeInto("Phone", "5555550100");
|
|
84
|
+
await userEvent.click(screen.getByRole("button", { name: "Save" }));
|
|
85
|
+
|
|
86
|
+
await expectLifecycleEvents(["Patient Updated"]);
|
|
87
|
+
expect(lifecycleEvent("Patient Updated")).toMatchObject({ patientId: "pat_1", snap_address_street1: true });
|
|
88
|
+
expectNothingTypedWasSent();
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe("PhotonPrescription's lifecycle events", () => {
|
|
93
|
+
const amoxicillin = { amo: [["Amoxicillin", "Capsule", "500 MG"]] } satisfies Parameters<
|
|
94
|
+
typeof fakeNetworkApi
|
|
95
|
+
>[0]["prescriptions"];
|
|
96
|
+
|
|
97
|
+
it("picking a drug that drafts the prescription sends Draft Prescription Added once, from the form", async () => {
|
|
98
|
+
renderCard(<PhotonPrescription patient="pat_1" />, {
|
|
99
|
+
GetPatient: () => payload("patient", "PatientPayload", patient),
|
|
100
|
+
...fakeNetworkApi({ prescriptions: amoxicillin }),
|
|
101
|
+
});
|
|
102
|
+
await draftAmoxicillin();
|
|
103
|
+
|
|
104
|
+
await expectLifecycleEvents(["Draft Prescription Added"]);
|
|
105
|
+
expect(lifecycleEvent("Draft Prescription Added")).toMatchObject({
|
|
106
|
+
prescriptionId: "rx_1",
|
|
107
|
+
draftPrescriptionSource: "form",
|
|
108
|
+
snap_treatment: true,
|
|
109
|
+
snap_instructions: true,
|
|
110
|
+
snap_dispense_quantity: true,
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("Add sends Prescription Signed once, after the draft's Draft Prescription Added", async () => {
|
|
115
|
+
renderCard(<PhotonPrescription patient="pat_1" />, {
|
|
116
|
+
GetPatient: () => payload("patient", "PatientPayload", patient),
|
|
117
|
+
...fakeNetworkApi({ prescriptions: amoxicillin }),
|
|
118
|
+
});
|
|
119
|
+
await draftAmoxicillin();
|
|
120
|
+
fireEvent.click(await screen.findByRole("button", { name: "Sign" }));
|
|
121
|
+
|
|
122
|
+
await expectLifecycleEvents(["Draft Prescription Added", "Prescription Signed"]);
|
|
123
|
+
expect(lifecycleEvent("Prescription Signed")).toMatchObject({
|
|
124
|
+
prescriptionId: "rx_1",
|
|
125
|
+
category: "ctaClicked",
|
|
126
|
+
screeningAlertCount: 0,
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("Add on a prescription showing a screening alert sends Prescription Signed counting the alert it acknowledged", async () => {
|
|
131
|
+
renderCard(<PhotonPrescription patient="pat_1" />, {
|
|
132
|
+
GetPatient: () => payload("patient", "PatientPayload", patient),
|
|
133
|
+
...fakeNetworkApi({ prescriptions: amoxicillin, screeningAlerts: [alert] }),
|
|
134
|
+
});
|
|
135
|
+
await draftAmoxicillin();
|
|
136
|
+
fireEvent.click(await screen.findByRole("button", { name: "Sign" }));
|
|
137
|
+
|
|
138
|
+
await expectLifecycleEvents(["Draft Prescription Added", "Prescription Signed"]);
|
|
139
|
+
expect(lifecycleEvent("Prescription Signed")).toMatchObject({ prescriptionId: "rx_1", screeningAlertCount: 1 });
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("a prescription opened by its id sends no Draft Prescription Added: it was drafted elsewhere", async () => {
|
|
143
|
+
renderCard(<PhotonPrescription prescription="rx_1" />, {
|
|
144
|
+
GetPrescription: () => payload("prescription", "PrescriptionPayload", rx({ id: "rx_1" })),
|
|
145
|
+
});
|
|
146
|
+
await screen.findByLabelText("Directions (sig)");
|
|
147
|
+
|
|
148
|
+
await expectLifecycleEvents([]);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
describe("PhotonOrder's lifecycle events", () => {
|
|
153
|
+
const sendableOrder = {
|
|
154
|
+
GetOrder: () => payload("order", "OrderPayload", order()),
|
|
155
|
+
GetPrescription: () => payload("prescription", "PrescriptionPayload", signed),
|
|
156
|
+
SubmitOrder: () => payload("order", "OrderPayload", order("SUBMITTED")),
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
it("Send sends Order Sent once, to the patient, in the same session as the card's Prescribe Workflow Viewed", async () => {
|
|
160
|
+
renderCard(<PhotonOrder order="ord_1" />, sendableOrder);
|
|
161
|
+
const send = await screen.findByRole("button", { name: /^Send prescriptions? to patient$/ });
|
|
162
|
+
await waitFor(() => expect(send).toBeEnabled());
|
|
163
|
+
fireEvent.click(send);
|
|
164
|
+
|
|
165
|
+
await expectLifecycleEvents(["Order Sent"]);
|
|
166
|
+
expect(lifecycleEvent("Order Sent")).toMatchObject({
|
|
167
|
+
orderId: "ord_1",
|
|
168
|
+
prescriptionCount: 1,
|
|
169
|
+
prescriptionIds: ["rx_a"],
|
|
170
|
+
fulfillmentType: "SEND_TO_PATIENT",
|
|
171
|
+
});
|
|
172
|
+
expect(lifecycleEvent("Order Sent").sessionId).toBe(sentEvent("Prescribe Workflow Viewed").sessionId);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("removing a drafted line sends Draft Prescription Deleted with its id", async () => {
|
|
176
|
+
renderCard(<PhotonOrder patient="pat_1" />, {
|
|
177
|
+
...draftOrder(),
|
|
178
|
+
RemovePrescriptionsFromOrder: () =>
|
|
179
|
+
payload("order", "OrderPayload", { ...order(), id: "ordd_1", prescriptions: [] }),
|
|
180
|
+
GetPatient: () => payload("patient", "PatientPayload", patient),
|
|
181
|
+
...fakeNetworkApi({ prescriptions: { amo: [["Amoxicillin", "Capsule", "500 MG"]] } }),
|
|
182
|
+
});
|
|
183
|
+
await draftAmoxicillin();
|
|
184
|
+
await expectLifecycleEvents(["Draft Prescription Added"]);
|
|
185
|
+
fireEvent.click(screen.getByRole("button", { name: "Remove prescription" }));
|
|
186
|
+
|
|
187
|
+
await expectLifecycleEvents(["Draft Prescription Added", "Draft Prescription Deleted"]);
|
|
188
|
+
expect(lifecycleEvent("Draft Prescription Deleted")).toMatchObject({ prescriptionId: "rx_1" });
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("with disableAnalytics, sending the order sends nothing", async () => {
|
|
192
|
+
const { callNames } = renderCard(<PhotonOrder order="ord_1" />, sendableOrder, { disableAnalytics: true });
|
|
193
|
+
const send = await screen.findByRole("button", { name: /^Send prescriptions? to patient$/ });
|
|
194
|
+
await waitFor(() => expect(send).toBeEnabled());
|
|
195
|
+
fireEvent.click(send);
|
|
196
|
+
await waitFor(() => expect(callNames()).toContain("SubmitOrder"));
|
|
197
|
+
|
|
198
|
+
await act(() => new Promise((settled) => setTimeout(settled, 300)));
|
|
199
|
+
expect(analytics.bodies()).toEqual([]);
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const LIFECYCLE_EVENTS = [
|
|
204
|
+
"Patient Created",
|
|
205
|
+
"Patient Updated",
|
|
206
|
+
"Draft Prescription Added",
|
|
207
|
+
"Draft Prescription Deleted",
|
|
208
|
+
"Screening Alert Acknowledged",
|
|
209
|
+
"Screening Alert Canceled",
|
|
210
|
+
"Prescription Signed",
|
|
211
|
+
"Order Sent",
|
|
212
|
+
];
|
|
213
|
+
|
|
214
|
+
interface SentEvent {
|
|
215
|
+
event: string;
|
|
216
|
+
properties: Record<string, unknown>;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function renderCard(card: React.ReactElement, script: NetworkScript, options?: { disableAnalytics?: boolean }) {
|
|
220
|
+
const { client, names } = fakeNetworkApiClient({ ...meAsPrescriber, ...script });
|
|
221
|
+
render(card, { wrapper: withAnalytics(client, options) });
|
|
222
|
+
return { callNames: names };
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async function addGraceHopper() {
|
|
226
|
+
await userEvent.type(await screen.findByLabelText("Find patient"), "Grace Hopper");
|
|
227
|
+
await userEvent.click(await screen.findByText("Add new patient"));
|
|
228
|
+
await userEvent.type(await screen.findByLabelText("Date of birth"), "1906-12-09");
|
|
229
|
+
await userEvent.selectOptions(screen.getByRole("combobox", { name: "Sex" }), "FEMALE");
|
|
230
|
+
await userEvent.click(screen.getByRole("button", { name: "Save patient" }));
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async function draftAmoxicillin() {
|
|
234
|
+
await userEvent.click(await screen.findByLabelText("Treatment"));
|
|
235
|
+
await userEvent.keyboard("Amo");
|
|
236
|
+
const product = await waitFor(() => {
|
|
237
|
+
const row = screen.getByRole<HTMLButtonElement>("button", { name: /^Amoxicillin/ });
|
|
238
|
+
expect(row.disabled).toBe(false);
|
|
239
|
+
return row;
|
|
240
|
+
});
|
|
241
|
+
await userEvent.click(product);
|
|
242
|
+
await screen.findByLabelText("Directions (sig)");
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function sent(): SentEvent[] {
|
|
246
|
+
return analytics.bodies().map((body) => JSON.parse(body) as SentEvent);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function sentEvent(name: string) {
|
|
250
|
+
return sent().find(({ event }) => event === name)!.properties;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function lifecycleEventsSent() {
|
|
254
|
+
return sent()
|
|
255
|
+
.map(({ event }) => event)
|
|
256
|
+
.filter((event) => LIFECYCLE_EVENTS.includes(event));
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function lifecycleEvent(name: string) {
|
|
260
|
+
return sentEvent(name);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async function expectLifecycleEvents(expected: string[]) {
|
|
264
|
+
await waitFor(() => expect(lifecycleEventsSent()).toEqual(expected));
|
|
265
|
+
await act(() => new Promise((settled) => setTimeout(settled, 300)));
|
|
266
|
+
expect(lifecycleEventsSent(), "each lifecycle event once, and none after").toEqual(expected);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function expectNothingTypedWasSent() {
|
|
270
|
+
const everythingSent = analytics.bodies().join("\n");
|
|
271
|
+
for (const typed of TYPED_BY_THE_PRESCRIBER) expect(everythingSent).not.toContain(typed);
|
|
272
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
import type { Changes, DraftPrescriptionSource, Patient, Prescription } from "@photonhealth/client";
|
|
3
|
+
import { usePhotonAnalytics } from "./PhotonAnalyticsProvider.tsx";
|
|
4
|
+
import { draftedNewPrescription, patientWriteOutcome } from "./lifecycleOutcomes.ts";
|
|
5
|
+
|
|
6
|
+
type PatientSave = () => Promise<{ changes: Changes }>;
|
|
7
|
+
|
|
8
|
+
export function useSavePatientAndSendEvent() {
|
|
9
|
+
const analytics = usePhotonAnalytics();
|
|
10
|
+
return useCallback(
|
|
11
|
+
async (patient: Patient, save: PatientSave) => {
|
|
12
|
+
const hadRecord = Boolean(patient.id);
|
|
13
|
+
const saved = await save();
|
|
14
|
+
const record = patient.state;
|
|
15
|
+
if (!record) return saved;
|
|
16
|
+
const outcome = patientWriteOutcome({ hadRecord, changes: saved.changes });
|
|
17
|
+
if (outcome === "created") void analytics.patientCreated(record);
|
|
18
|
+
if (outcome === "updated") void analytics.patientUpdated(record);
|
|
19
|
+
return saved;
|
|
20
|
+
},
|
|
21
|
+
[analytics],
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function useSendDraftPrescriptionAdded(prescription: Prescription | null) {
|
|
26
|
+
const analytics = usePhotonAnalytics();
|
|
27
|
+
const [seenBeforeItsRecord] = useState(() => new WeakSet<Prescription>());
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (!prescription) return;
|
|
30
|
+
const sendOnceDrafted = () => {
|
|
31
|
+
const record = prescription.state;
|
|
32
|
+
if (!record) return void seenBeforeItsRecord.add(prescription);
|
|
33
|
+
if (!seenBeforeItsRecord.delete(prescription)) return;
|
|
34
|
+
if (!draftedNewPrescription(prescription.changes)) return;
|
|
35
|
+
void analytics.draftPrescriptionAdded(record, draftSourceOf(prescription));
|
|
36
|
+
};
|
|
37
|
+
sendOnceDrafted();
|
|
38
|
+
return prescription.subscribe(sendOnceDrafted);
|
|
39
|
+
}, [prescription, analytics, seenBeforeItsRecord]);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function draftSourceOf(prescription: Prescription): DraftPrescriptionSource {
|
|
43
|
+
return prescription.templateId ? "template_prefill" : "form";
|
|
44
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { render, screen, waitFor } from "@testing-library/react";
|
|
2
|
+
import userEvent from "@testing-library/user-event";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
5
|
+
import { fakeNetworkApiClient } from "@photonhealth/client/testing";
|
|
6
|
+
import { TrackedField, useFieldInteractions } from "./fieldInteraction.tsx";
|
|
7
|
+
import {
|
|
8
|
+
buildEditedEventsFor,
|
|
9
|
+
meAsPrescriber,
|
|
10
|
+
recordAnalytics,
|
|
11
|
+
type RecordedAnalytics,
|
|
12
|
+
unmountAndRestoreFetch,
|
|
13
|
+
visitedWithoutEditing,
|
|
14
|
+
withAnalytics,
|
|
15
|
+
} from "../../test/analytics.fixtures.tsx";
|
|
16
|
+
|
|
17
|
+
function QuantityAndStrength({ showQuantity = true }: { showQuantity?: boolean }) {
|
|
18
|
+
const fieldInteraction = useFieldInteractions("add_prescription_form");
|
|
19
|
+
const [quantity, setQuantity] = useState("");
|
|
20
|
+
const [strength, setStrength] = useState<string | null>(null);
|
|
21
|
+
const strengthPicked = fieldInteraction("strength", { isOptional: false, hasValue: strength !== null });
|
|
22
|
+
const pickStrength = (picked: string) => {
|
|
23
|
+
strengthPicked.onChange(true);
|
|
24
|
+
setStrength(picked);
|
|
25
|
+
};
|
|
26
|
+
return (
|
|
27
|
+
<>
|
|
28
|
+
{showQuantity && (
|
|
29
|
+
<TrackedField formName="add_prescription_form" fieldName="dispenseQuantity" isOptional={false}>
|
|
30
|
+
<input aria-label="Quantity" value={quantity} onChange={(e) => setQuantity(e.target.value)} />
|
|
31
|
+
</TrackedField>
|
|
32
|
+
)}
|
|
33
|
+
<div role="group" aria-label="Strength" onFocus={strengthPicked.onFocus} onBlur={strengthPicked.onBlur}>
|
|
34
|
+
<button onClick={() => pickStrength("10 mg")}>10 mg</button>
|
|
35
|
+
<button onClick={() => pickStrength("20 mg")}>20 mg</button>
|
|
36
|
+
</div>
|
|
37
|
+
<input aria-label="Elsewhere" />
|
|
38
|
+
</>
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function TreatmentWithStrengthChips() {
|
|
43
|
+
const fieldInteraction = useFieldInteractions("add_prescription_form");
|
|
44
|
+
const treatment = fieldInteraction("treatment", { isOptional: false });
|
|
45
|
+
const strengthPicked = fieldInteraction("strength", { isOptional: false });
|
|
46
|
+
return (
|
|
47
|
+
<>
|
|
48
|
+
<div style={{ display: "contents" }} {...treatment}>
|
|
49
|
+
<input aria-label="Treatment" />
|
|
50
|
+
<button>Amoxicillin</button>
|
|
51
|
+
<div role="group" aria-label="Strength" onFocus={strengthPicked.onFocus} onBlur={strengthPicked.onBlur}>
|
|
52
|
+
<button onClick={() => strengthPicked.onChange(true)}>500 mg</button>
|
|
53
|
+
</div>
|
|
54
|
+
</div>
|
|
55
|
+
<input aria-label="Elsewhere" />
|
|
56
|
+
</>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const renderWithAnalytics = (ui: React.ReactElement, options: { disableAnalytics?: boolean } = {}) =>
|
|
61
|
+
render(ui, { wrapper: withAnalytics(fakeNetworkApiClient(meAsPrescriber).client, options) });
|
|
62
|
+
|
|
63
|
+
let analytics: RecordedAnalytics;
|
|
64
|
+
beforeEach(() => {
|
|
65
|
+
analytics = recordAnalytics();
|
|
66
|
+
});
|
|
67
|
+
afterEach(async () => {
|
|
68
|
+
await unmountAndRestoreFetch();
|
|
69
|
+
vi.restoreAllMocks();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe("useFieldInteractions", () => {
|
|
73
|
+
it("focus, typing and leaving a field send exactly focus, one change and blur", async () => {
|
|
74
|
+
renderWithAnalytics(<QuantityAndStrength />);
|
|
75
|
+
await userEvent.click(screen.getByLabelText("Quantity"));
|
|
76
|
+
await userEvent.keyboard("30");
|
|
77
|
+
await userEvent.click(screen.getByLabelText("Elsewhere"));
|
|
78
|
+
|
|
79
|
+
await analytics.expectEventLog([...buildEditedEventsFor("dispenseQuantity")]);
|
|
80
|
+
expect(analytics.sentFor("dispenseQuantity")).toEqual([
|
|
81
|
+
expect.objectContaining({
|
|
82
|
+
category: "fieldInteraction",
|
|
83
|
+
formName: "add_prescription_form",
|
|
84
|
+
fieldName: "dispenseQuantity",
|
|
85
|
+
interaction: "focus",
|
|
86
|
+
hasValue: false,
|
|
87
|
+
isOptional: false,
|
|
88
|
+
}),
|
|
89
|
+
expect.objectContaining({ interaction: "change", hasValue: true }),
|
|
90
|
+
expect.objectContaining({ interaction: "blur", hasValue: true, changed: true }),
|
|
91
|
+
]);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("blur says how long the field had focus, and whether it was changed", async () => {
|
|
95
|
+
const now = vi.spyOn(Date, "now").mockReturnValue(1_000);
|
|
96
|
+
renderWithAnalytics(<QuantityAndStrength />);
|
|
97
|
+
await userEvent.click(screen.getByLabelText("Quantity"));
|
|
98
|
+
now.mockReturnValue(3_500);
|
|
99
|
+
await userEvent.click(screen.getByLabelText("Elsewhere"));
|
|
100
|
+
|
|
101
|
+
await analytics.expectEventLog([...visitedWithoutEditing("dispenseQuantity")]);
|
|
102
|
+
expect(analytics.sentFor("dispenseQuantity")[1]).toMatchObject({ durationMs: 2_500, changed: false });
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("each focus is its own session: a change in the next one is sent again", async () => {
|
|
106
|
+
renderWithAnalytics(<QuantityAndStrength />);
|
|
107
|
+
const quantity = screen.getByLabelText("Quantity");
|
|
108
|
+
await userEvent.click(quantity);
|
|
109
|
+
await userEvent.keyboard("3");
|
|
110
|
+
await userEvent.click(screen.getByLabelText("Elsewhere"));
|
|
111
|
+
await userEvent.click(quantity);
|
|
112
|
+
await userEvent.keyboard("0");
|
|
113
|
+
await userEvent.click(screen.getByLabelText("Elsewhere"));
|
|
114
|
+
|
|
115
|
+
await analytics.expectEventLog([
|
|
116
|
+
...buildEditedEventsFor("dispenseQuantity"),
|
|
117
|
+
...buildEditedEventsFor("dispenseQuantity"),
|
|
118
|
+
]);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("a picker is one field: moving between its options sends no blur, and picking sends change", async () => {
|
|
122
|
+
renderWithAnalytics(<QuantityAndStrength />);
|
|
123
|
+
await userEvent.click(screen.getByRole("button", { name: "10 mg" }));
|
|
124
|
+
await userEvent.tab();
|
|
125
|
+
await userEvent.click(screen.getByRole("button", { name: "20 mg" }));
|
|
126
|
+
await userEvent.click(screen.getByLabelText("Elsewhere"));
|
|
127
|
+
|
|
128
|
+
await analytics.expectEventLog([...buildEditedEventsFor("strength")]);
|
|
129
|
+
expect(analytics.sentFor("strength").map((sent) => sent.hasValue)).toEqual([false, true, true]);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("a field removed while it has focus still sends its blur", async () => {
|
|
133
|
+
const { rerender } = renderWithAnalytics(<QuantityAndStrength />);
|
|
134
|
+
await userEvent.click(screen.getByLabelText("Quantity"));
|
|
135
|
+
await userEvent.keyboard("12");
|
|
136
|
+
rerender(<QuantityAndStrength showQuantity={false} />);
|
|
137
|
+
|
|
138
|
+
await analytics.expectEventLog([...buildEditedEventsFor("dispenseQuantity")]);
|
|
139
|
+
expect(analytics.sentFor("dispenseQuantity")[2]).toMatchObject({ hasValue: true, changed: true });
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("never sends what was typed", async () => {
|
|
143
|
+
renderWithAnalytics(<QuantityAndStrength />);
|
|
144
|
+
await userEvent.click(screen.getByLabelText("Quantity"));
|
|
145
|
+
await userEvent.keyboard("987654");
|
|
146
|
+
await userEvent.click(screen.getByLabelText("Elsewhere"));
|
|
147
|
+
|
|
148
|
+
await analytics.expectEventLog([...buildEditedEventsFor("dispenseQuantity")]);
|
|
149
|
+
for (const body of analytics.bodies()) expect(body).not.toContain("987654");
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("sends nothing with analytics off", async () => {
|
|
153
|
+
renderWithAnalytics(<QuantityAndStrength />, { disableAnalytics: true });
|
|
154
|
+
await userEvent.click(screen.getByLabelText("Quantity"));
|
|
155
|
+
await userEvent.keyboard("30");
|
|
156
|
+
await userEvent.click(screen.getByLabelText("Elsewhere"));
|
|
157
|
+
|
|
158
|
+
await analytics.expectEventLog([]);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("outside a PhotonProvider, the field still works and nothing is sent", async () => {
|
|
162
|
+
render(<QuantityAndStrength />);
|
|
163
|
+
await userEvent.click(screen.getByLabelText("Quantity"));
|
|
164
|
+
await userEvent.keyboard("30");
|
|
165
|
+
await userEvent.click(screen.getByLabelText("Elsewhere"));
|
|
166
|
+
|
|
167
|
+
await waitFor(() => expect(screen.getByLabelText<HTMLInputElement>("Quantity").value).toBe("30"));
|
|
168
|
+
await analytics.expectEventLog([]);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("moving within a field is one visit, and a field inside it takes focus for itself", async () => {
|
|
172
|
+
renderWithAnalytics(<TreatmentWithStrengthChips />);
|
|
173
|
+
await userEvent.click(screen.getByLabelText("Treatment"));
|
|
174
|
+
await userEvent.keyboard("amo");
|
|
175
|
+
await userEvent.click(screen.getByRole("button", { name: "Amoxicillin" }));
|
|
176
|
+
await userEvent.click(screen.getByRole("button", { name: "500 mg" }));
|
|
177
|
+
await userEvent.click(screen.getByLabelText("Treatment"));
|
|
178
|
+
await userEvent.click(screen.getByLabelText("Elsewhere"));
|
|
179
|
+
|
|
180
|
+
await analytics.expectEventLog([
|
|
181
|
+
...buildEditedEventsFor("treatment"),
|
|
182
|
+
...buildEditedEventsFor("strength"),
|
|
183
|
+
...visitedWithoutEditing("treatment"),
|
|
184
|
+
]);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { useEffect, useState, type ChangeEvent, type FocusEvent, type ReactNode } from "react";
|
|
2
|
+
import { useTrack } from "./PhotonAnalyticsProvider.tsx";
|
|
3
|
+
|
|
4
|
+
export type FormName = "find_patient" | "new_patient_form" | "update_patient_form" | "add_prescription_form";
|
|
5
|
+
|
|
6
|
+
type Interaction = "focus" | "change" | "blur";
|
|
7
|
+
|
|
8
|
+
export interface FieldInteractionOptions {
|
|
9
|
+
isOptional: boolean;
|
|
10
|
+
hasValue?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface FieldInteractionHandlers {
|
|
14
|
+
onFocus: (event: FocusEvent<Element>) => void;
|
|
15
|
+
onChange: (changedTo: unknown) => void;
|
|
16
|
+
onBlur: (event: FocusEvent<Element>) => void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface FocusedField {
|
|
20
|
+
owner: object;
|
|
21
|
+
fieldName: string;
|
|
22
|
+
element: Element;
|
|
23
|
+
focusedAt: number;
|
|
24
|
+
changed: boolean;
|
|
25
|
+
hasValue: boolean;
|
|
26
|
+
send: (interaction: Interaction, hasValue: boolean, blurProperties?: BlurProperties) => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface BlurProperties {
|
|
30
|
+
durationMs: number;
|
|
31
|
+
changed: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let focusedField: FocusedField | null = null;
|
|
35
|
+
const focusEventsClaimedByInnerField = new WeakSet<Event>();
|
|
36
|
+
|
|
37
|
+
function endFocusedField(hasValue?: boolean) {
|
|
38
|
+
const ended = focusedField;
|
|
39
|
+
if (!ended) return;
|
|
40
|
+
focusedField = null;
|
|
41
|
+
ended.send("blur", hasValue ?? ended.hasValue, {
|
|
42
|
+
durationMs: Math.max(0, Date.now() - ended.focusedAt),
|
|
43
|
+
changed: ended.changed,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function endFocusedFieldIfFocusLeft(owner: object) {
|
|
48
|
+
if (focusedField?.owner !== owner) return;
|
|
49
|
+
const { element } = focusedField;
|
|
50
|
+
if (!element.isConnected || !element.contains(element.ownerDocument.activeElement)) endFocusedField();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function useFieldInteractions(formName: FormName) {
|
|
54
|
+
const track = useTrack();
|
|
55
|
+
const [owner] = useState(() => ({}));
|
|
56
|
+
|
|
57
|
+
useEffect(() => endFocusedFieldIfFocusLeft(owner));
|
|
58
|
+
useEffect(
|
|
59
|
+
() => () => {
|
|
60
|
+
setTimeout(() => endFocusedFieldIfFocusLeft(owner));
|
|
61
|
+
},
|
|
62
|
+
[owner],
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
return function fieldInteraction(fieldName: string, options: FieldInteractionOptions): FieldInteractionHandlers {
|
|
66
|
+
const send = (interaction: Interaction, hasValue: boolean, blurProperties?: BlurProperties) =>
|
|
67
|
+
track("Field Interaction", "fieldInteraction", {
|
|
68
|
+
formName,
|
|
69
|
+
fieldName,
|
|
70
|
+
interaction,
|
|
71
|
+
hasValue,
|
|
72
|
+
isOptional: options.isOptional,
|
|
73
|
+
...blurProperties,
|
|
74
|
+
});
|
|
75
|
+
const isThisFieldFocused = () => focusedField?.owner === owner && focusedField.fieldName === fieldName;
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
onFocus: (event) => {
|
|
79
|
+
if (focusEventsClaimedByInnerField.has(event.nativeEvent)) return;
|
|
80
|
+
focusEventsClaimedByInnerField.add(event.nativeEvent);
|
|
81
|
+
if (isThisFieldFocused()) return;
|
|
82
|
+
endFocusedField();
|
|
83
|
+
const hasValue = options.hasValue ?? holdsValue(event.currentTarget);
|
|
84
|
+
focusedField = {
|
|
85
|
+
owner,
|
|
86
|
+
fieldName,
|
|
87
|
+
element: event.currentTarget,
|
|
88
|
+
focusedAt: Date.now(),
|
|
89
|
+
changed: false,
|
|
90
|
+
hasValue,
|
|
91
|
+
send,
|
|
92
|
+
};
|
|
93
|
+
send("focus", hasValue);
|
|
94
|
+
},
|
|
95
|
+
onChange: (changedTo) => {
|
|
96
|
+
const hasValue = hasValueAfterChange(changedTo, options.hasValue);
|
|
97
|
+
if (!isThisFieldFocused()) return send("change", hasValue);
|
|
98
|
+
focusedField!.hasValue = hasValue;
|
|
99
|
+
if (focusedField!.changed) return;
|
|
100
|
+
focusedField!.changed = true;
|
|
101
|
+
send("change", hasValue);
|
|
102
|
+
},
|
|
103
|
+
onBlur: (event) => {
|
|
104
|
+
if (!focusMovedWithin(event) && isThisFieldFocused())
|
|
105
|
+
endFocusedField(options.hasValue ?? holdsValue(event.currentTarget));
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function TrackedField({
|
|
112
|
+
formName,
|
|
113
|
+
fieldName,
|
|
114
|
+
isOptional,
|
|
115
|
+
hasValue,
|
|
116
|
+
children,
|
|
117
|
+
}: FieldInteractionOptions & { formName: FormName; fieldName: string; children: ReactNode }) {
|
|
118
|
+
const handlers = useFieldInteractions(formName)(fieldName, { isOptional, hasValue });
|
|
119
|
+
return (
|
|
120
|
+
<div style={{ display: "contents" }} {...handlers}>
|
|
121
|
+
{children}
|
|
122
|
+
</div>
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function focusMovedWithin(event: FocusEvent<Element>) {
|
|
127
|
+
const other = event.relatedTarget;
|
|
128
|
+
return other instanceof Node && other !== event.currentTarget && event.currentTarget.contains(other);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function hasValueAfterChange(changedTo: unknown, declared: boolean | undefined): boolean {
|
|
132
|
+
if (typeof changedTo === "boolean") return changedTo;
|
|
133
|
+
const event = changedTo as Partial<ChangeEvent<Element>> | undefined;
|
|
134
|
+
if (event?.currentTarget instanceof Element) return holdsValue(event.currentTarget);
|
|
135
|
+
return declared ?? true;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function holdsValue(element: Element): boolean {
|
|
139
|
+
if (element instanceof HTMLInputElement && (element.type === "checkbox" || element.type === "radio"))
|
|
140
|
+
return element.checked;
|
|
141
|
+
if (
|
|
142
|
+
element instanceof HTMLInputElement ||
|
|
143
|
+
element instanceof HTMLTextAreaElement ||
|
|
144
|
+
element instanceof HTMLSelectElement
|
|
145
|
+
)
|
|
146
|
+
return element.value.trim() !== "";
|
|
147
|
+
return [...element.querySelectorAll("input, textarea, select")].some(holdsValue);
|
|
148
|
+
}
|