@venizia/ardor-kernel 0.4.8-4 → 0.4.8-5

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/README.md CHANGED
@@ -9,6 +9,8 @@ bun add @venizia/ardor-kernel @venizia/ignis-inversion @venizia/ignis-filter @ve
9
9
 
10
10
  `@venizia/ignis-connectors` is an optional peer, needed only for `./repository`.
11
11
 
12
+ `@venizia/ardor-kernel/testing` holds fakes of `ResizeObserver`, `IntersectionObserver`, `matchMedia`, `Storage` and `navigator.clipboard` for your tests; the main entry does not include them.
13
+
12
14
  Most applications install [`@venizia/ardor`](https://www.npmjs.com/package/@venizia/ardor) instead,
13
15
  which re-exports this package.
14
16
 
@@ -0,0 +1,113 @@
1
+ import type { TStorageArea } from './common/index.js';
2
+ /**
3
+ * A `ResizeObserver` whose `observe()` records the element. `flush` delivers a size to the observers
4
+ * of an element; a callback runs at once, so code that waits for an animation frame still needs the
5
+ * test's own frame control.
6
+ */
7
+ export declare class FakeResizeObserver implements ResizeObserver {
8
+ readonly callback: ResizeObserverCallback;
9
+ /** The observers created since the last `install`, in creation order. */
10
+ static all: Array<FakeResizeObserver>;
11
+ /** The elements observed now. */
12
+ readonly targets: Array<Element>;
13
+ /** The options of each `observe` call, in order. */
14
+ readonly options: Array<ResizeObserverOptions | undefined>;
15
+ /** Every element passed to `unobserve`, in order. */
16
+ readonly unobserved: Array<Element>;
17
+ isDisconnected: boolean;
18
+ constructor(callback: ResizeObserverCallback);
19
+ /** Replaces `globalThis.ResizeObserver` with the fake and forgets the earlier observers. */
20
+ static install(): () => void;
21
+ /** Calls every observer of `element` with one entry of that size, for the content and the border box alike. */
22
+ static flush(opts: {
23
+ element: Element;
24
+ width: number;
25
+ height: number;
26
+ }): void;
27
+ observe(target: Element, options?: ResizeObserverOptions): void;
28
+ unobserve(target: Element): void;
29
+ disconnect(): void;
30
+ }
31
+ /** An `IntersectionObserver` whose `observe()` records the element; `flush` delivers one entry to the observers of an element. */
32
+ export declare class FakeIntersectionObserver implements IntersectionObserver {
33
+ readonly callback: IntersectionObserverCallback;
34
+ readonly options?: IntersectionObserverInit | undefined;
35
+ /** The observers created since the last `install`, in creation order. */
36
+ static all: Array<FakeIntersectionObserver>;
37
+ readonly root: null;
38
+ readonly rootMargin = "";
39
+ readonly scrollMargin = "";
40
+ readonly thresholds: ReadonlyArray<number>;
41
+ /** The elements observed now. */
42
+ readonly targets: Array<Element>;
43
+ isDisconnected: boolean;
44
+ constructor(callback: IntersectionObserverCallback, options?: IntersectionObserverInit | undefined);
45
+ /** Replaces `globalThis.IntersectionObserver` with the fake and forgets the earlier observers. */
46
+ static install(): () => void;
47
+ /** Calls every observer of `element` with one entry. `ratio` is 1 when the element intersects and 0 when not, unless given. */
48
+ static flush(opts: {
49
+ element: Element;
50
+ isIntersecting: boolean;
51
+ ratio?: number;
52
+ }): void;
53
+ observe(target: Element): void;
54
+ unobserve(target: Element): void;
55
+ disconnect(): void;
56
+ takeRecords(): Array<IntersectionObserverEntry>;
57
+ }
58
+ /** The `MediaQueryList` that `matchMedia` answers under `FakeMatchMedia.install`: one per query, `false` until `set`. */
59
+ export declare class FakeMatchMedia extends EventTarget {
60
+ readonly media: string;
61
+ matches: boolean;
62
+ /** The queries asked since the last `install`, with the list each one got. */
63
+ static readonly queries: Map<string, FakeMatchMedia>;
64
+ constructor(media: string, matches?: boolean);
65
+ /** Replaces `window.matchMedia` with a function that answers the list of the query. */
66
+ static install(): () => void;
67
+ /** The list of `query`, made on the first ask. */
68
+ static of(query: string): FakeMatchMedia;
69
+ /** Sets whether `query` matches and tells its listeners with a `change` event. */
70
+ static set(opts: {
71
+ query: string;
72
+ matches: boolean;
73
+ }): void;
74
+ }
75
+ /**
76
+ * A `Storage` that behaves like the real one: stored keys are own properties, so `Object.keys` lists
77
+ * them. Set `failReads` for a blocked storage (reads throw `SecurityError`) and `failWrites` for a
78
+ * full or blocked one (`setItem` and `clear` throw `QuotaExceededError`, `removeItem` throws
79
+ * `SecurityError`); both can change at any time. A key named like a member of the fake (`getItem`,
80
+ * `length`, `writes`, `failWrites` and the rest) would shadow it, so `setItem` throws a `TypeError`
81
+ * for it, where the real `Storage` accepts it.
82
+ */
83
+ export declare class FakeStorage implements Storage {
84
+ #private;
85
+ [name: string]: unknown;
86
+ get failReads(): boolean;
87
+ set failReads(value: boolean);
88
+ get failWrites(): boolean;
89
+ set failWrites(value: boolean);
90
+ /** The values passed to `setItem` that the storage accepted, in order. */
91
+ get writes(): Array<string>;
92
+ get length(): number;
93
+ /** Makes this the `localStorage` (the default) or the `sessionStorage` of the global. */
94
+ install(area?: TStorageArea): () => void;
95
+ getItem(key: string): string | null;
96
+ setItem(key: string, value: string): void;
97
+ removeItem(key: string): void;
98
+ clear(): void;
99
+ key(index: number): string | null;
100
+ }
101
+ /** A `navigator.clipboard` that keeps the text in memory; `isDenied` makes both methods reject as a refused permission does. */
102
+ export declare class FakeClipboard {
103
+ /** The text of every `writeText`, in order. */
104
+ readonly written: Array<string>;
105
+ /** What `readText` answers; `writeText` replaces it. */
106
+ text: string;
107
+ isDenied: boolean;
108
+ /** Makes this the `navigator.clipboard`. */
109
+ install(): () => void;
110
+ writeText(text: string): Promise<void>;
111
+ readText(): Promise<string>;
112
+ }
113
+ //# sourceMappingURL=testing.d.ts.map
@@ -0,0 +1,289 @@
1
+ /**
2
+ * Fakes of the browser APIs ARDOR's hooks and helpers use, for a test host that has none of them or
3
+ * whose own do nothing, such as happy-dom. Each fake puts itself on the global with `install`,
4
+ * which returns the function that puts the previous value back.
5
+ */
6
+ /** Defines `name` on `target` and answers the function that puts the previous own property back. */
7
+ const swap = (opts) => {
8
+ const { target, name, value } = opts;
9
+ const previous = Object.getOwnPropertyDescriptor(target, name);
10
+ Object.defineProperty(target, name, { value, configurable: true, writable: true });
11
+ return () => {
12
+ if (previous) {
13
+ Object.defineProperty(target, name, previous);
14
+ }
15
+ else {
16
+ Reflect.deleteProperty(target, name);
17
+ }
18
+ };
19
+ };
20
+ /** Removes the first `item` from `list`. */
21
+ const drop = (opts) => {
22
+ const index = opts.list.indexOf(opts.item);
23
+ if (index >= 0) {
24
+ opts.list.splice(index, 1);
25
+ }
26
+ };
27
+ /**
28
+ * A `ResizeObserver` whose `observe()` records the element. `flush` delivers a size to the observers
29
+ * of an element; a callback runs at once, so code that waits for an animation frame still needs the
30
+ * test's own frame control.
31
+ */
32
+ export class FakeResizeObserver {
33
+ /** The observers created since the last `install`, in creation order. */
34
+ static { this.all = []; }
35
+ constructor(callback) {
36
+ this.callback = callback;
37
+ /** The elements observed now. */
38
+ this.targets = [];
39
+ /** The options of each `observe` call, in order. */
40
+ this.options = [];
41
+ /** Every element passed to `unobserve`, in order. */
42
+ this.unobserved = [];
43
+ this.isDisconnected = false;
44
+ FakeResizeObserver.all.push(this);
45
+ }
46
+ /** Replaces `globalThis.ResizeObserver` with the fake and forgets the earlier observers. */
47
+ static install() {
48
+ FakeResizeObserver.all = [];
49
+ return swap({ target: globalThis, name: 'ResizeObserver', value: FakeResizeObserver });
50
+ }
51
+ /** Calls every observer of `element` with one entry of that size, for the content and the border box alike. */
52
+ static flush(opts) {
53
+ const { element, width, height } = opts;
54
+ const box = { x: 0, y: 0, width, height, top: 0, left: 0, right: width, bottom: height };
55
+ const size = [{ inlineSize: width, blockSize: height }];
56
+ const entry = {
57
+ target: element,
58
+ contentRect: { ...box, toJSON: () => box },
59
+ contentBoxSize: size,
60
+ borderBoxSize: size,
61
+ devicePixelContentBoxSize: [],
62
+ };
63
+ for (const observer of FakeResizeObserver.all) {
64
+ if (observer.targets.includes(element)) {
65
+ observer.callback([entry], observer);
66
+ }
67
+ }
68
+ }
69
+ observe(target, options) {
70
+ if (!this.targets.includes(target)) {
71
+ this.targets.push(target);
72
+ this.options.push(options);
73
+ }
74
+ }
75
+ unobserve(target) {
76
+ this.unobserved.push(target);
77
+ drop({ list: this.targets, item: target });
78
+ }
79
+ disconnect() {
80
+ this.isDisconnected = true;
81
+ this.targets.length = 0;
82
+ }
83
+ }
84
+ /** An `IntersectionObserver` whose `observe()` records the element; `flush` delivers one entry to the observers of an element. */
85
+ export class FakeIntersectionObserver {
86
+ /** The observers created since the last `install`, in creation order. */
87
+ static { this.all = []; }
88
+ constructor(callback, options) {
89
+ this.callback = callback;
90
+ this.options = options;
91
+ this.root = null;
92
+ this.rootMargin = '';
93
+ this.scrollMargin = '';
94
+ this.thresholds = [];
95
+ /** The elements observed now. */
96
+ this.targets = [];
97
+ this.isDisconnected = false;
98
+ FakeIntersectionObserver.all.push(this);
99
+ }
100
+ /** Replaces `globalThis.IntersectionObserver` with the fake and forgets the earlier observers. */
101
+ static install() {
102
+ FakeIntersectionObserver.all = [];
103
+ return swap({
104
+ target: globalThis,
105
+ name: 'IntersectionObserver',
106
+ value: FakeIntersectionObserver,
107
+ });
108
+ }
109
+ /** Calls every observer of `element` with one entry. `ratio` is 1 when the element intersects and 0 when not, unless given. */
110
+ static flush(opts) {
111
+ const { element, isIntersecting, ratio = isIntersecting ? 1 : 0 } = opts;
112
+ const box = { x: 0, y: 0, width: 10, height: 10, top: 0, left: 0, right: 10, bottom: 10 };
113
+ const rect = { ...box, toJSON: () => box };
114
+ const entry = {
115
+ target: element,
116
+ isIntersecting,
117
+ intersectionRatio: ratio,
118
+ boundingClientRect: rect,
119
+ intersectionRect: rect,
120
+ rootBounds: null,
121
+ time: 0,
122
+ };
123
+ for (const observer of FakeIntersectionObserver.all) {
124
+ if (observer.targets.includes(element)) {
125
+ observer.callback([entry], observer);
126
+ }
127
+ }
128
+ }
129
+ observe(target) {
130
+ if (!this.targets.includes(target)) {
131
+ this.targets.push(target);
132
+ }
133
+ }
134
+ unobserve(target) {
135
+ drop({ list: this.targets, item: target });
136
+ }
137
+ disconnect() {
138
+ this.isDisconnected = true;
139
+ this.targets.length = 0;
140
+ }
141
+ takeRecords() {
142
+ return [];
143
+ }
144
+ }
145
+ /** The `MediaQueryList` that `matchMedia` answers under `FakeMatchMedia.install`: one per query, `false` until `set`. */
146
+ export class FakeMatchMedia extends EventTarget {
147
+ /** The queries asked since the last `install`, with the list each one got. */
148
+ static { this.queries = new Map(); }
149
+ constructor(media, matches = false) {
150
+ super();
151
+ this.media = media;
152
+ this.matches = matches;
153
+ }
154
+ /** Replaces `window.matchMedia` with a function that answers the list of the query. */
155
+ static install() {
156
+ FakeMatchMedia.queries.clear();
157
+ return swap({
158
+ target: globalThis,
159
+ name: 'matchMedia',
160
+ value: (query) => FakeMatchMedia.of(query),
161
+ });
162
+ }
163
+ /** The list of `query`, made on the first ask. */
164
+ static of(query) {
165
+ const list = FakeMatchMedia.queries.get(query) ?? new FakeMatchMedia(query);
166
+ FakeMatchMedia.queries.set(query, list);
167
+ return list;
168
+ }
169
+ /** Sets whether `query` matches and tells its listeners with a `change` event. */
170
+ static set(opts) {
171
+ const list = FakeMatchMedia.of(opts.query);
172
+ list.matches = opts.matches;
173
+ list.dispatchEvent(new Event('change'));
174
+ }
175
+ }
176
+ /**
177
+ * A `Storage` that behaves like the real one: stored keys are own properties, so `Object.keys` lists
178
+ * them. Set `failReads` for a blocked storage (reads throw `SecurityError`) and `failWrites` for a
179
+ * full or blocked one (`setItem` and `clear` throw `QuotaExceededError`, `removeItem` throws
180
+ * `SecurityError`); both can change at any time. A key named like a member of the fake (`getItem`,
181
+ * `length`, `writes`, `failWrites` and the rest) would shadow it, so `setItem` throws a `TypeError`
182
+ * for it, where the real `Storage` accepts it.
183
+ */
184
+ export class FakeStorage {
185
+ #failReads = false;
186
+ #failWrites = false;
187
+ #writes = [];
188
+ get failReads() {
189
+ return this.#failReads;
190
+ }
191
+ set failReads(value) {
192
+ this.#failReads = value;
193
+ }
194
+ get failWrites() {
195
+ return this.#failWrites;
196
+ }
197
+ set failWrites(value) {
198
+ this.#failWrites = value;
199
+ }
200
+ /** The values passed to `setItem` that the storage accepted, in order. */
201
+ get writes() {
202
+ return this.#writes;
203
+ }
204
+ get length() {
205
+ return this.#keys().length;
206
+ }
207
+ /** Makes this the `localStorage` (the default) or the `sessionStorage` of the global. */
208
+ install(area = 'local') {
209
+ const name = area === 'session' ? 'sessionStorage' : 'localStorage';
210
+ return swap({ target: globalThis, name, value: this });
211
+ }
212
+ getItem(key) {
213
+ this.#assertReadable();
214
+ const value = Object.getOwnPropertyDescriptor(this, key)?.value;
215
+ return typeof value === 'string' ? value : null;
216
+ }
217
+ setItem(key, value) {
218
+ this.#assertWritable('The quota has been exceeded', 'QuotaExceededError');
219
+ if (Object.hasOwn(FakeStorage.prototype, key)) {
220
+ throw new TypeError(`FakeStorage cannot store the key "${key}": it is a member of the fake`);
221
+ }
222
+ const text = String(value);
223
+ Object.defineProperty(this, key, {
224
+ value: text,
225
+ enumerable: true,
226
+ configurable: true,
227
+ writable: true,
228
+ });
229
+ this.#writes.push(text);
230
+ }
231
+ removeItem(key) {
232
+ this.#assertWritable('Access is denied', 'SecurityError');
233
+ this.#assertReadable();
234
+ Reflect.deleteProperty(this, key);
235
+ }
236
+ clear() {
237
+ this.#assertWritable('The quota has been exceeded', 'QuotaExceededError');
238
+ for (const key of this.#keys()) {
239
+ Reflect.deleteProperty(this, key);
240
+ }
241
+ }
242
+ key(index) {
243
+ this.#assertReadable();
244
+ return this.#keys()[index] ?? null;
245
+ }
246
+ #keys() {
247
+ this.#assertReadable();
248
+ return Object.keys(this);
249
+ }
250
+ #assertReadable() {
251
+ if (this.#failReads) {
252
+ throw new DOMException('Access is denied', 'SecurityError');
253
+ }
254
+ }
255
+ #assertWritable(message, name) {
256
+ if (this.#failWrites) {
257
+ throw new DOMException(message, name);
258
+ }
259
+ }
260
+ }
261
+ /** A `navigator.clipboard` that keeps the text in memory; `isDenied` makes both methods reject as a refused permission does. */
262
+ export class FakeClipboard {
263
+ constructor() {
264
+ /** The text of every `writeText`, in order. */
265
+ this.written = [];
266
+ /** What `readText` answers; `writeText` replaces it. */
267
+ this.text = '';
268
+ this.isDenied = false;
269
+ }
270
+ /** Makes this the `navigator.clipboard`. */
271
+ install() {
272
+ return swap({ target: globalThis.navigator, name: 'clipboard', value: this });
273
+ }
274
+ writeText(text) {
275
+ if (this.isDenied) {
276
+ return Promise.reject(new DOMException('Write permission denied', 'NotAllowedError'));
277
+ }
278
+ this.written.push(text);
279
+ this.text = text;
280
+ return Promise.resolve();
281
+ }
282
+ readText() {
283
+ if (this.isDenied) {
284
+ return Promise.reject(new DOMException('Read permission denied', 'NotAllowedError'));
285
+ }
286
+ return Promise.resolve(this.text);
287
+ }
288
+ }
289
+ //# sourceMappingURL=testing.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@venizia/ardor-kernel",
3
- "version": "0.4.8-4",
3
+ "version": "0.4.8-5",
4
4
  "description": "ARDOR kernel - the isomorphic core of the ARDOR frontend framework: application base on IGNIS inversion, service and provider bases, request/auth constants, binding keys, the network request service, runtime config and the HTTP repository (from IGNIS). No React and no react-admin.",
