@openpeepshq/rn-components 0.22.0 → 0.23.0

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.
@@ -0,0 +1,626 @@
1
+ /**
2
+ * Unit tests for the RecordAudioSheet 90s cap and confirmability (#327).
3
+ *
4
+ * The auto-stop must be driven by the wall-clock interval (Date.now() deltas),
5
+ * not the recorder's throttle-racy position callback, and a completed take must
6
+ * be confirmable from the file the stop returns even if a final position event
7
+ * never lands. A still-open (not stopped) recording must not be confirmable.
8
+ */
9
+
10
+ const React = require('react');
11
+ const ReactTestRenderer = require('react-test-renderer');
12
+
13
+ let grantPermission = true;
14
+ let failStart = false;
15
+ const recorderInstances: unknown[] = [];
16
+
17
+ jest.mock('react-native', () => ({
18
+ View: (props: { children?: unknown }) =>
19
+ React.createElement('View', null, props.children),
20
+ PermissionsAndroid: {
21
+ PERMISSIONS: { RECORD_AUDIO: 'android.permission.RECORD_AUDIO' },
22
+ RESULTS: { GRANTED: 'granted', DENIED: 'denied' },
23
+ request: jest.fn(async () => (grantPermission ? 'granted' : 'denied')),
24
+ },
25
+ Platform: { OS: 'android' },
26
+ }));
27
+
28
+ jest.mock('react-i18next', () => ({
29
+ useTranslation: () => ({
30
+ t: (key: string) => key,
31
+ }),
32
+ }));
33
+
34
+ jest.mock('@openpeepshq/common', () => ({
35
+ VOICE_NOTE_MAX_SECONDS: 90,
36
+ VOICE_NOTE_AUTO_STOP_SECONDS: 89.5,
37
+ }));
38
+
39
+ jest.mock('react-native-fs', () => ({
40
+ __esModule: true,
41
+ default: {
42
+ // The sheet writes each take to `${CachesDirectoryPath}/voice_…`; the
43
+ // mock keeps the shape so the path-pin below exercises the real join.
44
+ CachesDirectoryPath: '/tmp/caches',
45
+ },
46
+ }));
47
+
48
+ jest.mock('@gorhom/bottom-sheet', () => ({
49
+ BottomSheetModal: 'BottomSheetModal',
50
+ }));
51
+
52
+ jest.mock('react-native-audio-recorder-player', () => {
53
+ class MockRecorder {
54
+ // Mirrors the real native contract: both platforms write a caller-supplied
55
+ // path verbatim and resolve `file://<path>` on start and stop, so the
56
+ // sheet's take URL is the file it actually recorded.
57
+ private lastStartPath?: string;
58
+ startRecorder = jest.fn(async (path?: string) => {
59
+ if (failStart) throw new Error('mic unavailable');
60
+ this.lastStartPath = path;
61
+ return path ? `file://${path}` : 'file:///rec.m4a';
62
+ });
63
+ // Stop honors the take's own path (same contract as the platforms above),
64
+ // so a stop that regressed to a fixed default would fail the
65
+ // take-URL assertions below instead of flowing a wrong file into upload.
66
+ stopRecorder = jest.fn(async () =>
67
+ this.lastStartPath ? `file://${this.lastStartPath}` : 'file:///rec.m4a'
68
+ );
69
+ addRecordBackListener = jest.fn();
70
+ removeRecordBackListener = jest.fn();
71
+ // Captures the position/finish callback so a test can fire `isFinished`
72
+ // (the record sheet's preview end-of-playback transition, mirroring the
73
+ // picker sheet's).
74
+ playListener?: (e: {
75
+ isFinished: boolean;
76
+ currentPosition: number;
77
+ duration: number;
78
+ }) => void;
79
+ addPlayBackListener = jest.fn((cb: unknown) => {
80
+ this.playListener = cb as (e: {
81
+ isFinished: boolean;
82
+ currentPosition: number;
83
+ duration: number;
84
+ }) => void;
85
+ });
86
+ removePlayBackListener = jest.fn();
87
+ pauseRecorder = jest.fn(async () => 'file:///rec.m4a');
88
+ resumeRecorder = jest.fn(async () => 'file:///rec.m4a');
89
+ startPlayer = jest.fn(async () => 'file:///rec.m4a');
90
+ stopPlayer = jest.fn(async () => 'file:///rec.m4a');
91
+ pausePlayer = jest.fn(async () => 'file:///rec.m4a');
92
+ resumePlayer = jest.fn(async () => 'file:///rec.m4a');
93
+ mmss = (s: number) =>
94
+ `00:${String(Math.max(0, Math.floor(s) % 60)).padStart(2, '0')}`;
95
+ constructor() {
96
+ recorderInstances.push(this);
97
+ }
98
+ }
99
+ return { __esModule: true, default: MockRecorder };
100
+ });
101
+
102
+ jest.mock('../common', () => ({
103
+ BaseSheet: (props: { children?: unknown; onDismiss?: () => void }) =>
104
+ React.createElement(
105
+ 'BaseSheet',
106
+ { onDismiss: props.onDismiss },
107
+ props.children
108
+ ),
109
+ SheetFooter: (props: {
110
+ disabled?: boolean;
111
+ onConfirm?: () => void;
112
+ onCancel?: () => void;
113
+ }) =>
114
+ React.createElement('ConfirmButton', {
115
+ 'data-disabled': props.disabled,
116
+ 'onPress': props.onConfirm,
117
+ 'onCancel': props.onCancel,
118
+ }),
119
+ }));
120
+
121
+ jest.mock('../../../ui/button', () => ({
122
+ Button: (props: {
123
+ children?: unknown;
124
+ onPress?: () => void;
125
+ size?: string;
126
+ variant?: string;
127
+ disabled?: boolean;
128
+ }) =>
129
+ React.createElement('Button', { onPress: props.onPress }, props.children),
130
+ }));
131
+
132
+ jest.mock('../../../ui/themed-text', () => ({
133
+ ThemedText: (props: { children?: unknown; className?: string }) =>
134
+ React.createElement('ThemedText', null, props.children),
135
+ }));
136
+
137
+ jest.mock('../../../icons/index', () => ({
138
+ CircleIcon: () => React.createElement('CircleIcon'),
139
+ PauseIcon: () => React.createElement('PauseIcon'),
140
+ PlayIcon: () => React.createElement('PlayIcon'),
141
+ SquareIcon: () => React.createElement('SquareIcon'),
142
+ Repeat2Icon: () => React.createElement('Repeat2Icon'),
143
+ }));
144
+
145
+ jest.mock('../../../../lib/bottom-sheet-ref', () => ({
146
+ bottomSheetDismiss: jest.fn(),
147
+ }));
148
+
149
+ import { RecordAudioSheet } from './record-audio-sheet';
150
+
151
+ const flush = async () => {
152
+ for (let i = 0; i < 30; i++) await Promise.resolve();
153
+ };
154
+
155
+ const render = (onComplete: (url: string) => void) =>
156
+ ReactTestRenderer.create(
157
+ React.createElement(RecordAudioSheet, {
158
+ ref: { current: null },
159
+ onComplete,
160
+ })
161
+ );
162
+
163
+ const findButtons = (tree: ReturnType<typeof ReactTestRenderer.create>) =>
164
+ tree.root.findAll((node) => node.type === 'Button');
165
+
166
+ const findConfirm = (tree: ReturnType<typeof ReactTestRenderer.create>) =>
167
+ tree.root.findAll((n) => n.type === 'ConfirmButton')[0];
168
+
169
+ describe('RecordAudioSheet 90s cap', () => {
170
+ beforeEach(() => {
171
+ grantPermission = true;
172
+ failStart = false;
173
+ recorderInstances.length = 0;
174
+ });
175
+
176
+ afterEach(() => {
177
+ jest.useRealTimers();
178
+ });
179
+
180
+ it('auto-stops at the wall-clock cap even if the position callback stalls', async () => {
181
+ const onComplete = jest.fn();
182
+ // Render under real timers, then flip to fake timers so the in-component
183
+ // wall-clock interval is the only thing advancing toward the cap.
184
+ const tree = await ReactTestRenderer.act(async () => {
185
+ jest.useFakeTimers();
186
+ return render(onComplete);
187
+ });
188
+
189
+ const start = findButtons(tree)[0];
190
+ await ReactTestRenderer.act(async () => {
191
+ start.props.onPress();
192
+ await flush();
193
+ });
194
+
195
+ const rec = recorderInstances[recorderInstances.length - 1] as {
196
+ startRecorder: jest.Mock;
197
+ stopRecorder: jest.Mock;
198
+ };
199
+ expect(rec.startRecorder).toHaveBeenCalledTimes(1);
200
+ expect(rec.stopRecorder).not.toHaveBeenCalled();
201
+
202
+ // The recorder's position callback never reports the cap (throttle-racy);
203
+ // only the wall-clock interval can stop the take.
204
+ await ReactTestRenderer.act(async () => {
205
+ jest.advanceTimersByTime(90 * 1000 + 1000);
206
+ await flush();
207
+ });
208
+
209
+ expect(rec.stopRecorder).toHaveBeenCalledTimes(1);
210
+ // The completed take is confirmable even though no final position event
211
+ // arrived — confirm is enabled and hands the file over.
212
+ expect(findConfirm(tree).props['data-disabled']).toBe(false);
213
+ await ReactTestRenderer.act(async () => {
214
+ findConfirm(tree).props.onPress();
215
+ await flush();
216
+ });
217
+ // End-to-end: the take handed to onComplete is this take's own
218
+ // `file://<start path>`, not a fixed default — a stop that regressed to
219
+ // a hard-coded path would fail here.
220
+ expect(onComplete).toHaveBeenCalledWith(
221
+ `file://${rec.startRecorder.mock.calls[0][0]}`
222
+ );
223
+
224
+ // Item 6: the native recorder is created lazily and not re-created on the
225
+ // many re-renders the ticks caused.
226
+ expect(recorderInstances.length).toBe(1);
227
+ });
228
+
229
+ it('does not confirm a still-open (not stopped) recording', async () => {
230
+ const onComplete = jest.fn();
231
+ const tree = await ReactTestRenderer.act(async () => {
232
+ jest.useFakeTimers();
233
+ return render(onComplete);
234
+ });
235
+
236
+ const start = findButtons(tree)[0];
237
+ await ReactTestRenderer.act(async () => {
238
+ start.props.onPress();
239
+ await flush();
240
+ });
241
+
242
+ // Mid-recording: confirm is disabled and the handler no-ops.
243
+ expect(findConfirm(tree).props['data-disabled']).toBe(true);
244
+ await ReactTestRenderer.act(async () => {
245
+ findConfirm(tree).props.onPress();
246
+ await flush();
247
+ });
248
+ expect(onComplete).not.toHaveBeenCalled();
249
+ });
250
+
251
+ it('shows the distinct micFailed copy when startRecorder throws, and Start stays usable for a retry', async () => {
252
+ const onComplete = jest.fn();
253
+ const tree = await ReactTestRenderer.act(async () => {
254
+ return render(onComplete);
255
+ });
256
+
257
+ // The copy is the identity-mapped i18n key; searching the rendered tree
258
+ // string is renderer-agnostic.
259
+ const rendered = () => JSON.stringify(tree.toJSON());
260
+
261
+ // The failed copy is absent before any start attempt.
262
+ expect(rendered()).not.toContain('posts.form.voiceNote.micFailed');
263
+
264
+ // A start whose startRecorder throws surfaces the failed copy (not the
265
+ // denied copy) and leaves the take unconfirmable.
266
+ failStart = true;
267
+ const start = findButtons(tree)[0];
268
+ await ReactTestRenderer.act(async () => {
269
+ start.props.onPress();
270
+ await flush();
271
+ });
272
+ expect(rendered()).toContain('posts.form.voiceNote.micFailed');
273
+ expect(rendered()).not.toContain('posts.form.voiceNote.micError');
274
+ expect(findConfirm(tree).props['data-disabled']).toBe(true);
275
+
276
+ // The failed state does not lock the recorder out: a retry start (now
277
+ // succeeding) produces a take, clears the failed copy, and confirms it.
278
+ failStart = false;
279
+ await ReactTestRenderer.act(async () => {
280
+ start.props.onPress();
281
+ await flush();
282
+ });
283
+ expect(rendered()).not.toContain('posts.form.voiceNote.micFailed');
284
+ const stop = findButtons(tree).find(
285
+ (b) => b.findAll((d) => d.type === 'SquareIcon').length > 0
286
+ );
287
+ expect(stop).toBeTruthy();
288
+ await ReactTestRenderer.act(async () => {
289
+ stop.props.onPress();
290
+ await flush();
291
+ });
292
+ expect(findConfirm(tree).props['data-disabled']).toBe(false);
293
+ await ReactTestRenderer.act(async () => {
294
+ findConfirm(tree).props.onPress();
295
+ await flush();
296
+ });
297
+ // The confirmed take's URL is the retry take's own path (the failed
298
+ // attempt never produced a file), not a fixed default.
299
+ const retryRec = recorderInstances[recorderInstances.length - 1] as {
300
+ startRecorder: jest.Mock;
301
+ };
302
+ expect(onComplete).toHaveBeenCalledWith(
303
+ `file://${retryRec.startRecorder.mock.calls[1][0]}`
304
+ );
305
+ });
306
+
307
+ it('shows the distinct mic-denied copy when the permission is refused', async () => {
308
+ const onComplete = jest.fn();
309
+ const tree = await ReactTestRenderer.act(async () => {
310
+ return render(onComplete);
311
+ });
312
+
313
+ const rendered = () => JSON.stringify(tree.toJSON());
314
+ expect(rendered()).not.toContain('posts.form.voiceNote.micError');
315
+
316
+ grantPermission = false;
317
+ const start = findButtons(tree)[0];
318
+ await ReactTestRenderer.act(async () => {
319
+ start.props.onPress();
320
+ await flush();
321
+ });
322
+
323
+ // The denied copy is shown (not the failed-start copy) and the take is
324
+ // unconfirmable — nothing was ever recorded.
325
+ expect(rendered()).toContain('posts.form.voiceNote.micError');
326
+ expect(rendered()).not.toContain('posts.form.voiceNote.micFailed');
327
+ expect(findConfirm(tree).props['data-disabled']).toBe(true);
328
+ expect(onComplete).not.toHaveBeenCalled();
329
+
330
+ // Granting later and starting again records normally.
331
+ grantPermission = true;
332
+ await ReactTestRenderer.act(async () => {
333
+ start.props.onPress();
334
+ await flush();
335
+ });
336
+ expect(rendered()).not.toContain('posts.form.voiceNote.micError');
337
+ const rec = recorderInstances[recorderInstances.length - 1] as {
338
+ startRecorder: jest.Mock;
339
+ };
340
+ expect(rec.startRecorder).toHaveBeenCalledTimes(1);
341
+ });
342
+
343
+ it('stops the in-flight take and cleans up when the sheet is dismissed while recording', async () => {
344
+ const onComplete = jest.fn();
345
+ const tree = await ReactTestRenderer.act(async () => {
346
+ jest.useFakeTimers();
347
+ return render(onComplete);
348
+ });
349
+
350
+ const start = findButtons(tree)[0];
351
+ await ReactTestRenderer.act(async () => {
352
+ start.props.onPress();
353
+ await flush();
354
+ });
355
+
356
+ // Swipe/back dismiss mid-take: the recorder must be stopped (mic released)
357
+ // before the state is cleared.
358
+ const baseSheet = tree.root.findAll((n) => n.type === 'BaseSheet')[0];
359
+ await ReactTestRenderer.act(async () => {
360
+ baseSheet.props.onDismiss();
361
+ await flush();
362
+ });
363
+
364
+ const rec = recorderInstances[recorderInstances.length - 1] as {
365
+ stopRecorder: jest.Mock;
366
+ };
367
+ expect(rec.stopRecorder).toHaveBeenCalledTimes(1);
368
+
369
+ // Back to the idle state: the take is gone, confirm is disabled, and
370
+ // nothing was handed to the composer.
371
+ expect(findConfirm(tree).props['data-disabled']).toBe(true);
372
+ expect(onComplete).not.toHaveBeenCalled();
373
+
374
+ // A fresh recording still works after the dismiss-reset.
375
+ const startAgain = findButtons(tree)[0];
376
+ await ReactTestRenderer.act(async () => {
377
+ startAgain.props.onPress();
378
+ await flush();
379
+ });
380
+ const rec2 = recorderInstances[recorderInstances.length - 1] as {
381
+ startRecorder: jest.Mock;
382
+ stopRecorder: jest.Mock;
383
+ };
384
+ expect(rec2.startRecorder).toHaveBeenCalledTimes(2);
385
+ });
386
+
387
+ it('writes each take to a distinct output path so a confirmed take cannot be overwritten', async () => {
388
+ const onComplete = jest.fn();
389
+ const tree = await ReactTestRenderer.act(async () => {
390
+ jest.useFakeTimers();
391
+ return render(onComplete);
392
+ });
393
+
394
+ const start = findButtons(tree)[0];
395
+ await ReactTestRenderer.act(async () => {
396
+ start.props.onPress();
397
+ await flush();
398
+ });
399
+
400
+ const rec = recorderInstances[recorderInstances.length - 1] as {
401
+ startRecorder: jest.Mock;
402
+ stopRecorder: jest.Mock;
403
+ startPlayer: jest.Mock;
404
+ };
405
+ // Each take gets its own cache file: the library default ("DEFAULT")
406
+ // resolves every take to one fixed name (sound.mp4/sound.m4a), so a
407
+ // second take would overwrite the bytes of a previously confirmed take
408
+ // before the picker's Done uploads them.
409
+ const path1 = rec.startRecorder.mock.calls[0][0];
410
+ expect(typeof path1).toBe('string');
411
+ expect(path1.startsWith('/tmp/caches/voice_')).toBe(true);
412
+ expect(path1.endsWith('.m4a')).toBe(true);
413
+
414
+ // Confirm take A, re-record, and start take B in the same sheet session.
415
+ const stop = findButtons(tree).find(
416
+ (b) => b.findAll((d) => d.type === 'SquareIcon').length > 0
417
+ );
418
+ expect(stop).toBeTruthy();
419
+ await ReactTestRenderer.act(async () => {
420
+ stop.props.onPress();
421
+ await flush();
422
+ });
423
+ await ReactTestRenderer.act(async () => {
424
+ findConfirm(tree).props.onPress();
425
+ await flush();
426
+ });
427
+ expect(onComplete).toHaveBeenCalledTimes(1);
428
+ // The confirmed take is take A's own file — the URL chain
429
+ // start → stop → confirm → onComplete hands over the recorded path.
430
+ expect(onComplete).toHaveBeenLastCalledWith(`file://${path1}`);
431
+
432
+ // The in-sheet preview plays take A's own file: startPlayer(uri) with a
433
+ // falsy uri falls back to the library's "DEFAULT", a fixed
434
+ // sound.mp4/sound.m4a that — with per-take filenames — never matches the
435
+ // confirmed take, so the preview would play the wrong audio or error.
436
+ const play = findButtons(tree).find(
437
+ (b) => b.findAll((d) => d.type === 'PlayIcon').length > 0
438
+ );
439
+ expect(play).toBeTruthy();
440
+ await ReactTestRenderer.act(async () => {
441
+ play.props.onPress();
442
+ await flush();
443
+ });
444
+ expect(rec.startPlayer).toHaveBeenCalledWith(`file://${path1}`);
445
+
446
+ const reRecord = findButtons(tree).find(
447
+ (b) => b.findAll((d) => d.type === 'Repeat2Icon').length > 0
448
+ );
449
+ expect(reRecord).toBeTruthy();
450
+ await ReactTestRenderer.act(async () => {
451
+ reRecord.props.onPress();
452
+ await flush();
453
+ });
454
+ const startAgain = findButtons(tree)[0];
455
+ await ReactTestRenderer.act(async () => {
456
+ startAgain.props.onPress();
457
+ await flush();
458
+ });
459
+
460
+ expect(rec.startRecorder).toHaveBeenCalledTimes(2);
461
+ const path2 = rec.startRecorder.mock.calls[1][0];
462
+ expect(path2).not.toBe(path1);
463
+ expect(path2.startsWith('/tmp/caches/voice_')).toBe(true);
464
+ expect(path2.endsWith('.m4a')).toBe(true);
465
+ });
466
+
467
+ it('re-record: discards the confirmed take (and stops its preview) and the fresh take is the one handed to onComplete', async () => {
468
+ // Round-16 review item 8: the web modal's Re-record flow had no RN
469
+ // counterpart. Re-record must drop the previous take entirely — its
470
+ // preview stops and it is not confirmable — and a take recorded after it
471
+ // is the only one handed to the composer.
472
+ const onComplete = jest.fn();
473
+ const tree = await ReactTestRenderer.act(async () => {
474
+ jest.useFakeTimers();
475
+ return render(onComplete);
476
+ });
477
+
478
+ // Record and stop take A; it is confirmable and its preview is playable.
479
+ const start = findButtons(tree)[0];
480
+ await ReactTestRenderer.act(async () => {
481
+ start.props.onPress();
482
+ await flush();
483
+ });
484
+ const stop = findButtons(tree).find(
485
+ (b) => b.findAll((d) => d.type === 'SquareIcon').length > 0
486
+ );
487
+ expect(stop).toBeTruthy();
488
+ await ReactTestRenderer.act(async () => {
489
+ stop.props.onPress();
490
+ await flush();
491
+ });
492
+ expect(findConfirm(tree).props['data-disabled']).toBe(false);
493
+
494
+ // Start the preview playback, then re-record: the in-flight preview must
495
+ // be stopped (a discarded take must not keep playing) and the take must
496
+ // be gone — confirm disabled, start visible again, re-record gone.
497
+ const play = findButtons(tree).find(
498
+ (b) => b.findAll((d) => d.type === 'PlayIcon').length > 0
499
+ );
500
+ expect(play).toBeTruthy();
501
+ await ReactTestRenderer.act(async () => {
502
+ play.props.onPress();
503
+ await flush();
504
+ });
505
+ const rec = recorderInstances[recorderInstances.length - 1] as {
506
+ startPlayer: jest.Mock;
507
+ stopPlayer: jest.Mock;
508
+ };
509
+ expect(rec.startPlayer).toHaveBeenCalledTimes(1);
510
+
511
+ const reRecord = findButtons(tree).find(
512
+ (b) => b.findAll((d) => d.type === 'Repeat2Icon').length > 0
513
+ );
514
+ expect(reRecord).toBeTruthy();
515
+ await ReactTestRenderer.act(async () => {
516
+ reRecord.props.onPress();
517
+ await flush();
518
+ });
519
+ expect(rec.stopPlayer).toHaveBeenCalled();
520
+ expect(findConfirm(tree).props['data-disabled']).toBe(true);
521
+ expect(
522
+ findButtons(tree).find(
523
+ (b) => b.findAll((d) => d.type === 'Repeat2Icon').length > 0
524
+ )
525
+ ).toBeFalsy();
526
+ expect(onComplete).not.toHaveBeenCalled();
527
+
528
+ // A fresh take (B) is recorded, and it — not the discarded take A — is
529
+ // the one handed to the composer.
530
+ const startAgain = findButtons(tree)[0];
531
+ await ReactTestRenderer.act(async () => {
532
+ startAgain.props.onPress();
533
+ await flush();
534
+ });
535
+ const stopAgain = findButtons(tree).find(
536
+ (b) => b.findAll((d) => d.type === 'SquareIcon').length > 0
537
+ );
538
+ expect(stopAgain).toBeTruthy();
539
+ await ReactTestRenderer.act(async () => {
540
+ stopAgain.props.onPress();
541
+ await flush();
542
+ });
543
+ expect(findConfirm(tree).props['data-disabled']).toBe(false);
544
+ await ReactTestRenderer.act(async () => {
545
+ findConfirm(tree).props.onPress();
546
+ await flush();
547
+ });
548
+ expect(onComplete).toHaveBeenCalledTimes(1);
549
+ const pathB = rec.startRecorder.mock.calls[1][0];
550
+ expect(onComplete).toHaveBeenLastCalledWith(`file://${pathB}`);
551
+ });
552
+
553
+ it('flips back to play when the take finishes naturally (isFinished), like the picker sheet', async () => {
554
+ // Round-20 review item 2: the record sheet's *own* preview had no
555
+ // end-of-playback transition — the play loop is the same one the picker
556
+ // sheet drives off `isFinished`, so a take that ends on its own must tear
557
+ // down the player and reset the play state (one tap replays) instead of
558
+ // leaving the button stuck "stop-like" and the `{playTime} / {recordTime}`
559
+ // row frozen at the take's end values.
560
+ const onComplete = jest.fn();
561
+ const tree = await ReactTestRenderer.act(async () => {
562
+ jest.useFakeTimers();
563
+ return render(onComplete);
564
+ });
565
+
566
+ // Record and stop a take so the preview section (and play button) render.
567
+ const start = findButtons(tree)[0];
568
+ await ReactTestRenderer.act(async () => {
569
+ start.props.onPress();
570
+ await flush();
571
+ });
572
+ const stop = findButtons(tree).find(
573
+ (b) => b.findAll((d) => d.type === 'SquareIcon').length > 0
574
+ );
575
+ expect(stop).toBeTruthy();
576
+ await ReactTestRenderer.act(async () => {
577
+ stop.props.onPress();
578
+ await flush();
579
+ });
580
+ expect(findConfirm(tree).props['data-disabled']).toBe(false);
581
+
582
+ // Start the preview playback; the listener is registered on the recorder.
583
+ const play = findButtons(tree).find(
584
+ (b) => b.findAll((d) => d.type === 'PlayIcon').length > 0
585
+ );
586
+ expect(play).toBeTruthy();
587
+ await ReactTestRenderer.act(async () => {
588
+ play.props.onPress();
589
+ await flush();
590
+ });
591
+ const rec = recorderInstances[recorderInstances.length - 1] as {
592
+ startPlayer: jest.Mock;
593
+ stopPlayer: jest.Mock;
594
+ playListener?: (e: {
595
+ isFinished: boolean;
596
+ currentPosition: number;
597
+ duration: number;
598
+ }) => void;
599
+ };
600
+ expect(rec.startPlayer).toHaveBeenCalledTimes(1);
601
+
602
+ // The player reports the natural end of the take via `isFinished`.
603
+ expect(typeof rec.playListener).toBe('function');
604
+ await ReactTestRenderer.act(async () => {
605
+ rec.playListener!({
606
+ isFinished: true,
607
+ currentPosition: 10000,
608
+ duration: 10000,
609
+ });
610
+ await flush();
611
+ });
612
+
613
+ // The take stopped and the play state reset — the control is back to Play
614
+ // and one tap replays it (a fresh startPlayer on the same recorder).
615
+ expect(rec.stopPlayer).toHaveBeenCalled();
616
+ const playAgain = findButtons(tree).find(
617
+ (b) => b.findAll((d) => d.type === 'PlayIcon').length > 0
618
+ );
619
+ expect(playAgain).toBeTruthy();
620
+ await ReactTestRenderer.act(async () => {
621
+ playAgain.props.onPress();
622
+ await flush();
623
+ });
624
+ expect(rec.startPlayer).toHaveBeenCalledTimes(2);
625
+ });
626
+ });