@blotch/angular-lynx-testing-library 22.3.1 → 22.3.3

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 (69) hide show
  1. package/dist/app-state.d.ts +34 -0
  2. package/dist/app-state.d.ts.map +1 -0
  3. package/dist/app-state.js +33 -0
  4. package/dist/apple-sign-in.d.ts +116 -0
  5. package/dist/apple-sign-in.d.ts.map +1 -0
  6. package/dist/apple-sign-in.js +179 -0
  7. package/dist/audio-recorder.d.ts +95 -0
  8. package/dist/audio-recorder.d.ts.map +1 -0
  9. package/dist/audio-recorder.js +250 -0
  10. package/dist/back-button.d.ts +43 -0
  11. package/dist/back-button.d.ts.map +1 -0
  12. package/dist/back-button.js +77 -0
  13. package/dist/barcode-scanner.d.ts +59 -0
  14. package/dist/barcode-scanner.d.ts.map +1 -0
  15. package/dist/barcode-scanner.js +100 -0
  16. package/dist/biometrics.d.ts +79 -0
  17. package/dist/biometrics.d.ts.map +1 -0
  18. package/dist/biometrics.js +151 -0
  19. package/dist/bluetooth.d.ts +121 -0
  20. package/dist/bluetooth.d.ts.map +1 -0
  21. package/dist/bluetooth.js +369 -0
  22. package/dist/calendar.d.ts +68 -0
  23. package/dist/calendar.d.ts.map +1 -0
  24. package/dist/calendar.js +120 -0
  25. package/dist/file-picker.d.ts +76 -0
  26. package/dist/file-picker.d.ts.map +1 -0
  27. package/dist/file-picker.js +125 -0
  28. package/dist/fire-event.d.ts.map +1 -1
  29. package/dist/fire-event.js +58 -8
  30. package/dist/geolocation.d.ts +94 -0
  31. package/dist/geolocation.d.ts.map +1 -0
  32. package/dist/geolocation.js +255 -0
  33. package/dist/global-props.d.ts +17 -0
  34. package/dist/global-props.d.ts.map +1 -0
  35. package/dist/global-props.js +35 -0
  36. package/dist/image-picker.d.ts +84 -0
  37. package/dist/image-picker.d.ts.map +1 -0
  38. package/dist/image-picker.js +140 -0
  39. package/dist/index.d.ts +35 -0
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +19 -0
  42. package/dist/motion.d.ts +90 -0
  43. package/dist/motion.d.ts.map +1 -0
  44. package/dist/motion.js +169 -0
  45. package/dist/network.d.ts +26 -0
  46. package/dist/network.d.ts.map +1 -0
  47. package/dist/network.js +31 -0
  48. package/dist/nfc.d.ts +85 -0
  49. package/dist/nfc.d.ts.map +1 -0
  50. package/dist/nfc.js +253 -0
  51. package/dist/photo-library.d.ts +58 -0
  52. package/dist/photo-library.d.ts.map +1 -0
  53. package/dist/photo-library.js +91 -0
  54. package/dist/render.d.ts.map +1 -1
  55. package/dist/render.js +19 -1
  56. package/dist/setup.js +86 -7
  57. package/dist/share-target.d.ts +56 -0
  58. package/dist/share-target.d.ts.map +1 -0
  59. package/dist/share-target.js +92 -0
  60. package/dist/store-review.d.ts +30 -0
  61. package/dist/store-review.d.ts.map +1 -0
  62. package/dist/store-review.js +49 -0
  63. package/dist/tracking.d.ts +60 -0
  64. package/dist/tracking.d.ts.map +1 -0
  65. package/dist/tracking.js +94 -0
  66. package/dist/wifi.d.ts +77 -0
  67. package/dist/wifi.d.ts.map +1 -0
  68. package/dist/wifi.js +148 -0
  69. package/package.json +3 -3
