@pih/esm-referrals-queue-app 2.1.1-pre.158 → 2.1.1-pre.162

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.
@@ -1,6 +1,5 @@
1
1
  import React from 'react';
2
- import { of } from 'rxjs';
3
- import { render, fireEvent, screen, within, waitFor } from '@testing-library/react';
2
+ import { act, render, fireEvent, screen, within, waitFor } from '@testing-library/react';
4
3
  import MockDate from 'mockdate';
5
4
  import ReferralsQueue from './referrals-queue.component';
6
5
  import { findNearestVisitLocationUuid, getReferrals } from './referrals-queue.resource';
@@ -8,6 +7,7 @@ import { findNearestVisitLocationUuid, getReferrals } from './referrals-queue.re
8
7
  const mockedUseConfig = jest.fn();
9
8
  const mockedUseLocations = jest.fn();
10
9
  const mockedUseSession = jest.fn();
10
+ const mockedErrorHandler = jest.fn();
11
11
 
12
12
  jest.mock('./referrals-queue.resource');
13
13
  jest.mock('@openmrs/esm-framework', () => ({
@@ -15,6 +15,7 @@ jest.mock('@openmrs/esm-framework', () => ({
15
15
  useConfig: () => mockedUseConfig(),
16
16
  useLocations: () => mockedUseLocations(),
17
17
  useSession: () => mockedUseSession(),
18
+ createErrorHandler: () => mockedErrorHandler,
18
19
  }));
19
20
 
20
21
  const mockedGetReferrals = getReferrals as jest.Mock;
@@ -87,15 +88,17 @@ describe('referrals queue', () => {
87
88
  pendingStatuses: ['Pending status', 'Referral unmet'],
88
89
  });
89
90
  });
90
- beforeEach(() => {
91
+ beforeEach(async () => {
91
92
  MockDate.set(todayString + 'T10:00:00.000-0400');
93
+ mockedErrorHandler.mockReset();
92
94
  mockedGetReferrals.mockReset();
93
- mockedGetReferrals.mockReturnValue(of(referrals));
95
+ mockedGetReferrals.mockResolvedValue(referrals);
94
96
  mockedUseLocations.mockReturnValue(locations);
95
97
  mockedUseSession.mockReturnValue({ sessionLocation: locations[0] });
96
98
  mockedFindNearestVisitLocationUuid.mockReset();
97
99
  mockedFindNearestVisitLocationUuid.mockResolvedValue(undefined);
98
100
  view = render(<ReferralsQueue />);
101
+ await screen.findByText(referrals[0].zl_emr_id);
99
102
  });
100
103
 
101
104
  afterEach(() => {
@@ -106,6 +109,38 @@ describe('referrals queue', () => {
106
109
  // in beforeEach
107
110
  });
108
111
 
112
+ it('fetches referrals for the session location with an abort controller', () => {
113
+ expect(mockedGetReferrals).toHaveBeenCalledWith(
114
+ expect.objectContaining({ locationUuid: locations[0].uuid, locale: 'en' }),
115
+ expect.any(AbortController),
116
+ );
117
+ });
118
+
119
+ it('reports an error when fetching referrals fails', async () => {
120
+ view.unmount();
121
+ const error = new Error('network error');
122
+ mockedGetReferrals.mockRejectedValue(error);
123
+
124
+ render(<ReferralsQueue />);
125
+
126
+ await waitFor(() => expect(mockedErrorHandler).toHaveBeenCalledWith(error));
127
+ });
128
+
129
+ it('aborts the pending request and ignores its result after unmounting', async () => {
130
+ view.unmount();
131
+ let resolveReferrals: (value: unknown) => void;
132
+ mockedGetReferrals.mockReturnValue(new Promise((resolve) => (resolveReferrals = resolve)));
133
+
134
+ const { unmount } = render(<ReferralsQueue />);
135
+ const abortController: AbortController = mockedGetReferrals.mock.lastCall[1];
136
+ unmount();
137
+
138
+ expect(abortController.signal.aborted).toBe(true);
139
+ resolveReferrals(referrals);
140
+ await Promise.resolve();
141
+ expect(mockedErrorHandler).not.toHaveBeenCalled();
142
+ });
143
+
109
144
  it('renders the expected fields', () => {
110
145
  for (let referral of referrals) {
111
146
  const row = screen.getByText(referral.zl_emr_id).closest('tr');
@@ -204,7 +239,7 @@ describe('referrals queue', () => {
204
239
  );
205
240
  });
206
241
 
207
- it('refetches referrals for the newly selected location', () => {
242
+ it('refetches referrals for the newly selected location', async () => {
208
243
  fireEvent.click(
209
244
  screen.getByLabelText('Location', {
210
245
  selector: 'button',
@@ -215,7 +250,12 @@ describe('referrals queue', () => {
215
250
  selector: 'div',
216
251
  }),
217
252
  );
218
- expect(mockedGetReferrals).toHaveBeenLastCalledWith(expect.objectContaining({ locationUuid: locations[1].uuid }));
253
+ expect(mockedGetReferrals).toHaveBeenLastCalledWith(
254
+ expect.objectContaining({ locationUuid: locations[1].uuid }),
255
+ expect.any(AbortController),
256
+ );
257
+ // let the refetch settle so its state update happens inside act
258
+ await act(() => mockedGetReferrals.mock.results.at(-1).value);
219
259
  });
220
260
 
221
261
  it('filters by statuses, with dropdown inferred from data', () => {