@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.
- package/dist/app-state.d.ts +34 -0
- package/dist/app-state.d.ts.map +1 -0
- package/dist/app-state.js +33 -0
- package/dist/apple-sign-in.d.ts +116 -0
- package/dist/apple-sign-in.d.ts.map +1 -0
- package/dist/apple-sign-in.js +179 -0
- package/dist/audio-recorder.d.ts +95 -0
- package/dist/audio-recorder.d.ts.map +1 -0
- package/dist/audio-recorder.js +250 -0
- package/dist/back-button.d.ts +43 -0
- package/dist/back-button.d.ts.map +1 -0
- package/dist/back-button.js +77 -0
- package/dist/barcode-scanner.d.ts +59 -0
- package/dist/barcode-scanner.d.ts.map +1 -0
- package/dist/barcode-scanner.js +100 -0
- package/dist/biometrics.d.ts +79 -0
- package/dist/biometrics.d.ts.map +1 -0
- package/dist/biometrics.js +151 -0
- package/dist/bluetooth.d.ts +121 -0
- package/dist/bluetooth.d.ts.map +1 -0
- package/dist/bluetooth.js +369 -0
- package/dist/calendar.d.ts +68 -0
- package/dist/calendar.d.ts.map +1 -0
- package/dist/calendar.js +120 -0
- package/dist/file-picker.d.ts +76 -0
- package/dist/file-picker.d.ts.map +1 -0
- package/dist/file-picker.js +125 -0
- package/dist/fire-event.d.ts.map +1 -1
- package/dist/fire-event.js +58 -8
- package/dist/geolocation.d.ts +94 -0
- package/dist/geolocation.d.ts.map +1 -0
- package/dist/geolocation.js +255 -0
- package/dist/global-props.d.ts +17 -0
- package/dist/global-props.d.ts.map +1 -0
- package/dist/global-props.js +35 -0
- package/dist/image-picker.d.ts +84 -0
- package/dist/image-picker.d.ts.map +1 -0
- package/dist/image-picker.js +140 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -0
- package/dist/motion.d.ts +90 -0
- package/dist/motion.d.ts.map +1 -0
- package/dist/motion.js +169 -0
- package/dist/network.d.ts +26 -0
- package/dist/network.d.ts.map +1 -0
- package/dist/network.js +31 -0
- package/dist/nfc.d.ts +85 -0
- package/dist/nfc.d.ts.map +1 -0
- package/dist/nfc.js +253 -0
- package/dist/photo-library.d.ts +58 -0
- package/dist/photo-library.d.ts.map +1 -0
- package/dist/photo-library.js +91 -0
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +19 -1
- package/dist/setup.js +86 -7
- package/dist/share-target.d.ts +56 -0
- package/dist/share-target.d.ts.map +1 -0
- package/dist/share-target.js +92 -0
- package/dist/store-review.d.ts +30 -0
- package/dist/store-review.d.ts.map +1 -0
- package/dist/store-review.js +49 -0
- package/dist/tracking.d.ts +60 -0
- package/dist/tracking.d.ts.map +1 -0
- package/dist/tracking.js +94 -0
- package/dist/wifi.d.ts +77 -0
- package/dist/wifi.d.ts.map +1 -0
- package/dist/wifi.js +148 -0
- package/package.json +3 -3
package/dist/motion.d.ts
ADDED
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Takes the phone offline, or brings it back online, so a spec can check what
|
|
3
|
+
* the app shows without a connection and what it does once it's back.
|
|
4
|
+
* `LynxNetwork.online()` becomes `online`.
|
|
5
|
+
*
|
|
6
|
+
* It reaches the app as it does on a phone, as a change to the host's global
|
|
7
|
+
* props: change detection runs on its own, so wait for it with
|
|
8
|
+
* `waitForUpdate()`. Wait between two changes too: an `effect()` runs once
|
|
9
|
+
* for both otherwise, and never sees the phone go offline. Each test starts
|
|
10
|
+
* online.
|
|
11
|
+
*
|
|
12
|
+
* It changes only what `LynxNetwork` reports: requests go on as the spec's
|
|
13
|
+
* fakes answer them.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* await render(Orders);
|
|
18
|
+
* setOnline(false);
|
|
19
|
+
* await waitForUpdate();
|
|
20
|
+
* expect(screen.getByText('An internet connection is required')).toBeTruthy();
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @param online - `false` to take the phone offline, `true` to bring it back.
|
|
24
|
+
*/
|
|
25
|
+
export declare const setOnline: (online: boolean) => void;
|
|
26
|
+
//# sourceMappingURL=network.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../src/network.ts"],"names":[],"mappings":"AAQA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,SAAS,GAAI,QAAQ,OAAO,KAAG,IACK,CAAC"}
|
package/dist/network.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { setGlobalProp } from './global-props.js';
|
|
2
|
+
/**
|
|
3
|
+
* The global prop the shells send the connection in (see the runtime's
|
|
4
|
+
* `LynxNetwork`).
|
|
5
|
+
*/
|
|
6
|
+
const ONLINE_PROP = 'online';
|
|
7
|
+
/**
|
|
8
|
+
* Takes the phone offline, or brings it back online, so a spec can check what
|
|
9
|
+
* the app shows without a connection and what it does once it's back.
|
|
10
|
+
* `LynxNetwork.online()` becomes `online`.
|
|
11
|
+
*
|
|
12
|
+
* It reaches the app as it does on a phone, as a change to the host's global
|
|
13
|
+
* props: change detection runs on its own, so wait for it with
|
|
14
|
+
* `waitForUpdate()`. Wait between two changes too: an `effect()` runs once
|
|
15
|
+
* for both otherwise, and never sees the phone go offline. Each test starts
|
|
16
|
+
* online.
|
|
17
|
+
*
|
|
18
|
+
* It changes only what `LynxNetwork` reports: requests go on as the spec's
|
|
19
|
+
* fakes answer them.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* await render(Orders);
|
|
24
|
+
* setOnline(false);
|
|
25
|
+
* await waitForUpdate();
|
|
26
|
+
* expect(screen.getByText('An internet connection is required')).toBeTruthy();
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @param online - `false` to take the phone offline, `true` to bring it back.
|
|
30
|
+
*/
|
|
31
|
+
export const setOnline = (online) => setGlobalProp(ONLINE_PROP, online, 'setOnline');
|
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,cAAc,GAAG;IAC3B,kCAAkC;IAClC,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAC/B,8DAA8D;IAC9D,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAC3B,kCAAkC;IAClC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC3B,CAAC;AAEF,6DAA6D;AAC7D,MAAM,MAAM,iBAAiB,GAAG,OAAO,CACrC,YAAY,EACZ,UAAU,GAAG,SAAS,GAAG,MAAM,GAAG,QAAQ,CAC3C,CAAC;AAiJF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,gBAAgB,GAAI,eAAe;IAC9C,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACzC,KAAG,IAgDH,CAAC;AAoCF;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,SAAS,GAAI,MAAK,UAAe,KAAG,IAOhD,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,cAAc,GACzB,MAAM,iBAAiB,EACvB,UAAU,MAAM,KACf,IAQF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,GAAI,OAAO,OAAO,CAAC,QAAQ,EAAE,SAAS,CAAC,KAAG,IAEjE,CAAC;AAEF,gEAAgE;AAChE,eAAO,MAAM,UAAU,QAAO,cAAc,GAAG,SAU9C,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,SAAS,QAAO,SAAS,CAAC,SAAS,SAAS,EAAE,CAAC,EAE3D,CAAC"}
|
package/dist/nfc.js
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { ɵencodeNfcRecord, ɵparseNfcRecord, } from '@blotch/angular-lynx';
|
|
2
|
+
/** What `LynxNfc` calls, named as the shells name their module. */
|
|
3
|
+
const MODULE = 'AngularLynxNfc';
|
|
4
|
+
const STOPPED = { error: 'stopped' };
|
|
5
|
+
/**
|
|
6
|
+
* The bytes of a record's base64 field, which records cross the module as.
|
|
7
|
+
*/
|
|
8
|
+
const fromBase64 = (text) => Uint8Array.from(atob(text), (character) => character.charCodeAt(0));
|
|
9
|
+
/**
|
|
10
|
+
* The bytes an NDEF message of `records` takes on a tag: each record's
|
|
11
|
+
* header, lengths, type and payload (NFC Forum NDEF 1.0, 3.2), with a
|
|
12
|
+
* short record's one-byte length for a payload under 256 bytes.
|
|
13
|
+
*/
|
|
14
|
+
const messageLength = (records) => records.reduce((sum, record) => {
|
|
15
|
+
// The runtime writes no record IDs.
|
|
16
|
+
const payload = fromBase64(record.payload).length;
|
|
17
|
+
return (sum +
|
|
18
|
+
1 +
|
|
19
|
+
1 +
|
|
20
|
+
(payload < 256 ? 1 : 4) +
|
|
21
|
+
fromBase64(record.type).length +
|
|
22
|
+
payload);
|
|
23
|
+
}, 0);
|
|
24
|
+
const tagAnswer = (records, tag) => ({
|
|
25
|
+
tag: {
|
|
26
|
+
records,
|
|
27
|
+
writable: tag.writable ?? true,
|
|
28
|
+
capacity: tag.capacity ?? 888,
|
|
29
|
+
...(tag.id !== undefined && { id: tag.id }),
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
/**
|
|
33
|
+
* The module's answer to a tap, as the shells work it out.
|
|
34
|
+
*/
|
|
35
|
+
const answerTap = (module, tag) => {
|
|
36
|
+
if (tag.ndef === false) {
|
|
37
|
+
return {
|
|
38
|
+
error: 'tagNotNdef',
|
|
39
|
+
message: "This tag doesn't hold NDEF records.",
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
const write = module.session?.write;
|
|
43
|
+
if (!write) {
|
|
44
|
+
return tagAnswer((tag.records ?? []).map(ɵencodeNfcRecord), tag);
|
|
45
|
+
}
|
|
46
|
+
if (tag.writable === false) {
|
|
47
|
+
return {
|
|
48
|
+
error: 'readOnly',
|
|
49
|
+
message: "This tag is locked, so it can't be written to.",
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
const length = messageLength(write);
|
|
53
|
+
const capacity = tag.capacity ?? 888;
|
|
54
|
+
if (length > capacity) {
|
|
55
|
+
return {
|
|
56
|
+
error: 'tooLarge',
|
|
57
|
+
message: `The records take ${length} bytes, and this tag holds ${capacity}.`,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
module.writes.push(write.map((record) => ɵparseNfcRecord(record)));
|
|
61
|
+
return tagAnswer(write, tag);
|
|
62
|
+
};
|
|
63
|
+
const deliver = (module, answer) => {
|
|
64
|
+
// A scan skips a tag it couldn't read, as the shells do.
|
|
65
|
+
if (module.session?.multiple && answer['error'] !== undefined)
|
|
66
|
+
return;
|
|
67
|
+
const waiting = module.waiting;
|
|
68
|
+
if (waiting) {
|
|
69
|
+
module.waiting = undefined;
|
|
70
|
+
waiting(answer);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
module.answers.push(answer);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const end = (module, answer) => {
|
|
77
|
+
module.session = undefined;
|
|
78
|
+
module.ended = answer;
|
|
79
|
+
const waiting = module.waiting;
|
|
80
|
+
module.waiting = undefined;
|
|
81
|
+
waiting?.(answer);
|
|
82
|
+
};
|
|
83
|
+
const handle = (module, pending) => {
|
|
84
|
+
if ('tap' in pending) {
|
|
85
|
+
if (module.claimed) {
|
|
86
|
+
module.pending.push(pending);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (!module.session?.multiple)
|
|
90
|
+
module.claimed = true;
|
|
91
|
+
deliver(module, answerTap(module, pending.tap));
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
end(module, pending.fail);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Gives the environment's background thread an `AngularLynxNfc` module, as
|
|
99
|
+
* the apps `ng build --platform` makes with angular.json's `permissions.nfc`
|
|
100
|
+
* have. Its phone reads tags (`ready`) until {@link setNfcState} says
|
|
101
|
+
* otherwise, and a tag comes near it only when {@link tapNfcTag} brings one.
|
|
102
|
+
* `LynxNfc` reaches it through the background thread, as it reaches the
|
|
103
|
+
* shells' module, so specs see the runtime's own behavior.
|
|
104
|
+
*
|
|
105
|
+
* It runs each time the environment injects the background thread's
|
|
106
|
+
* globals: when it's created, and on the reset after each test, so each test
|
|
107
|
+
* starts with a phone that reads tags, no session and no writes.
|
|
108
|
+
*/
|
|
109
|
+
export const installNfcModule = (threadGlobals) => {
|
|
110
|
+
const module = {
|
|
111
|
+
phone: 'ready',
|
|
112
|
+
session: undefined,
|
|
113
|
+
claimed: false,
|
|
114
|
+
waiting: undefined,
|
|
115
|
+
answers: [],
|
|
116
|
+
pending: [],
|
|
117
|
+
ended: STOPPED,
|
|
118
|
+
writes: [],
|
|
119
|
+
state(done) {
|
|
120
|
+
done({ state: this.phone });
|
|
121
|
+
},
|
|
122
|
+
start(options, done) {
|
|
123
|
+
if (this.phone !== 'ready') {
|
|
124
|
+
done({ error: this.phone });
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
// LynxNfc opens one session at a time itself.
|
|
128
|
+
this.session = options;
|
|
129
|
+
this.claimed = false;
|
|
130
|
+
this.answers.length = 0;
|
|
131
|
+
done({});
|
|
132
|
+
// What the spec did before the app asked.
|
|
133
|
+
// One at a time: a failure ends the session, a read or a write takes
|
|
134
|
+
// only one tag, and what came after waits for the next session.
|
|
135
|
+
while (this.session && !this.claimed && this.pending.length > 0) {
|
|
136
|
+
handle(this, this.pending.shift());
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
next(done) {
|
|
140
|
+
if (this.answers.length > 0) {
|
|
141
|
+
done(this.answers.shift());
|
|
142
|
+
}
|
|
143
|
+
else if (!this.session) {
|
|
144
|
+
done(this.ended);
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
this.waiting = done;
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
stop(_options, done) {
|
|
151
|
+
if (this.session)
|
|
152
|
+
end(this, STOPPED);
|
|
153
|
+
done({});
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
threadGlobals.NativeModules = {
|
|
157
|
+
...threadGlobals.NativeModules,
|
|
158
|
+
[MODULE]: module,
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
/**
|
|
162
|
+
* The fake module, read through the environment rather than kept here:
|
|
163
|
+
* under `ng test`, the setup file and the specs can load different copies of
|
|
164
|
+
* this file.
|
|
165
|
+
*/
|
|
166
|
+
const fakeModule = (name) => {
|
|
167
|
+
const env = globalThis.lynxTestingEnv;
|
|
168
|
+
const module = env?.backgroundThread.globalThis.NativeModules?.[MODULE];
|
|
169
|
+
if (!env || !module) {
|
|
170
|
+
throw new Error(`${name}() needs the testing library's environment, which its setup file installs.`);
|
|
171
|
+
}
|
|
172
|
+
return { env, module };
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* Runs `action` on the background thread, where the shells answer.
|
|
176
|
+
*/
|
|
177
|
+
const onBackground = (env, action) => {
|
|
178
|
+
env.switchToBackgroundThread();
|
|
179
|
+
try {
|
|
180
|
+
action();
|
|
181
|
+
}
|
|
182
|
+
finally {
|
|
183
|
+
env.switchToMainThread();
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* Brings a tag near the phone, as the person would: the open
|
|
188
|
+
* `LynxNfc.read()` resolves with its records, the open `write()` writes to
|
|
189
|
+
* it, and an open `scan()` calls back with it. When no session is open yet,
|
|
190
|
+
* the next one that opens takes it. The answer reaches the app through the
|
|
191
|
+
* background thread, as on a phone: wait for it with `waitForUpdate()`.
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```ts
|
|
195
|
+
* fireEvent.tap(screen.getByText('Scan your card'));
|
|
196
|
+
* tapNfcTag({ records: [{ type: 'url', value: 'https://example.com/m/42' }] });
|
|
197
|
+
* await waitForUpdate();
|
|
198
|
+
* expect(screen.getByText('Member 42')).toBeInTheDocument();
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
export const tapNfcTag = (tag = {}) => {
|
|
202
|
+
const { env, module } = fakeModule('tapNfcTag');
|
|
203
|
+
if (!module.session) {
|
|
204
|
+
module.pending.push({ tap: tag });
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
onBackground(env, () => handle(module, { tap: tag }));
|
|
208
|
+
};
|
|
209
|
+
/**
|
|
210
|
+
* Ends the open session, or the next one as soon as it opens, as iOS ends
|
|
211
|
+
* one: `canceled` when the person closes the scan sheet, `timeout` after a
|
|
212
|
+
* minute without a tag, `failed` otherwise. The open `read()` or `write()`
|
|
213
|
+
* rejects with an `NfcError` of `code`, and an open `scan()` ends with it.
|
|
214
|
+
*/
|
|
215
|
+
export const failNfcSession = (code, message) => {
|
|
216
|
+
const { env, module } = fakeModule('failNfcSession');
|
|
217
|
+
const fail = { error: code, ...(message !== undefined && { message }) };
|
|
218
|
+
if (!module.session) {
|
|
219
|
+
module.pending.push({ fail });
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
onBackground(env, () => handle(module, { fail }));
|
|
223
|
+
};
|
|
224
|
+
/**
|
|
225
|
+
* Whether the phone reads tags: `ready` (the default each test starts
|
|
226
|
+
* with), `disabled` (NFC turned off in Android's settings) or
|
|
227
|
+
* `unsupported` (no reader). `LynxNfc.state()` reads it when the app starts
|
|
228
|
+
* and each time it comes back to the foreground; send it there with
|
|
229
|
+
* `setAppState()`.
|
|
230
|
+
*/
|
|
231
|
+
export const setNfcState = (state) => {
|
|
232
|
+
fakeModule('setNfcState').module.phone = state;
|
|
233
|
+
};
|
|
234
|
+
/** The session the app has open, or undefined while none is. */
|
|
235
|
+
export const nfcSession = () => {
|
|
236
|
+
const session = fakeModule('nfcSession').module.session;
|
|
237
|
+
if (!session)
|
|
238
|
+
return undefined;
|
|
239
|
+
return {
|
|
240
|
+
...(session.alertMessage !== undefined && {
|
|
241
|
+
alertMessage: session.alertMessage,
|
|
242
|
+
}),
|
|
243
|
+
multiple: session.multiple === true,
|
|
244
|
+
writing: session.write !== undefined,
|
|
245
|
+
};
|
|
246
|
+
};
|
|
247
|
+
/**
|
|
248
|
+
* The records each `LynxNfc.write()` wrote to a tag in this test, oldest
|
|
249
|
+
* first, as a read would give them back.
|
|
250
|
+
*/
|
|
251
|
+
export const nfcWrites = () => [
|
|
252
|
+
...fakeModule('nfcWrites').module.writes,
|
|
253
|
+
];
|