@praxisjs/composables 0.1.5 → 1.0.0
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/CHANGELOG.md +47 -0
- package/dist/__tests__/browser.test.js +151 -82
- package/dist/__tests__/browser.test.js.map +1 -1
- package/dist/__tests__/dom.test.js +168 -124
- package/dist/__tests__/dom.test.js.map +1 -1
- package/dist/__tests__/utilities.test.js +155 -193
- package/dist/__tests__/utilities.test.js.map +1 -1
- package/dist/browser.d.ts +55 -12
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +105 -49
- package/dist/browser.js.map +1 -1
- package/dist/dom.d.ts +60 -25
- package/dist/dom.d.ts.map +1 -1
- package/dist/dom.js +108 -74
- package/dist/dom.js.map +1 -1
- package/dist/index.d.ts +3 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/utilities.d.ts +68 -26
- package/dist/utilities.d.ts.map +1 -1
- package/dist/utilities.js +102 -94
- package/dist/utilities.js.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/browser.test.ts +172 -92
- package/src/__tests__/dom.test.ts +184 -135
- package/src/__tests__/utilities.test.ts +185 -213
- package/src/browser.ts +134 -53
- package/src/dom.ts +129 -82
- package/src/index.ts +11 -18
- package/src/utilities.ts +144 -111
|
@@ -1,147 +1,227 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
|
-
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { MediaQuery, ColorScheme, Mouse, KeyCombo, Idle } from "../browser";
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
function mockMatchMedia(matches: boolean, onAdd?: (_: string, fn: (e: MediaQueryListEvent) => void) => void) {
|
|
7
|
+
const mql = {
|
|
8
|
+
matches,
|
|
9
|
+
addEventListener: onAdd ?? vi.fn(),
|
|
10
|
+
removeEventListener: vi.fn(),
|
|
11
|
+
} as unknown as MediaQueryList;
|
|
12
|
+
vi.stubGlobal("matchMedia", () => mql);
|
|
13
|
+
return mql;
|
|
14
|
+
}
|
|
7
15
|
|
|
8
|
-
|
|
9
|
-
it("returns computed that reflects matchMedia result", () => {
|
|
10
|
-
Object.defineProperty(window, "matchMedia", {
|
|
11
|
-
writable: true,
|
|
12
|
-
value: vi.fn((query: string) => ({
|
|
13
|
-
matches: query === "(prefers-color-scheme: dark)",
|
|
14
|
-
addEventListener: vi.fn(),
|
|
15
|
-
removeEventListener: vi.fn(),
|
|
16
|
-
})),
|
|
17
|
-
});
|
|
16
|
+
// ── MediaQuery ────────────────────────────────────────────────────────────────
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
describe("MediaQuery", () => {
|
|
19
|
+
beforeEach(() => { vi.restoreAllMocks(); });
|
|
21
20
|
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
it("reads initial match state", () => {
|
|
22
|
+
mockMatchMedia(true);
|
|
23
|
+
const mq = new MediaQuery("(min-width: 768px)");
|
|
24
|
+
const { matches } = mq.setup() as { matches: () => boolean };
|
|
25
|
+
expect(matches()).toBe(true);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("updates matches when change event fires", () => {
|
|
29
|
+
let changeHandler: ((e: MediaQueryListEvent) => void) | undefined;
|
|
30
|
+
mockMatchMedia(false, (_: string, fn) => { changeHandler = fn; });
|
|
31
|
+
|
|
32
|
+
const mq = new MediaQuery("(min-width: 768px)");
|
|
33
|
+
const { matches } = mq.setup() as { matches: () => boolean };
|
|
34
|
+
expect(matches()).toBe(false);
|
|
35
|
+
|
|
36
|
+
changeHandler!({ matches: true } as MediaQueryListEvent);
|
|
37
|
+
expect(matches()).toBe(true);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("removes event listener on unmount", () => {
|
|
41
|
+
const mql = mockMatchMedia(false);
|
|
42
|
+
const mq = new MediaQuery("(min-width: 768px)");
|
|
43
|
+
mq.setup();
|
|
44
|
+
mq.onUnmount();
|
|
45
|
+
expect((mql.removeEventListener as ReturnType<typeof vi.fn>)).toHaveBeenCalledWith("change", expect.any(Function));
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("onUnmount is safe to call without setup (covers false branch)", () => {
|
|
49
|
+
const mq = new MediaQuery("(min-width: 768px)");
|
|
50
|
+
expect(() => mq.onUnmount()).not.toThrow();
|
|
24
51
|
});
|
|
25
52
|
});
|
|
26
53
|
|
|
27
|
-
//
|
|
54
|
+
// ── ColorScheme ───────────────────────────────────────────────────────────────
|
|
55
|
+
|
|
56
|
+
describe("ColorScheme", () => {
|
|
57
|
+
beforeEach(() => { vi.restoreAllMocks(); });
|
|
58
|
+
|
|
59
|
+
it("exposes isDark and isLight as computed inverses", () => {
|
|
60
|
+
let changeHandler: ((e: MediaQueryListEvent) => void) | undefined;
|
|
61
|
+
mockMatchMedia(false, (_: string, fn) => { changeHandler = fn; });
|
|
62
|
+
|
|
63
|
+
const cs = new ColorScheme();
|
|
64
|
+
const { isDark, isLight } = cs.setup() as { isDark: () => boolean; isLight: () => boolean };
|
|
28
65
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
Object.defineProperty(window, "matchMedia", {
|
|
32
|
-
writable: true,
|
|
33
|
-
value: vi.fn(() => ({
|
|
34
|
-
matches: true,
|
|
35
|
-
addEventListener: vi.fn(),
|
|
36
|
-
removeEventListener: vi.fn(),
|
|
37
|
-
})),
|
|
38
|
-
});
|
|
66
|
+
expect(isDark()).toBe(false);
|
|
67
|
+
expect(isLight()).toBe(true);
|
|
39
68
|
|
|
40
|
-
|
|
69
|
+
changeHandler!({ matches: true } as MediaQueryListEvent);
|
|
41
70
|
expect(isDark()).toBe(true);
|
|
42
71
|
expect(isLight()).toBe(false);
|
|
43
72
|
});
|
|
73
|
+
|
|
74
|
+
it("removes event listener on unmount", () => {
|
|
75
|
+
const mql = mockMatchMedia(false);
|
|
76
|
+
const cs = new ColorScheme();
|
|
77
|
+
cs.setup();
|
|
78
|
+
cs.onUnmount();
|
|
79
|
+
expect((mql.removeEventListener as ReturnType<typeof vi.fn>)).toHaveBeenCalledWith("change", expect.any(Function));
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("onUnmount is safe to call without setup (covers false branch)", () => {
|
|
83
|
+
const cs = new ColorScheme();
|
|
84
|
+
expect(() => cs.onUnmount()).not.toThrow();
|
|
85
|
+
});
|
|
44
86
|
});
|
|
45
87
|
|
|
46
|
-
//
|
|
88
|
+
// ── Mouse ─────────────────────────────────────────────────────────────────────
|
|
47
89
|
|
|
48
|
-
describe("
|
|
90
|
+
describe("Mouse", () => {
|
|
49
91
|
it("starts at (0, 0)", () => {
|
|
50
|
-
const
|
|
92
|
+
const mouse = new Mouse();
|
|
93
|
+
const { x, y } = mouse.setup() as { x: () => number; y: () => number };
|
|
51
94
|
expect(x()).toBe(0);
|
|
52
95
|
expect(y()).toBe(0);
|
|
53
96
|
});
|
|
54
97
|
|
|
55
|
-
it("updates on mousemove
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
);
|
|
60
|
-
expect(x()).toBe(
|
|
61
|
-
expect(y()).toBe(
|
|
98
|
+
it("updates x and y on mousemove", () => {
|
|
99
|
+
const mouse = new Mouse();
|
|
100
|
+
const { x, y } = mouse.setup() as { x: () => number; y: () => number };
|
|
101
|
+
|
|
102
|
+
window.dispatchEvent(new MouseEvent("mousemove", { clientX: 120, clientY: 80 }));
|
|
103
|
+
expect(x()).toBe(120);
|
|
104
|
+
expect(y()).toBe(80);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("removes listener on unmount", () => {
|
|
108
|
+
const remove = vi.spyOn(window, "removeEventListener");
|
|
109
|
+
const mouse = new Mouse();
|
|
110
|
+
mouse.setup();
|
|
111
|
+
mouse.onUnmount();
|
|
112
|
+
expect(remove).toHaveBeenCalledWith("mousemove", expect.any(Function));
|
|
62
113
|
});
|
|
63
114
|
});
|
|
64
115
|
|
|
65
|
-
//
|
|
116
|
+
// ── KeyCombo ──────────────────────────────────────────────────────────────────
|
|
66
117
|
|
|
67
|
-
describe("
|
|
68
|
-
it("starts as
|
|
69
|
-
const
|
|
118
|
+
describe("KeyCombo", () => {
|
|
119
|
+
it("starts as not pressed", () => {
|
|
120
|
+
const kc = new KeyCombo("ctrl+s");
|
|
121
|
+
const { pressed } = kc.setup() as { pressed: () => boolean };
|
|
70
122
|
expect(pressed()).toBe(false);
|
|
71
123
|
});
|
|
72
124
|
|
|
73
|
-
it("
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
);
|
|
125
|
+
it("sets pressed=true when matching combo is pressed", () => {
|
|
126
|
+
const kc = new KeyCombo("ctrl+s");
|
|
127
|
+
const { pressed } = kc.setup() as { pressed: () => boolean };
|
|
128
|
+
|
|
129
|
+
window.dispatchEvent(new KeyboardEvent("keydown", { ctrlKey: true, key: "s" }));
|
|
78
130
|
expect(pressed()).toBe(true);
|
|
79
131
|
});
|
|
80
132
|
|
|
81
|
-
it("resets
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
);
|
|
133
|
+
it("resets pressed on keyup", () => {
|
|
134
|
+
const kc = new KeyCombo("ctrl+s");
|
|
135
|
+
const { pressed } = kc.setup() as { pressed: () => boolean };
|
|
136
|
+
|
|
137
|
+
window.dispatchEvent(new KeyboardEvent("keydown", { ctrlKey: true, key: "s" }));
|
|
86
138
|
window.dispatchEvent(new KeyboardEvent("keyup"));
|
|
87
139
|
expect(pressed()).toBe(false);
|
|
88
140
|
});
|
|
141
|
+
|
|
142
|
+
it("does not set pressed for wrong key", () => {
|
|
143
|
+
const kc = new KeyCombo("ctrl+s");
|
|
144
|
+
const { pressed } = kc.setup() as { pressed: () => boolean };
|
|
145
|
+
|
|
146
|
+
window.dispatchEvent(new KeyboardEvent("keydown", { ctrlKey: true, key: "z" }));
|
|
147
|
+
expect(pressed()).toBe(false);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("removes listeners on unmount", () => {
|
|
151
|
+
const remove = vi.spyOn(window, "removeEventListener");
|
|
152
|
+
const kc = new KeyCombo("ctrl+s");
|
|
153
|
+
kc.setup();
|
|
154
|
+
kc.onUnmount();
|
|
155
|
+
expect(remove).toHaveBeenCalledWith("keydown", expect.any(Function));
|
|
156
|
+
expect(remove).toHaveBeenCalledWith("keyup", expect.any(Function));
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("matches combo without ctrl modifier (ctrl branch: `: true`)", () => {
|
|
160
|
+
const kc = new KeyCombo("s");
|
|
161
|
+
const { pressed } = kc.setup() as { pressed: () => boolean };
|
|
162
|
+
// No ctrlKey needed — parts doesn't include "ctrl" so ctrl = true always
|
|
163
|
+
window.dispatchEvent(new KeyboardEvent("keydown", { key: "s" }));
|
|
164
|
+
expect(pressed()).toBe(true);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("matches combo with shift modifier (shift branch: `e.shiftKey`)", () => {
|
|
168
|
+
const kc = new KeyCombo("ctrl+shift+s");
|
|
169
|
+
const { pressed } = kc.setup() as { pressed: () => boolean };
|
|
170
|
+
window.dispatchEvent(new KeyboardEvent("keydown", { ctrlKey: true, shiftKey: true, key: "s" }));
|
|
171
|
+
expect(pressed()).toBe(true);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("does not match when shift is required but not pressed", () => {
|
|
175
|
+
const kc = new KeyCombo("ctrl+shift+s");
|
|
176
|
+
const { pressed } = kc.setup() as { pressed: () => boolean };
|
|
177
|
+
window.dispatchEvent(new KeyboardEvent("keydown", { ctrlKey: true, shiftKey: false, key: "s" }));
|
|
178
|
+
expect(pressed()).toBe(false);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("matches combo with alt modifier (alt branch: `e.altKey`)", () => {
|
|
182
|
+
const kc = new KeyCombo("ctrl+alt+s");
|
|
183
|
+
const { pressed } = kc.setup() as { pressed: () => boolean };
|
|
184
|
+
window.dispatchEvent(new KeyboardEvent("keydown", { ctrlKey: true, altKey: true, key: "s" }));
|
|
185
|
+
expect(pressed()).toBe(true);
|
|
186
|
+
});
|
|
89
187
|
});
|
|
90
188
|
|
|
91
|
-
//
|
|
189
|
+
// ── Idle ──────────────────────────────────────────────────────────────────────
|
|
92
190
|
|
|
93
|
-
describe("
|
|
191
|
+
describe("Idle", () => {
|
|
94
192
|
it("starts as not idle", () => {
|
|
95
193
|
vi.useFakeTimers();
|
|
96
|
-
const idle =
|
|
97
|
-
|
|
194
|
+
const idle = new Idle(500);
|
|
195
|
+
const { idle: isIdle } = idle.setup() as { idle: () => boolean };
|
|
196
|
+
expect(isIdle()).toBe(false);
|
|
98
197
|
vi.useRealTimers();
|
|
99
198
|
});
|
|
100
199
|
|
|
101
|
-
it("becomes idle after
|
|
200
|
+
it("becomes idle after timeout", () => {
|
|
102
201
|
vi.useFakeTimers();
|
|
103
|
-
const
|
|
202
|
+
const composable = new Idle(500);
|
|
203
|
+
const { idle: isIdle } = composable.setup() as { idle: () => boolean };
|
|
104
204
|
vi.advanceTimersByTime(500);
|
|
105
|
-
expect(
|
|
205
|
+
expect(isIdle()).toBe(true);
|
|
106
206
|
vi.useRealTimers();
|
|
107
207
|
});
|
|
108
208
|
|
|
109
|
-
it("resets idle on
|
|
209
|
+
it("resets idle timer on activity", () => {
|
|
110
210
|
vi.useFakeTimers();
|
|
111
|
-
const
|
|
112
|
-
|
|
211
|
+
const composable = new Idle(500);
|
|
212
|
+
const { idle: isIdle } = composable.setup() as { idle: () => boolean };
|
|
213
|
+
vi.advanceTimersByTime(400);
|
|
113
214
|
window.dispatchEvent(new MouseEvent("mousemove"));
|
|
114
|
-
vi.advanceTimersByTime(
|
|
115
|
-
expect(
|
|
215
|
+
vi.advanceTimersByTime(400);
|
|
216
|
+
expect(isIdle()).toBe(false);
|
|
116
217
|
vi.useRealTimers();
|
|
117
218
|
});
|
|
118
|
-
});
|
|
119
219
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
writable: true,
|
|
127
|
-
value: vi.fn(() => ({
|
|
128
|
-
matches: false,
|
|
129
|
-
addEventListener: vi.fn((_event: string, handler: (e: MediaQueryListEvent) => void) => {
|
|
130
|
-
changeHandler = handler;
|
|
131
|
-
}),
|
|
132
|
-
removeEventListener: vi.fn(),
|
|
133
|
-
})),
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
const matches = useMediaQuery("(min-width: 768px)");
|
|
137
|
-
expect(matches()).toBe(false);
|
|
138
|
-
|
|
139
|
-
// Simulate the media query becoming true
|
|
140
|
-
changeHandler!({ matches: true } as MediaQueryListEvent);
|
|
141
|
-
expect(matches()).toBe(true);
|
|
142
|
-
|
|
143
|
-
// Simulate it reverting
|
|
144
|
-
changeHandler!({ matches: false } as MediaQueryListEvent);
|
|
145
|
-
expect(matches()).toBe(false);
|
|
220
|
+
it("removes listeners on unmount", () => {
|
|
221
|
+
const remove = vi.spyOn(window, "removeEventListener");
|
|
222
|
+
const composable = new Idle(500);
|
|
223
|
+
composable.setup();
|
|
224
|
+
composable.onUnmount();
|
|
225
|
+
expect(remove).toHaveBeenCalled();
|
|
146
226
|
});
|
|
147
227
|
});
|