@openmrs/esm-billing-app 1.0.2-pre.98 → 1.0.2-pre.982

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 (214) hide show
  1. package/.eslintrc +16 -2
  2. package/README.md +54 -9
  3. package/__mocks__/bills.mock.ts +12 -0
  4. package/__mocks__/react-i18next.js +6 -5
  5. package/dist/1119.js +1 -1
  6. package/dist/1146.js +1 -2
  7. package/dist/1146.js.map +1 -1
  8. package/dist/1197.js +1 -1
  9. package/dist/1537.js +1 -0
  10. package/dist/1537.js.map +1 -0
  11. package/dist/1856.js +1 -0
  12. package/dist/1856.js.map +1 -0
  13. package/dist/2146.js +1 -1
  14. package/dist/2524.js +1 -0
  15. package/dist/2524.js.map +1 -0
  16. package/dist/2690.js +1 -1
  17. package/dist/3099.js +1 -1
  18. package/dist/3584.js +1 -1
  19. package/dist/3717.js +2 -0
  20. package/dist/3717.js.map +1 -0
  21. package/dist/4055.js +1 -1
  22. package/dist/4132.js +1 -1
  23. package/dist/4300.js +1 -1
  24. package/dist/4335.js +1 -1
  25. package/dist/4618.js +1 -1
  26. package/dist/4652.js +1 -1
  27. package/dist/4724.js +1 -0
  28. package/dist/4724.js.map +1 -0
  29. package/dist/4739.js +1 -1
  30. package/dist/4739.js.map +1 -1
  31. package/dist/4944.js +1 -1
  32. package/dist/5173.js +1 -1
  33. package/dist/5241.js +1 -1
  34. package/dist/5442.js +1 -1
  35. package/dist/5661.js +1 -1
  36. package/dist/6022.js +1 -1
  37. package/dist/6468.js +1 -1
  38. package/dist/6540.js +1 -1
  39. package/dist/6540.js.map +1 -1
  40. package/dist/6679.js +1 -1
  41. package/dist/6840.js +1 -1
  42. package/dist/6859.js +1 -1
  43. package/dist/7097.js +1 -1
  44. package/dist/7159.js +1 -1
  45. package/dist/723.js +1 -1
  46. package/dist/7255.js +1 -1
  47. package/dist/7255.js.map +1 -1
  48. package/dist/7617.js +1 -1
  49. package/dist/795.js +1 -1
  50. package/dist/8163.js +1 -1
  51. package/dist/8349.js +1 -1
  52. package/dist/8572.js +1 -0
  53. package/dist/8572.js.map +1 -0
  54. package/dist/8618.js +1 -1
  55. package/dist/8708.js +2 -0
  56. package/dist/{6557.js.LICENSE.txt → 8708.js.LICENSE.txt} +22 -0
  57. package/dist/8708.js.map +1 -0
  58. package/dist/890.js +1 -1
  59. package/dist/9214.js +1 -1
  60. package/dist/9538.js +1 -1
  61. package/dist/9569.js +1 -1
  62. package/dist/961.js +1 -1
  63. package/dist/961.js.map +1 -1
  64. package/dist/986.js +1 -1
  65. package/dist/9879.js +1 -1
  66. package/dist/9895.js +1 -1
  67. package/dist/9900.js +1 -1
  68. package/dist/9913.js +1 -1
  69. package/dist/main.js +1 -1
  70. package/dist/main.js.map +1 -1
  71. package/dist/openmrs-esm-billing-app.js +1 -1
  72. package/dist/openmrs-esm-billing-app.js.buildmanifest.json +271 -285
  73. package/dist/openmrs-esm-billing-app.js.map +1 -1
  74. package/dist/routes.json +1 -1
  75. package/e2e/README.md +19 -18
  76. package/e2e/core/test.ts +1 -1
  77. package/e2e/fixtures/api.ts +1 -1
  78. package/e2e/specs/sample-test.spec.ts +0 -1
  79. package/e2e/support/github/Dockerfile +1 -1
  80. package/package.json +18 -15
  81. package/src/bill-history/bill-history.component.tsx +20 -28
  82. package/src/bill-history/bill-history.scss +4 -94
  83. package/src/bill-history/bill-history.test.tsx +37 -78
  84. package/src/bill-item-actions/bill-item-actions.scss +21 -5
  85. package/src/bill-item-actions/edit-bill-item.modal.tsx +226 -0
  86. package/src/bill-item-actions/edit-bill-item.test.tsx +233 -40
  87. package/src/billable-services/bill-waiver/bill-selection.component.tsx +5 -5
  88. package/src/billable-services/bill-waiver/bill-waiver-form.component.tsx +34 -37
  89. package/src/billable-services/bill-waiver/patient-bills.component.tsx +7 -7
  90. package/src/billable-services/bill-waiver/utils.ts +13 -3
  91. package/src/billable-services/{create-edit/add-billable-service.scss → billable-service-form/billable-service-form.scss} +32 -64
  92. package/src/billable-services/billable-service-form/billable-service-form.test.tsx +1048 -0
  93. package/src/billable-services/billable-service-form/billable-service-form.workspace.tsx +515 -0
  94. package/src/billable-services/billable-service.resource.ts +71 -27
  95. package/src/billable-services/billable-services-home.component.tsx +13 -42
  96. package/src/billable-services/billable-services-left-panel-link.component.tsx +48 -0
  97. package/src/billable-services/billable-services-left-panel-menu.component.tsx +46 -0
  98. package/src/billable-services/billable-services-menu-item/item.component.tsx +5 -4
  99. package/src/billable-services/billable-services.component.tsx +156 -152
  100. package/src/billable-services/billable-services.scss +29 -0
  101. package/src/billable-services/billable-services.test.tsx +6 -49
  102. package/src/billable-services/cash-point/add-cash-point.modal.tsx +170 -0
  103. package/src/billable-services/cash-point/cash-point-configuration.component.tsx +19 -193
  104. package/src/billable-services/cash-point/cash-point-configuration.scss +1 -5
  105. package/src/billable-services/dashboard/dashboard.component.tsx +0 -2
  106. package/src/billable-services/payment-modes/delete-payment-mode.modal.tsx +77 -0
  107. package/src/billable-services/payment-modes/payment-mode-form.modal.tsx +131 -0
  108. package/src/billable-services/payment-modes/payment-modes-config.component.tsx +139 -0
  109. package/src/billable-services/{payyment-modes → payment-modes}/payment-modes-config.scss +5 -4
  110. package/src/billable-services-admin-card-link.component.test.tsx +2 -2
  111. package/src/billable-services-admin-card-link.component.tsx +1 -1
  112. package/src/billing-dashboard/billing-dashboard.scss +1 -1
  113. package/src/billing-form/billing-checkin-form.component.tsx +21 -17
  114. package/src/billing-form/billing-checkin-form.test.tsx +99 -26
  115. package/src/billing-form/billing-form.component.tsx +226 -289
  116. package/src/billing-form/billing-form.scss +143 -0
  117. package/src/billing-form/visit-attributes/visit-attributes-form.component.tsx +1 -1
  118. package/src/billing.resource.ts +69 -74
  119. package/src/bills-table/bills-table.component.tsx +3 -3
  120. package/src/bills-table/bills-table.test.tsx +98 -54
  121. package/src/config-schema.ts +52 -24
  122. package/src/dashboard.meta.ts +4 -2
  123. package/src/helpers/functions.ts +5 -4
  124. package/src/index.ts +71 -9
  125. package/src/invoice/invoice-table.component.tsx +36 -70
  126. package/src/invoice/invoice-table.scss +8 -5
  127. package/src/invoice/invoice-table.test.tsx +273 -62
  128. package/src/invoice/invoice.component.tsx +39 -33
  129. package/src/invoice/invoice.scss +11 -4
  130. package/src/invoice/invoice.test.tsx +324 -120
  131. package/src/invoice/payments/invoice-breakdown/invoice-breakdown.scss +9 -9
  132. package/src/invoice/payments/payment-form/payment-form.component.tsx +43 -34
  133. package/src/invoice/payments/payment-form/payment-form.scss +5 -6
  134. package/src/invoice/payments/payment-form/payment-form.test.tsx +216 -66
  135. package/src/invoice/payments/payment-history/payment-history.component.tsx +6 -4
  136. package/src/invoice/payments/payment-history/payment-history.test.tsx +9 -14
  137. package/src/invoice/payments/payments.component.tsx +55 -67
  138. package/src/invoice/payments/payments.scss +4 -3
  139. package/src/invoice/payments/payments.test.tsx +282 -0
  140. package/src/invoice/payments/utils.ts +15 -27
  141. package/src/invoice/printable-invoice/print-receipt.component.tsx +3 -3
  142. package/src/invoice/printable-invoice/print-receipt.test.tsx +14 -25
  143. package/src/invoice/printable-invoice/printable-footer.component.tsx +2 -2
  144. package/src/invoice/printable-invoice/printable-footer.test.tsx +4 -13
  145. package/src/invoice/printable-invoice/printable-invoice-header.component.tsx +20 -11
  146. package/src/invoice/printable-invoice/printable-invoice-header.test.tsx +95 -16
  147. package/src/invoice/printable-invoice/printable-invoice.component.tsx +21 -35
  148. package/src/left-panel-link.test.tsx +1 -4
  149. package/src/metrics-cards/metrics-cards.component.tsx +16 -6
  150. package/src/metrics-cards/metrics-cards.scss +4 -0
  151. package/src/metrics-cards/metrics-cards.test.tsx +18 -5
  152. package/src/modal/require-payment-modal.test.tsx +27 -22
  153. package/src/modal/{require-payment-modal.component.tsx → require-payment.modal.tsx} +18 -19
  154. package/src/routes.json +44 -20
  155. package/src/types/index.ts +87 -24
  156. package/translations/am.json +135 -78
  157. package/translations/ar.json +136 -79
  158. package/translations/ar_SY.json +136 -79
  159. package/translations/bn.json +138 -81
  160. package/translations/de.json +136 -79
  161. package/translations/en.json +136 -79
  162. package/translations/en_US.json +136 -79
  163. package/translations/es.json +135 -78
  164. package/translations/es_MX.json +136 -79
  165. package/translations/fr.json +141 -84
  166. package/translations/he.json +135 -78
  167. package/translations/hi.json +136 -79
  168. package/translations/hi_IN.json +136 -79
  169. package/translations/id.json +136 -79
  170. package/translations/it.json +162 -105
  171. package/translations/ka.json +136 -79
  172. package/translations/km.json +135 -78
  173. package/translations/ku.json +136 -79
  174. package/translations/ky.json +136 -79
  175. package/translations/lg.json +136 -79
  176. package/translations/ne.json +136 -79
  177. package/translations/pl.json +136 -79
  178. package/translations/pt.json +136 -79
  179. package/translations/pt_BR.json +136 -79
  180. package/translations/qu.json +136 -79
  181. package/translations/ro_RO.json +222 -165
  182. package/translations/ru_RU.json +136 -79
  183. package/translations/si.json +136 -79
  184. package/translations/sw.json +136 -79
  185. package/translations/sw_KE.json +136 -79
  186. package/translations/tr.json +136 -79
  187. package/translations/tr_TR.json +136 -79
  188. package/translations/uk.json +136 -79
  189. package/translations/uz.json +136 -79
  190. package/translations/uz@Latn.json +136 -79
  191. package/translations/uz_UZ.json +136 -79
  192. package/translations/vi.json +136 -79
  193. package/translations/zh.json +136 -79
  194. package/translations/zh_CN.json +166 -109
  195. package/dist/1146.js.LICENSE.txt +0 -21
  196. package/dist/2352.js +0 -1
  197. package/dist/2352.js.map +0 -1
  198. package/dist/246.js +0 -1
  199. package/dist/246.js.map +0 -1
  200. package/dist/4689.js +0 -2
  201. package/dist/4689.js.map +0 -1
  202. package/dist/6557.js +0 -2
  203. package/dist/6557.js.map +0 -1
  204. package/dist/8638.js +0 -1
  205. package/dist/8638.js.map +0 -1
  206. package/dist/9968.js +0 -1
  207. package/dist/9968.js.map +0 -1
  208. package/src/bill-item-actions/edit-bill-item.component.tsx +0 -221
  209. package/src/billable-services/create-edit/add-billable-service.component.tsx +0 -401
  210. package/src/billable-services/create-edit/add-billable-service.test.tsx +0 -154
  211. package/src/billable-services/dashboard/service-metrics.component.tsx +0 -41
  212. package/src/billable-services/payyment-modes/payment-modes-config.component.tsx +0 -280
  213. package/src/invoice/payments/payments.component.test.tsx +0 -121
  214. /package/dist/{4689.js.LICENSE.txt → 3717.js.LICENSE.txt} +0 -0
