@epilot/zenith-react 0.2.0-next.4 → 0.2.0-next.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.
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Test setup for consumers of this package, for Vitest and Jest alike.
3
+ *
4
+ * WHY THIS EXISTS. Base UI, which sits under Switch, Checkbox, Radio and Select, re-dispatches a
5
+ * label's click as `new PointerEvent("click")`, the event type browsers use for clicks. jsdom
6
+ * added `PointerEvent` in 27; on an older jsdom (26 and below) the constructor does not exist, the
7
+ * dispatch throws, and the click never reaches the control. Nothing is wrong in the app.
8
+ *
9
+ * It installs a `PointerEvent` only where there is none. It mirrors Chromium's defaults for
10
+ * `new PointerEvent(type)`, and steps aside wherever the environment already has one, so it is
11
+ * safe to keep after a jsdom upgrade. It does nothing outside a DOM (a Node test environment).
12
+ *
13
+ * USAGE
14
+ *
15
+ * // vitest.config.ts
16
+ * export default defineConfig({
17
+ * test: {
18
+ * environment: "jsdom",
19
+ * setupFiles: ["@epilot/zenith-react/testing/setup.cjs"],
20
+ * },
21
+ * })
22
+ *
23
+ * // jest.config.cjs
24
+ * const griffel = require("@epilot/zenith-react/testing/jest-preset.cjs")
25
+ *
26
+ * module.exports = griffel.withGriffel(
27
+ * {
28
+ * testEnvironment: "jsdom",
29
+ * setupFiles: ["@epilot/zenith-react/testing/setup.cjs"],
30
+ * // Jest 28+ resolves package exports with the `require` condition, which
31
+ * // @epilot/zenith-react does not have: without the mapping the first import
32
+ * // fails with "Cannot find module".
33
+ * moduleNameMapper: {
34
+ * "^@epilot/zenith-react$":
35
+ * "<rootDir>/node_modules/@epilot/zenith-react/dist/index.js",
36
+ * "^@epilot/zenith-runtime/v1$":
37
+ * "<rootDir>/node_modules/@epilot/zenith-runtime/dist/v1/index.js",
38
+ * },
39
+ * },
40
+ * { alsoTransform: ["@epilot/zenith-runtime", "@base-ui", "@floating-ui"] }
41
+ * )
42
+ *
43
+ * Keep it in `setupFiles` (it runs before the test framework and the first import of a component);
44
+ * a setup run later still works, but a module that read `PointerEvent` at import time would not
45
+ * see it.
46
+ */
47
+ function installPointerEvent(scope) {
48
+ if (typeof scope.MouseEvent === "undefined") return false
49
+ if (typeof scope.PointerEvent !== "undefined") return false
50
+
51
+ class PointerEvent extends scope.MouseEvent {
52
+ constructor(type, init = {}) {
53
+ super(type, init)
54
+ this.pointerId = init.pointerId ?? 0
55
+ this.width = init.width ?? 1
56
+ this.height = init.height ?? 1
57
+ this.pressure = init.pressure ?? 0
58
+ this.tangentialPressure = init.tangentialPressure ?? 0
59
+ this.tiltX = init.tiltX ?? 0
60
+ this.tiltY = init.tiltY ?? 0
61
+ this.twist = init.twist ?? 0
62
+ this.pointerType = init.pointerType ?? ""
63
+ this.isPrimary = init.isPrimary ?? false
64
+ this._coalescedEvents = init.coalescedEvents ?? []
65
+ this._predictedEvents = init.predictedEvents ?? []
66
+ }
67
+
68
+ getCoalescedEvents() {
69
+ return [...this._coalescedEvents]
70
+ }
71
+
72
+ getPredictedEvents() {
73
+ return [...this._predictedEvents]
74
+ }
75
+ }
76
+
77
+ scope.PointerEvent = PointerEvent
78
+ // Vitest copies the jsdom window onto `globalThis` once; the class has to reach both.
79
+ if (typeof globalThis !== "undefined" && globalThis !== scope) {
80
+ globalThis.PointerEvent = PointerEvent
81
+ }
82
+ return true
83
+ }
84
+
85
+ if (typeof window !== "undefined") installPointerEvent(window)
86
+
87
+ module.exports = { installPointerEvent }