5
5
  "keywords": [
6
6
  "ardor",
@@ -56,6 +56,11 @@
56
56
  "import": "./dist/base/repositories/index.js",
57
57
  "default": "./dist/base/repositories/index.js"
58
58
  },
59
+ "./testing": {
60
+ "types": "./dist/testing.d.ts",
61
+ "import": "./dist/testing.js",
62
+ "default": "./dist/testing.js"
63
+ },
59
64
  "./package.json": "./package.json"
60
65
  },
61
66
  "publishConfig": {
@@ -69,11 +74,11 @@
69
74
  "@size-limit/preset-small-lib": "^14.2.0",
70
75
  "@types/bun": "^1.4.3",
71
76
  "@venizia/dev-configs": "^0.4.8-2",
72
- "@venizia/ignis-connectors": "^0.4.8-3",
77
+ "@venizia/ignis-connectors": "^0.4.8-4",
73
78
  "@venizia/ignis-filter": "^0.4.7-2",
74
- "@venizia/ignis-helpers": "^0.4.8-3",
79
+ "@venizia/ignis-helpers": "^0.4.8-4",
75
80
  "@venizia/ignis-inversion": "^0.4.7-2",
76
- "@venizia/ignis-kernel": "^0.4.8-3",
81
+ "@venizia/ignis-kernel": "^0.4.8-4",
77
82
  "eslint": "^10.13.0",
78
83
  "oxlint": "1.89.0",
79
84
  "oxlint-tsgolint": "7.0.2004",
@@ -83,11 +88,11 @@
83
88
  "typescript": "^6.0.3"
84
89
  },
85
90
  "peerDependencies": {
86
- "@venizia/ignis-connectors": "^0.4.8-3",
91
+ "@venizia/ignis-connectors": "^0.4.8-4",
87
92
  "@venizia/ignis-filter": "^0.4.7-2",
88
- "@venizia/ignis-helpers": "^0.4.8-3",
93
+ "@venizia/ignis-helpers": "^0.4.8-4",
89
94
  "@venizia/ignis-inversion": "^0.4.7-2",
90
- "@venizia/ignis-kernel": "^0.4.8-3",
95
+ "@venizia/ignis-kernel": "^0.4.8-4",
91
96
  "reflect-metadata": ">=0.2.2"
92
97
  },
93
98
  "peerDependenciesMeta": {
@@ -105,6 +110,11 @@
105
110
  "reflect-metadata"
106
111
  ],
107
112
  "limit": "7660 B"
113
+ },
114
+ {
115
+ "path": "dist/testing.js",
116
+ "import": "*",
117
+ "limit": "1340 B"
108
118
  }
109
119
  ],
110
120
  "engines": {