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

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 (52) 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 +57 -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/back-button.d.ts +43 -0
  8. package/dist/back-button.d.ts.map +1 -0
  9. package/dist/back-button.js +77 -0
  10. package/dist/barcode-scanner.d.ts +59 -0
  11. package/dist/barcode-scanner.d.ts.map +1 -0
  12. package/dist/barcode-scanner.js +100 -0
  13. package/dist/biometrics.d.ts +79 -0
  14. package/dist/biometrics.d.ts.map +1 -0
  15. package/dist/biometrics.js +151 -0
  16. package/dist/bluetooth.d.ts +121 -0
  17. package/dist/bluetooth.d.ts.map +1 -0
  18. package/dist/bluetooth.js +369 -0
  19. package/dist/calendar.d.ts +68 -0
  20. package/dist/calendar.d.ts.map +1 -0
  21. package/dist/calendar.js +120 -0
  22. package/dist/file-picker.d.ts +76 -0
  23. package/dist/file-picker.d.ts.map +1 -0
  24. package/dist/file-picker.js +125 -0
  25. package/dist/index.d.ts +28 -0
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +15 -0
  28. package/dist/motion.d.ts +90 -0
  29. package/dist/motion.d.ts.map +1 -0
  30. package/dist/motion.js +169 -0
  31. package/dist/nfc.d.ts +85 -0
  32. package/dist/nfc.d.ts.map +1 -0
  33. package/dist/nfc.js +253 -0
  34. package/dist/photo-library.d.ts +58 -0
  35. package/dist/photo-library.d.ts.map +1 -0
  36. package/dist/photo-library.js +91 -0
  37. package/dist/render.d.ts.map +1 -1
  38. package/dist/render.js +19 -1
  39. package/dist/setup.js +59 -0
  40. package/dist/share-target.d.ts +56 -0
  41. package/dist/share-target.d.ts.map +1 -0
  42. package/dist/share-target.js +92 -0
  43. package/dist/store-review.d.ts +30 -0
  44. package/dist/store-review.d.ts.map +1 -0
  45. package/dist/store-review.js +49 -0
  46. package/dist/tracking.d.ts +60 -0
  47. package/dist/tracking.d.ts.map +1 -0
  48. package/dist/tracking.js +94 -0
  49. package/dist/wifi.d.ts +77 -0
  50. package/dist/wifi.d.ts.map +1 -0
  51. package/dist/wifi.js +148 -0
  52. package/package.json +3 -3
