@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.
Files changed (272) hide show
  1. package/README.md +52 -9
  2. package/dist/adapters/tailwind.d.ts.map +1 -1
  3. package/dist/adapters/tailwind.js +7 -4
  4. package/dist/adapters/tailwind.js.map +1 -1
  5. package/dist/analytics/PhotonAnalyticsProvider.d.ts +19 -0
  6. package/dist/analytics/PhotonAnalyticsProvider.d.ts.map +1 -0
  7. package/dist/analytics/PhotonAnalyticsProvider.js +76 -0
  8. package/dist/analytics/PhotonAnalyticsProvider.js.map +1 -0
  9. package/dist/analytics/cardLifecycle.d.ts +10 -0
  10. package/dist/analytics/cardLifecycle.d.ts.map +1 -0
  11. package/dist/analytics/cardLifecycle.js +43 -0
  12. package/dist/analytics/cardLifecycle.js.map +1 -0
  13. package/dist/analytics/fieldInteraction.d.ts +18 -0
  14. package/dist/analytics/fieldInteraction.d.ts.map +1 -0
  15. package/dist/analytics/fieldInteraction.js +102 -0
  16. package/dist/analytics/fieldInteraction.js.map +1 -0
  17. package/dist/analytics/lifecycleOutcomes.d.ts +8 -0
  18. package/dist/analytics/lifecycleOutcomes.d.ts.map +1 -0
  19. package/dist/analytics/lifecycleOutcomes.js +16 -0
  20. package/dist/analytics/lifecycleOutcomes.js.map +1 -0
  21. package/dist/analytics/tracker.d.ts +19 -0
  22. package/dist/analytics/tracker.d.ts.map +1 -0
  23. package/dist/analytics/tracker.js +41 -0
  24. package/dist/analytics/tracker.js.map +1 -0
  25. package/dist/analytics/viewed.d.ts +11 -0
  26. package/dist/analytics/viewed.d.ts.map +1 -0
  27. package/dist/analytics/viewed.js +16 -0
  28. package/dist/analytics/viewed.js.map +1 -0
  29. package/dist/components/PhotonOrder.d.ts +18 -18
  30. package/dist/components/PhotonOrder.d.ts.map +1 -1
  31. package/dist/components/PhotonOrder.js +34 -19
  32. package/dist/components/PhotonOrder.js.map +1 -1
  33. package/dist/components/PhotonPatient.d.ts +7 -35
  34. package/dist/components/PhotonPatient.d.ts.map +1 -1
  35. package/dist/components/PhotonPatient.js +6 -78
  36. package/dist/components/PhotonPatient.js.map +1 -1
  37. package/dist/components/PhotonPrescription.d.ts +15 -26
  38. package/dist/components/PhotonPrescription.d.ts.map +1 -1
  39. package/dist/components/PhotonPrescription.js +6 -360
  40. package/dist/components/PhotonPrescription.js.map +1 -1
  41. package/dist/components/format.d.ts +2 -2
  42. package/dist/components/format.js +2 -2
  43. package/dist/components/index.d.ts +1 -1
  44. package/dist/components/index.d.ts.map +1 -1
  45. package/dist/components/index.js +1 -1
  46. package/dist/components/index.js.map +1 -1
  47. package/dist/components/open.d.ts +11 -13
  48. package/dist/components/open.d.ts.map +1 -1
  49. package/dist/components/open.js +7 -2
  50. package/dist/components/open.js.map +1 -1
  51. package/dist/components/order/DraftOrderPrompt.d.ts +18 -0
  52. package/dist/components/order/DraftOrderPrompt.d.ts.map +1 -0
  53. package/dist/components/order/DraftOrderPrompt.js +26 -0
  54. package/dist/components/order/DraftOrderPrompt.js.map +1 -0
  55. package/dist/components/patient/PatientCard/PatientCard.d.ts +30 -0
  56. package/dist/components/patient/PatientCard/PatientCard.d.ts.map +1 -0
  57. package/dist/components/patient/PatientCard/PatientCard.js +101 -0
  58. package/dist/components/patient/PatientCard/PatientCard.js.map +1 -0
  59. package/dist/components/patient/PatientCard/usePatientToShow.d.ts +22 -0
  60. package/dist/components/patient/PatientCard/usePatientToShow.d.ts.map +1 -0
  61. package/dist/components/patient/PatientCard/usePatientToShow.js +40 -0
  62. package/dist/components/patient/PatientCard/usePatientToShow.js.map +1 -0
  63. package/dist/components/patient/PatientIntakeForm.d.ts +16 -0
  64. package/dist/components/patient/PatientIntakeForm.d.ts.map +1 -0
  65. package/dist/components/patient/PatientIntakeForm.js +114 -0
  66. package/dist/components/patient/PatientIntakeForm.js.map +1 -0
  67. package/dist/components/patient/PatientSearch.d.ts +12 -0
  68. package/dist/components/patient/PatientSearch.d.ts.map +1 -0
  69. package/dist/components/patient/PatientSearch.js +148 -0
  70. package/dist/components/patient/PatientSearch.js.map +1 -0
  71. package/dist/components/patient/{Benefits.d.ts → PatientSummary/BenefitsInputForm.d.ts} +2 -2
  72. package/dist/components/patient/PatientSummary/BenefitsInputForm.d.ts.map +1 -0
  73. package/dist/components/patient/{Benefits.js → PatientSummary/BenefitsInputForm.js} +6 -5
  74. package/dist/components/patient/PatientSummary/BenefitsInputForm.js.map +1 -0
  75. package/dist/components/patient/{Clinical.d.ts → PatientSummary/ClinicalInputForm.d.ts} +15 -16
  76. package/dist/components/patient/PatientSummary/ClinicalInputForm.d.ts.map +1 -0
  77. package/dist/components/patient/{Clinical.js → PatientSummary/ClinicalInputForm.js} +29 -28
  78. package/dist/components/patient/PatientSummary/ClinicalInputForm.js.map +1 -0
  79. package/dist/components/patient/PatientSummary/CollapsedSummary.d.ts +5 -0
  80. package/dist/components/patient/PatientSummary/CollapsedSummary.d.ts.map +1 -0
  81. package/dist/components/patient/PatientSummary/CollapsedSummary.js +30 -0
  82. package/dist/components/patient/PatientSummary/CollapsedSummary.js.map +1 -0
  83. package/dist/components/patient/PatientSummary/PatientDetailsForm.d.ts +10 -0
  84. package/dist/components/patient/PatientSummary/PatientDetailsForm.d.ts.map +1 -0
  85. package/dist/components/patient/PatientSummary/PatientDetailsForm.js +65 -0
  86. package/dist/components/patient/PatientSummary/PatientDetailsForm.js.map +1 -0
  87. package/dist/components/patient/PatientSummary/PatientDetailsSchema.d.ts +53 -0
  88. package/dist/components/patient/PatientSummary/PatientDetailsSchema.d.ts.map +1 -0
  89. package/dist/components/patient/PatientSummary/PatientDetailsSchema.js +78 -0
  90. package/dist/components/patient/PatientSummary/PatientDetailsSchema.js.map +1 -0
  91. package/dist/components/patient/PatientSummary/PatientSummary.d.ts +46 -0
  92. package/dist/components/patient/PatientSummary/PatientSummary.d.ts.map +1 -0
  93. package/dist/components/patient/PatientSummary/PatientSummary.js +57 -0
  94. package/dist/components/patient/PatientSummary/PatientSummary.js.map +1 -0
  95. package/dist/components/patient/PatientSummary/SummaryHeader.d.ts +19 -0
  96. package/dist/components/patient/PatientSummary/SummaryHeader.d.ts.map +1 -0
  97. package/dist/components/patient/PatientSummary/SummaryHeader.js +22 -0
  98. package/dist/components/patient/PatientSummary/SummaryHeader.js.map +1 -0
  99. package/dist/components/patient/fields.d.ts +7 -26
  100. package/dist/components/patient/fields.d.ts.map +1 -1
  101. package/dist/components/patient/fields.js +11 -52
  102. package/dist/components/patient/fields.js.map +1 -1
  103. package/dist/components/patient/parsePatientSearchQuery.d.ts +16 -0
  104. package/dist/components/patient/parsePatientSearchQuery.d.ts.map +1 -0
  105. package/dist/components/patient/{query.js → parsePatientSearchQuery.js} +3 -10
  106. package/dist/components/patient/parsePatientSearchQuery.js.map +1 -0
  107. package/dist/components/prescription/PrescriptionCard.d.ts +51 -0
  108. package/dist/components/prescription/PrescriptionCard.d.ts.map +1 -0
  109. package/dist/components/prescription/PrescriptionCard.js +186 -0
  110. package/dist/components/prescription/PrescriptionCard.js.map +1 -0
  111. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.d.ts +15 -0
  112. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.d.ts.map +1 -0
  113. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.js +18 -0
  114. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.js.map +1 -0
  115. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.d.ts +23 -0
  116. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.d.ts.map +1 -0
  117. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.js +42 -0
  118. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.js.map +1 -0
  119. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts +27 -0
  120. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts.map +1 -0
  121. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js +169 -0
  122. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js.map +1 -0
  123. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.d.ts +8 -0
  124. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.d.ts.map +1 -0
  125. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.js +12 -0
  126. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.js.map +1 -0
  127. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.d.ts +11 -0
  128. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.d.ts.map +1 -0
  129. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.js +25 -0
  130. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.js.map +1 -0
  131. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.d.ts +50 -0
  132. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.d.ts.map +1 -0
  133. package/dist/components/{TreatmentPicker.js → prescription/TreatmentSearch/TreatmentPicker.js} +113 -105
  134. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.js.map +1 -0
  135. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.d.ts +28 -0
  136. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.d.ts.map +1 -0
  137. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.js +146 -0
  138. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.js.map +1 -0
  139. package/dist/components/schemaErrors.d.ts +13 -0
  140. package/dist/components/schemaErrors.d.ts.map +1 -0
  141. package/dist/components/schemaErrors.js +25 -0
  142. package/dist/components/schemaErrors.js.map +1 -0
  143. package/dist/components/search.d.ts +1 -1
  144. package/dist/components/search.js +1 -1
  145. package/dist/index.d.ts +2 -0
  146. package/dist/index.d.ts.map +1 -1
  147. package/dist/index.js +1 -0
  148. package/dist/index.js.map +1 -1
  149. package/dist/meta/version.d.ts +5 -0
  150. package/dist/meta/version.d.ts.map +1 -0
  151. package/dist/meta/version.js +5 -0
  152. package/dist/meta/version.js.map +1 -0
  153. package/dist/primitives/PhotonProvider.d.ts +3 -1
  154. package/dist/primitives/PhotonProvider.d.ts.map +1 -1
  155. package/dist/primitives/PhotonProvider.js +23 -11
  156. package/dist/primitives/PhotonProvider.js.map +1 -1
  157. package/dist/primitives/defaults.d.ts.map +1 -1
  158. package/dist/primitives/defaults.js +6 -3
  159. package/dist/primitives/defaults.js.map +1 -1
  160. package/dist/primitives/photonContext.d.ts +3 -0
  161. package/dist/primitives/photonContext.d.ts.map +1 -0
  162. package/dist/primitives/photonContext.js +3 -0
  163. package/dist/primitives/photonContext.js.map +1 -0
  164. package/dist/primitives/types.d.ts +15 -3
  165. package/dist/primitives/types.d.ts.map +1 -1
  166. package/dist/primitives/types.js.map +1 -1
  167. package/dist/primitives/useProvidedPhoton.d.ts +8 -0
  168. package/dist/primitives/useProvidedPhoton.d.ts.map +1 -0
  169. package/dist/primitives/useProvidedPhoton.js +8 -0
  170. package/dist/primitives/useProvidedPhoton.js.map +1 -0
  171. package/dist/styles.css +45 -3
  172. package/dist/test-utils/contract.d.ts.map +1 -1
  173. package/dist/test-utils/contract.js +23 -2
  174. package/dist/test-utils/contract.js.map +1 -1
  175. package/package.json +8 -3
  176. package/src/adapters/tailwind.tsx +52 -24
  177. package/src/analytics/PhotonAnalyticsProvider.test.tsx +190 -0
  178. package/src/analytics/PhotonAnalyticsProvider.tsx +151 -0
  179. package/src/analytics/cardLifecycle.test.tsx +272 -0
  180. package/src/analytics/cardLifecycle.ts +44 -0
  181. package/src/analytics/fieldInteraction.test.tsx +186 -0
  182. package/src/analytics/fieldInteraction.tsx +148 -0
  183. package/src/analytics/lifecycleOutcomes.test.ts +78 -0
  184. package/src/analytics/lifecycleOutcomes.ts +29 -0
  185. package/src/analytics/tracker.test.ts +52 -0
  186. package/src/analytics/tracker.ts +49 -0
  187. package/src/analytics/usePhotonAnalytics.test.tsx +106 -0
  188. package/src/analytics/viewed.ts +27 -0
  189. package/src/components/PhotonOrder.test.tsx +394 -0
  190. package/src/components/PhotonOrder.tsx +79 -53
  191. package/src/components/PhotonPatient.tsx +15 -166
  192. package/src/components/PhotonPrescription.test.tsx +120 -0
  193. package/src/components/PhotonPrescription.tsx +23 -583
  194. package/src/components/ScreeningAlerts.test.tsx +81 -0
  195. package/src/components/format.ts +2 -2
  196. package/src/components/index.ts +1 -1
  197. package/src/components/open.ts +13 -16
  198. package/src/components/order/DraftOrderPrompt.test.tsx +77 -0
  199. package/src/components/order/DraftOrderPrompt.tsx +81 -0
  200. package/src/components/patient/PatientCard/PatientCard.test.tsx +255 -0
  201. package/src/components/patient/PatientCard/PatientCard.tsx +177 -0
  202. package/src/components/patient/PatientCard/usePatientToShow.ts +44 -0
  203. package/src/components/patient/PatientIntakeForm.test.tsx +262 -0
  204. package/src/components/patient/PatientIntakeForm.tsx +222 -0
  205. package/src/components/patient/PatientSearch.test.tsx +204 -0
  206. package/src/components/patient/PatientSearch.tsx +221 -0
  207. package/src/components/patient/PatientSummary/BenefitsInputForm.test.tsx +164 -0
  208. package/src/components/patient/{Benefits.tsx → PatientSummary/BenefitsInputForm.tsx} +24 -16
  209. package/src/components/patient/PatientSummary/ClinicalInputForm.test.tsx +247 -0
  210. package/src/components/patient/{Clinical.tsx → PatientSummary/ClinicalInputForm.tsx} +53 -48
  211. package/src/components/patient/PatientSummary/CollapsedSummary.test.tsx +90 -0
  212. package/src/components/patient/PatientSummary/CollapsedSummary.tsx +46 -0
  213. package/src/components/patient/PatientSummary/PatientDetailsForm.test.tsx +201 -0
  214. package/src/components/patient/PatientSummary/PatientDetailsForm.tsx +320 -0
  215. package/src/components/patient/PatientSummary/PatientDetailsSchema.test.ts +296 -0
  216. package/src/components/patient/PatientSummary/PatientDetailsSchema.ts +95 -0
  217. package/src/components/patient/PatientSummary/PatientSummary.test.tsx +194 -0
  218. package/src/components/patient/PatientSummary/PatientSummary.tsx +225 -0
  219. package/src/components/patient/PatientSummary/SummaryHeader.test.tsx +183 -0
  220. package/src/components/patient/PatientSummary/SummaryHeader.tsx +85 -0
  221. package/src/components/patient/fields.tsx +12 -124
  222. package/src/components/patient/parsePatientSearchQuery.test.ts +107 -0
  223. package/src/components/patient/{query.ts → parsePatientSearchQuery.ts} +4 -15
  224. package/src/components/prescription/PrescriptionCard.test.tsx +247 -0
  225. package/src/components/prescription/PrescriptionCard.tsx +361 -0
  226. package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.test.tsx +82 -0
  227. package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.tsx +41 -0
  228. package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.test.tsx +206 -0
  229. package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.tsx +99 -0
  230. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.test.tsx +453 -0
  231. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.tsx +238 -0
  232. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.test.ts +63 -0
  233. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.ts +15 -0
  234. package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.test.tsx +72 -0
  235. package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.tsx +41 -0
  236. package/src/components/prescription/TreatmentSearch/TreatmentPicker.tsx +375 -0
  237. package/src/components/prescription/TreatmentSearch/TreatmentSearch.test.tsx +695 -0
  238. package/src/components/prescription/TreatmentSearch/TreatmentSearch.tsx +220 -0
  239. package/src/components/schemaErrors.test.ts +49 -0
  240. package/src/components/schemaErrors.ts +34 -0
  241. package/src/components/search.tsx +1 -1
  242. package/src/index.ts +2 -0
  243. package/src/meta/version.test.ts +11 -0
  244. package/src/meta/version.ts +4 -0
  245. package/src/primitives/PhotonProvider.tsx +30 -15
  246. package/src/primitives/defaults.tsx +44 -21
  247. package/src/primitives/photonContext.ts +4 -0
  248. package/src/primitives/types.ts +15 -3
  249. package/src/primitives/useProvidedPhoton.ts +21 -0
  250. package/src/styles.css +45 -3
  251. package/src/test-utils/contract.tsx +45 -2
  252. package/dist/components/TreatmentPicker.d.ts +0 -45
  253. package/dist/components/TreatmentPicker.d.ts.map +0 -1
  254. package/dist/components/TreatmentPicker.js.map +0 -1
  255. package/dist/components/patient/Benefits.d.ts.map +0 -1
  256. package/dist/components/patient/Benefits.js.map +0 -1
  257. package/dist/components/patient/Clinical.d.ts.map +0 -1
  258. package/dist/components/patient/Clinical.js.map +0 -1
  259. package/dist/components/patient/IntakeForm.d.ts +0 -10
  260. package/dist/components/patient/IntakeForm.d.ts.map +0 -1
  261. package/dist/components/patient/IntakeForm.js +0 -324
  262. package/dist/components/patient/IntakeForm.js.map +0 -1
  263. package/dist/components/patient/Summary.d.ts +0 -48
  264. package/dist/components/patient/Summary.d.ts.map +0 -1
  265. package/dist/components/patient/Summary.js +0 -80
  266. package/dist/components/patient/Summary.js.map +0 -1
  267. package/dist/components/patient/query.d.ts +0 -23
  268. package/dist/components/patient/query.d.ts.map +0 -1
  269. package/dist/components/patient/query.js.map +0 -1
  270. package/src/components/TreatmentPicker.tsx +0 -350
  271. package/src/components/patient/IntakeForm.tsx +0 -532
  272. package/src/components/patient/Summary.tsx +0 -310