@@ -0,0 +1,1048 @@
1
+ import React from 'react';
2
+ import userEvent from '@testing-library/user-event';
3
+ import { render, screen, fireEvent } from '@testing-library/react';
4
+ import { type FetchResponse } from '@openmrs/esm-framework';
5
+ import {
6
+ createBillableService,
7
+ updateBillableService,
8
+ useBillableServices,
9
+ useConceptsSearch,
10
+ usePaymentModes,
11
+ useServiceTypes,
12
+ } from '../billable-service.resource';
13
+ import BillableServiceFormWorkspace, {
14
+ transformServiceToFormData,
15
+ normalizePrice,
16
+ getAvailablePaymentModes,
17
+ } from './billable-service-form.workspace';
18
+ import type { BillableService } from '../../types';
19
+
20
+ const mockUseBillableServices = jest.mocked(useBillableServices);
21
+ const mockUsePaymentModes = jest.mocked(usePaymentModes);
22
+ const mockUseServiceTypes = jest.mocked(useServiceTypes);
23
+ const mockCreateBillableService = jest.mocked(createBillableService);
24
+ const mockUpdateBillableService = jest.mocked(updateBillableService);
25
+ const mockUseConceptsSearch = jest.mocked(useConceptsSearch);
26
+
27
+ jest.mock('../billable-service.resource', () => ({
28
+ useBillableServices: jest.fn(),
29
+ usePaymentModes: jest.fn(),
30
+ useServiceTypes: jest.fn(),
31
+ createBillableService: jest.fn(),
32
+ updateBillableService: jest.fn(),
33
+ useConceptsSearch: jest.fn(),
34
+ }));
35
+
36
+ const mockPaymentModes = [
37
+ { uuid: '63eff7a4-6f82-43c4-a333-dbcc58fe9f74', name: 'Cash', description: 'Cash Payment', retired: false },
38
+ {
39
+ uuid: 'beac329b-f1dc-4a33-9e7c-d95821a137a6',
40
+ name: 'Insurance',
41
+ description: 'Insurance method of payment',
42
+ retired: false,
43
+ },
44
+ {
45
+ uuid: '28989582-e8c3-46b0-96d0-c249cb06d5c6',
46
+ name: 'MPESA',
47
+ description: 'Mobile money method of payment',
48
+ retired: false,
49
+ },
50
+ ];
51
+
52
+ const mockServiceTypes = [
53
+ { uuid: 'c9604249-db0a-4d03-b074-fc6bc2fa13e6', display: 'Lab service' },
54
+ { uuid: 'b75e466f-a6f5-4d5e-849a-84424d3c85cd', display: 'Pharmacy service' },
55
+ { uuid: 'ce914b2d-44f6-4b6c-933f-c57a3938e35b', display: 'Peer educator service' },
56
+ { uuid: 'c23d3224-2218-4007-8f22-e1f3d5a8e58a', display: 'Nutrition service' },
57
+ { uuid: '65487ff4-63b3-452a-8985-6a1f4a0cc08d', display: 'TB service' },
58
+ { uuid: '9db142d5-5cc4-4c05-9f83-06ed294caa67', display: 'Family planning service' },
59
+ { uuid: 'a487a743-62ce-4f93-a66b-c5154ee8987d', display: 'Adherence counselling service' },
60
+ ];
61
+
62
+ // Test helpers
63
+ const setupMocks = () => {
64
+ mockUseBillableServices.mockReturnValue({
65
+ billableServices: [],
66
+ isLoading: false,
67
+ error: null,
68
+ mutate: jest.fn(),
69
+ isValidating: false,
70
+ });
71
+ mockUsePaymentModes.mockReturnValue({
72
+ paymentModes: mockPaymentModes,
73
+ error: null,
74
+ isLoadingPaymentModes: false,
75
+ mutate: jest.fn(),
76
+ });
77
+ mockUseServiceTypes.mockReturnValue({ serviceTypes: mockServiceTypes, error: false, isLoadingServiceTypes: false });
78
+ mockUseConceptsSearch.mockReturnValue({ searchResults: [], isSearching: false, error: null });
79
+ };
80
+
81
+ const renderBillableServicesForm = (props = {}) => {
82
+ const defaultProps = {
83
+ closeWorkspace: jest.fn(),
84
+ closeWorkspaceWithSavedChanges: jest.fn(),
85
+ ...props,
86
+ };
87
+ setupMocks();
88
+ return render(<BillableServiceFormWorkspace {...defaultProps} />);
89
+ };
90
+
91
+ interface FillOptions {
92
+ serviceName?: string;
93
+ shortName?: string;
94
+ skipPrice?: boolean;
95
+ }
96
+
97
+ const fillRequiredFields = async (user, options: FillOptions = {}) => {
98
+ const { serviceName = 'Test Service Name', shortName = 'Test Short Name', skipPrice = false } = options;
99
+
100
+ if (serviceName) {
101
+ await user.type(screen.getByRole('textbox', { name: /Service name/i }), serviceName);
102
+ }
103
+ if (shortName) {
104
+ await user.type(screen.getByRole('textbox', { name: /Short name/i }), shortName);
105
+ }
106
+
107
+ await user.click(screen.getByRole('combobox', { name: /Service type/i }));
108
+ await user.click(screen.getByRole('option', { name: /Lab service/i }));
109
+
110
+ await user.click(screen.getByRole('combobox', { name: /Payment mode/i }));
111
+ await user.click(screen.getByRole('option', { name: /Cash/i }));
112
+
113
+ if (!skipPrice) {
114
+ const priceInput = screen.getByRole('spinbutton', { name: /Selling Price/i });
115
+ await user.type(priceInput, '100');
116
+ }
117
+ };
118
+
119
+ const submitForm = async () => {
120
+ const user = userEvent.setup();
121
+ const saveButton = screen.getByRole('button', { name: /save/i });
122
+ await user.click(saveButton);
123
+ };
124
+
125
+ describe('BillableServiceFormWorkspace', () => {
126
+ test('should render billable services form and generate correct payload', async () => {
127
+ const user = userEvent.setup();
128
+ const mockCloseWorkspace = jest.fn();
129
+ renderBillableServicesForm({ closeWorkspace: mockCloseWorkspace });
130
+
131
+ await fillRequiredFields(user);
132
+ mockCreateBillableService.mockResolvedValue({} as FetchResponse<any>);
133
+
134
+ await submitForm();
135
+
136
+ expect(mockCreateBillableService).toHaveBeenCalledTimes(1);
137
+ expect(mockCreateBillableService).toHaveBeenCalledWith({
138
+ name: 'Test Service Name',
139
+ shortName: 'Test Short Name',
140
+ serviceType: 'c9604249-db0a-4d03-b074-fc6bc2fa13e6',
141
+ servicePrices: [
142
+ {
143
+ paymentMode: '63eff7a4-6f82-43c4-a333-dbcc58fe9f74',
144
+ price: 100,
145
+ name: 'Cash',
146
+ },
147
+ ],
148
+ serviceStatus: 'ENABLED',
149
+ concept: undefined,
150
+ });
151
+ });
152
+
153
+ describe('Workspace Interactions', () => {
154
+ test('should call closeWorkspace when Cancel button is clicked', async () => {
155
+ const user = userEvent.setup();
156
+ const mockCloseWorkspace = jest.fn();
157
+ renderBillableServicesForm({ closeWorkspace: mockCloseWorkspace });
158
+
159
+ const cancelButton = screen.getByRole('button', { name: /cancel/i });
160
+ await user.click(cancelButton);
161
+
162
+ expect(mockCloseWorkspace).toHaveBeenCalledTimes(1);
163
+ });
164
+
165
+ test('should call closeWorkspaceWithSavedChanges after successful save', async () => {
166
+ const user = userEvent.setup();
167
+ const mockCloseWorkspaceWithSavedChanges = jest.fn();
168
+ renderBillableServicesForm({ closeWorkspaceWithSavedChanges: mockCloseWorkspaceWithSavedChanges });
169
+
170
+ await fillRequiredFields(user);
171
+ mockCreateBillableService.mockResolvedValue({} as FetchResponse<any>);
172
+ await submitForm();
173
+
174
+ // Wait for async submission
175
+ await new Promise((resolve) => setTimeout(resolve, 100));
176
+
177
+ expect(mockCloseWorkspaceWithSavedChanges).toHaveBeenCalledTimes(1);
178
+ });
179
+
180
+ test('should disable buttons during submission', async () => {
181
+ const user = userEvent.setup();
182
+ let resolveCreate: (value: any) => void;
183
+ const createPromise = new Promise((resolve) => {
184
+ resolveCreate = resolve;
185
+ });
186
+ mockCreateBillableService.mockReturnValue(createPromise as any);
187
+
188
+ renderBillableServicesForm();
189
+
190
+ await fillRequiredFields(user);
191
+ const saveButton = screen.getByRole('button', { name: /save/i });
192
+ const cancelButton = screen.getByRole('button', { name: /cancel/i });
193
+
194
+ // Click save to trigger submission
195
+ await user.click(saveButton);
196
+
197
+ // Buttons should be disabled during submission
198
+ expect(saveButton).toBeDisabled();
199
+ expect(cancelButton).toBeDisabled();
200
+
201
+ // Resolve the promise to complete submission
202
+ resolveCreate!({} as FetchResponse<any>);
203
+ await new Promise((resolve) => setTimeout(resolve, 100));
204
+ });
205
+
206
+ test('should show loading indicator in save button during submission', async () => {
207
+ const user = userEvent.setup();
208
+ let resolveCreate: (value: any) => void;
209
+ const createPromise = new Promise((resolve) => {
210
+ resolveCreate = resolve;
211
+ });
212
+ mockCreateBillableService.mockReturnValue(createPromise as any);
213
+
214
+ renderBillableServicesForm();
215
+
216
+ await fillRequiredFields(user);
217
+ const saveButton = screen.getByRole('button', { name: /save/i });
218
+
219
+ await user.click(saveButton);
220
+
221
+ // Should show loading indicator
222
+ expect(await screen.findByText(/saving/i)).toBeInTheDocument();
223
+
224
+ // Resolve the promise
225
+ resolveCreate!({} as FetchResponse<any>);
226
+ await new Promise((resolve) => setTimeout(resolve, 100));
227
+ });
228
+
229
+ test('should call onWorkspaceClose callback after successful edit', async () => {
230
+ const mockOnWorkspaceClose = jest.fn();
231
+ const mockServiceToEdit: BillableService = {
232
+ uuid: 'test-uuid',
233
+ name: 'Test Service',
234
+ shortName: 'TS',
235
+ serviceStatus: 'ENABLED',
236
+ serviceType: {
237
+ uuid: 'type-uuid',
238
+ display: 'Lab service',
239
+ },
240
+ concept: null,
241
+ servicePrices: [
242
+ {
243
+ uuid: 'price-uuid',
244
+ name: 'Cash',
245
+ price: 100,
246
+ paymentMode: {
247
+ uuid: '63eff7a4-6f82-43c4-a333-dbcc58fe9f74',
248
+ name: 'Cash',
249
+ },
250
+ },
251
+ ],
252
+ };
253
+
254
+ renderBillableServicesForm({ serviceToEdit: mockServiceToEdit, onWorkspaceClose: mockOnWorkspaceClose });
255
+
256
+ mockUpdateBillableService.mockResolvedValue({} as FetchResponse<any>);
257
+ await submitForm();
258
+
259
+ // Wait for async submission
260
+ await new Promise((resolve) => setTimeout(resolve, 100));
261
+
262
+ expect(mockOnWorkspaceClose).toHaveBeenCalledTimes(1);
263
+ });
264
+ });
265
+
266
+ describe('Form Validation', () => {
267
+ test('should accept form submission without short name (short name is optional)', async () => {
268
+ const user = userEvent.setup();
269
+ renderBillableServicesForm();
270
+
271
+ // Fill required fields but skip short name
272
+ await user.type(screen.getByRole('textbox', { name: /Service name/i }), 'Lab Test');
273
+
274
+ await user.click(screen.getByRole('combobox', { name: /Service type/i }));
275
+ await user.click(screen.getByRole('option', { name: /Lab service/i }));
276
+
277
+ await user.click(screen.getByRole('combobox', { name: /Payment mode/i }));
278
+ await user.click(screen.getByRole('option', { name: /Cash/i }));
279
+
280
+ const priceInput = screen.getByRole('spinbutton', { name: /Selling Price/i });
281
+ await user.type(priceInput, '50');
282
+
283
+ mockCreateBillableService.mockResolvedValue({} as FetchResponse<any>);
284
+
285
+ await submitForm();
286
+
287
+ expect(mockCreateBillableService).toHaveBeenCalledWith(
288
+ expect.objectContaining({
289
+ name: 'Lab Test',
290
+ shortName: '', // Empty string is valid
291
+ }),
292
+ );
293
+ });
294
+
295
+ test('should enforce 255 character limit on service name input', async () => {
296
+ const user = userEvent.setup();
297
+ renderBillableServicesForm();
298
+
299
+ const longName = 'A'.repeat(300); // Try to type 300 characters
300
+ const input = screen.getByRole('textbox', { name: /Service name/i });
301
+ await user.type(input, longName);
302
+
303
+ // Input should be truncated to 255 chars due to maxLength attribute
304
+ expect(input).toHaveValue('A'.repeat(255));
305
+ });
306
+
307
+ test('should enforce 255 character limit on short name input', async () => {
308
+ const user = userEvent.setup();
309
+ renderBillableServicesForm();
310
+
311
+ const longShortName = 'B'.repeat(300); // Try to type 300 characters
312
+ const input = screen.getByRole('textbox', { name: /Short name/i });
313
+ await user.type(input, longShortName);
314
+
315
+ // Input should be truncated to 255 chars due to maxLength attribute
316
+ expect(input).toHaveValue('B'.repeat(255));
317
+ });
318
+
319
+ test('should show "Price must be greater than 0" error for zero price', async () => {
320
+ const user = userEvent.setup();
321
+ renderBillableServicesForm();
322
+
323
+ await fillRequiredFields(user, { skipPrice: true });
324
+
325
+ const priceInput = screen.getByRole('spinbutton', { name: /selling price/i });
326
+ await user.type(priceInput, '0');
327
+
328
+ await submitForm();
329
+
330
+ expect(screen.getByText('Price must be greater than 0')).toBeInTheDocument();
331
+ expect(mockCreateBillableService).not.toHaveBeenCalled();
332
+ });
333
+
334
+ test('should show "Price must be greater than 0" error for negative price', async () => {
335
+ const user = userEvent.setup();
336
+ renderBillableServicesForm();
337
+
338
+ await fillRequiredFields(user, { skipPrice: true });
339
+
340
+ const priceInput = screen.getByRole('spinbutton', { name: /Selling Price/i });
341
+ await user.type(priceInput, '-10');
342
+
343
+ await submitForm();
344
+
345
+ expect(screen.getByText('Price must be greater than 0')).toBeInTheDocument();
346
+ expect(mockCreateBillableService).not.toHaveBeenCalled();
347
+ });
348
+
349
+ test('should show "Service name is required" error when service name is empty', async () => {
350
+ const user = userEvent.setup();
351
+ renderBillableServicesForm();
352
+
353
+ // Fill all fields except service name
354
+ await user.type(screen.getByRole('textbox', { name: /Short name/i }), 'Test Short Name');
355
+
356
+ await user.click(screen.getByRole('combobox', { name: /Service type/i }));
357
+ await user.click(screen.getByRole('option', { name: /Lab service/i }));
358
+
359
+ await user.click(screen.getByRole('combobox', { name: /Payment mode/i }));
360
+ await user.click(screen.getByRole('option', { name: /Cash/i }));
361
+
362
+ const priceInput = screen.getByRole('spinbutton', { name: /Selling Price/i });
363
+ await user.type(priceInput, '100');
364
+
365
+ await submitForm();
366
+
367
+ expect(await screen.findByText('Service name is required')).toBeInTheDocument();
368
+ expect(mockCreateBillableService).not.toHaveBeenCalled();
369
+ });
370
+
371
+ test('should accept valid decimal price values', async () => {
372
+ const user = userEvent.setup();
373
+ renderBillableServicesForm();
374
+
375
+ await fillRequiredFields(user, { skipPrice: true });
376
+
377
+ const priceInput = screen.getByRole('spinbutton', { name: /selling price/i });
378
+ await user.type(priceInput, '10.50');
379
+
380
+ mockCreateBillableService.mockResolvedValue({} as FetchResponse<any>);
381
+
382
+ await submitForm();
383
+
384
+ expect(screen.queryByText('Price is required')).not.toBeInTheDocument();
385
+ expect(screen.queryByText('Price must be greater than 0')).not.toBeInTheDocument();
386
+ expect(mockCreateBillableService).toHaveBeenCalledTimes(1);
387
+ expect(mockCreateBillableService).toHaveBeenCalledWith({
388
+ name: 'Test Service Name',
389
+ shortName: 'Test Short Name',
390
+ serviceType: 'c9604249-db0a-4d03-b074-fc6bc2fa13e6',
391
+ servicePrices: [
392
+ {
393
+ paymentMode: '63eff7a4-6f82-43c4-a333-dbcc58fe9f74',
394
+ price: 10.5,
395
+ name: 'Cash',
396
+ },
397
+ ],
398
+ serviceStatus: 'ENABLED',
399
+ concept: undefined,
400
+ });
401
+ });
402
+
403
+ test('should show "Service type is required" error when not selected', async () => {
404
+ const user = userEvent.setup();
405
+ renderBillableServicesForm();
406
+
407
+ await user.type(screen.getByRole('textbox', { name: /Service name/i }), 'Test Service');
408
+ await user.type(screen.getByRole('textbox', { name: /Short name/i }), 'Test Short Name');
409
+
410
+ await user.click(screen.getByRole('combobox', { name: /Payment mode/i }));
411
+ await user.click(screen.getByRole('option', { name: /Cash/i }));
412
+
413
+ const priceInput = screen.getByRole('spinbutton', { name: /Selling Price/i });
414
+ await user.type(priceInput, '100');
415
+
416
+ await submitForm();
417
+
418
+ expect(await screen.findByText('Service type is required')).toBeInTheDocument();
419
+ expect(mockCreateBillableService).not.toHaveBeenCalled();
420
+ });
421
+
422
+ test('should show "Payment mode is required" error when not selected', async () => {
423
+ const user = userEvent.setup();
424
+ renderBillableServicesForm();
425
+
426
+ await user.type(screen.getByRole('textbox', { name: /Service name/i }), 'Test Service');
427
+ await user.type(screen.getByRole('textbox', { name: /Short name/i }), 'Test Short Name');
428
+
429
+ await user.click(screen.getByRole('combobox', { name: /Service type/i }));
430
+ await user.click(screen.getByRole('option', { name: /Lab service/i }));
431
+
432
+ const priceInput = screen.getByRole('spinbutton', { name: /Selling Price/i });
433
+ await user.type(priceInput, '100');
434
+
435
+ await submitForm();
436
+
437
+ expect(await screen.findByText('Payment mode is required')).toBeInTheDocument();
438
+ expect(mockCreateBillableService).not.toHaveBeenCalled();
439
+ });
440
+
441
+ test('should show "Price is required" error when price field is empty', async () => {
442
+ const user = userEvent.setup();
443
+ renderBillableServicesForm();
444
+
445
+ await fillRequiredFields(user, { skipPrice: true });
446
+
447
+ await submitForm();
448
+
449
+ expect(await screen.findByText('Price is required')).toBeInTheDocument();
450
+ expect(mockCreateBillableService).not.toHaveBeenCalled();
451
+ });
452
+ });
453
+
454
+ describe('Edit Mode', () => {
455
+ const mockServiceToEdit: BillableService = {
456
+ uuid: 'existing-service-uuid',
457
+ name: 'X-Ray Service',
458
+ shortName: 'XRay',
459
+ serviceStatus: 'ENABLED',
460
+ serviceType: {
461
+ uuid: 'c9604249-db0a-4d03-b074-fc6bc2fa13e6',
462
+ display: 'Lab service',
463
+ },
464
+ concept: null,
465
+ servicePrices: [
466
+ {
467
+ uuid: 'price-uuid',
468
+ name: 'Cash',
469
+ price: 150,
470
+ paymentMode: {
471
+ uuid: '63eff7a4-6f82-43c4-a333-dbcc58fe9f74',
472
+ name: 'Cash',
473
+ },
474
+ },
475
+ ],
476
+ };
477
+
478
+ test('should populate form with existing service data', () => {
479
+ renderBillableServicesForm({ serviceToEdit: mockServiceToEdit });
480
+
481
+ expect(screen.getByText('X-Ray Service')).toBeInTheDocument(); // Service name shown as label
482
+ expect(screen.getByDisplayValue('XRay')).toBeInTheDocument(); // Short name
483
+ });
484
+
485
+ test('should call updateBillableService instead of createBillableService', async () => {
486
+ const user = userEvent.setup();
487
+ const mockCloseWorkspace = jest.fn();
488
+ renderBillableServicesForm({ serviceToEdit: mockServiceToEdit, closeWorkspace: mockCloseWorkspace });
489
+
490
+ const shortNameInput = screen.getByDisplayValue('XRay');
491
+ await user.clear(shortNameInput);
492
+ await user.type(shortNameInput, 'X-RAY');
493
+
494
+ mockUpdateBillableService.mockResolvedValue({} as FetchResponse<any>);
495
+
496
+ await submitForm();
497
+
498
+ expect(mockUpdateBillableService).toHaveBeenCalledTimes(1);
499
+ expect(mockUpdateBillableService).toHaveBeenCalledWith('existing-service-uuid', {
500
+ name: 'X-Ray Service',
501
+ shortName: 'X-RAY',
502
+ serviceType: 'c9604249-db0a-4d03-b074-fc6bc2fa13e6',
503
+ servicePrices: [
504
+ {
505
+ paymentMode: '63eff7a4-6f82-43c4-a333-dbcc58fe9f74',
506
+ price: 150,
507
+ name: 'Cash',
508
+ },
509
+ ],
510
+ serviceStatus: 'ENABLED',
511
+ concept: undefined,
512
+ });
513
+ expect(mockCreateBillableService).not.toHaveBeenCalled();
514
+ });
515
+
516
+ test('should call onWorkspaceClose callback after successful edit', async () => {
517
+ const mockOnWorkspaceClose = jest.fn();
518
+ renderBillableServicesForm({ serviceToEdit: mockServiceToEdit, onWorkspaceClose: mockOnWorkspaceClose });
519
+
520
+ mockUpdateBillableService.mockResolvedValue({} as FetchResponse<any>);
521
+
522
+ await submitForm();
523
+
524
+ expect(mockOnWorkspaceClose).toHaveBeenCalledTimes(1);
525
+ });
526
+
527
+ test('should not allow editing service name in edit mode', () => {
528
+ renderBillableServicesForm({ serviceToEdit: mockServiceToEdit });
529
+
530
+ // Service name should be displayed as a label, not an editable input
531
+ expect(screen.getByText('X-Ray Service')).toBeInTheDocument();
532
+ expect(screen.queryByRole('textbox', { name: /Service name/i })).not.toBeInTheDocument();
533
+ });
534
+
535
+ test('should handle asynchronous loading of dependencies and populate form correctly', async () => {
536
+ // Scenario: User opens edit form, but payment modes/service types haven't loaded yet
537
+ // The form should wait for dependencies to load, then populate correctly
538
+
539
+ renderBillableServicesForm({ serviceToEdit: mockServiceToEdit });
540
+
541
+ // After dependencies load (handled by renderBillableServicesForm's setupMocks),
542
+ // form should display with populated data
543
+ expect(await screen.findByText('X-Ray Service')).toBeInTheDocument();
544
+ expect(screen.getByDisplayValue('XRay')).toBeInTheDocument();
545
+
546
+ // This test verifies the useEffect that calls reset() when dependencies load
547
+ // The behavior is: even if payment modes/types load after initial render,
548
+ // the form will update to show the service data
549
+ });
550
+ });
551
+
552
+ describe('Dynamic Payment Options', () => {
553
+ test('should add new payment option when clicking "Add payment option" button', async () => {
554
+ const user = userEvent.setup();
555
+ renderBillableServicesForm();
556
+
557
+ const addButton = screen.getByRole('button', { name: /Add payment option/i });
558
+ await user.click(addButton);
559
+
560
+ const paymentModeDropdowns = screen.getAllByRole('combobox', { name: /Payment mode/i });
561
+ expect(paymentModeDropdowns).toHaveLength(2);
562
+ });
563
+
564
+ test('should be able to add multiple payment options', async () => {
565
+ const user = userEvent.setup();
566
+ renderBillableServicesForm();
567
+
568
+ // Add a second payment option
569
+ const addButton = screen.getByRole('button', { name: /Add payment option/i });
570
+ await user.click(addButton);
571
+
572
+ const paymentModeDropdowns = screen.getAllByRole('combobox', { name: /Payment mode/i });
573
+ expect(paymentModeDropdowns).toHaveLength(2);
574
+ });
575
+
576
+ test('should allow adding multiple payment options with different payment modes', async () => {
577
+ const user = userEvent.setup();
578
+ renderBillableServicesForm();
579
+
580
+ // Add second payment option
581
+ const addButton = screen.getByRole('button', { name: /Add payment option/i });
582
+ await user.click(addButton);
583
+
584
+ // Fill in first payment option
585
+ const paymentModeDropdowns = screen.getAllByRole('combobox', { name: /Payment mode/i });
586
+ await user.click(paymentModeDropdowns[0]);
587
+ await user.click(screen.getByRole('option', { name: /Cash/i }));
588
+
589
+ const priceInputs = screen.getAllByRole('spinbutton', { name: /Selling Price/i });
590
+ await user.type(priceInputs[0], '100');
591
+
592
+ // Fill in second payment option
593
+ await user.click(paymentModeDropdowns[1]);
594
+ await user.click(screen.getByRole('option', { name: /Insurance/i }));
595
+ await user.type(priceInputs[1], '80');
596
+
597
+ // Fill other required fields
598
+ await user.type(screen.getByRole('textbox', { name: /Service name/i }), 'Multi-price Service');
599
+ await user.type(screen.getByRole('textbox', { name: /Short name/i }), 'MPS');
600
+ await user.click(screen.getByRole('combobox', { name: /Service type/i }));
601
+ await user.click(screen.getByRole('option', { name: /Lab service/i }));
602
+
603
+ mockCreateBillableService.mockResolvedValue({} as FetchResponse<any>);
604
+ await submitForm();
605
+
606
+ expect(mockCreateBillableService).toHaveBeenCalledWith(
607
+ expect.objectContaining({
608
+ servicePrices: [
609
+ {
610
+ paymentMode: '63eff7a4-6f82-43c4-a333-dbcc58fe9f74',
611
+ price: 100,
612
+ name: 'Cash',
613
+ },
614
+ {
615
+ paymentMode: 'beac329b-f1dc-4a33-9e7c-d95821a137a6',
616
+ price: 80,
617
+ name: 'Insurance',
618
+ },
619
+ ],
620
+ }),
621
+ );
622
+ });
623
+
624
+ test('should validate each payment option independently', async () => {
625
+ const user = userEvent.setup();
626
+ renderBillableServicesForm();
627
+
628
+ // Add second payment option
629
+ const addButton = screen.getByRole('button', { name: /Add payment option/i });
630
+ await user.click(addButton);
631
+
632
+ // Fill first payment option correctly
633
+ const paymentModeDropdowns = screen.getAllByRole('combobox', { name: /Payment mode/i });
634
+ await user.click(paymentModeDropdowns[0]);
635
+ await user.click(screen.getByRole('option', { name: /Cash/i }));
636
+
637
+ const priceInputs = screen.getAllByRole('spinbutton', { name: /Selling Price/i });
638
+ await user.type(priceInputs[0], '100');
639
+
640
+ // Leave second payment option incomplete (no price)
641
+ await user.click(paymentModeDropdowns[1]);
642
+ await user.click(screen.getByRole('option', { name: /Insurance/i }));
643
+
644
+ // Fill other required fields
645
+ await user.type(screen.getByRole('textbox', { name: /Service name/i }), 'Test Service');
646
+ await user.type(screen.getByRole('textbox', { name: /Short name/i }), 'TS');
647
+ await user.click(screen.getByRole('combobox', { name: /Service type/i }));
648
+ await user.click(screen.getByRole('option', { name: /Lab service/i }));
649
+
650
+ await submitForm();
651
+
652
+ // Should show error for the second payment option's missing price
653
+ const priceErrors = await screen.findAllByText('Price is required');
654
+ expect(priceErrors.length).toBeGreaterThan(0);
655
+ expect(mockCreateBillableService).not.toHaveBeenCalled();
656
+ });
657
+
658
+ test('should allow selecting different payment modes in multiple fields', async () => {
659
+ const user = userEvent.setup();
660
+ renderBillableServicesForm();
661
+
662
+ // Add second and third payment options
663
+ const addButton = screen.getByRole('button', { name: /Add payment option/i });
664
+ await user.click(addButton);
665
+ await user.click(addButton);
666
+
667
+ // Select different payment modes in each field
668
+ const paymentModeDropdowns = screen.getAllByRole('combobox', { name: /Payment mode/i });
669
+
670
+ await user.click(paymentModeDropdowns[0]);
671
+ await user.click(screen.getByRole('option', { name: /Cash/i }));
672
+
673
+ await user.click(paymentModeDropdowns[1]);
674
+ await user.click(screen.getByRole('option', { name: /Insurance/i }));
675
+
676
+ await user.click(paymentModeDropdowns[2]);
677
+ await user.click(screen.getByRole('option', { name: /MPESA/i }));
678
+
679
+ const priceInputs = screen.getAllByRole('spinbutton', { name: /Selling Price/i });
680
+ await user.type(priceInputs[0], '100');
681
+ await user.type(priceInputs[1], '80');
682
+ await user.type(priceInputs[2], '90');
683
+
684
+ // Fill other required fields
685
+ await user.type(screen.getByRole('textbox', { name: /Service name/i }), 'Multi-mode Service');
686
+ await user.type(screen.getByRole('textbox', { name: /Short name/i }), 'MMS');
687
+ await user.click(screen.getByRole('combobox', { name: /Service type/i }));
688
+ await user.click(screen.getByRole('option', { name: /Lab service/i }));
689
+
690
+ mockCreateBillableService.mockResolvedValue({} as FetchResponse<any>);
691
+ await submitForm();
692
+
693
+ // Verify all three payment modes were submitted
694
+ expect(mockCreateBillableService).toHaveBeenCalledWith(
695
+ expect.objectContaining({
696
+ servicePrices: [
697
+ {
698
+ paymentMode: '63eff7a4-6f82-43c4-a333-dbcc58fe9f74',
699
+ price: 100,
700
+ name: 'Cash',
701
+ },
702
+ {
703
+ paymentMode: 'beac329b-f1dc-4a33-9e7c-d95821a137a6',
704
+ price: 80,
705
+ name: 'Insurance',
706
+ },
707
+ {
708
+ paymentMode: '28989582-e8c3-46b0-96d0-c249cb06d5c6',
709
+ price: 90,
710
+ name: 'MPESA',
711
+ },
712
+ ],
713
+ }),
714
+ );
715
+ });
716
+ });
717
+
718
+ describe('Error Handling', () => {
719
+ test('should display error snackbar when create API call fails', async () => {
720
+ const user = userEvent.setup();
721
+ renderBillableServicesForm();
722
+
723
+ await fillRequiredFields(user);
724
+
725
+ const errorMessage = 'Network error';
726
+ mockCreateBillableService.mockRejectedValue(new Error(errorMessage));
727
+
728
+ await submitForm();
729
+
730
+ // Wait for async operations to complete
731
+ await new Promise((resolve) => setTimeout(resolve, 100));
732
+
733
+ expect(mockCreateBillableService).toHaveBeenCalledTimes(1);
734
+ });
735
+
736
+ test('should display error snackbar when update API call fails', async () => {
737
+ const user = userEvent.setup();
738
+ const mockServiceToEdit: BillableService = {
739
+ uuid: 'service-uuid',
740
+ name: 'Test Service',
741
+ shortName: 'TS',
742
+ serviceStatus: 'ENABLED',
743
+ serviceType: {
744
+ uuid: 'c9604249-db0a-4d03-b074-fc6bc2fa13e6',
745
+ display: 'Lab service',
746
+ },
747
+ concept: null,
748
+ servicePrices: [
749
+ {
750
+ uuid: 'price-uuid',
751
+ name: 'Cash',
752
+ price: 100,
753
+ paymentMode: {
754
+ uuid: '63eff7a4-6f82-43c4-a333-dbcc58fe9f74',
755
+ name: 'Cash',
756
+ },
757
+ },
758
+ ],
759
+ };
760
+
761
+ renderBillableServicesForm({ serviceToEdit: mockServiceToEdit });
762
+
763
+ const errorMessage = 'Update failed';
764
+ mockUpdateBillableService.mockRejectedValue(new Error(errorMessage));
765
+
766
+ await submitForm();
767
+
768
+ // Wait for async operations to complete
769
+ await new Promise((resolve) => setTimeout(resolve, 100));
770
+
771
+ expect(mockUpdateBillableService).toHaveBeenCalledTimes(1);
772
+ });
773
+ });
774
+ });
775
+
776
+ describe('Helper Functions', () => {
777
+ describe('transformServiceToFormData', () => {
778
+ test('should return default form data when no service is provided', () => {
779
+ const result = transformServiceToFormData();
780
+
781
+ expect(result).toEqual({
782
+ name: '',
783
+ shortName: '',
784
+ serviceType: null,
785
+ concept: null,
786
+ payment: [{ paymentMode: '', price: '' }],
787
+ });
788
+ });
789
+
790
+ test('should return default form data when undefined service is provided', () => {
791
+ const result = transformServiceToFormData(undefined);
792
+
793
+ expect(result).toEqual({
794
+ name: '',
795
+ shortName: '',
796
+ serviceType: null,
797
+ concept: null,
798
+ payment: [{ paymentMode: '', price: '' }],
799
+ });
800
+ });
801
+
802
+ test('should transform a complete service to form data', () => {
803
+ const service: BillableService = {
804
+ uuid: 'service-uuid',
805
+ name: 'X-Ray',
806
+ shortName: 'XRay',
807
+ serviceStatus: 'ENABLED',
808
+ serviceType: {
809
+ uuid: 'type-uuid',
810
+ display: 'Lab service',
811
+ },
812
+ concept: {
813
+ uuid: 'concept-search-result-uuid',
814
+ concept: {
815
+ uuid: 'concept-uuid',
816
+ display: 'Radiology',
817
+ },
818
+ display: 'Radiology',
819
+ },
820
+ servicePrices: [
821
+ {
822
+ uuid: 'price-uuid-1',
823
+ name: 'Cash',
824
+ price: 100,
825
+ paymentMode: {
826
+ uuid: 'payment-mode-uuid-1',
827
+ name: 'Cash',
828
+ },
829
+ },
830
+ {
831
+ uuid: 'price-uuid-2',
832
+ name: 'Insurance',
833
+ price: 80,
834
+ paymentMode: {
835
+ uuid: 'payment-mode-uuid-2',
836
+ name: 'Insurance',
837
+ },
838
+ },
839
+ ],
840
+ };
841
+
842
+ const result = transformServiceToFormData(service);
843
+
844
+ expect(result).toEqual({
845
+ name: 'X-Ray',
846
+ shortName: 'XRay',
847
+ serviceType: {
848
+ uuid: 'type-uuid',
849
+ display: 'Lab service',
850
+ },
851
+ concept: {
852
+ uuid: 'concept-search-result-uuid',
853
+ display: 'Radiology',
854
+ },
855
+ payment: [
856
+ {
857
+ paymentMode: 'payment-mode-uuid-1',
858
+ price: 100,
859
+ },
860
+ {
861
+ paymentMode: 'payment-mode-uuid-2',
862
+ price: 80,
863
+ },
864
+ ],
865
+ });
866
+ });
867
+
868
+ test('should handle service without concept', () => {
869
+ const service: BillableService = {
870
+ uuid: 'service-uuid',
871
+ name: 'Basic Service',
872
+ shortName: 'BS',
873
+ serviceStatus: 'ENABLED',
874
+ serviceType: {
875
+ uuid: 'type-uuid',
876
+ display: 'General',
877
+ },
878
+ concept: null,
879
+ servicePrices: [
880
+ {
881
+ uuid: 'price-uuid',
882
+ name: 'Cash',
883
+ price: 50,
884
+ paymentMode: {
885
+ uuid: 'payment-mode-uuid',
886
+ name: 'Cash',
887
+ },
888
+ },
889
+ ],
890
+ };
891
+
892
+ const result = transformServiceToFormData(service);
893
+
894
+ expect(result.concept).toBeNull();
895
+ });
896
+
897
+ test('should handle service with missing or empty price using nullish coalescing', () => {
898
+ const service: BillableService = {
899
+ uuid: 'service-uuid',
900
+ name: 'Test Service',
901
+ shortName: 'TS',
902
+ serviceStatus: 'ENABLED',
903
+ serviceType: {
904
+ uuid: 'type-uuid',
905
+ display: 'General',
906
+ },
907
+ concept: null,
908
+ servicePrices: [
909
+ {
910
+ uuid: 'price-uuid',
911
+ name: 'Cash',
912
+ price: 0, // Falsy but valid
913
+ paymentMode: {
914
+ uuid: 'payment-mode-uuid',
915
+ name: 'Cash',
916
+ },
917
+ },
918
+ ],
919
+ };
920
+
921
+ const result = transformServiceToFormData(service);
922
+
923
+ // Price 0 should be preserved (not converted to empty string)
924
+ expect(result.payment[0].price).toBe(0);
925
+ });
926
+ });
927
+
928
+ describe('normalizePrice', () => {
929
+ test('should return number as-is', () => {
930
+ expect(normalizePrice(100)).toBe(100);
931
+ expect(normalizePrice(10.5)).toBe(10.5);
932
+ expect(normalizePrice(0)).toBe(0);
933
+ });
934
+
935
+ test('should convert string to number', () => {
936
+ expect(normalizePrice('100')).toBe(100);
937
+ expect(normalizePrice('10.5')).toBe(10.5);
938
+ expect(normalizePrice('0')).toBe(0);
939
+ });
940
+
941
+ test('should handle decimal strings correctly', () => {
942
+ expect(normalizePrice('10.99')).toBe(10.99);
943
+ expect(normalizePrice('0.50')).toBe(0.5);
944
+ });
945
+
946
+ test('should handle undefined by converting to NaN', () => {
947
+ expect(normalizePrice(undefined)).toBeNaN();
948
+ });
949
+
950
+ test('should handle empty string by converting to NaN', () => {
951
+ expect(normalizePrice('')).toBeNaN();
952
+ });
953
+
954
+ test('should handle invalid string by converting to NaN', () => {
955
+ expect(normalizePrice('invalid')).toBeNaN();
956
+ });
957
+ });
958
+
959
+ describe('getAvailablePaymentModes', () => {
960
+ const allPaymentModes = [
961
+ { uuid: 'cash-uuid', name: 'Cash' },
962
+ { uuid: 'insurance-uuid', name: 'Insurance' },
963
+ { uuid: 'mpesa-uuid', name: 'MPESA' },
964
+ ];
965
+
966
+ test('should return all payment modes when no modes are selected', () => {
967
+ const fields = [{ paymentMode: '', price: '' }];
968
+ const result = getAvailablePaymentModes(allPaymentModes, fields, 0, '');
969
+
970
+ expect(result).toEqual(allPaymentModes);
971
+ });
972
+
973
+ test('should exclude already-selected payment modes from other fields', () => {
974
+ const fields = [
975
+ { paymentMode: 'cash-uuid', price: '100' },
976
+ { paymentMode: '', price: '' },
977
+ ];
978
+ const result = getAvailablePaymentModes(allPaymentModes, fields, 1, '');
979
+
980
+ expect(result).toEqual([
981
+ { uuid: 'insurance-uuid', name: 'Insurance' },
982
+ { uuid: 'mpesa-uuid', name: 'MPESA' },
983
+ ]);
984
+ expect(result).not.toContainEqual({ uuid: 'cash-uuid', name: 'Cash' });
985
+ });
986
+
987
+ test('should keep current field selection visible even if selected elsewhere', () => {
988
+ const fields = [
989
+ { paymentMode: 'cash-uuid', price: '100' },
990
+ { paymentMode: 'insurance-uuid', price: '80' },
991
+ ];
992
+ // Field 0 should still see "Cash" as an option
993
+ const result = getAvailablePaymentModes(allPaymentModes, fields, 0, 'cash-uuid');
994
+
995
+ expect(result).toContainEqual({ uuid: 'cash-uuid', name: 'Cash' });
996
+ expect(result).not.toContainEqual({ uuid: 'insurance-uuid', name: 'Insurance' });
997
+ expect(result).toContainEqual({ uuid: 'mpesa-uuid', name: 'MPESA' });
998
+ });
999
+
1000
+ test('should filter multiple selected payment modes', () => {
1001
+ const fields = [
1002
+ { paymentMode: 'cash-uuid', price: '100' },
1003
+ { paymentMode: 'insurance-uuid', price: '80' },
1004
+ { paymentMode: '', price: '' },
1005
+ ];
1006
+ const result = getAvailablePaymentModes(allPaymentModes, fields, 2, '');
1007
+
1008
+ expect(result).toEqual([{ uuid: 'mpesa-uuid', name: 'MPESA' }]);
1009
+ });
1010
+
1011
+ test('should handle empty payment mode values correctly', () => {
1012
+ const fields = [
1013
+ { paymentMode: '', price: '' },
1014
+ { paymentMode: 'cash-uuid', price: '100' },
1015
+ { paymentMode: '', price: '' },
1016
+ ];
1017
+ // Field 0 should see all modes except Cash
1018
+ const result = getAvailablePaymentModes(allPaymentModes, fields, 0, '');
1019
+
1020
+ expect(result).toEqual([
1021
+ { uuid: 'insurance-uuid', name: 'Insurance' },
1022
+ { uuid: 'mpesa-uuid', name: 'MPESA' },
1023
+ ]);
1024
+ });
1025
+
1026
+ test('should work with generic types having uuid property', () => {
1027
+ const customModes = [
1028
+ { uuid: 'a', customProp: 'value1' },
1029
+ { uuid: 'b', customProp: 'value2' },
1030
+ ];
1031
+ const fields = [
1032
+ { paymentMode: 'a', price: '50' },
1033
+ { paymentMode: '', price: '' },
1034
+ ];
1035
+
1036
+ const result = getAvailablePaymentModes(customModes, fields, 1, '');
1037
+
1038
+ expect(result).toEqual([{ uuid: 'b', customProp: 'value2' }]);
1039
+ });
1040
+
1041
+ test('should return all modes when only current field has a selection', () => {
1042
+ const fields = [{ paymentMode: 'cash-uuid', price: '100' }];
1043
+ const result = getAvailablePaymentModes(allPaymentModes, fields, 0, 'cash-uuid');
1044
+
1045
+ expect(result).toEqual(allPaymentModes);
1046
+ });
1047
+ });
1048
+ });