@@ -0,0 +1,125 @@
1
+ /** What `LynxFilePicker` calls, as the shells name their module. */
2
+ const MODULE = 'AngularLynxFilePicker';
3
+ const toBytes = (content) => typeof content === 'string' ? new TextEncoder().encode(content) : content;
4
+ const toBase64 = (bytes) => {
5
+ let binary = '';
6
+ for (const byte of bytes)
7
+ binary += String.fromCharCode(byte);
8
+ return btoa(binary);
9
+ };
10
+ const fromBase64 = (base64) => Uint8Array.from(atob(base64), (char) => char.charCodeAt(0));
11
+ /**
12
+ * Gives the environment's background thread an `AngularLynxFilePicker`
13
+ * module, as the apps `ng build --platform` makes with angular.json's
14
+ * `filePicker` have. It stands for a person who closes the picker until a
15
+ * spec says what they pick ({@link setPickedFiles}), and who saves every
16
+ * file the app offers ({@link savedFiles}) unless a spec has them close the
17
+ * screen ({@link setFilePickerCanceled}). The runtime reaches it through the
18
+ * background thread, as it reaches the shells' module, so specs see the
19
+ * runtime's own behavior, its size limit included.
20
+ *
21
+ * It runs each time the environment injects the background thread's
22
+ * globals: when it's created, and on the reset after each test, so each test
23
+ * starts with nothing to pick and nothing saved.
24
+ */
25
+ export const installFilePickerModule = (threadGlobals) => {
26
+ const module = {
27
+ picks: undefined,
28
+ canceled: false,
29
+ saved: [],
30
+ pick(request, done) {
31
+ if (this.canceled || this.picks === undefined) {
32
+ return done({ canceled: true });
33
+ }
34
+ const files = this.picks.map((file) => ({
35
+ bytes: toBytes(file.content),
36
+ name: file.name,
37
+ type: file.type ?? 'application/octet-stream',
38
+ }));
39
+ const total = files.reduce((sum, file) => sum + file.bytes.length, 0);
40
+ // LynxFilePicker always sends its limit, as it does to the shells.
41
+ if (total > request.maxBytes) {
42
+ return done({ error: 'too-large' });
43
+ }
44
+ done({
45
+ files: files.map(({ bytes, name, type }) => ({
46
+ base64: toBase64(bytes),
47
+ name,
48
+ type,
49
+ })),
50
+ });
51
+ },
52
+ save(file, done) {
53
+ if (this.canceled)
54
+ return done({ saved: false });
55
+ const bytes = fromBase64(file.base64);
56
+ this.saved.push({
57
+ name: file.name,
58
+ type: file.type,
59
+ bytes,
60
+ text: new TextDecoder().decode(bytes),
61
+ });
62
+ done({ saved: true });
63
+ },
64
+ };
65
+ threadGlobals.NativeModules = {
66
+ ...threadGlobals.NativeModules,
67
+ [MODULE]: module,
68
+ };
69
+ };
70
+ /**
71
+ * The fake module, read through the environment rather than kept here:
72
+ * under `ng test`, the setup file and the specs can load different copies of
73
+ * this module.
74
+ */
75
+ const fakeModule = (helper) => {
76
+ const module = globalThis.lynxTestingEnv?.backgroundThread.globalThis
77
+ .NativeModules?.[MODULE];
78
+ if (!module) {
79
+ throw new Error(`${helper}() needs the testing library's environment, which its setup file installs.`);
80
+ }
81
+ return module;
82
+ };
83
+ /**
84
+ * Has the person pick `files` each time the app opens `LynxFilePicker`'s
85
+ * picker in this test, so `pick()` resolves with them. Until a spec calls
86
+ * it, the person closes the picker, and `pick()` resolves with `[]`. Files
87
+ * over the runtime's 20 MiB limit, together, reject with `too-large`, as on
88
+ * a phone.
89
+ *
90
+ * @example
91
+ * ```ts
92
+ * setPickedFiles([{ name: 'receipt.pdf', type: 'application/pdf', content: pdfBytes }]);
93
+ * fireEvent.tap(screen.getByText('Attach receipt'));
94
+ * await waitForUpdate();
95
+ * expect(screen.getByText('receipt.pdf')).toBeInTheDocument();
96
+ * ```
97
+ */
98
+ export const setPickedFiles = (files) => {
99
+ const module = fakeModule('setPickedFiles');
100
+ module.picks = [...files];
101
+ module.canceled = false;
102
+ };
103
+ /**
104
+ * Has the person close `LynxFilePicker`'s picker and save screen for the
105
+ * rest of this test: `pick()` resolves with `[]`, and `save()` with false.
106
+ * {@link setPickedFiles} undoes it.
107
+ */
108
+ export const setFilePickerCanceled = () => {
109
+ fakeModule('setFilePickerCanceled').canceled = true;
110
+ };
111
+ /**
112
+ * The files the app saved with `LynxFilePicker.save()` in this test, oldest
113
+ * first. Each test starts with none.
114
+ *
115
+ * @example
116
+ * ```ts
117
+ * fireEvent.tap(screen.getByText('Export'));
118
+ * await waitForUpdate();
119
+ * expect(savedFiles()).toMatchObject([{ name: 'report.csv', type: 'text/csv' }]);
120
+ * expect(savedFiles()[0]!.text).toContain('Total,42');
121
+ * ```
122
+ */
123
+ export const savedFiles = () => [
124
+ ...fakeModule('savedFiles').saved,
125
+ ];
package/dist/index.d.ts CHANGED
@@ -1,10 +1,38 @@
1
1
  export { render, cleanup, waitForUpdate } from './render.js';
