-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterlinearizerLoader.test.tsx
More file actions
304 lines (268 loc) · 9.72 KB
/
Copy pathInterlinearizerLoader.test.tsx
File metadata and controls
304 lines (268 loc) · 9.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/** @file Unit tests for components/InterlinearizerLoader.tsx. */
/// <reference types="jest" />
/// <reference types="@testing-library/jest-dom" />
import { useLocalizedStrings, useSetting } from '@papi/frontend/react';
import type { SerializedVerseRef } from '@sillsdev/scripture';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import type { Book } from 'interlinearizer';
import useInterlinearizerBookData from '../../hooks/useInterlinearizerBookData';
import useOptimisticBooleanSetting from '../../hooks/useOptimisticBooleanSetting';
import InterlinearizerLoader from '../../components/InterlinearizerLoader';
jest.mock('../../hooks/useInterlinearizerBookData');
jest.mock('../../hooks/useOptimisticBooleanSetting');
jest.mock('../../components/ContinuousScrollToggle', () => ({
__esModule: true,
default: ({
checked,
disabled,
onCheckedChange,
}: {
checked: boolean;
disabled: boolean;
onCheckedChange: (v: boolean) => void;
}) => (
<button
aria-label="continuous scroll"
data-testid="continuous-scroll-toggle"
data-checked={String(checked)}
data-disabled={String(disabled)}
onClick={() => onCheckedChange(!checked)}
type="button"
/>
),
}));
jest.mock('../../components/ScriptureNavControls', () => ({
__esModule: true,
default: () => <div data-testid="scripture-nav-controls" />,
}));
jest.mock('../../components/ContinuousView', () => ({
__esModule: true,
default: () => <div data-testid="continuous-view" />,
}));
type CapturedInterlinearizerProps = {
continuousScroll: boolean;
};
let capturedInterlinearizerProps: CapturedInterlinearizerProps | undefined;
jest.mock('../../components/Interlinearizer', () => ({
__esModule: true,
default: (props: CapturedInterlinearizerProps) => {
capturedInterlinearizerProps = props;
return <div data-testid="interlinearizer" />;
},
}));
const defaultScrRef: SerializedVerseRef = { book: 'GEN', chapterNum: 1, verseNum: 1 };
const testProjectId = 'test-project-id';
/** A minimal Book used as the successful hook result. */
const TEST_BOOK: Book = {
id: 'GEN',
bookRef: 'GEN',
textVersion: 'v1',
segments: [],
};
/**
* Returns a `useWebViewScrollGroupScrRef` hook stub bound to the given reference and setter.
*
* @param scrRef - Scripture reference to expose; defaults to GEN 1:1
* @param setScrRef - Setter callback; defaults to a no-op
*/
function makeScrollGroupHook(
scrRef: SerializedVerseRef = defaultScrRef,
setScrRef: (r: SerializedVerseRef) => void = () => {},
) {
return (): [
SerializedVerseRef,
(r: SerializedVerseRef) => void,
number | undefined,
(id: number | undefined) => void,
] => [scrRef, setScrRef, undefined, () => {}];
}
/**
* Configures useInterlinearizerBookData to return the given state.
*
* @param overrides - Partial hook result; all fields default to a successful loaded state
*/
function mockBookData(
overrides: Partial<{
book: Book | undefined;
chapterSegments: Book['segments'];
isLoading: boolean;
bookError: string | undefined;
tokenizeError: { message: string; raw: unknown } | undefined;
}> = {},
): void {
jest.mocked(useInterlinearizerBookData).mockReturnValue({
book: TEST_BOOK,
chapterSegments: [],
isLoading: false,
bookError: undefined,
tokenizeError: undefined,
...overrides,
});
}
/**
* Configures useOptimisticBooleanSetting to return the given state.
*
* @param value - The current boolean value; defaults to `false`
* @param onChange - The change handler; defaults to a jest.fn()
* @param isLoading - Whether the setting is loading; defaults to `false`
*/
function mockOptimisticSetting(
value = false,
onChange: jest.Mock = jest.fn(),
isLoading = false,
): jest.Mock {
jest.mocked(useOptimisticBooleanSetting).mockReturnValue({ value, onChange, isLoading });
return onChange;
}
describe('InterlinearizerLoader', () => {
beforeEach(() => {
capturedInterlinearizerProps = undefined;
mockBookData();
mockOptimisticSetting();
jest.mocked(useLocalizedStrings).mockReturnValue([{}, false]);
jest.mocked(useSetting).mockReturnValue(['simple', jest.fn(), jest.fn(), false]);
});
it('shows nav controls when interface mode is power', () => {
jest.mocked(useSetting).mockReturnValue(['power', jest.fn(), jest.fn(), false]);
render(
<InterlinearizerLoader
projectId={testProjectId}
useWebViewScrollGroupScrRef={makeScrollGroupHook()}
/>,
);
expect(screen.getByTestId('scripture-nav-controls')).toBeInTheDocument();
expect(screen.getByTestId('interlinearizer')).toBeInTheDocument();
});
it('hides nav controls when interface mode is simple', () => {
render(
<InterlinearizerLoader
projectId={testProjectId}
useWebViewScrollGroupScrRef={makeScrollGroupHook()}
/>,
);
expect(screen.queryByTestId('scripture-nav-controls')).not.toBeInTheDocument();
expect(screen.getByTestId('interlinearizer')).toBeInTheDocument();
});
it('shows Loading when book data has not arrived', () => {
mockBookData({ book: undefined, isLoading: true });
render(
<InterlinearizerLoader
projectId={testProjectId}
useWebViewScrollGroupScrRef={makeScrollGroupHook()}
/>,
);
expect(screen.getByText('Loading…')).toBeInTheDocument();
});
it('shows an error when no USJ book is available for the project', () => {
mockBookData({
book: undefined,
bookError: 'No USJ book available for GEN in project test-project-id',
});
render(
<InterlinearizerLoader
projectId={testProjectId}
useWebViewScrollGroupScrRef={makeScrollGroupHook()}
/>,
);
expect(screen.getByRole('heading', { name: /error loading book/i })).toBeInTheDocument();
expect(screen.getByText(/no usj book available for gen in project/i)).toBeInTheDocument();
});
it('shows an error heading and message when book data is a PlatformError', () => {
mockBookData({ book: undefined, bookError: 'Project not found' });
render(
<InterlinearizerLoader
projectId={testProjectId}
useWebViewScrollGroupScrRef={makeScrollGroupHook()}
/>,
);
expect(screen.getByRole('heading', { name: /error loading book/i })).toBeInTheDocument();
expect(screen.getByText(/project not found/i)).toBeInTheDocument();
});
it('shows an error heading and message when tokenization throws an Error', () => {
mockBookData({
book: undefined,
tokenizeError: { message: 'parse failure', raw: new Error('parse failure') },
});
render(
<InterlinearizerLoader
projectId={testProjectId}
useWebViewScrollGroupScrRef={makeScrollGroupHook()}
/>,
);
expect(screen.getByRole('heading', { name: /error processing book/i })).toBeInTheDocument();
expect(screen.getByText('parse failure')).toBeInTheDocument();
});
it('shows an error message when tokenization throws a non-Error value', () => {
mockBookData({
book: undefined,
tokenizeError: { message: 'unexpected string error', raw: 'unexpected string error' },
});
render(
<InterlinearizerLoader
projectId={testProjectId}
useWebViewScrollGroupScrRef={makeScrollGroupHook()}
/>,
);
expect(screen.getByRole('heading', { name: /error processing book/i })).toBeInTheDocument();
expect(screen.getByText('unexpected string error')).toBeInTheDocument();
});
it('passes a book-stable ref to BookUSJ so chapter and verse changes do not re-fetch the book', () => {
// The book-stable ref logic lives in useInterlinearizerBookData, which is tested in its own
// test file. This test verifies that InterlinearizerLoader passes scrRef into the hook and that
// re-rendering with a new verse does not cause the hook to receive a new book ref.
const { rerender } = render(
<InterlinearizerLoader
projectId={testProjectId}
useWebViewScrollGroupScrRef={makeScrollGroupHook()}
/>,
);
rerender(
<InterlinearizerLoader
projectId={testProjectId}
useWebViewScrollGroupScrRef={makeScrollGroupHook({
book: 'GEN',
chapterNum: 2,
verseNum: 5,
})}
/>,
);
const { calls } = jest.mocked(useInterlinearizerBookData).mock;
expect(calls.length).toBeGreaterThanOrEqual(2);
calls.forEach((args) => expect(args[0].projectId).toBe(testProjectId));
});
it('passes continuousScroll from useOptimisticBooleanSetting to Interlinearizer', () => {
mockOptimisticSetting(true);
render(
<InterlinearizerLoader
projectId={testProjectId}
useWebViewScrollGroupScrRef={makeScrollGroupHook()}
/>,
);
expect(capturedInterlinearizerProps?.continuousScroll).toBe(true);
});
it('passes checked and disabled from useOptimisticBooleanSetting to ContinuousScrollToggle', () => {
mockOptimisticSetting(true, jest.fn(), true);
render(
<InterlinearizerLoader
projectId={testProjectId}
useWebViewScrollGroupScrRef={makeScrollGroupHook()}
/>,
);
const toggle = screen.getByTestId('continuous-scroll-toggle');
expect(toggle).toHaveAttribute('data-checked', 'true');
expect(toggle).toHaveAttribute('data-disabled', 'true');
});
it('wires ContinuousScrollToggle onCheckedChange to the onChange from useOptimisticBooleanSetting', async () => {
const mockOnChange = jest.fn();
mockOptimisticSetting(false, mockOnChange);
render(
<InterlinearizerLoader
projectId={testProjectId}
useWebViewScrollGroupScrRef={makeScrollGroupHook()}
/>,
);
await userEvent.click(screen.getByTestId('continuous-scroll-toggle'));
expect(mockOnChange).toHaveBeenCalledWith(true);
});
});