@openmrs/esm-billing-app 1.0.2-pre.861 → 1.0.2-pre.866

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.
@@ -4,17 +4,20 @@ import { render, screen } from '@testing-library/react';
4
4
  import { navigate, type FetchResponse } from '@openmrs/esm-framework';
5
5
  import {
6
6
  createBillableService,
7
+ updateBillableService,
7
8
  useBillableServices,
8
9
  useConceptsSearch,
9
10
  usePaymentModes,
10
11
  useServiceTypes,
11
12
  } from '../billable-service.resource';
12
- import AddBillableService from './add-billable-service.component';
13
+ import AddBillableService, { transformServiceToFormData, normalizePrice } from './add-billable-service.component';
14
+ import type { BillableService } from '../../types';
13
15
 
14
16
  const mockUseBillableServices = jest.mocked(useBillableServices);
15
17
  const mockUsePaymentModes = jest.mocked(usePaymentModes);
16
18
  const mockUseServiceTypes = jest.mocked(useServiceTypes);
17
19
  const mockCreateBillableService = jest.mocked(createBillableService);
20
+ const mockUpdateBillableService = jest.mocked(updateBillableService);
18
21
  const mockUseConceptsSearch = jest.mocked(useConceptsSearch);
19
22
 
20
23
  jest.mock('../billable-service.resource', () => ({
@@ -153,6 +156,58 @@ describe('AddBillableService', () => {
153
156
  });
154
157
 
155
158
  describe('Form Validation', () => {
159
+ test('should accept form submission without short name (short name is optional)', async () => {
160
+ const user = userEvent.setup();
161
+ renderAddBillableService();
162
+
163
+ // Fill required fields but skip short name
164
+ await user.type(screen.getByRole('textbox', { name: /Service name/i }), 'Lab Test');
165
+
166
+ await user.click(screen.getByRole('combobox', { name: /Service type/i }));
167
+ await user.click(screen.getByRole('option', { name: /Lab service/i }));
168
+
169
+ await user.click(screen.getByRole('combobox', { name: /Payment mode/i }));
170
+ await user.click(screen.getByRole('option', { name: /Cash/i }));
171
+
172
+ const priceInput = screen.getByRole('spinbutton', { name: /Selling Price/i });
173
+ await user.type(priceInput, '50');
174
+
175
+ mockCreateBillableService.mockResolvedValue({} as FetchResponse<any>);
176
+
177
+ await submitForm(user);
178
+
179
+ expect(mockCreateBillableService).toHaveBeenCalledWith(
180
+ expect.objectContaining({
181
+ name: 'Lab Test',
182
+ shortName: '', // Empty string is valid
183
+ }),
184
+ );
185
+ });
186
+
187
+ test('should enforce 255 character limit on service name input', async () => {
188
+ const user = userEvent.setup();
189
+ renderAddBillableService();
190
+
191
+ const longName = 'A'.repeat(300); // Try to type 300 characters
192
+ const input = screen.getByRole('textbox', { name: /Service name/i });
193
+ await user.type(input, longName);
194
+
195
+ // Input should be truncated to 255 chars due to maxLength attribute
196
+ expect(input).toHaveValue('A'.repeat(255));
197
+ });
198
+
199
+ test('should enforce 255 character limit on short name input', async () => {
200
+ const user = userEvent.setup();
201
+ renderAddBillableService();
202
+
203
+ const longShortName = 'B'.repeat(300); // Try to type 300 characters
204
+ const input = screen.getByRole('textbox', { name: /Short name/i });
205
+ await user.type(input, longShortName);
206
+
207
+ // Input should be truncated to 255 chars due to maxLength attribute
208
+ expect(input).toHaveValue('B'.repeat(255));
209
+ });
210
+
156
211
  test('should show "Price must be greater than 0" error for zero price', async () => {
157
212
  const user = userEvent.setup();
158
213
  renderAddBillableService();
@@ -236,5 +291,507 @@ describe('AddBillableService', () => {
236
291
  concept: undefined,
237
292
  });
238
293
  });
294
+
295
+ test('should show "Service type is required" error when not selected', async () => {
296
+ const user = userEvent.setup();
297
+ renderAddBillableService();
298
+
299
+ await user.type(screen.getByRole('textbox', { name: /Service name/i }), 'Test Service');
300
+ await user.type(screen.getByRole('textbox', { name: /Short name/i }), 'Test Short Name');
301
+
302
+ await user.click(screen.getByRole('combobox', { name: /Payment mode/i }));
303
+ await user.click(screen.getByRole('option', { name: /Cash/i }));
304
+
305
+ const priceInput = screen.getByRole('spinbutton', { name: /Selling Price/i });
306
+ await user.type(priceInput, '100');
307
+
308
+ await submitForm(user);
309
+
310
+ expect(screen.getByText('Service type is required')).toBeInTheDocument();
311
+ expect(mockCreateBillableService).not.toHaveBeenCalled();
312
+ });
313
+
314
+ test('should show "Payment mode is required" error when not selected', async () => {
315
+ const user = userEvent.setup();
316
+ renderAddBillableService();
317
+
318
+ await user.type(screen.getByRole('textbox', { name: /Service name/i }), 'Test Service');
319
+ await user.type(screen.getByRole('textbox', { name: /Short name/i }), 'Test Short Name');
320
+
321
+ await user.click(screen.getByRole('combobox', { name: /Service type/i }));
322
+ await user.click(screen.getByRole('option', { name: /Lab service/i }));
323
+
324
+ const priceInput = screen.getByRole('spinbutton', { name: /Selling Price/i });
325
+ await user.type(priceInput, '100');
326
+
327
+ await submitForm(user);
328
+
329
+ expect(screen.getByText('Payment mode is required')).toBeInTheDocument();
330
+ expect(mockCreateBillableService).not.toHaveBeenCalled();
331
+ });
332
+
333
+ test('should show "Price is required" error when price field is empty', async () => {
334
+ const user = userEvent.setup();
335
+ renderAddBillableService();
336
+
337
+ await fillRequiredFields(user, { skipPrice: true });
338
+
339
+ await submitForm(user);
340
+
341
+ expect(screen.getByText('Price is required')).toBeInTheDocument();
342
+ expect(mockCreateBillableService).not.toHaveBeenCalled();
343
+ });
344
+ });
345
+
346
+ describe('Edit Mode', () => {
347
+ const mockServiceToEdit: BillableService = {
348
+ uuid: 'existing-service-uuid',
349
+ name: 'X-Ray Service',
350
+ shortName: 'XRay',
351
+ serviceStatus: 'ENABLED',
352
+ serviceType: {
353
+ uuid: 'c9604249-db0a-4d03-b074-fc6bc2fa13e6',
354
+ display: 'Lab service',
355
+ },
356
+ concept: null,
357
+ servicePrices: [
358
+ {
359
+ uuid: 'price-uuid',
360
+ name: 'Cash',
361
+ price: 150,
362
+ paymentMode: {
363
+ uuid: '63eff7a4-6f82-43c4-a333-dbcc58fe9f74',
364
+ name: 'Cash',
365
+ },
366
+ },
367
+ ],
368
+ };
369
+
370
+ test('should populate form with existing service data', () => {
371
+ renderAddBillableService({ serviceToEdit: mockServiceToEdit });
372
+
373
+ expect(screen.getByText('Edit billable service')).toBeInTheDocument();
374
+ expect(screen.getByText('X-Ray Service')).toBeInTheDocument(); // Service name shown as label
375
+ expect(screen.getByDisplayValue('XRay')).toBeInTheDocument(); // Short name
376
+ });
377
+
378
+ test('should call updateBillableService instead of createBillableService', async () => {
379
+ const user = userEvent.setup();
380
+ const mockOnClose = jest.fn();
381
+ renderAddBillableService({ serviceToEdit: mockServiceToEdit, onClose: mockOnClose });
382
+
383
+ const shortNameInput = screen.getByDisplayValue('XRay');
384
+ await user.clear(shortNameInput);
385
+ await user.type(shortNameInput, 'X-RAY');
386
+
387
+ mockUpdateBillableService.mockResolvedValue({} as FetchResponse<any>);
388
+
389
+ await submitForm(user);
390
+
391
+ expect(mockUpdateBillableService).toHaveBeenCalledTimes(1);
392
+ expect(mockUpdateBillableService).toHaveBeenCalledWith('existing-service-uuid', {
393
+ name: 'X-Ray Service',
394
+ shortName: 'X-RAY',
395
+ serviceType: 'c9604249-db0a-4d03-b074-fc6bc2fa13e6',
396
+ servicePrices: [
397
+ {
398
+ paymentMode: '63eff7a4-6f82-43c4-a333-dbcc58fe9f74',
399
+ price: 150,
400
+ name: 'Cash',
401
+ },
402
+ ],
403
+ serviceStatus: 'ENABLED',
404
+ concept: undefined,
405
+ });
406
+ expect(mockCreateBillableService).not.toHaveBeenCalled();
407
+ expect(mockOnClose).toHaveBeenCalledTimes(1);
408
+ });
409
+
410
+ test('should call onServiceUpdated callback after successful edit', async () => {
411
+ const user = userEvent.setup();
412
+ const mockOnServiceUpdated = jest.fn();
413
+ renderAddBillableService({ serviceToEdit: mockServiceToEdit, onServiceUpdated: mockOnServiceUpdated });
414
+
415
+ mockUpdateBillableService.mockResolvedValue({} as FetchResponse<any>);
416
+
417
+ await submitForm(user);
418
+
419
+ expect(mockOnServiceUpdated).toHaveBeenCalledTimes(1);
420
+ });
421
+
422
+ test('should not allow editing service name in edit mode', () => {
423
+ renderAddBillableService({ serviceToEdit: mockServiceToEdit });
424
+
425
+ // Service name should be displayed as a label, not an editable input
426
+ expect(screen.getByText('X-Ray Service')).toBeInTheDocument();
427
+ expect(screen.queryByRole('textbox', { name: /Service name/i })).not.toBeInTheDocument();
428
+ });
429
+
430
+ test('should handle asynchronous loading of dependencies and populate form correctly', async () => {
431
+ // Scenario: User opens edit form, but payment modes/service types haven't loaded yet
432
+ // The form should wait for dependencies to load, then populate correctly
433
+
434
+ renderAddBillableService({ serviceToEdit: mockServiceToEdit });
435
+
436
+ // After dependencies load (handled by renderAddBillableService's setupMocks),
437
+ // form should display with populated data
438
+ await screen.findByText('Edit billable service');
439
+ expect(screen.getByText('X-Ray Service')).toBeInTheDocument();
440
+ expect(screen.getByDisplayValue('XRay')).toBeInTheDocument();
441
+
442
+ // This test verifies the useEffect that calls reset() when dependencies load
443
+ // The behavior is: even if payment modes/types load after initial render,
444
+ // the form will update to show the service data
445
+ });
446
+ });
447
+
448
+ describe('Dynamic Payment Options', () => {
449
+ test('should add new payment option when clicking "Add payment option" button', async () => {
450
+ const user = userEvent.setup();
451
+ renderAddBillableService();
452
+
453
+ const addButton = screen.getByRole('button', { name: /Add payment option/i });
454
+ await user.click(addButton);
455
+
456
+ const paymentModeDropdowns = screen.getAllByRole('combobox', { name: /Payment mode/i });
457
+ expect(paymentModeDropdowns).toHaveLength(2);
458
+ });
459
+
460
+ test('should be able to add multiple payment options', async () => {
461
+ const user = userEvent.setup();
462
+ renderAddBillableService();
463
+
464
+ // Add a second payment option
465
+ const addButton = screen.getByRole('button', { name: /Add payment option/i });
466
+ await user.click(addButton);
467
+
468
+ const paymentModeDropdowns = screen.getAllByRole('combobox', { name: /Payment mode/i });
469
+ expect(paymentModeDropdowns).toHaveLength(2);
470
+ });
471
+
472
+ test('should allow adding multiple payment options with different payment modes', async () => {
473
+ const user = userEvent.setup();
474
+ renderAddBillableService();
475
+
476
+ // Add second payment option
477
+ const addButton = screen.getByRole('button', { name: /Add payment option/i });
478
+ await user.click(addButton);
479
+
480
+ // Fill in first payment option
481
+ const paymentModeDropdowns = screen.getAllByRole('combobox', { name: /Payment mode/i });
482
+ await user.click(paymentModeDropdowns[0]);
483
+ await user.click(screen.getByRole('option', { name: /Cash/i }));
484
+
485
+ const priceInputs = screen.getAllByRole('spinbutton', { name: /Selling Price/i });
486
+ await user.type(priceInputs[0], '100');
487
+
488
+ // Fill in second payment option
489
+ await user.click(paymentModeDropdowns[1]);
490
+ await user.click(screen.getByRole('option', { name: /Insurance/i }));
491
+ await user.type(priceInputs[1], '80');
492
+
493
+ // Fill other required fields
494
+ await user.type(screen.getByRole('textbox', { name: /Service name/i }), 'Multi-price Service');
495
+ await user.type(screen.getByRole('textbox', { name: /Short name/i }), 'MPS');
496
+ await user.click(screen.getByRole('combobox', { name: /Service type/i }));
497
+ await user.click(screen.getByRole('option', { name: /Lab service/i }));
498
+
499
+ mockCreateBillableService.mockResolvedValue({} as FetchResponse<any>);
500
+ await submitForm(user);
501
+
502
+ expect(mockCreateBillableService).toHaveBeenCalledWith(
503
+ expect.objectContaining({
504
+ servicePrices: [
505
+ {
506
+ paymentMode: '63eff7a4-6f82-43c4-a333-dbcc58fe9f74',
507
+ price: 100,
508
+ name: 'Cash',
509
+ },
510
+ {
511
+ paymentMode: 'beac329b-f1dc-4a33-9e7c-d95821a137a6',
512
+ price: 80,
513
+ name: 'Insurance',
514
+ },
515
+ ],
516
+ }),
517
+ );
518
+ });
519
+
520
+ test('should validate each payment option independently', async () => {
521
+ const user = userEvent.setup();
522
+ renderAddBillableService();
523
+
524
+ // Add second payment option
525
+ const addButton = screen.getByRole('button', { name: /Add payment option/i });
526
+ await user.click(addButton);
527
+
528
+ // Fill first payment option correctly
529
+ const paymentModeDropdowns = screen.getAllByRole('combobox', { name: /Payment mode/i });
530
+ await user.click(paymentModeDropdowns[0]);
531
+ await user.click(screen.getByRole('option', { name: /Cash/i }));
532
+
533
+ const priceInputs = screen.getAllByRole('spinbutton', { name: /Selling Price/i });
534
+ await user.type(priceInputs[0], '100');
535
+
536
+ // Leave second payment option incomplete (no price)
537
+ await user.click(paymentModeDropdowns[1]);
538
+ await user.click(screen.getByRole('option', { name: /Insurance/i }));
539
+
540
+ // Fill other required fields
541
+ await user.type(screen.getByRole('textbox', { name: /Service name/i }), 'Test Service');
542
+ await user.type(screen.getByRole('textbox', { name: /Short name/i }), 'TS');
543
+ await user.click(screen.getByRole('combobox', { name: /Service type/i }));
544
+ await user.click(screen.getByRole('option', { name: /Lab service/i }));
545
+
546
+ await submitForm(user);
547
+
548
+ // Should show error for the second payment option's missing price
549
+ const priceErrors = screen.getAllByText('Price is required');
550
+ expect(priceErrors.length).toBeGreaterThan(0);
551
+ expect(mockCreateBillableService).not.toHaveBeenCalled();
552
+ });
553
+ });
554
+
555
+ describe('Error Handling', () => {
556
+ test('should display error snackbar when create API call fails', async () => {
557
+ const user = userEvent.setup();
558
+ renderAddBillableService();
559
+
560
+ await fillRequiredFields(user);
561
+
562
+ const errorMessage = 'Network error';
563
+ mockCreateBillableService.mockRejectedValue(new Error(errorMessage));
564
+
565
+ await submitForm(user);
566
+
567
+ // Wait for async operations
568
+ await screen.findByRole('button', { name: /save/i });
569
+
570
+ expect(mockCreateBillableService).toHaveBeenCalledTimes(1);
571
+ expect(navigate).not.toHaveBeenCalled();
572
+ });
573
+
574
+ test('should display error snackbar when update API call fails', async () => {
575
+ const user = userEvent.setup();
576
+ const mockServiceToEdit: BillableService = {
577
+ uuid: 'service-uuid',
578
+ name: 'Test Service',
579
+ shortName: 'TS',
580
+ serviceStatus: 'ENABLED',
581
+ serviceType: {
582
+ uuid: 'c9604249-db0a-4d03-b074-fc6bc2fa13e6',
583
+ display: 'Lab service',
584
+ },
585
+ concept: null,
586
+ servicePrices: [
587
+ {
588
+ uuid: 'price-uuid',
589
+ name: 'Cash',
590
+ price: 100,
591
+ paymentMode: {
592
+ uuid: '63eff7a4-6f82-43c4-a333-dbcc58fe9f74',
593
+ name: 'Cash',
594
+ },
595
+ },
596
+ ],
597
+ };
598
+
599
+ renderAddBillableService({ serviceToEdit: mockServiceToEdit });
600
+
601
+ const errorMessage = 'Update failed';
602
+ mockUpdateBillableService.mockRejectedValue(new Error(errorMessage));
603
+
604
+ await submitForm(user);
605
+
606
+ // Wait for async operations
607
+ await screen.findByRole('button', { name: /save/i });
608
+
609
+ expect(mockUpdateBillableService).toHaveBeenCalledTimes(1);
610
+ expect(navigate).not.toHaveBeenCalled();
611
+ });
612
+ });
613
+ });
614
+
615
+ describe('Helper Functions', () => {
616
+ describe('transformServiceToFormData', () => {
617
+ test('should return default form data when no service is provided', () => {
618
+ const result = transformServiceToFormData();
619
+
620
+ expect(result).toEqual({
621
+ name: '',
622
+ shortName: '',
623
+ serviceType: null,
624
+ concept: null,
625
+ payment: [{ paymentMode: '', price: '' }],
626
+ });
627
+ });
628
+
629
+ test('should return default form data when undefined service is provided', () => {
630
+ const result = transformServiceToFormData(undefined);
631
+
632
+ expect(result).toEqual({
633
+ name: '',
634
+ shortName: '',
635
+ serviceType: null,
636
+ concept: null,
637
+ payment: [{ paymentMode: '', price: '' }],
638
+ });
639
+ });
640
+
641
+ test('should transform a complete service to form data', () => {
642
+ const service: BillableService = {
643
+ uuid: 'service-uuid',
644
+ name: 'X-Ray',
645
+ shortName: 'XRay',
646
+ serviceStatus: 'ENABLED',
647
+ serviceType: {
648
+ uuid: 'type-uuid',
649
+ display: 'Lab service',
650
+ },
651
+ concept: {
652
+ uuid: 'concept-search-result-uuid',
653
+ concept: {
654
+ uuid: 'concept-uuid',
655
+ display: 'Radiology',
656
+ },
657
+ display: 'Radiology',
658
+ },
659
+ servicePrices: [
660
+ {
661
+ uuid: 'price-uuid-1',
662
+ name: 'Cash',
663
+ price: 100,
664
+ paymentMode: {
665
+ uuid: 'payment-mode-uuid-1',
666
+ name: 'Cash',
667
+ },
668
+ },
669
+ {
670
+ uuid: 'price-uuid-2',
671
+ name: 'Insurance',
672
+ price: 80,
673
+ paymentMode: {
674
+ uuid: 'payment-mode-uuid-2',
675
+ name: 'Insurance',
676
+ },
677
+ },
678
+ ],
679
+ };
680
+
681
+ const result = transformServiceToFormData(service);
682
+
683
+ expect(result).toEqual({
684
+ name: 'X-Ray',
685
+ shortName: 'XRay',
686
+ serviceType: {
687
+ uuid: 'type-uuid',
688
+ display: 'Lab service',
689
+ },
690
+ concept: {
691
+ uuid: 'concept-search-result-uuid',
692
+ display: 'Radiology',
693
+ },
694
+ payment: [
695
+ {
696
+ paymentMode: 'payment-mode-uuid-1',
697
+ price: 100,
698
+ },
699
+ {
700
+ paymentMode: 'payment-mode-uuid-2',
701
+ price: 80,
702
+ },
703
+ ],
704
+ });
705
+ });
706
+
707
+ test('should handle service without concept', () => {
708
+ const service: BillableService = {
709
+ uuid: 'service-uuid',
710
+ name: 'Basic Service',
711
+ shortName: 'BS',
712
+ serviceStatus: 'ENABLED',
713
+ serviceType: {
714
+ uuid: 'type-uuid',
715
+ display: 'General',
716
+ },
717
+ concept: null,
718
+ servicePrices: [
719
+ {
720
+ uuid: 'price-uuid',
721
+ name: 'Cash',
722
+ price: 50,
723
+ paymentMode: {
724
+ uuid: 'payment-mode-uuid',
725
+ name: 'Cash',
726
+ },
727
+ },
728
+ ],
729
+ };
730
+
731
+ const result = transformServiceToFormData(service);
732
+
733
+ expect(result.concept).toBeNull();
734
+ });
735
+
736
+ test('should handle service with missing or empty price using nullish coalescing', () => {
737
+ const service: BillableService = {
738
+ uuid: 'service-uuid',
739
+ name: 'Test Service',
740
+ shortName: 'TS',
741
+ serviceStatus: 'ENABLED',
742
+ serviceType: {
743
+ uuid: 'type-uuid',
744
+ display: 'General',
745
+ },
746
+ concept: null,
747
+ servicePrices: [
748
+ {
749
+ uuid: 'price-uuid',
750
+ name: 'Cash',
751
+ price: 0, // Falsy but valid
752
+ paymentMode: {
753
+ uuid: 'payment-mode-uuid',
754
+ name: 'Cash',
755
+ },
756
+ },
757
+ ],
758
+ };
759
+
760
+ const result = transformServiceToFormData(service);
761
+
762
+ // Price 0 should be preserved (not converted to empty string)
763
+ expect(result.payment[0].price).toBe(0);
764
+ });
765
+ });
766
+
767
+ describe('normalizePrice', () => {
768
+ test('should return number as-is', () => {
769
+ expect(normalizePrice(100)).toBe(100);
770
+ expect(normalizePrice(10.5)).toBe(10.5);
771
+ expect(normalizePrice(0)).toBe(0);
772
+ });
773
+
774
+ test('should convert string to number', () => {
775
+ expect(normalizePrice('100')).toBe(100);
776
+ expect(normalizePrice('10.5')).toBe(10.5);
777
+ expect(normalizePrice('0')).toBe(0);
778
+ });
779
+
780
+ test('should handle decimal strings correctly', () => {
781
+ expect(normalizePrice('10.99')).toBe(10.99);
782
+ expect(normalizePrice('0.50')).toBe(0.5);
783
+ });
784
+
785
+ test('should handle undefined by converting to NaN', () => {
786
+ expect(normalizePrice(undefined)).toBeNaN();
787
+ });
788
+
789
+ test('should handle empty string by converting to NaN', () => {
790
+ expect(normalizePrice('')).toBeNaN();
791
+ });
792
+
793
+ test('should handle invalid string by converting to NaN', () => {
794
+ expect(normalizePrice('invalid')).toBeNaN();
795
+ });
239
796
  });
240
797
  });