@@ -0,0 +1,250 @@
1
+ /** What `LynxAudioRecorder` calls and hears, named as the shells name them. */
2
+ const MODULE = 'AngularLynxAudioRecorder';
3
+ const EVENT = 'angularLynxAudioRecorder';
4
+ /**
5
+ * What a recording holds when a spec doesn't say: a few bytes, which is all
6
+ * an app's code sees of a clip it uploads or shares.
7
+ */
8
+ const DEFAULT_BYTES = new Uint8Array([0, 0, 0, 24, 102, 116, 121, 112]);
9
+ const toBase64 = (bytes) => {
10
+ let binary = '';
11
+ for (const byte of bytes)
12
+ binary += String.fromCharCode(byte);
13
+ return btoa(binary);
14
+ };
15
+ const failure = (error, message) => ({ error, message });
16
+ const environment = () => globalThis.lynxTestingEnv;
17
+ /**
18
+ * Sends one of the shells' global events from the background thread, where
19
+ * they arrive, and comes back to the main thread, where specs run.
20
+ */
21
+ const emit = (payload) => {
22
+ const env = environment();
23
+ env.switchToBackgroundThread();
24
+ try {
25
+ env.backgroundThread.globalThis
26
+ .lynx.getJSModule('GlobalEventEmitter')
27
+ .emit(EVENT, [payload]);
28
+ }
29
+ finally {
30
+ env.switchToMainThread();
31
+ }
32
+ };
33
+ const elapsed = (open) => open.recorded + (open.since === undefined ? 0 : Date.now() - open.since);
34
+ /**
35
+ * Gives the environment's background thread an `AngularLynxAudioRecorder`
36
+ * module, as the apps `ng build --platform` makes with
37
+ * `permissions.microphone` have. Its person hasn't been asked yet: the first
38
+ * start asks, and they allow it, unless {@link setMicrophonePermission} says
39
+ * otherwise. Each recording holds a few bytes, or what
40
+ * {@link setRecordedAudio} says. `LynxAudioRecorder` reaches it through the
41
+ * background thread, as it reaches the shells' module, so specs see the
42
+ * runtime's own behavior.
43
+ *
44
+ * It runs each time the environment injects the background thread's globals:
45
+ * when it's created, and on the reset after each test, so each test starts
46
+ * afresh. Only `NativeModules.AngularLynxAudioRecorder` is added.
47
+ */
48
+ export const installAudioRecorderModule = (threadGlobals) => {
49
+ /**
50
+ * Stops the open recording, and describes the clip it made.
51
+ */
52
+ const finish = () => {
53
+ const open = module.open;
54
+ if (!open)
55
+ return undefined;
56
+ module.open = undefined;
57
+ const bytes = module.audio.bytes ?? DEFAULT_BYTES;
58
+ const mimeType = module.audio.mimeType ?? 'audio/mp4';
59
+ const name = `recording-${++module.clips}.${mimeType === 'audio/mp4' ? 'm4a' : 'audio'}`;
60
+ const uri = `file:///fake/recordings/${name}`;
61
+ // A copy, so a spec changing its array afterwards can't change this clip.
62
+ // Not slice(): a Buffer's slice() shares its memory.
63
+ module.files.set(uri, new Uint8Array(bytes));
64
+ return {
65
+ uri,
66
+ name,
67
+ durationMs: module.audio.durationMs ?? Math.round(elapsed(open)),
68
+ mimeType,
69
+ size: bytes.length,
70
+ };
71
+ };
72
+ const module = {
73
+ permissionState: 'undetermined',
74
+ audio: {},
75
+ open: undefined,
76
+ ended: undefined,
77
+ clips: 0,
78
+ files: new Map(),
79
+ permission: (done) => done({ permission: module.permissionState }),
80
+ requestPermission: (done) => {
81
+ // The person allows it when asked, as most do.
82
+ if (module.permissionState === 'undetermined') {
83
+ module.permissionState = 'granted';
84
+ }
85
+ done({ permission: module.permissionState });
86
+ },
87
+ start: (options, done) => {
88
+ module.requestPermission(() => { });
89
+ if (module.permissionState === 'unsupported') {
90
+ done(failure('unavailable', "The fake phone can't record."));
91
+ return;
92
+ }
93
+ if (module.permissionState === 'denied') {
94
+ done(failure('denied', "The person didn't allow the microphone."));
95
+ return;
96
+ }
97
+ module.open = {
98
+ id: options.recording,
99
+ ...(options.maxDuration !== undefined && {
100
+ maxDuration: options.maxDuration,
101
+ }),
102
+ recorded: 0,
103
+ since: Date.now(),
104
+ };
105
+ module.ended = undefined;
106
+ done({});
107
+ },
108
+ // The runtime pauses only a recording that records, and resumes only
109
+ // one that's paused, and refuses a second start itself.
110
+ pause: (done) => {
111
+ const open = module.open;
112
+ open.recorded = elapsed(open);
113
+ open.since = undefined;
114
+ done({});
115
+ },
116
+ resume: (done) => {
117
+ module.open.since = Date.now();
118
+ done({});
119
+ },
120
+ stop: (done) => {
121
+ const clip = finish() ?? module.ended;
122
+ done(clip ? { clip } : failure('failed', 'Nothing is recording.'));
123
+ },
124
+ cancel: (done) => {
125
+ module.open = undefined;
126
+ module.ended = undefined;
127
+ done({});
128
+ },
129
+ read: ({ uri }, done) => {
130
+ const bytes = module.files.get(uri);
131
+ done(bytes
132
+ ? { base64: toBase64(bytes) }
133
+ : failure('failed', "The recording couldn't be read."));
134
+ },
135
+ };
136
+ threadGlobals.NativeModules = {
137
+ ...threadGlobals.NativeModules,
138
+ [MODULE]: module,
139
+ };
140
+ };
141
+ const fakeModule = (caller) => {
142
+ // Read through the environment rather than kept here: under `ng test`, the
143
+ // setup file and the specs can load different copies of this module.
144
+ const module = environment()?.backgroundThread.globalThis.NativeModules?.[MODULE];
145
+ if (!module) {
146
+ throw new Error(`${caller}() needs the testing library's environment, which its setup file installs.`);
147
+ }
148
+ return module;
149
+ };
150
+ /**
151
+ * Sets whether the fake phone lets the app use its microphone: what
152
+ * `LynxAudioRecorder.permission` reads, and what `start()` finds. `denied`
153
+ * has `start()` reject with `denied`, and `unsupported` with `unavailable`,
154
+ * as on a phone without the module. `undetermined`, where each test starts,
155
+ * has the next start ask and the person allow it.
156
+ *
157
+ * Set it before the component that injects `LynxAudioRecorder` renders: the
158
+ * service reads the permission when it's created, and each time the app
159
+ * comes back to the foreground.
160
+ */
161
+ export const setMicrophonePermission = (permission) => {
162
+ fakeModule('setMicrophonePermission').permissionState = permission;
163
+ };
164
+ /**
165
+ * Sets what the fake phone's recordings hold from now on: the bytes a
166
+ * clip's `blob()` resolves with, its type and its length.
167
+ *
168
+ * @example
169
+ * ```ts
170
+ * setRecordedAudio({ bytes: voiceNote, durationMs: 4200 });
171
+ * await render(VoiceNote);
172
+ * fireEvent.tap(screen.getByText('Record'));
173
+ * await waitForUpdate();
174
+ * fireEvent.tap(screen.getByText('Stop'));
175
+ * expect(await screen.findByText('0:04')).toBeInTheDocument();
176
+ * ```
177
+ */
178
+ export const setRecordedAudio = (audio) => {
179
+ fakeModule('setRecordedAudio').audio = audio;
180
+ };
181
+ /**
182
+ * The recording the app has open, or undefined while none is, such as
183
+ * before `start()` and after `stop()`.
184
+ */
185
+ export const audioRecording = () => {
186
+ const open = fakeModule('audioRecording').open;
187
+ if (!open)
188
+ return undefined;
189
+ return {
190
+ state: open.since === undefined ? 'paused' : 'recording',
191
+ ...(open.maxDuration !== undefined && { maxDuration: open.maxDuration }),
192
+ };
193
+ };
194
+ /**
195
+ * Sends the open recording's level (0..1) and its length so far by the
196
+ * clock, as the shells do about ten times a second. Once it's as long as its
197
+ * `maxDuration`, it ends instead, as the shells end it, and the app's
198
+ * `stop()` resolves with the clip. Does nothing while none records.
199
+ */
200
+ export const emitAudioLevel = (level) => {
201
+ const module = fakeModule('emitAudioLevel');
202
+ const open = module.open;
203
+ if (!open || open.since === undefined)
204
+ return;
205
+ const durationMs = Math.round(elapsed(open));
206
+ if (open.maxDuration !== undefined && durationMs >= open.maxDuration) {
207
+ module.ended = finishOpen(module);
208
+ emit({ type: 'ended', recording: open.id, clip: module.ended });
209
+ return;
210
+ }
211
+ emit({ type: 'level', recording: open.id, level, durationMs });
212
+ };
213
+ /**
214
+ * Pauses the open recording as the system does for a phone call or the app
215
+ * going to the background: the app's recording turns `paused`, and its
216
+ * `resume()` carries on. Does nothing while none records.
217
+ */
218
+ export const interruptAudioRecording = () => {
219
+ const module = fakeModule('interruptAudioRecording');
220
+ const open = module.open;
221
+ if (!open || open.since === undefined)
222
+ return;
223
+ open.recorded = elapsed(open);
224
+ open.since = undefined;
225
+ emit({ type: 'paused', recording: open.id });
226
+ };
227
+ /**
228
+ * Ends the open recording with an error, as the shells do when the
229
+ * microphone fails: the app's recording turns `stopped`, its `error` holds
230
+ * an `AudioRecorderError` with code `failed` and `message`, and its `stop()`
231
+ * rejects with it. Does nothing while none is open.
232
+ */
233
+ export const failAudioRecording = (message = 'The microphone failed.') => {
234
+ const module = fakeModule('failAudioRecording');
235
+ const open = module.open;
236
+ if (!open)
237
+ return;
238
+ module.open = undefined;
239
+ emit({ type: 'ended', recording: open.id, error: 'failed', message });
240
+ };
241
+ /**
242
+ * The open recording's clip, through the module's own stop.
243
+ */
244
+ const finishOpen = (module) => {
245
+ let clip;
246
+ module.stop((answer) => {
247
+ clip = answer.clip;
248
+ });
249
+ return clip;
250
+ };
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Gives the environment's background thread an `AngularLynxShell` module with
3
+ * the Android shell's `setBackHandlerEnabled()`, so that {@link
4
+ * pressBackButton} knows whether the app takes back, as Android does.
5
+ * `LynxBackButton` reaches it through the background thread, as it reaches
6
+ * the shell's module, so specs see the runtime's own behavior.
7
+ *
8
+ * It runs each time the environment injects the background thread's globals:
9
+ * when it's created, and on the reset after each test, so each test starts
10
+ * with the handler off, as an app does. Only `NativeModules.AngularLynxShell`
11
+ * is added: every other module stays as it was.
12
+ */
13
+ export declare const installShellModule: (threadGlobals: {
14
+ NativeModules?: Record<string, unknown>;
15
+ }) => void;
16
+ /**
17
+ * Presses Android's back button, so a spec can check what the app does with
18
+ * it: close what's open on top (a handler registered with
19
+ * `LynxBackButton.on()`), or go back a screen through the router.
20
+ *
21
+ * As on Android, the press reaches the app only while the app has said it
22
+ * takes back. Otherwise Android would leave the app, and nothing reaches it:
23
+ * the call returns `false` then. The press reaches the app as it does on a
24
+ * phone, through the background thread: wait for it with `waitForUpdate()`.
25
+ *
26
+ * An `<overlay>` takes back before the app does, on a phone: fire its
27
+ * `requestclose` event for that instead.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * await render(Search);
32
+ * fireEvent.tap(screen.getByText('Search'));
33
+ * await waitForUpdate();
34
+ * expect(pressBackButton()).toBe(true);
35
+ * await waitForUpdate();
36
+ * expect(screen.queryByText('Results')).toBeNull();
37
+ * ```
38
+ *
39
+ * @returns Whether the app took the press: `false` when Android would have
40
+ * left the app.
41
+ */
42
+ export declare const pressBackButton: () => boolean;
43
+ //# sourceMappingURL=back-button.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"back-button.d.ts","sourceRoot":"","sources":["../src/back-button.ts"],"names":[],"mappings":"AAwBA;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,kBAAkB,GAAI,eAAe;IAChD,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACzC,KAAG,IAWH,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,eAAe,QAAO,OA2BlC,CAAC"}
@@ -0,0 +1,77 @@
1
+ /** What `LynxBackButton` calls and hears, named as the Android shell names them. */
2
+ const MODULE = 'AngularLynxShell';
3
+ const EVENT = 'angularLynxBack';
4
+ /**
5
+ * Gives the environment's background thread an `AngularLynxShell` module with
6
+ * the Android shell's `setBackHandlerEnabled()`, so that {@link
7
+ * pressBackButton} knows whether the app takes back, as Android does.
8
+ * `LynxBackButton` reaches it through the background thread, as it reaches
9
+ * the shell's module, so specs see the runtime's own behavior.
10
+ *
11
+ * It runs each time the environment injects the background thread's globals:
12
+ * when it's created, and on the reset after each test, so each test starts
13
+ * with the handler off, as an app does. Only `NativeModules.AngularLynxShell`
14
+ * is added: every other module stays as it was.
15
+ */
16
+ export const installShellModule = (threadGlobals) => {
17
+ const module = {
18
+ enabled: false,
19
+ setBackHandlerEnabled(enabled) {
20
+ module.enabled = enabled;
21
+ },
22
+ };
23
+ threadGlobals.NativeModules = {
24
+ ...threadGlobals.NativeModules,
25
+ [MODULE]: module,
26
+ };
27
+ };
28
+ /**
29
+ * Presses Android's back button, so a spec can check what the app does with
30
+ * it: close what's open on top (a handler registered with
31
+ * `LynxBackButton.on()`), or go back a screen through the router.
32
+ *
33
+ * As on Android, the press reaches the app only while the app has said it
34
+ * takes back. Otherwise Android would leave the app, and nothing reaches it:
35
+ * the call returns `false` then. The press reaches the app as it does on a
36
+ * phone, through the background thread: wait for it with `waitForUpdate()`.
37
+ *
38
+ * An `<overlay>` takes back before the app does, on a phone: fire its
39
+ * `requestclose` event for that instead.
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * await render(Search);
44
+ * fireEvent.tap(screen.getByText('Search'));
45
+ * await waitForUpdate();
46
+ * expect(pressBackButton()).toBe(true);
47
+ * await waitForUpdate();
48
+ * expect(screen.queryByText('Results')).toBeNull();
49
+ * ```
50
+ *
51
+ * @returns Whether the app took the press: `false` when Android would have
52
+ * left the app.
53
+ */
54
+ export const pressBackButton = () => {
55
+ // Read through the environment rather than kept here: under `ng test`, the
56
+ // setup file and the specs can load different copies of this module.
57
+ const env = globalThis.lynxTestingEnv;
58
+ const background = env?.backgroundThread.globalThis;
59
+ const module = background?.NativeModules?.[MODULE];
60
+ if (!env || !module) {
61
+ throw new Error("pressBackButton() needs the testing library's environment, which its setup file installs.");
62
+ }
63
+ if (!module.enabled)
64
+ return false;
65
+ // What the shell sends while its handler is enabled. A global event
66
+ // arrives on the background thread, and the listener there calls the
67
+ // app's on the main thread through that thread's `lynx`, so the event is
68
+ // sent from it. Specs run on the main thread, which comes back afterwards.
69
+ env.switchToBackgroundThread();
70
+ try {
71
+ background?.lynx?.getJSModule?.('GlobalEventEmitter')?.emit(EVENT);
72
+ }
73
+ finally {
74
+ env.switchToMainThread();
75
+ }
76
+ return true;
77
+ };
@@ -0,0 +1,59 @@
1
+ /** A code the fake scanner reads, as `LynxBarcodeScanner.scan()` resolves. */
2
+ export type QueuedBarcodeScan = {
3
+ /** The kind of code, one of `@blotch/angular-lynx`'s `BarcodeFormat`s. */
4
+ readonly format: string;
5
+ /** What the code holds. */
6
+ readonly value: string;
7
+ };
8
+ /** Why a scan read no code, as `BarcodeScannerError`'s `code` names it. */
9
+ export type BarcodeScanFailure = 'unavailable' | 'denied' | 'failed';
10
+ /**
11
+ * Gives the environment's background thread an `AngularLynxBarcodeScanner`
12
+ * module, as the apps `ng build --platform` makes with `permissions.camera`
13
+ * have. There's no camera to point in a spec, so a scan stays open until
14
+ * {@link queueBarcodeScan}, {@link cancelBarcodeScan} or
15
+ * {@link failBarcodeScan} answers it, or ends at once with what one of them
16
+ * queued before it opened. `LynxBarcodeScanner` reaches it through the
17
+ * background thread, as it reaches the shells' module, so specs see the
18
+ * runtime's own behavior.
19
+ *
20
+ * It runs each time the environment injects the background thread's globals:
21
+ * when it's created, and on the reset after each test, so each test starts
22
+ * with no scan open and nothing queued. Only
23
+ * `NativeModules.AngularLynxBarcodeScanner` is added: every other module
24
+ * stays as it was.
25
+ */
26
+ export declare const installBarcodeScannerModule: (threadGlobals: {
27
+ NativeModules?: Record<string, unknown>;
28
+ }) => void;
29
+ /**
30
+ * Has the scanner read `scan`, as if the person pointed the camera at it: the
31
+ * open `LynxBarcodeScanner.scan()` resolves with it, or, when none is open,
32
+ * the next one does as soon as it's called. Each call answers one scan, in
33
+ * order. The answer reaches the app through the background thread, as on a
34
+ * phone: wait for it with `waitForUpdate()`.
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * await render(CheckIn);
39
+ * queueBarcodeScan({ format: 'qr', value: 'TICKET-42' });
40
+ * fireEvent.tap(screen.getByText('Scan ticket'));
41
+ * await waitForUpdate();
42
+ * expect(screen.getByText('Welcome, ticket 42')).toBeInTheDocument();
43
+ * ```
44
+ */
45
+ export declare const queueBarcodeScan: (scan: QueuedBarcodeScan) => void;
46
+ /**
47
+ * Closes the scanner without a code, as the person does with Cancel: the open
48
+ * `LynxBarcodeScanner.scan()`, or the next one, rejects with a
49
+ * `BarcodeScannerError` whose code is `canceled`.
50
+ */
51
+ export declare const cancelBarcodeScan: () => void;
52
+ /**
53
+ * Ends the open `LynxBarcodeScanner.scan()`, or the next one, with a
54
+ * `BarcodeScannerError` of `code`: `denied` for a person who doesn't let the
55
+ * app use the camera, `unavailable` for a phone without a scanner, or
56
+ * `failed`.
57
+ */
58
+ export declare const failBarcodeScan: (code: BarcodeScanFailure, message?: string) => void;
59
+ //# sourceMappingURL=barcode-scanner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"barcode-scanner.d.ts","sourceRoot":"","sources":["../src/barcode-scanner.ts"],"names":[],"mappings":"AAGA,8EAA8E;AAC9E,MAAM,MAAM,iBAAiB,GAAG;IAC9B,0EAA0E;IAC1E,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,2BAA2B;IAC3B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,2EAA2E;AAC3E,MAAM,MAAM,kBAAkB,GAAG,aAAa,GAAG,QAAQ,GAAG,QAAQ,CAAC;AA0BrE;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,2BAA2B,GAAI,eAAe;IACzD,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACzC,KAAG,IAqBH,CAAC;AAgCF;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,gBAAgB,GAAI,MAAM,iBAAiB,KAAG,IACa,CAAC;AAEzE;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,QAAO,IAIjC,CAAC;AAEL;;;;;GAKG;AACH,eAAO,MAAM,eAAe,GAC1B,MAAM,kBAAkB,EACxB,UAAU,MAAM,KACf,IAIC,CAAC"}
@@ -0,0 +1,100 @@
1
+ /** What `LynxBarcodeScanner` calls, named as the shells name their module. */
2
+ const MODULE = 'AngularLynxBarcodeScanner';
3
+ /**
4
+ * Gives the environment's background thread an `AngularLynxBarcodeScanner`
5
+ * module, as the apps `ng build --platform` makes with `permissions.camera`
6
+ * have. There's no camera to point in a spec, so a scan stays open until
7
+ * {@link queueBarcodeScan}, {@link cancelBarcodeScan} or
8
+ * {@link failBarcodeScan} answers it, or ends at once with what one of them
9
+ * queued before it opened. `LynxBarcodeScanner` reaches it through the
10
+ * background thread, as it reaches the shells' module, so specs see the
11
+ * runtime's own behavior.
12
+ *
13
+ * It runs each time the environment injects the background thread's globals:
14
+ * when it's created, and on the reset after each test, so each test starts
15
+ * with no scan open and nothing queued. Only
16
+ * `NativeModules.AngularLynxBarcodeScanner` is added: every other module
17
+ * stays as it was.
18
+ */
19
+ export const installBarcodeScannerModule = (threadGlobals) => {
20
+ const module = {
21
+ queued: [],
22
+ open: undefined,
23
+ scan(_request, done) {
24
+ if (this.open) {
25
+ done({ error: 'failed', message: 'A scan is already open.' });
26
+ return;
27
+ }
28
+ const answer = this.queued.shift();
29
+ if (answer) {
30
+ done(answer);
31
+ return;
32
+ }
33
+ this.open = done;
34
+ },
35
+ };
36
+ threadGlobals.NativeModules = {
37
+ ...threadGlobals.NativeModules,
38
+ [MODULE]: module,
39
+ };
40
+ };
41
+ const answer = (name, result) => {
42
+ // Read through the environment rather than kept here: under `ng test`, the
43
+ // setup file and the specs can load different copies of this module.
44
+ const env = globalThis.lynxTestingEnv;
45
+ const module = env?.backgroundThread.globalThis.NativeModules?.[MODULE];
46
+ if (!env || !module) {
47
+ throw new Error(`${name}() needs the testing library's environment, which its setup file installs.`);
48
+ }
49
+ const open = module.open;
50
+ if (!open) {
51
+ module.queued.push(result);
52
+ return;
53
+ }
54
+ module.open = undefined;
55
+ // The shells call back on the background thread, which relays the answer
56
+ // to the main thread, where specs run: it comes back afterwards.
57
+ env.switchToBackgroundThread();
58
+ try {
59
+ open(result);
60
+ }
61
+ finally {
62
+ env.switchToMainThread();
63
+ }
64
+ };
65
+ /**
66
+ * Has the scanner read `scan`, as if the person pointed the camera at it: the
67
+ * open `LynxBarcodeScanner.scan()` resolves with it, or, when none is open,
68
+ * the next one does as soon as it's called. Each call answers one scan, in
69
+ * order. The answer reaches the app through the background thread, as on a
70
+ * phone: wait for it with `waitForUpdate()`.
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * await render(CheckIn);
75
+ * queueBarcodeScan({ format: 'qr', value: 'TICKET-42' });
76
+ * fireEvent.tap(screen.getByText('Scan ticket'));
77
+ * await waitForUpdate();
78
+ * expect(screen.getByText('Welcome, ticket 42')).toBeInTheDocument();
79
+ * ```
80
+ */
81
+ export const queueBarcodeScan = (scan) => answer('queueBarcodeScan', { format: scan.format, value: scan.value });
82
+ /**
83
+ * Closes the scanner without a code, as the person does with Cancel: the open
84
+ * `LynxBarcodeScanner.scan()`, or the next one, rejects with a
85
+ * `BarcodeScannerError` whose code is `canceled`.
86
+ */
87
+ export const cancelBarcodeScan = () => answer('cancelBarcodeScan', {
88
+ error: 'canceled',
89
+ message: 'The scan was canceled.',
90
+ });
91
+ /**
92
+ * Ends the open `LynxBarcodeScanner.scan()`, or the next one, with a
93
+ * `BarcodeScannerError` of `code`: `denied` for a person who doesn't let the
94
+ * app use the camera, `unavailable` for a phone without a scanner, or
95
+ * `failed`.
96
+ */
97
+ export const failBarcodeScan = (code, message) => answer('failBarcodeScan', {
98
+ error: code,
99
+ ...(message !== undefined && { message }),
100
+ });
@@ -0,0 +1,79 @@
1
+ import type { BiometricsErrorCode, BiometryType } from '@blotch/angular-lynx';
2
+ /** The phone {@link setBiometrics} describes. */
3
+ export type FakeBiometrics = {
4
+ /** The sensor, `faceID` unless it's set. `none` for a phone without one. */
5
+ readonly type?: BiometryType;
6
+ /** Whether a face or a finger is set up, true unless it's set. */
7
+ readonly enrolled?: boolean;
8
+ /** Whether the phone has a passcode, true unless it's set. */
9
+ readonly deviceCredential?: boolean;
10
+ };
11
+ /**
12
+ * How the next prompt ends, as {@link queueBiometricResult} queues it: the
13
+ * person confirms (`success`), or the error `LynxBiometrics` rejects with.
14
+ */
15
+ export type BiometricResult = 'success' | BiometricsErrorCode;
16
+ /** A prompt the app showed, as {@link biometricPrompts} lists it. */
17
+ export type BiometricPrompt = {
18
+ /** The reason it gave, or `''` for a guarded read that gave none. */
19
+ readonly reason: string;
20
+ /** Whether it let the person use the passcode instead. */
21
+ readonly allowDeviceCredential: boolean;
22
+ };
23
+ /**
24
+ * Gives the environment's background thread an `AngularLynxBiometrics`
25
+ * module, as the apps `ng build --platform` makes with
26
+ * `permissions.biometrics` have. It stands for a phone with Face ID set up
27
+ * (see {@link setBiometrics}), where each prompt succeeds unless a spec
28
+ * queues another outcome ({@link queueBiometricResult}), and it keeps the
29
+ * values `LynxSecureStorage` stores with `requireBiometrics`. The runtime
30
+ * reaches it through the background thread, as it reaches the shells'
31
+ * module, so specs see the runtime's own behavior.
32
+ *
33
+ * It runs each time the environment injects the background thread's
34
+ * globals: when it's created, and on the reset after each test, so each test
35
+ * starts with that phone, nothing queued and nothing stored.
36
+ */
37
+ export declare const installBiometricsModule: (threadGlobals: {
38
+ NativeModules?: Record<string, unknown>;
39
+ }) => void;
40
+ /**
41
+ * Describes the phone `LynxBiometrics` sees in this test: its sensor,
42
+ * whether a face or a finger is set up, and whether it has a passcode. Each
43
+ * test starts with Face ID set up behind a passcode; what isn't given keeps
44
+ * that value. `{ type: 'none' }` is a phone without a sensor, or an app
45
+ * without `permissions.biometrics`: prompts then reject with `unavailable`,
46
+ * as does `LynxSecureStorage`'s `requireBiometrics`.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * setBiometrics({ type: 'fingerprint', enrolled: false });
51
+ * await render(Unlock);
52
+ * expect(screen.getByText('Set up a fingerprint to unlock')).toBeInTheDocument();
53
+ * ```
54
+ */
55
+ export declare const setBiometrics: (phone: FakeBiometrics) => void;
56
+ /**
57
+ * Has the next biometric prompt end with `result`: the person confirms
58
+ * (`success`), or `LynxBiometrics.authenticate()`, or a `LynxSecureStorage`
59
+ * read of a value kept behind biometrics, rejects with a `BiometricsError`
60
+ * of that code. Results queue in order, one per prompt; once they run out,
61
+ * prompts succeed. A prompt the phone can't show (see {@link setBiometrics})
62
+ * takes none.
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * queueBiometricResult('canceled');
67
+ * fireEvent.tap(screen.getByText('Unlock'));
68
+ * await waitForUpdate();
69
+ * expect(screen.getByText('Still locked')).toBeInTheDocument();
70
+ * ```
71
+ */
72
+ export declare const queueBiometricResult: (result: BiometricResult) => void;
73
+ /**
74
+ * The biometric prompts the app has shown in this test, oldest first: each
75
+ * `authenticate()`, and each read of a value kept behind biometrics. Each
76
+ * test starts with none.
77
+ */
78
+ export declare const biometricPrompts: () => readonly BiometricPrompt[];
79
+ //# sourceMappingURL=biometrics.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"biometrics.d.ts","sourceRoot":"","sources":["../src/biometrics.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAK9E,iDAAiD;AACjD,MAAM,MAAM,cAAc,GAAG;IAC3B,4EAA4E;IAC5E,QAAQ,CAAC,IAAI,CAAC,EAAE,YAAY,CAAC;IAC7B,kEAAkE;IAClE,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;IAC5B,8DAA8D;IAC9D,QAAQ,CAAC,gBAAgB,CAAC,EAAE,OAAO,CAAC;CACrC,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,eAAe,GAAG,SAAS,GAAG,mBAAmB,CAAC;AAE9D,qEAAqE;AACrE,MAAM,MAAM,eAAe,GAAG;IAC5B,qEAAqE;IACrE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,0DAA0D;IAC1D,QAAQ,CAAC,qBAAqB,EAAE,OAAO,CAAC;CACzC,CAAC;AA2BF;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,uBAAuB,GAAI,eAAe;IACrD,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACzC,KAAG,IAoDH,CAAC;AAkCF;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,aAAa,GAAI,OAAO,cAAc,KAAG,IAGrD,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,oBAAoB,GAAI,QAAQ,eAAe,KAAG,IAE9D,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,QAAO,SAAS,eAAe,EAE3D,CAAC"}