2
2
  export type { RenderInputs, RenderOptions, RenderResult } from './render.js';
3
3
  export type { ComponentRef } from '@angular/core';
4
+ export { setAppState } from './app-state.js';
5
+ export type { AppState } from './app-state.js';
6
+ export { pressBackButton } from './back-button.js';
7
+ export { biometricPrompts, queueBiometricResult, setBiometrics, } from './biometrics.js';
8
+ export type { BiometricPrompt, BiometricResult, FakeBiometrics, } from './biometrics.js';
9
+ export { savedFiles, setFilePickerCanceled, setPickedFiles, } from './file-picker.js';
10
+ export type { FakePickedFile, SavedFile } from './file-picker.js';
11
+ export { cancelBarcodeScan, failBarcodeScan, queueBarcodeScan, } from './barcode-scanner.js';
12
+ export type { BarcodeScanFailure, QueuedBarcodeScan, } from './barcode-scanner.js';
13
+ export { addBluetoothDevice, setBluetoothState } from './bluetooth.js';
14
+ export type { FakeBluetoothCharacteristic, FakeBluetoothDevice, FakeBluetoothDeviceRef, FakeBluetoothWrite, } from './bluetooth.js';
4
15
  export { fireEvent, eventMap } from './fire-event.js';
16
+ export { savedPhotos, setPhotoLibraryPermission } from './photo-library.js';
17
+ export type { FakePhotoLibraryPermission, SavedPhoto, } from './photo-library.js';
5
18
  export { openedURLs, openLink } from './linking.js';
19
+ export { emitMotion, setHeading, shakeDevice } from './motion.js';
20
+ export type { EmittedHeading, EmittedMotion, MotionVector } from './motion.js';
6
21
  export type { OpenedURL } from './linking.js';
7
22
  export { runShortcut } from './shortcuts.js';
23
+ export { receiveShare } from './share-target.js';
24
+ export type { FakeShare } from './share-target.js';
25
+ export { reviewRequests } from './store-review.js';
26
+ export { setTrackingAnswer, setTrackingStatus, trackingPrompts, } from './tracking.js';
27
+ export type { FakeTrackingStatus } from './tracking.js';
28
+ export { answerCalendarSheet, calendarEvents, failCalendarSheet, } from './calendar.js';
29
+ export type { CalendarSheetAnswer, CalendarSheetEvent, CalendarSheetFailure, } from './calendar.js';
30
+ export { failNfcSession, nfcSession, nfcWrites, setNfcState, tapNfcTag, } from './nfc.js';
31
+ export type { FakeNfcTag, NfcSessionFailure, NfcSessionInfo } from './nfc.js';
32
+ export { allowWifiNetworkRead, denyWifiNetworkRead, queueWifiJoinResult, setWifiNetwork, wifiJoins, wifiLeaves, } from './wifi.js';
33
+ export type { FakeWifiFailure, FakeWifiNetwork, WifiJoinRequest, } from './wifi.js';
34
+ export { appleSignInRequests, setAppleCredentialState, setAppleSignInAvailable, setAppleSignInError, setAppleSignInResult, } from './apple-sign-in.js';
35
+ export type { AppleSignInRequest, FakeAppleCredentialState, FakeAppleSignInResult, } from './apple-sign-in.js';
8
36
  export { screen, within, getQueriesForElement, queries } from './queries.js';
9
37
  export type { BoundAccessibilityLabelQueries, BoundLynxQueries, LynxQueries, LynxScreen, } from './queries.js';
10
38
  export { queryAllByAccessibilityLabel, queryByAccessibilityLabel, getAllByAccessibilityLabel, getByAccessibilityLabel, findAllByAccessibilityLabel, findByAccessibilityLabel, } from './accessibility-label-queries.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC7D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Update } from './render.js';