@@ -0,0 +1,695 @@
1
+ import { act, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
2
+ import userEvent from "@testing-library/user-event";
3
+ import { describe, expect, it } from "vitest";
4
+ import { payload, rx, fakeNetworkApiClient } from "@photonhealth/client/testing";
5
+ import { PhotonPrescription, type PhotonPrescriptionProps } from "../../../index.ts";
6
+ import {
7
+ choices,
8
+ fakeNetworkApi,
9
+ option,
10
+ patient,
11
+ pickDrug,
12
+ resolvedTo,
13
+ tapDrug,
14
+ withPhoton,
15
+ type DrugSearches,
16
+ } from "../../../../test/cards.fixtures.tsx";
17
+
18
+ // Treatment search on a prescription card: finding the drug, picking its form and strength, and what the card shows
19
+ // once the product is picked — through <PhotonPrescription>, which it saves through. In the stock building blocks:
20
+ // every set of building blocks is held to the same contract.
21
+ const lisinoprilTablets10And20Mg = choices(
22
+ option("Lisinopril Oral Tablet 10 MG", "med_10"),
23
+ option("Lisinopril Oral Tablet 20 MG", "med_20"),
24
+ );
25
+ const amoxicillinInTwoFormsAndAmoxapine = choices(
26
+ option("Amoxicillin Oral Capsule 500 MG", "med_cap500"),
27
+ option("Amoxicillin Oral Capsule 250 MG", "med_cap250"),
28
+ option("Amoxicillin Oral Suspension Reconstituted 400 MG/5ML", "med_sus400"),
29
+ option("Amoxapine Oral Tablet 25 MG", "med_amoxapine"),
30
+ );
31
+ const amoxicillinCapsulesAndAmoxicillinClavulanate = choices(
32
+ option("Amoxicillin Oral Capsule 250 MG", "med_cap250"),
33
+ option("Amoxicillin Oral Capsule 500 MG", "med_cap500"),
34
+ option("Amoxicillin-Pot Clavulanate Oral Tablet 875-125 MG", "med_clav"),
35
+ );
36
+ const lisinoprilAsTabletsAndSolutions = choices(
37
+ option("Lisinopril Oral Tablet 10 MG", "med_tab10"),
38
+ option("Lisinopril Oral Tablet 20 MG", "med_tab20"),
39
+ option("Lisinopril Oral Solution 10 MG", "med_sol10"),
40
+ option("Lisinopril Oral Solution 5 MG", "med_sol5"),
41
+ );
42
+ const amoxicillinAmoxapineAndBotoxSearches: DrugSearches = {
43
+ amox: [
44
+ ["Amoxicillin", "Capsule", "250 MG"],
45
+ ["Amoxicillin", "Capsule", "500 MG"],
46
+ ["Amoxicillin", "Tablet", "500 MG"],
47
+ ["Amoxicillin", "Tablet", "875 MG"],
48
+ ["Amoxapine", "Tablet", "100 MG"],
49
+ ["Amoxapine", "Tablet", "150 MG"],
50
+ ],
51
+ botox: [
52
+ ["Botox", "Injection", "100 UNT"],
53
+ ["Botox", "Injection", "200 UNT"],
54
+ ],
55
+ };
56
+
57
+ const amoxicillinAmongFiveDrugs = choices(
58
+ option("Amoxicillin Oral Capsule 500 MG", "med_cap500"),
59
+ option("Amoxicillin Oral Suspension Reconstituted 400 MG/5ML", "med_sus400"),
60
+ option("Amoxicillin Trihydrate Oral Suspension Reconstituted 250 MG/5ML", "med_tri250"),
61
+ option("Amoxicillin-Pot Clavulanate Oral Tablet 875-125 MG", "med_clav"),
62
+ option("Amoxicillin Sodium Injection 500 MG", "med_sodium"),
63
+ option("Amoxapine Oral Tablet 25 MG", "med_amoxapine"),
64
+ );
65
+
66
+ describe("TreatmentSearch", () => {
67
+ describe("searching", () => {
68
+ it("is a type-ahead: from 3 characters, once typing pauses — and never drafts until a drug is picked", async () => {
69
+ const component = renderComponent({ lookupPrescriptions: lisinoprilTablets10And20Mg });
70
+ const lookups = () => component.calls.filter((c) => c.name === "LookupPrescription");
71
+
72
+ const med = await component.typeIntoTreatment("li");
73
+ await new Promise((r) => setTimeout(r, 400));
74
+ expect(lookups()).toHaveLength(0); // under 3 characters: nothing
75
+ fireEvent.change(med, { target: { value: "lis" } });
76
+ fireEvent.change(med, { target: { value: "lisino" } }); // each keystroke restarts the wait
77
+ await waitFor(() => expect(lookups()).toHaveLength(1), { timeout: 2000 });
78
+ expect(lookups()[0]!.vars.treatment.name).toBe("lisino");
79
+ // The search spins in the field it's for — no footer spinner that opens and closes.
80
+ expect(med.getAttribute("aria-busy")).toBe("true");
81
+ expect(screen.queryByRole("status")).toBeNull();
82
+
83
+ fireEvent.change(med, { target: { value: "lisinopril" } });
84
+ await waitFor(() => expect(lookups()).toHaveLength(2), { timeout: 2000 });
85
+ expect(lookups().every((c) => !("patient" in c.vars))).toBe(true);
86
+ expect(component.wasPrescriptionDrafted()).toBe(false); // typing never drafts
87
+
88
+ await pickDrug("Lisinopril Oral Tablet 10 MG");
89
+ expect(await component.waitForDraftedPrescription()).toMatchObject({
90
+ patient: { id: "pat_1" },
91
+ treatment: { id: "med_10" },
92
+ });
93
+ });
94
+
95
+ it("a search that comes back with nothing declared doesn't pick a drug — nothing is drafted", async () => {
96
+ const component = renderComponent({
97
+ lookupPrescriptions: payload("prescription", "PrescriptionPayload", null),
98
+ });
99
+ const med = await component.typeIntoTreatment("amoxicillin 500 mg capsule");
100
+ fireEvent.keyDown(med, { key: "Enter" });
101
+ await waitFor(() => expect(component.names()).toContain("LookupPrescription"));
102
+ await new Promise((r) => setTimeout(r, 50));
103
+ expect(component.wasPrescriptionDrafted()).toBe(false);
104
+ expect(screen.queryByLabelText("Directions (sig)")).toBeNull(); // still choosing
105
+ });
106
+
107
+ it("a name the server resolves drafts once — the read-back doesn't re-send the typed name", async () => {
108
+ const stored = rx({
109
+ treatment: { ...rx().treatment, name: "Amoxicillin" },
110
+ signing: { ...rx().signing, contentHash: "h2" },
111
+ });
112
+ const component = renderComponent({
113
+ script: {
114
+ LookupPrescription: resolvedTo("trt_1"),
115
+ SyncPrescription: () =>
116
+ payload("prescription", "PrescriptionPayload", {
117
+ ...stored,
118
+ signing: { ...stored.signing, contentHash: "h1" },
119
+ }),
120
+ GetPrescription: () => payload("prescription", "PrescriptionPayload", stored),
121
+ UpdatePrescription: () => payload("prescription", "PrescriptionPayload", stored),
122
+ },
123
+ });
124
+ const med = await component.typeIntoTreatment("amoxicillin 500 mg capsule");
125
+ fireEvent.keyDown(med, { key: "Enter" });
126
+ await screen.findByRole("button", { name: "Sign" });
127
+ await new Promise((r) => setTimeout(r, 700)); // past the type-ahead debounce
128
+ expect(component.names()).not.toContain("UpdatePrescription");
129
+ expect(screen.getByRole("heading", { name: /amoxicillin/i })).toBeTruthy();
130
+ });
131
+ });
132
+
133
+ describe("going back to Treatment from the drug list", () => {
134
+ it("a letter typed in the list, entered with Tab, goes into Treatment", async () => {
135
+ const user = userEvent.setup();
136
+ const component = renderComponent({ lookupPrescriptions: amoxicillinCapsulesAndAmoxicillinClavulanate });
137
+ const med = await component.findTreatmentField();
138
+ await enterDrugListFromTreatment(user, med, "{Tab}");
139
+
140
+ await user.keyboard("x");
141
+ expect(document.activeElement).toBe(med);
142
+ expect(med.value).toBe("amoxx");
143
+ });
144
+
145
+ it("a letter typed in the list, entered with ↓, goes into Treatment", async () => {
146
+ const user = userEvent.setup();
147
+ const component = renderComponent({ lookupPrescriptions: amoxicillinCapsulesAndAmoxicillinClavulanate });
148
+ const med = await component.findTreatmentField();
149
+ await enterDrugListFromTreatment(user, med, "{ArrowDown}");
150
+
151
+ await user.keyboard("x");
152
+ expect(document.activeElement).toBe(med);
153
+ expect(med.value).toBe("amoxx");
154
+ });
155
+
156
+ it("clicking Treatment from the list puts the cursor back in it, and it takes typing", async () => {
157
+ const user = userEvent.setup();
158
+ const component = renderComponent({ lookupPrescriptions: amoxicillinCapsulesAndAmoxicillinClavulanate });
159
+ const med = await component.findTreatmentField();
160
+ await enterDrugListFromTreatment(user, med, "{ArrowDown}");
161
+
162
+ await user.click(med);
163
+ expect(document.activeElement).toBe(med);
164
+ await user.keyboard("x");
165
+ expect(med.value).toBe("amoxx");
166
+ });
167
+
168
+ it("the list's own keys still act on it: ↓ moves the highlight, a digit picks", async () => {
169
+ const user = userEvent.setup();
170
+ const component = renderComponent({ lookupPrescriptions: amoxicillinCapsulesAndAmoxicillinClavulanate });
171
+ const med = await component.findTreatmentField();
172
+ await enterDrugListFromTreatment(user, med, "{ArrowDown}");
173
+
174
+ expect(component.highlightedDrugRowText()).toMatch(/^Amoxicillin\s*Oral capsule/);
175
+ await user.keyboard("{ArrowDown}");
176
+ expect(component.highlightedDrugRowText()).toMatch(/^Amoxicillin-Pot Clavulanate/);
177
+ await user.keyboard("2");
178
+ expect(await component.waitForDraftedPrescription()).toMatchObject({ treatment: { id: "med_clav" } });
179
+ });
180
+ });
181
+
182
+ describe("browsing the drug list", () => {
183
+ it("has one row per drug: a drug that's one product says it under its name, and no chips until one is picked", async () => {
184
+ const component = renderComponent({ lookupPrescriptions: amoxicillinInTwoFormsAndAmoxapine });
185
+ await component.typeIntoTreatment("amox");
186
+ const picker = (await screen.findByRole("list", { name: "Pick a treatment" })).parentElement!;
187
+ expect(screen.getByRole("button", { name: "Amoxapine Oral Tablet 25 MG" })).toBeTruthy();
188
+ expect(within(picker).getByText("Oral tablet · 25 mg")).toBeTruthy();
189
+ expect(screen.getByRole("button", { name: /^Amoxicillin\s*2 forms · 3 strengths$/ })).toBeTruthy();
190
+ expect(screen.queryByRole("group")).toBeNull();
191
+ // No keycaps or key hints: the keys work without being labelled.
192
+ expect(within(picker).queryByText(/to switch|to change|medications match|Selected/)).toBeNull();
193
+ });
194
+
195
+ it("takes focus when it holds a few drugs; a key that types goes back to Treatment, ↓ comes back", async () => {
196
+ const component = renderComponent({ lookupPrescriptions: amoxicillinInTwoFormsAndAmoxapine });
197
+ const med = await component.typeIntoTreatment("amox");
198
+ const picker = await component.waitForDrugListToTakeFocus();
199
+ expect(component.highlightedDrugRowText()).toMatch(/^Amoxapine/);
200
+ fireEvent.keyDown(picker, { key: "2" });
201
+ expect(document.activeElement).toBe(med);
202
+ fireEvent.keyDown(med, { key: "ArrowDown" });
203
+ expect(document.activeElement).toBe(picker);
204
+ });
205
+
206
+ it("entered with ↓, its keys move the highlight and never pick: J / K, D to the next (wrapping), Home / End", async () => {
207
+ const component = renderComponent({ lookupPrescriptions: amoxicillinInTwoFormsAndAmoxapine });
208
+ const picker = await component.enterDrugListWithArrowDown(await component.typeIntoTreatment("amox"));
209
+ expect(component.highlightedDrugRowText()).toMatch(/^Amoxapine/);
210
+ fireEvent.keyDown(picker, { key: "j" });
211
+ expect(component.highlightedDrugRowText()).toMatch(/^Amoxicillin/);
212
+ fireEvent.keyDown(picker, { key: "k" });
213
+ expect(component.highlightedDrugRowText()).toMatch(/^Amoxapine/);
214
+ fireEvent.keyDown(picker, { key: "G" });
215
+ expect(component.highlightedDrugRowText()).toMatch(/^Amoxicillin/);
216
+ fireEvent.keyDown(picker, { key: "d" }); // past the last, back to the first
217
+ expect(component.highlightedDrugRowText()).toMatch(/^Amoxapine/);
218
+ fireEvent.keyDown(picker, { key: "End" });
219
+ expect(document.activeElement).toBe(picker);
220
+ expect(component.wasPrescriptionDrafted()).toBe(false);
221
+ });
222
+
223
+ it("is one tab stop: Tab moves the highlight, and past the last row Tab leaves the list", async () => {
224
+ const component = renderComponent({ lookupPrescriptions: amoxicillinCapsulesAndAmoxicillinClavulanate });
225
+ await component.typeIntoTreatment("amoxicillin");
226
+ const picker = (await screen.findByRole("list", { name: "Pick a treatment" })).parentElement!;
227
+ expect(screen.queryByText("Pick a treatment")).toBeNull(); // the list's name, for assistive tech — not shown
228
+ expect([...picker.querySelectorAll("button")].filter((b) => b.tabIndex === 0)).toEqual([]);
229
+
230
+ picker.focus();
231
+ fireEvent.keyDown(picker, { key: "Tab" });
232
+ expect(
233
+ screen.getByRole("button", { name: "Amoxicillin-Pot Clavulanate Oral Tablet 875-125 MG", current: true }),
234
+ ).toBeTruthy();
235
+ expect(document.activeElement).toBe(picker);
236
+ expect(fireEvent.keyDown(picker, { key: "Tab" })).toBe(true); // the browser's Tab, out of the list
237
+ fireEvent.keyDown(picker, { key: "Tab", shiftKey: true });
238
+ expect(component.highlightedDrugRowText()).toMatch(/^Amoxicillin/);
239
+ });
240
+ });
241
+
242
+ describe("picking a drug", () => {
243
+ it("puts its name in Treatment and closes the list; its forms and strengths show as chips, none pressed", async () => {
244
+ const { component, med } = await pickAmoxicillinFromDrugListWithKeys();
245
+ expect(med.value).toBe("Amoxicillin");
246
+ expect(component.openDrugList()).toBeNull();
247
+ expect(component.chipLabelsIn("Form")).toEqual(["Oral capsule", "Oral suspension reconstituted400 mg/5 mL"]);
248
+ expect(component.chipLabelsIn("Strength")).toEqual(["250 mg", "400 mg/5 mL", "500 mg"]);
249
+ expect(screen.queryByRole("button", { pressed: true })).toBeNull();
250
+ expect(document.activeElement).toBe(screen.getByRole("button", { name: /^Oral capsule$/ }));
251
+ });
252
+
253
+ it("F moves along the forms (wrapping); Esc goes back to Treatment", async () => {
254
+ const { component, med } = await pickAmoxicillinFromDrugListWithKeys();
255
+ const capsule = screen.getByRole("button", { name: /^Oral capsule$/ });
256
+ fireEvent.keyDown(capsule, { key: "f" });
257
+ expect(document.activeElement).toBe(
258
+ within(component.chipGroup("Form")).getByRole("button", { name: /Oral Suspension Reconstituted/ }),
259
+ );
260
+ fireEvent.keyDown(document.activeElement!, { key: "f" });
261
+ expect(document.activeElement).toBe(capsule);
262
+ fireEvent.keyDown(capsule, { key: "Escape" });
263
+ expect(document.activeElement).toBe(med);
264
+ });
265
+
266
+ it("clicking Treatment lists the drugs again without searching; Esc closes them, back to the chips", async () => {
267
+ const { component, med } = await pickAmoxicillinFromDrugListWithKeys();
268
+ const lookups = component.calls.filter((c) => c.name === "LookupPrescription").length;
269
+ fireEvent.click(med);
270
+ expect(screen.getByRole("button", { name: "Amoxapine Oral Tablet 25 MG" })).toBeTruthy();
271
+ expect(screen.queryByRole("group", { name: "Form" })).toBeNull();
272
+ fireEvent.keyDown(med, { key: "Escape" });
273
+ expect(component.openDrugList()).toBeNull();
274
+ expect(screen.getByRole("group", { name: "Form" })).toBeTruthy();
275
+ expect(component.calls.filter((c) => c.name === "LookupPrescription")).toHaveLength(lookups);
276
+ });
277
+
278
+ it("a form picked greys the strengths it doesn't come in; S moves along them and a click picks the product", async () => {
279
+ const { component } = await pickAmoxicillinFromDrugListWithKeys();
280
+ fireEvent.click(screen.getByRole("button", { name: /^Oral capsule$/ }));
281
+ expect(screen.getByRole("button", { name: /^Oral capsule$/, pressed: true })).toBeTruthy();
282
+ expect(component.greyedChipLabelsIn("Strength")).toEqual(["400 mg/5 mL"]);
283
+ expect(document.activeElement).toBe(screen.getByRole("button", { name: "Amoxicillin Oral Capsule 250 MG" }));
284
+ expect(within(component.chipGroup("Strength")).queryByRole("button", { pressed: true })).toBeNull();
285
+ fireEvent.keyDown(document.activeElement!, { key: "s" }); // a greyed chip is still a stop
286
+ fireEvent.keyDown(document.activeElement!, { key: "s" });
287
+ expect(document.activeElement).toBe(screen.getByRole("button", { name: "Amoxicillin Oral Capsule 500 MG" }));
288
+ expect(component.wasPrescriptionDrafted()).toBe(false); // no key so far has chosen
289
+ component.pressEnterOnFocusedButton();
290
+ expect(await component.waitForDraftedPrescription()).toMatchObject({ treatment: { id: "med_cap500" } });
291
+ });
292
+
293
+ it("a form with two strengths only narrows them; tapping it again takes it back", async () => {
294
+ const { component } = await pickAmoxicillinFromDrugListWithKeys();
295
+ fireEvent.click(screen.getByRole("button", { name: /^Oral capsule$/ }));
296
+ expect(screen.getByRole("button", { name: /^Oral capsule$/, pressed: true })).toBeTruthy();
297
+ fireEvent.click(screen.getByRole("button", { name: /^Oral capsule$/ }));
298
+ expect(screen.getByRole("button", { name: /^Oral capsule$/, pressed: false })).toBeTruthy();
299
+ expect(component.wasPrescriptionDrafted()).toBe(false);
300
+ });
301
+
302
+ it("a form with one strength: its chip chooses the product", async () => {
303
+ const { component } = await pickAmoxicillinFromDrugListWithKeys();
304
+ fireEvent.click(
305
+ within(component.chipGroup("Form")).getByRole("button", {
306
+ name: "Amoxicillin Oral Suspension Reconstituted 400 MG/5ML",
307
+ }),
308
+ );
309
+ expect(await component.waitForDraftedPrescription()).toMatchObject({ treatment: { id: "med_sus400" } });
310
+ });
311
+
312
+ it("a strength only one form comes in: its chip chooses the product", async () => {
313
+ const { component } = await pickAmoxicillinFromDrugListWithKeys();
314
+ fireEvent.click(
315
+ within(component.chipGroup("Strength")).getByRole("button", { name: "Amoxicillin Oral Capsule 500 MG" }),
316
+ );
317
+ expect(await component.waitForDraftedPrescription()).toMatchObject({ treatment: { id: "med_cap500" } });
318
+ });
319
+
320
+ it("another drug's row, after one was picked: a drug that's one product chooses it", async () => {
321
+ const { component, med } = await pickAmoxicillinFromDrugListWithKeys();
322
+ expect(screen.queryByRole("button", { name: "Amoxapine Oral Tablet 25 MG" })).toBeNull();
323
+ fireEvent.click(med);
324
+ fireEvent.click(screen.getByRole("button", { name: "Amoxapine Oral Tablet 25 MG" }));
325
+ expect(await component.waitForDraftedPrescription()).toMatchObject({ treatment: { id: "med_amoxapine" } });
326
+ });
327
+ });
328
+
329
+ describe("picking from the keyboard", () => {
330
+ it("Enter in Treatment picks the first drug, and focus goes to its first chip", async () => {
331
+ const component = renderComponent({ lookupPrescriptions: amoxicillinCapsulesAndAmoxicillinClavulanate });
332
+ const med = await component.typeIntoTreatment("amoxicillin");
333
+ await screen.findByRole("list", { name: "Pick a treatment" });
334
+ fireEvent.keyDown(med, { key: "Enter" });
335
+ await pressEnterOnFocusedFirstStrengthChip(component);
336
+ expect(await component.waitForDraftedPrescription()).toMatchObject({ treatment: { id: "med_cap250" } });
337
+ });
338
+
339
+ it("Option + 1 in Treatment picks the first drug (the digit isn't typed), and focus goes to its first chip", async () => {
340
+ const component = renderComponent({ lookupPrescriptions: amoxicillinCapsulesAndAmoxicillinClavulanate });
341
+ const med = await component.typeIntoTreatment("amoxicillin");
342
+ const rows = await screen.findByRole("list", { name: "Pick a treatment" });
343
+ med.focus();
344
+ // Nothing highlighted until focus is in the list (the list may have had it: it goes on the next render).
345
+ await waitFor(() => expect(rows.querySelector("[data-selected]")).toBeNull());
346
+ fireEvent.keyDown(med, { key: "¡", code: "Digit1", altKey: true });
347
+ expect(med.value).toBe("Amoxicillin");
348
+ await pressEnterOnFocusedFirstStrengthChip(component);
349
+ expect(await component.waitForDraftedPrescription()).toMatchObject({ treatment: { id: "med_cap250" } });
350
+ });
351
+
352
+ it("a tap on a drug's row picks it, and focus goes to its first chip", async () => {
353
+ const component = renderComponent({ lookupPrescriptions: amoxicillinCapsulesAndAmoxicillinClavulanate });
354
+ await component.typeIntoTreatment("amoxicillin");
355
+ await screen.findByRole("list", { name: "Pick a treatment" });
356
+ fireEvent.click(screen.getByRole("button", { name: /^Amoxicillin\s*Oral capsule · 2 strengths$/ }));
357
+ await pressEnterOnFocusedFirstStrengthChip(component);
358
+ expect(await component.waitForDraftedPrescription()).toMatchObject({ treatment: { id: "med_cap250" } });
359
+ });
360
+
361
+ it("a digit in the list on a drug that's one product chooses it", async () => {
362
+ const component = renderComponent({ lookupPrescriptions: amoxicillinCapsulesAndAmoxicillinClavulanate });
363
+ const med = await component.typeIntoTreatment("amoxicillin");
364
+ await screen.findByRole("list", { name: "Pick a treatment" });
365
+ fireEvent.keyDown(med, { key: "ArrowDown" });
366
+ fireEvent.keyDown(document.activeElement!, { key: "2" });
367
+ expect(await component.waitForDraftedPrescription()).toMatchObject({ treatment: { id: "med_clav" } });
368
+ });
369
+
370
+ it("a drug picked from the list: its chips are ordinary tab stops, and Esc goes back to Treatment", async () => {
371
+ const component = renderComponent({ lookupPrescriptions: amoxicillinCapsulesAndAmoxicillinClavulanate });
372
+ await component.typeIntoTreatment("amoxicillin");
373
+ const picker = (await screen.findByRole("list", { name: "Pick a treatment" })).parentElement!;
374
+ picker.focus();
375
+ fireEvent.keyDown(picker, { key: "Enter" });
376
+ expect(component.openDrugList()).toBeNull();
377
+ const chips = within(component.chipGroup("Strength")).getAllByRole("button");
378
+ expect(document.activeElement).toBe(chips[0]);
379
+ expect(chips.map((b) => b.tabIndex)).toEqual([0, 0]);
380
+ fireEvent.keyDown(document.activeElement!, { key: "Escape" });
381
+ expect(document.activeElement).toBe(screen.getByLabelText("Treatment"));
382
+ });
383
+ });
384
+
385
+ describe("picking a form and a strength", () => {
386
+ it("both show from the start, none pressed", async () => {
387
+ const component = await pickLisinoprilInTwoForms();
388
+ expect(component.chipLabelsIn("Form")).toEqual(["Oral solution", "Oral tablet"]);
389
+ expect(component.chipLabelsIn("Strength")).toEqual(["5 mg", "10 mg", "20 mg"]);
390
+ expect(screen.queryByRole("button", { pressed: true })).toBeNull();
391
+ });
392
+
393
+ it("the form first: it greys the strengths it doesn't come in, then the strength settles the product", async () => {
394
+ const component = await pickLisinoprilInTwoForms();
395
+ fireEvent.click(screen.getByRole("button", { name: "Oral solution" }));
396
+ expect(screen.getByRole("button", { name: "Oral solution", pressed: true })).toBeTruthy();
397
+ expect(component.chipLabelsIn("Strength")).toHaveLength(3); // all still there
398
+ expect(component.greyedChipLabelsIn("Strength")).toEqual(["20 mg"]);
399
+ expect(document.activeElement).toBe(within(component.chipGroup("Strength")).getAllByRole("button")[0]);
400
+ expect(component.wasPrescriptionDrafted()).toBe(false);
401
+ fireEvent.click(screen.getByRole("button", { name: "Lisinopril Oral Solution 10 MG" }));
402
+ expect(await component.waitForDraftedPrescription()).toMatchObject({ treatment: { id: "med_sol10" } });
403
+ });
404
+
405
+ it("the strength first: both forms come in 10 mg, so none is greyed, then the form settles the product", async () => {
406
+ const component = await pickLisinoprilInTwoForms();
407
+ fireEvent.click(screen.getByRole("button", { name: "10 mg" }));
408
+ expect(screen.getByRole("button", { name: "10 mg", pressed: true })).toBeTruthy();
409
+ expect(component.greyedChipLabelsIn("Form")).toEqual([]);
410
+ expect(document.activeElement).toBe(within(component.chipGroup("Form")).getAllByRole("button")[0]);
411
+ expect(component.wasPrescriptionDrafted()).toBe(false);
412
+ fireEvent.click(screen.getByRole("button", { name: "Lisinopril Oral Solution 10 MG" }));
413
+ expect(await component.waitForDraftedPrescription()).toMatchObject({ treatment: { id: "med_sol10" } });
414
+ });
415
+
416
+ it("a greyed chip can still be picked: it drops the other pick, and the prescriber confirms the other", async () => {
417
+ const component = await pickLisinoprilInTwoForms();
418
+ fireEvent.click(within(component.chipGroup("Form")).getByRole("button", { name: "Oral solution" }));
419
+ const twenty = within(component.chipGroup("Strength")).getByRole("button", { name: "20 mg" });
420
+ expect(twenty.dataset.muted).toBeDefined(); // no 20 mg solution
421
+ // Picked anyway: the solution goes — and though only the tablet comes in 20 mg, it isn't chosen for them.
422
+ fireEvent.click(twenty);
423
+ expect(within(component.chipGroup("Strength")).getByRole("button", { pressed: true }).textContent).toBe("20 mg");
424
+ expect(within(component.chipGroup("Form")).queryByRole("button", { pressed: true })).toBeNull();
425
+ expect(component.greyedChipLabelsIn("Form")).toEqual(["Oral solution"]);
426
+ expect(document.activeElement).toBe(
427
+ within(component.chipGroup("Form")).getByRole("button", { name: "Lisinopril Oral Tablet 20 MG" }),
428
+ );
429
+ await new Promise((r) => setTimeout(r, 0));
430
+ expect(component.wasPrescriptionDrafted()).toBe(false);
431
+ component.pressEnterOnFocusedButton(); // the prescriber confirms the form
432
+ expect(await component.waitForDraftedPrescription()).toMatchObject({ treatment: { id: "med_tab20" } });
433
+ });
434
+ });
435
+
436
+ describe("selecting a product", () => {
437
+ it("heads the card at once, a spinner where its directions go, until it saves", async () => {
438
+ let release: () => void = () => {};
439
+ const released = new Promise<void>((resolve) => (release = resolve));
440
+ const component = renderComponent({
441
+ lookupPrescriptions: lisinoprilTablets10And20Mg,
442
+ script: {
443
+ SyncPrescription: () =>
444
+ released.then(() => payload("prescription", "PrescriptionPayload", rx({ id: "rx_lis" }))),
445
+ },
446
+ });
447
+ await component.typeIntoTreatment("lisinopril");
448
+ await pickDrug("Lisinopril Oral Tablet 10 MG");
449
+
450
+ // Saving: the product heads the card, a spinner (no words) in its body; the search and the directions wait.
451
+ expect(screen.getByRole("heading", { name: "Lisinopril" })).toBeTruthy();
452
+ expect(screen.getByRole("status").textContent).toBe("");
453
+ expect(screen.queryByLabelText("Treatment")).toBeNull();
454
+ expect(screen.queryByRole("group", { name: "Strength" })).toBeNull();
455
+ expect(screen.queryByLabelText("Directions (sig)")).toBeNull();
456
+ await waitFor(() => expect(component.names()).toContain("SyncPrescription")); // sent, not answered
457
+ release();
458
+ expect(await screen.findByLabelText("Directions (sig)")).toBeTruthy();
459
+ expect(screen.queryByRole("status")).toBeNull();
460
+ expect(component.openDrugList()).toBeNull();
461
+ });
462
+
463
+ it("heads the card by its name, under it its form and strength, with its directions in place of Treatment", async () => {
464
+ const component = renderComponent({
465
+ lookupPrescriptions: lisinoprilTablets10And20Mg,
466
+ script: {
467
+ SyncPrescription: () =>
468
+ payload(
469
+ "prescription",
470
+ "PrescriptionPayload",
471
+ // network-api returns only the ingredient as the name; the pick's name heads the card.
472
+ rx({ id: "rx_lis", treatment: { ...rx().treatment, id: "med_10", name: "Lisinopril", ndc: null } }),
473
+ ),
474
+ },
475
+ });
476
+ await component.typeIntoTreatment("lisinopril");
477
+ await pickDrug("Lisinopril Oral Tablet 10 MG");
478
+
479
+ expect(await screen.findByRole("heading", { name: "Lisinopril" })).toBeTruthy();
480
+ expect(screen.getByText("Oral tablet · 10 mg")).toBeTruthy();
481
+ await waitFor(() => expect(screen.queryByLabelText("Treatment")).toBeNull());
482
+ expect(screen.getByLabelText("Directions (sig)")).toBeTruthy();
483
+ });
484
+ });
485
+
486
+ describe("clearing the treatment", () => {
487
+ it("Clear treatment empties the field: no drugs listed, no form to pick", async () => {
488
+ const { component, med } = await pickAmoxicillinFromFakeNetworkApi();
489
+ expect(med.value).toBe("Amoxicillin");
490
+ fireEvent.click(await screen.findByRole("button", { name: "Clear treatment" }));
491
+ expect(med.value).toBe("");
492
+ expect(component.openDrugList()).toBeNull();
493
+ expect(screen.queryByRole("group", { name: "Form" })).toBeNull();
494
+ });
495
+
496
+ it("a different drug can be searched for before the product is settled", async () => {
497
+ const { component, med } = await pickAmoxicillinFromFakeNetworkApi();
498
+ fireEvent.click(component.chipByLabel("Form", "Capsule"));
499
+ fireEvent.change(med, { target: { value: "botox" } });
500
+ await waitFor(() => expect(within(component.openDrugList()!).getByText("Botox")).toBeTruthy(), { timeout: 2000 });
501
+ expect(within(component.openDrugList()!).queryByText("Amoxicillin")).toBeNull();
502
+ });
503
+
504
+ it("once the product is settled, the search goes: the server's directions, and no way to change the drug", async () => {
505
+ const { component } = await pickAmoxicillinFromFakeNetworkApi();
506
+ await pickCapsule500Mg(component);
507
+ expect((await component.findDirectionsField()).value).toBe("Take as directed");
508
+ expect((screen.getByLabelText("Quantity") as HTMLInputElement).value).toBe("30");
509
+ expect(screen.queryByLabelText("Treatment")).toBeNull();
510
+ expect(screen.queryByRole("button", { name: "Change treatment" })).toBeNull();
511
+ });
512
+ });
513
+ });
514
+
515
+ type NetworkScript = Parameters<typeof fakeNetworkApiClient>[0];
516
+
517
+ type RenderComponentOptions = {
518
+ photonPrescriptionProps?: PhotonPrescriptionProps;
519
+ lookupPrescriptions?: unknown;
520
+ script?: NetworkScript;
521
+ };
522
+
523
+ type RenderedComponent = ReturnType<typeof renderComponent>;
524
+
525
+ function renderComponent({
526
+ photonPrescriptionProps = { patient: "pat_1" },
527
+ lookupPrescriptions = null,
528
+ script = {},
529
+ }: RenderComponentOptions = {}) {
530
+ const { client, calls, names } = fakeNetworkApiClient({
531
+ GetPatient: () => payload("patient", "PatientPayload", patient),
532
+ LookupPrescription: () => lookupPrescriptions,
533
+ SyncPrescription: () => payload("prescription", "PrescriptionPayload", rx({ id: "rx_1" })),
534
+ ...script,
535
+ });
536
+ render(<PhotonPrescription {...photonPrescriptionProps} />, { wrapper: withPhoton(client) });
537
+
538
+ const findTreatmentField = () => screen.findByLabelText("Treatment") as Promise<HTMLInputElement>;
539
+ const openDrugList = () => screen.queryByRole("list", { name: "Pick a treatment" });
540
+ const chipGroup = (name: string) => screen.getByRole("group", { name });
541
+ const chipsIn = (name: string) => within(chipGroup(name)).getAllByRole("button");
542
+ const waitForDrugListToTakeFocus = async () => {
543
+ const picker = (await screen.findByRole("list", { name: "Pick a treatment" })).parentElement!;
544
+ await waitFor(() => expect(picker.querySelector('[aria-current="true"]')).not.toBeNull());
545
+ expect(document.activeElement).toBe(picker);
546
+ return picker;
547
+ };
548
+ return {
549
+ calls,
550
+ names,
551
+ findTreatmentField,
552
+ typeIntoTreatment: async (text: string) => {
553
+ const med = await findTreatmentField();
554
+ med.focus();
555
+ fireEvent.change(med, { target: { value: text } });
556
+ return med;
557
+ },
558
+ wasPrescriptionDrafted: () => names().includes("SyncPrescription"),
559
+ waitForDraftedPrescription: async () => {
560
+ await waitFor(() => expect(names()).toContain("SyncPrescription"));
561
+ return calls.find((c) => c.name === "SyncPrescription")!.vars;
562
+ },
563
+ openDrugList,
564
+ highlightedDrugRowText: () =>
565
+ openDrugList()?.parentElement?.querySelector('[aria-current="true"]')?.textContent ?? "",
566
+ waitForDrugListToTakeFocus,
567
+ enterDrugListWithArrowDown: async (med: HTMLInputElement) => {
568
+ const picker = await waitForDrugListToTakeFocus();
569
+ med.focus();
570
+ fireEvent.keyDown(med, { key: "ArrowDown" });
571
+ expect(document.activeElement).toBe(picker);
572
+ return picker;
573
+ },
574
+ chipGroup,
575
+ chipLabelsIn: (name: string) => chipsIn(name).map((b) => b.textContent),
576
+ greyedChipLabelsIn: (name: string) =>
577
+ chipsIn(name)
578
+ .filter((b) => b.dataset.muted !== undefined)
579
+ .map((b) => b.textContent),
580
+ chipByLabel: (name: string, label: string) =>
581
+ chipsIn(name).find((b) => b.textContent === label)! as HTMLButtonElement,
582
+ findDirectionsField: () => screen.findByLabelText("Directions (sig)") as Promise<HTMLTextAreaElement>,
583
+ pressEnterOnFocusedButton: () => fireEvent.click(document.activeElement!),
584
+ };
585
+ }
586
+
587
+ async function pickAmoxicillinFromDrugListWithKeys() {
588
+ const component = renderComponent({ lookupPrescriptions: amoxicillinInTwoFormsAndAmoxapine });
589
+ const med = await component.typeIntoTreatment("amox");
590
+ const picker = await component.enterDrugListWithArrowDown(med);
591
+ fireEvent.keyDown(picker, { key: "j" });
592
+ fireEvent.keyDown(picker, { key: "Enter" });
593
+ return { component, med };
594
+ }
595
+
596
+ async function pickLisinoprilInTwoForms() {
597
+ const component = renderComponent({ lookupPrescriptions: lisinoprilAsTabletsAndSolutions });
598
+ await component.typeIntoTreatment("lisinopril");
599
+ await tapDrug(/^Lisinopril/);
600
+ await screen.findByRole("group", { name: "Form" });
601
+ return component;
602
+ }
603
+
604
+ async function pickAmoxicillinFromFakeNetworkApi() {
605
+ const component = renderComponent({
606
+ script: fakeNetworkApi({ prescriptions: amoxicillinAmoxapineAndBotoxSearches }),
607
+ });
608
+ const med = await component.typeIntoTreatment("amox");
609
+ await tapDrug(/^Amoxicillin/);
610
+ return { component, med };
611
+ }
612
+
613
+ async function pickCapsule500Mg(component: RenderedComponent) {
614
+ fireEvent.click(component.chipByLabel("Form", "Capsule"));
615
+ fireEvent.click(component.chipByLabel("Strength", "500 mg"));
616
+ await component.findDirectionsField();
617
+ }
618
+
619
+ async function pressEnterOnFocusedFirstStrengthChip(component: RenderedComponent) {
620
+ const first = await screen.findByRole("button", { name: "Amoxicillin Oral Capsule 250 MG" });
621
+ await waitFor(() => expect(document.activeElement).toBe(first));
622
+ component.pressEnterOnFocusedButton();
623
+ }
624
+
625
+ async function enterDrugListFromTreatment(
626
+ user: ReturnType<typeof userEvent.setup>,
627
+ med: HTMLInputElement,
628
+ key: "{Tab}" | "{ArrowDown}",
629
+ ) {
630
+ await user.click(med);
631
+ await user.keyboard("amox");
632
+ await screen.findByRole("list", { name: "Pick a treatment" });
633
+ await waitFor(() => expect(document.activeElement).not.toBe(med));
634
+ await user.click(med);
635
+ await user.keyboard(key);
636
+ expect(document.activeElement).not.toBe(med);
637
+ }
638
+
639
+ describe("TreatmentSearch: one click on a drug picks it", () => {
640
+ const typeAndClickTheFirstDrug = async (lookupPrescriptions: ReturnType<typeof choices>, typed: string) => {
641
+ const user = userEvent.setup();
642
+ const component = renderComponent({ lookupPrescriptions });
643
+ await user.click(await component.findTreatmentField());
644
+ await user.keyboard(typed);
645
+ const list = await screen.findByRole("list", { name: "Pick a treatment" });
646
+ const [firstDrug] = within(list).getAllByRole("button");
647
+ await waitFor(() => expect((firstDrug as HTMLButtonElement).disabled).toBe(false));
648
+ return { component, user, firstDrug: firstDrug! };
649
+ };
650
+
651
+ it("the exact name, among a few drugs: the list takes focus, and clicking the highlighted first drug picks it", async () => {
652
+ const { component, user } = await typeAndClickTheFirstDrug(amoxicillinInTwoFormsAndAmoxapine, "amoxicillin");
653
+ await component.waitForDrugListToTakeFocus();
654
+ expect(component.highlightedDrugRowText()).toMatch(/^Amoxicillin/);
655
+
656
+ await user.click(within(component.openDrugList()!).getAllByRole("button")[0]!);
657
+
658
+ expect(await screen.findByRole("group", { name: "Form" })).toBeTruthy();
659
+ });
660
+
661
+ it("part of a name, among a few drugs: the list takes focus, and clicking the highlighted first drug picks it", async () => {
662
+ const { component, user } = await typeAndClickTheFirstDrug(lisinoprilAsTabletsAndSolutions, "lisino");
663
+ await component.waitForDrugListToTakeFocus();
664
+ expect(component.highlightedDrugRowText()).toMatch(/^Lisinopril/);
665
+
666
+ await user.click(within(component.openDrugList()!).getAllByRole("button")[0]!);
667
+
668
+ expect(await screen.findByRole("group", { name: "Form" })).toBeTruthy();
669
+ });
670
+
671
+ it("the exact name, among five drugs or more: the list doesn't take focus, and clicking the first drug picks it", async () => {
672
+ const { component, user, firstDrug } = await typeAndClickTheFirstDrug(amoxicillinAmongFiveDrugs, "amoxicillin");
673
+ expect(component.highlightedDrugRowText()).toBe("");
674
+ expect(firstDrug.textContent).toMatch(/^Amoxicillin/);
675
+
676
+ await user.click(firstDrug);
677
+
678
+ expect(await screen.findByRole("group", { name: "Form" })).toBeTruthy();
679
+ });
680
+
681
+ it("where pressing a drug doesn't focus it (Safari, Firefox on macOS), the click on the highlighted first drug still picks it", async () => {
682
+ const { component } = await typeAndClickTheFirstDrug(lisinoprilAsTabletsAndSolutions, "lisino");
683
+ await component.waitForDrugListToTakeFocus();
684
+ const pressed = within(component.openDrugList()!).getAllByRole("button")[0]!;
685
+
686
+ fireEvent.pointerDown(pressed);
687
+ fireEvent.mouseDown(pressed);
688
+ act(() => (document.activeElement as HTMLElement).blur());
689
+ fireEvent.pointerUp(pressed);
690
+ fireEvent.mouseUp(pressed);
691
+ fireEvent.click(pressed);
692
+
693
+ expect(await screen.findByRole("group", { name: "Form" })).toBeTruthy();
694
+ });
695
+ });