2
+ export { setAppState } from './app-state.js';
3
+ export { pressBackButton } from './back-button.js';
4
+ export { biometricPrompts, queueBiometricResult, setBiometrics, } from './biometrics.js';
5
+ export { savedFiles, setFilePickerCanceled, setPickedFiles, } from './file-picker.js';
6
+ export { cancelBarcodeScan, failBarcodeScan, queueBarcodeScan, } from './barcode-scanner.js';
7
+ export { addBluetoothDevice, setBluetoothState } from './bluetooth.js';
2
8
  export { fireEvent, eventMap } from './fire-event.js';
9
+ export { savedPhotos, setPhotoLibraryPermission } from './photo-library.js';
3
10
  export { openedURLs, openLink } from './linking.js';
11
+ export { emitMotion, setHeading, shakeDevice } from './motion.js';
4
12
  export { runShortcut } from './shortcuts.js';
13
+ export { receiveShare } from './share-target.js';
14
+ export { reviewRequests } from './store-review.js';
15
+ export { setTrackingAnswer, setTrackingStatus, trackingPrompts, } from './tracking.js';
16
+ export { answerCalendarSheet, calendarEvents, failCalendarSheet, } from './calendar.js';
17
+ export { failNfcSession, nfcSession, nfcWrites, setNfcState, tapNfcTag, } from './nfc.js';
18
+ export { allowWifiNetworkRead, denyWifiNetworkRead, queueWifiJoinResult, setWifiNetwork, wifiJoins, wifiLeaves, } from './wifi.js';
19
+ export { appleSignInRequests, setAppleCredentialState, setAppleSignInAvailable, setAppleSignInError, setAppleSignInResult, } from './apple-sign-in.js';
5
20
  export { screen, within, getQueriesForElement, queries } from './queries.js';
6
21
  export { queryAllByAccessibilityLabel, queryByAccessibilityLabel, getAllByAccessibilityLabel, getByAccessibilityLabel, findAllByAccessibilityLabel, findByAccessibilityLabel, } from './accessibility-label-queries.js';
7
22
  export { queryAllByRole, queryByRole, getAllByRole, getByRole, findAllByRole, findByRole, } from './role-queries.js';
@@ -0,0 +1,90 @@
1
+ /** A vector along the phone's axes, as {@link emitMotion} takes it. */
2
+ export type MotionVector = {
3
+ readonly x: number;
4
+ readonly y: number;
5
+ readonly z: number;
6
+ };
7
+ /** What {@link emitMotion} sends: any of the sensors, sampled at once. */
8
+ export type EmittedMotion = {
9
+ /** The acceleration with gravity, in m/s² (face up at rest: z 9.81). */
10
+ readonly acceleration?: MotionVector;
11
+ /** How fast the phone turns around each axis, in radians a second. */
12
+ readonly rotation?: MotionVector;
13
+ /** Which way it faces, as `DeviceOrientationEvent`'s angles in degrees. */
14
+ readonly orientation?: {
15
+ readonly alpha: number;
16
+ readonly beta: number;
17
+ readonly gamma: number;
18
+ };
19
+ /**
20
+ * When it was measured, in milliseconds since the epoch. A second after
21
+ * the last sample unless it's given (or `Date.now()`, if that's later), so
22
+ * a watch whose `interval` is a second or less never skips it. A slower
23
+ * watch needs samples given timestamps at least its interval apart.
24
+ */
25
+ readonly timestamp?: number;
26
+ };
27
+ /** What {@link setHeading} takes besides a magnetic heading alone. */
28
+ export type EmittedHeading = {
29
+ readonly magneticHeading: number;
30
+ readonly trueHeading?: number | null;
31
+ readonly accuracy?: number | null;
32
+ readonly timestamp?: number;
33
+ };
34
+ /**
35
+ * Gives the environment's background thread an `AngularLynxMotion` module,
36
+ * as the apps `ng build --platform` makes with angular.json's `motion` have:
37
+ * a phone with every sensor, which samples only what {@link emitMotion},
38
+ * {@link setHeading} and {@link shakeDevice} send, and only while the app
39
+ * runs the sensor, as a phone sends nothing for a sensor nobody watches.
40
+ *
41
+ * It runs each time the environment injects the background thread's
42
+ * globals: when it's created, and on the reset after each test.
43
+ */
44
+ export declare const installMotionModule: (threadGlobals: {
45
+ NativeModules?: Record<string, unknown>;
46
+ }) => void;
47
+ /**
48
+ * Sends a sample of the phone's motion sensors, as the shells do, to the
49
+ * `LynxMotion` watches the app has: `watchAcceleration()`,
50
+ * `watchRotation()` and `watchOrientation()`. A sensor nothing watches
51
+ * isn't running, so its part reaches nobody, as on a phone. It arrives
52
+ * through the background thread: wait for it with `waitForUpdate()`.
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * await render(Level);
57
+ * emitMotion({ orientation: { alpha: 0, beta: 0, gamma: 12 } });
58
+ * await waitForUpdate();
59
+ * expect(screen.getByText('12°')).toBeInTheDocument();
60
+ * ```
61
+ */
62
+ export declare const emitMotion: (motion: EmittedMotion) => void;
63
+ /**
64
+ * Sends a compass heading, as the shells do, to the app's
65
+ * `LynxMotion.watchHeading()` watches: degrees clockwise from magnetic
66
+ * north, or an object with the true heading and the accuracy too (each
67
+ * `null` unless it's given). Nothing arrives while nothing watches.
68
+ *
69
+ * @example
70
+ * ```ts
71
+ * setHeading(90);
72
+ * await waitForUpdate();
73
+ * expect(screen.getByText('E')).toBeInTheDocument();
74
+ * ```
75
+ */
76
+ export declare const setHeading: (heading: number | EmittedHeading) => void;
77
+ /**
78
+ * Shakes the phone, as the person does, so the app's `LynxMotion.onShake()`
79
+ * handlers run. The shells hear a shake themselves, so it arrives as one
80
+ * event, not as accelerometer samples. Nothing happens while nothing listens.
81
+ *
82
+ * @example
83
+ * ```ts
84
+ * shakeDevice();
85
+ * await waitForUpdate();
86
+ * expect(screen.getByText('Undone')).toBeInTheDocument();
87
+ * ```
88
+ */
89
+ export declare const shakeDevice: () => void;
90
+ //# sourceMappingURL=motion.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"motion.d.ts","sourceRoot":"","sources":["../src/motion.ts"],"names":[],"mappings":"AAmCA,uEAAuE;AACvE,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,0EAA0E;AAC1E,MAAM,MAAM,aAAa,GAAG;IAC1B,wEAAwE;IACxE,QAAQ,CAAC,YAAY,CAAC,EAAE,YAAY,CAAC;IACrC,sEAAsE;IACtE,QAAQ,CAAC,QAAQ,CAAC,EAAE,YAAY,CAAC;IACjC,2EAA2E;IAC3E,QAAQ,CAAC,WAAW,CAAC,EAAE;QACrB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;QACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QACtB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;KACxB,CAAC;IACF;;;;;OAKG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B,CAAC;AAEF,sEAAsE;AACtE,MAAM,MAAM,cAAc,GAAG;IAC3B,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrC,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,mBAAmB,GAAI,eAAe;IACjD,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACzC,KAAG,IAmBH,CAAC;AAwEF;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,UAAU,GAAI,QAAQ,aAAa,KAAG,IAiBlD,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,UAAU,GAAI,SAAS,MAAM,GAAG,cAAc,KAAG,IAiB7D,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,WAAW,QAAO,IAI9B,CAAC"}
package/dist/motion.js ADDED
@@ -0,0 +1,169 @@
1
+ /** What `LynxMotion` calls and hears, named as the shells name them. */
2
+ const MODULE = 'AngularLynxMotion';
3
+ const EVENT = 'angularLynxMotion';
4
+ /**
5
+ * Gives the environment's background thread an `AngularLynxMotion` module,
6
+ * as the apps `ng build --platform` makes with angular.json's `motion` have:
7
+ * a phone with every sensor, which samples only what {@link emitMotion},
8
+ * {@link setHeading} and {@link shakeDevice} send, and only while the app
9
+ * runs the sensor, as a phone sends nothing for a sensor nobody watches.
10
+ *
11
+ * It runs each time the environment injects the background thread's
12
+ * globals: when it's created, and on the reset after each test.
13
+ */
14
+ export const installMotionModule = (threadGlobals) => {
15
+ const running = new Set();
16
+ const module = {
17
+ running,
18
+ last: -Infinity,
19
+ start: (sensor) => void running.add(sensor),
20
+ stop: (sensor) => void running.delete(sensor),
21
+ availability: (done) => done({
22
+ acceleration: true,
23
+ rotation: true,
24
+ orientation: true,
25
+ heading: true,
26
+ }),
27
+ };
28
+ threadGlobals.NativeModules = {
29
+ ...threadGlobals.NativeModules,
30
+ [MODULE]: module,
31
+ };
32
+ };
33
+ const environment = (caller) => {
34
+ // Read through the environment rather than kept here: under `ng test`, the
35
+ // setup file and the specs can load different copies of this module.
36
+ const env = globalThis.lynxTestingEnv;
37
+ const module = env?.backgroundThread.globalThis.NativeModules?.[MODULE];
38
+ if (!env || !module) {
39
+ throw new Error(`${caller}() needs the testing library's environment, which its setup file installs.`);
40
+ }
41
+ return { env, module };
42
+ };
43
+ /**
44
+ * The time of the next sample: as given, or after the last one.
45
+ */
46
+ const nextTime = (module, timestamp) => {
47
+ const t = timestamp ?? Math.max(Date.now(), module.last + 1000);
48
+ // The latest, not the last: after a spec's own timestamp that went back,
49
+ // a sample without one still comes after every sample before it, of any
50
+ // sensor, so a watch never skips it.
51
+ module.last = Math.max(module.last, t);
52
+ return t;
53
+ };
54
+ /**
55
+ * Sends the batch as the shells' global event. It arrives on the background
56
+ * thread, whose listener calls the app's on the main thread through that
57
+ * thread's `lynx`, so it's sent from there. Specs run on the main thread,
58
+ * which comes back afterwards.
59
+ */
60
+ const send = (env, batch) => {
61
+ env.switchToBackgroundThread();
62
+ try {
63
+ env.backgroundThread.globalThis.lynx
64
+ ?.getJSModule?.('GlobalEventEmitter')
65
+ ?.emit(EVENT, [batch]);
66
+ }
67
+ finally {
68
+ env.switchToMainThread();
69
+ }
70
+ };
71
+ /**
72
+ * The rotation matrix the shells send for an orientation, which `LynxMotion`
73
+ * turns back into the angles: Rz(alpha) · Rx(beta) · Ry(gamma).
74
+ */
75
+ const matrix = (alpha, beta, gamma) => {
76
+ const rad = Math.PI / 180;
77
+ const [ca, sa] = [Math.cos(alpha * rad), Math.sin(alpha * rad)];
78
+ const [cb, sb] = [Math.cos(beta * rad), Math.sin(beta * rad)];
79
+ const [cg, sg] = [Math.cos(gamma * rad), Math.sin(gamma * rad)];
80
+ return [
81
+ ca * cg - sa * sb * sg,
82
+ -cb * sa,
83
+ ca * sg + cg * sa * sb,
84
+ cg * sa + ca * sb * sg,
85
+ ca * cb,
86
+ sa * sg - ca * cg * sb,
87
+ -cb * sg,
88
+ sb,
89
+ cb * cg,
90
+ ];
91
+ };
92
+ /**
93
+ * Sends a sample of the phone's motion sensors, as the shells do, to the
94
+ * `LynxMotion` watches the app has: `watchAcceleration()`,
95
+ * `watchRotation()` and `watchOrientation()`. A sensor nothing watches
96
+ * isn't running, so its part reaches nobody, as on a phone. It arrives
97
+ * through the background thread: wait for it with `waitForUpdate()`.
98
+ *
99
+ * @example
100
+ * ```ts
101
+ * await render(Level);
102
+ * emitMotion({ orientation: { alpha: 0, beta: 0, gamma: 12 } });
103
+ * await waitForUpdate();
104
+ * expect(screen.getByText('12°')).toBeInTheDocument();
105
+ * ```
106
+ */
107
+ export const emitMotion = (motion) => {
108
+ const { env, module } = environment('emitMotion');
109
+ const t = nextTime(module, motion.timestamp);
110
+ const batch = {};
111
+ if (motion.acceleration && module.running.has('acceleration')) {
112
+ const { x, y, z } = motion.acceleration;
113
+ batch['acceleration'] = [t, x, y, z];
114
+ }
115
+ if (motion.rotation && module.running.has('rotation')) {
116
+ const { x, y, z } = motion.rotation;
117
+ batch['rotation'] = [t, x, y, z];
118
+ }
119
+ if (motion.orientation && module.running.has('orientation')) {
120
+ const { alpha, beta, gamma } = motion.orientation;
121
+ batch['orientation'] = [t, ...matrix(alpha, beta, gamma)];
122
+ }
123
+ send(env, batch);
124
+ };
125
+ /**
126
+ * Sends a compass heading, as the shells do, to the app's
127
+ * `LynxMotion.watchHeading()` watches: degrees clockwise from magnetic
128
+ * north, or an object with the true heading and the accuracy too (each
129
+ * `null` unless it's given). Nothing arrives while nothing watches.
130
+ *
131
+ * @example
132
+ * ```ts
133
+ * setHeading(90);
134
+ * await waitForUpdate();
135
+ * expect(screen.getByText('E')).toBeInTheDocument();
136
+ * ```
137
+ */
138
+ export const setHeading = (heading) => {
139
+ const { env, module } = environment('setHeading');
140
+ if (!module.running.has('heading'))
141
+ return;
142
+ const { magneticHeading, trueHeading = null, accuracy = null, timestamp, } = typeof heading === 'number' ? { magneticHeading: heading } : heading;
143
+ send(env, {
144
+ heading: [
145
+ nextTime(module, timestamp),
146
+ magneticHeading,
147
+ trueHeading ?? -1,
148
+ accuracy ?? -1,
149
+ ],
150
+ });
151
+ };
152
+ /**
153
+ * Shakes the phone, as the person does, so the app's `LynxMotion.onShake()`
154
+ * handlers run. The shells hear a shake themselves, so it arrives as one
155
+ * event, not as accelerometer samples. Nothing happens while nothing listens.
156
+ *
157
+ * @example
158
+ * ```ts
159
+ * shakeDevice();
160
+ * await waitForUpdate();
161
+ * expect(screen.getByText('Undone')).toBeInTheDocument();
162
+ * ```
163
+ */
164
+ export const shakeDevice = () => {
165
+ const { env, module } = environment('shakeDevice');
166
+ if (!module.running.has('shake'))
167
+ return;
168
+ send(env, { shake: [nextTime(module)] });
169
+ };
package/dist/nfc.d.ts ADDED
@@ -0,0 +1,85 @@
1
+ import { type NfcErrorCode, type NfcRecord, type NfcRecordInit, type NfcState } from '@blotch/angular-lynx';
2
+ /** A tag {@link tapNfcTag} brings near the phone. */
3
+ export type FakeNfcTag = {
4
+ /** What it holds, which a read resolves with. None when left out. */
5
+ readonly records?: readonly NfcRecordInit[];
6
+ /** Whether a write can write to it. True when left out. */
7
+ readonly writable?: boolean;
8
+ /**
9
+ * The most bytes its NDEF message takes: a write of more rejects with
10
+ * `tooLarge`. 888 when left out, an NTAG216's.
11
+ */
12
+ readonly capacity?: number;
13
+ /** Its serial number, as hex, as Android reports it. */
14
+ readonly id?: string;
15
+ /**
16
+ * False for a tag that holds no NDEF (a payment card): a read or write
17
+ * rejects with `tagNotNdef`, and a scan skips it. True when left out.
18
+ */
19
+ readonly ndef?: boolean;
20
+ };
21
+ /** A session the app has open, as {@link nfcSession} reports it. */
22
+ export type NfcSessionInfo = {
23
+ /** The text iOS's sheet shows. */
24
+ readonly alertMessage?: string;
25
+ /** True for `LynxNfc.scan()`, false for a read or a write. */
26
+ readonly multiple: boolean;
27
+ /** True for `LynxNfc.write()`. */
28
+ readonly writing: boolean;
29
+ };
30
+ /** Why a session ends, as {@link failNfcSession} ends it. */
31
+ export type NfcSessionFailure = Extract<NfcErrorCode, 'canceled' | 'timeout' | 'busy' | 'failed'>;
32
+ /**
33
+ * Gives the environment's background thread an `AngularLynxNfc` module, as
34
+ * the apps `ng build --platform` makes with angular.json's `permissions.nfc`
35
+ * have. Its phone reads tags (`ready`) until {@link setNfcState} says
36
+ * otherwise, and a tag comes near it only when {@link tapNfcTag} brings one.
37
+ * `LynxNfc` reaches it through the background thread, as it reaches the
38
+ * shells' module, so specs see the runtime's own behavior.
39
+ *
40
+ * It runs each time the environment injects the background thread's
41
+ * globals: when it's created, and on the reset after each test, so each test
42
+ * starts with a phone that reads tags, no session and no writes.
43
+ */
44
+ export declare const installNfcModule: (threadGlobals: {
45
+ NativeModules?: Record<string, unknown>;
46
+ }) => void;
47
+ /**
48
+ * Brings a tag near the phone, as the person would: the open
49
+ * `LynxNfc.read()` resolves with its records, the open `write()` writes to
50
+ * it, and an open `scan()` calls back with it. When no session is open yet,
51
+ * the next one that opens takes it. The answer reaches the app through the
52
+ * background thread, as on a phone: wait for it with `waitForUpdate()`.
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * fireEvent.tap(screen.getByText('Scan your card'));
57
+ * tapNfcTag({ records: [{ type: 'url', value: 'https://example.com/m/42' }] });
58
+ * await waitForUpdate();
59
+ * expect(screen.getByText('Member 42')).toBeInTheDocument();
60
+ * ```
61
+ */
62
+ export declare const tapNfcTag: (tag?: FakeNfcTag) => void;
63
+ /**
64
+ * Ends the open session, or the next one as soon as it opens, as iOS ends
65
+ * one: `canceled` when the person closes the scan sheet, `timeout` after a
66
+ * minute without a tag, `failed` otherwise. The open `read()` or `write()`
67
+ * rejects with an `NfcError` of `code`, and an open `scan()` ends with it.
68
+ */
69
+ export declare const failNfcSession: (code: NfcSessionFailure, message?: string) => void;
70
+ /**
71
+ * Whether the phone reads tags: `ready` (the default each test starts
72
+ * with), `disabled` (NFC turned off in Android's settings) or
73
+ * `unsupported` (no reader). `LynxNfc.state()` reads it when the app starts
74
+ * and each time it comes back to the foreground; send it there with
75
+ * `setAppState()`.
76
+ */
77
+ export declare const setNfcState: (state: Exclude<NfcState, "unknown">) => void;
78
+ /** The session the app has open, or undefined while none is. */
79
+ export declare const nfcSession: () => NfcSessionInfo | undefined;
80
+ /**
81
+ * The records each `LynxNfc.write()` wrote to a tag in this test, oldest
82
+ * first, as a read would give them back.
83
+ */
84
+ export declare const nfcWrites: () => readonly (readonly NfcRecord[])[];
85
+ //# sourceMappingURL=nfc.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nfc.d.ts","sourceRoot":"","sources":["../src/nfc.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,KAAK,QAAQ,EAGd,MAAM,sBAAsB,CAAC;AAK9B,qDAAqD;AACrD,MAAM,MAAM,UAAU,GAAG;IACvB,qEAAqE;IACrE,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,aAAa,EAAE,CAAC;IAC5C,2DAA2D;IAC3D,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;IAC5B;;;OAGG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,wDAAwD;IACxD,QAAQ,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF,oEAAoE;AACpE,MAAM,MAAM,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