@pyreon/hooks 0.11.1 → 0.11.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/package.json +8 -7
- package/src/__tests__/useBreakpoint.test.ts +148 -0
- package/src/__tests__/useClickOutside.test.ts +144 -0
- package/src/__tests__/useColorScheme.test.ts +101 -0
- package/src/__tests__/useControllableState.test.ts +45 -0
- package/src/__tests__/useDebouncedCallback.test.ts +80 -0
- package/src/__tests__/useDebouncedValue.test.ts +112 -0
- package/src/__tests__/useElementSize.test.ts +190 -0
- package/src/__tests__/useFocus.test.ts +52 -0
- package/src/__tests__/useFocusTrap.test.ts +162 -0
- package/src/__tests__/useHover.test.ts +62 -0
- package/src/__tests__/useIntersection.test.ts +154 -0
- package/src/__tests__/useInterval.test.ts +48 -0
- package/src/__tests__/useIsomorphicLayoutEffect.test.ts +9 -0
- package/src/__tests__/useKeyboard.test.ts +144 -0
- package/src/__tests__/useLatest.test.ts +24 -0
- package/src/__tests__/useMediaQuery.test.ts +143 -0
- package/src/__tests__/useMergedRef.test.ts +48 -0
- package/src/__tests__/usePrevious.test.ts +70 -0
- package/src/__tests__/useReducedMotion.test.ts +96 -0
- package/src/__tests__/useRootSize.test.ts +27 -0
- package/src/__tests__/useScrollLock.test.ts +123 -0
- package/src/__tests__/useSpacing.test.ts +23 -0
- package/src/__tests__/useThemeValue.test.ts +12 -0
- package/src/__tests__/useThrottledCallback.test.ts +56 -0
- package/src/__tests__/useTimeout.test.ts +54 -0
- package/src/__tests__/useToggle.test.ts +82 -0
- package/src/__tests__/useUpdateEffect.test.ts +48 -0
- package/src/__tests__/useWindowResize.test.ts +139 -0
- package/src/index.ts +56 -0
- package/src/useBreakpoint.ts +52 -0
- package/src/useClickOutside.ts +23 -0
- package/src/useClipboard.ts +51 -0
- package/src/useColorScheme.ts +10 -0
- package/src/useControllableState.ts +39 -0
- package/src/useDebouncedCallback.ts +57 -0
- package/src/useDebouncedValue.ts +24 -0
- package/src/useDialog.ts +83 -0
- package/src/useElementSize.ts +38 -0
- package/src/useEventListener.ts +31 -0
- package/src/useFocus.ts +24 -0
- package/src/useFocusTrap.ts +42 -0
- package/src/useHover.ts +30 -0
- package/src/useInfiniteScroll.ts +115 -0
- package/src/useIntersection.ts +30 -0
- package/src/useInterval.ts +31 -0
- package/src/useIsomorphicLayoutEffect.ts +19 -0
- package/src/useKeyboard.ts +28 -0
- package/src/useLatest.ts +17 -0
- package/src/useMediaQuery.ts +27 -0
- package/src/useMergedRef.ts +24 -0
- package/src/useOnline.ts +31 -0
- package/src/usePrevious.ts +18 -0
- package/src/useReducedMotion.ts +8 -0
- package/src/useRootSize.ts +28 -0
- package/src/useScrollLock.ts +37 -0
- package/src/useSpacing.ts +26 -0
- package/src/useThemeValue.ts +21 -0
- package/src/useThrottledCallback.ts +30 -0
- package/src/useTimeAgo.ts +134 -0
- package/src/useTimeout.ts +42 -0
- package/src/useToggle.ts +22 -0
- package/src/useUpdateEffect.ts +24 -0
- package/src/useWindowResize.ts +39 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest"
|
|
2
|
+
|
|
3
|
+
let mountCallbacks: Array<() => void> = []
|
|
4
|
+
let unmountCallbacks: Array<() => void> = []
|
|
5
|
+
|
|
6
|
+
vi.mock("@pyreon/core", () => ({
|
|
7
|
+
onMount: (fn: () => unknown) => {
|
|
8
|
+
mountCallbacks.push(fn as () => void)
|
|
9
|
+
},
|
|
10
|
+
onUnmount: (fn: () => void) => {
|
|
11
|
+
unmountCallbacks.push(fn)
|
|
12
|
+
},
|
|
13
|
+
}))
|
|
14
|
+
|
|
15
|
+
import { useKeyboard } from "../useKeyboard"
|
|
16
|
+
|
|
17
|
+
describe("useKeyboard", () => {
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
mountCallbacks = []
|
|
20
|
+
unmountCallbacks = []
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it("calls handler when the target key is pressed", () => {
|
|
24
|
+
const handler = vi.fn()
|
|
25
|
+
useKeyboard("Enter", handler)
|
|
26
|
+
mountCallbacks.forEach((cb) => {
|
|
27
|
+
cb()
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
const event = new KeyboardEvent("keydown", { key: "Enter", bubbles: true })
|
|
31
|
+
document.dispatchEvent(event)
|
|
32
|
+
|
|
33
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
34
|
+
expect(handler).toHaveBeenCalledWith(event)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it("does not call handler for different keys", () => {
|
|
38
|
+
const handler = vi.fn()
|
|
39
|
+
useKeyboard("Enter", handler)
|
|
40
|
+
mountCallbacks.forEach((cb) => {
|
|
41
|
+
cb()
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const event = new KeyboardEvent("keydown", { key: "Escape", bubbles: true })
|
|
45
|
+
document.dispatchEvent(event)
|
|
46
|
+
|
|
47
|
+
expect(handler).not.toHaveBeenCalled()
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it("listens for keydown by default", () => {
|
|
51
|
+
const handler = vi.fn()
|
|
52
|
+
const addSpy = vi.spyOn(document, "addEventListener")
|
|
53
|
+
useKeyboard("Escape", handler)
|
|
54
|
+
mountCallbacks.forEach((cb) => {
|
|
55
|
+
cb()
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
expect(addSpy).toHaveBeenCalledWith("keydown", expect.any(Function))
|
|
59
|
+
addSpy.mockRestore()
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it("supports keyup event option", () => {
|
|
63
|
+
const handler = vi.fn()
|
|
64
|
+
useKeyboard("Space", handler, { event: "keyup" })
|
|
65
|
+
mountCallbacks.forEach((cb) => {
|
|
66
|
+
cb()
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
const keydownEvent = new KeyboardEvent("keydown", { key: "Space", bubbles: true })
|
|
70
|
+
document.dispatchEvent(keydownEvent)
|
|
71
|
+
expect(handler).not.toHaveBeenCalled()
|
|
72
|
+
|
|
73
|
+
const keyupEvent = new KeyboardEvent("keyup", { key: "Space", bubbles: true })
|
|
74
|
+
document.dispatchEvent(keyupEvent)
|
|
75
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it("supports custom target", () => {
|
|
79
|
+
const handler = vi.fn()
|
|
80
|
+
const customTarget = document.createElement("div")
|
|
81
|
+
document.body.appendChild(customTarget)
|
|
82
|
+
const addSpy = vi.spyOn(customTarget, "addEventListener")
|
|
83
|
+
|
|
84
|
+
useKeyboard("Enter", handler, { target: customTarget })
|
|
85
|
+
mountCallbacks.forEach((cb) => {
|
|
86
|
+
cb()
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
expect(addSpy).toHaveBeenCalledWith("keydown", expect.any(Function))
|
|
90
|
+
|
|
91
|
+
const event = new KeyboardEvent("keydown", { key: "Enter", bubbles: true })
|
|
92
|
+
customTarget.dispatchEvent(event)
|
|
93
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
94
|
+
|
|
95
|
+
addSpy.mockRestore()
|
|
96
|
+
document.body.removeChild(customTarget)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it("removes listeners on unmount", () => {
|
|
100
|
+
const handler = vi.fn()
|
|
101
|
+
const removeSpy = vi.spyOn(document, "removeEventListener")
|
|
102
|
+
useKeyboard("Enter", handler)
|
|
103
|
+
mountCallbacks.forEach((cb) => {
|
|
104
|
+
cb()
|
|
105
|
+
})
|
|
106
|
+
unmountCallbacks.forEach((cb) => {
|
|
107
|
+
cb()
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
expect(removeSpy).toHaveBeenCalledWith("keydown", expect.any(Function))
|
|
111
|
+
removeSpy.mockRestore()
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it("removes listeners from custom target on unmount", () => {
|
|
115
|
+
const handler = vi.fn()
|
|
116
|
+
const customTarget = document.createElement("div")
|
|
117
|
+
const removeSpy = vi.spyOn(customTarget, "removeEventListener")
|
|
118
|
+
|
|
119
|
+
useKeyboard("Enter", handler, { target: customTarget })
|
|
120
|
+
mountCallbacks.forEach((cb) => {
|
|
121
|
+
cb()
|
|
122
|
+
})
|
|
123
|
+
unmountCallbacks.forEach((cb) => {
|
|
124
|
+
cb()
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
expect(removeSpy).toHaveBeenCalledWith("keydown", expect.any(Function))
|
|
128
|
+
removeSpy.mockRestore()
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it("calls handler multiple times for repeated key presses", () => {
|
|
132
|
+
const handler = vi.fn()
|
|
133
|
+
useKeyboard("a", handler)
|
|
134
|
+
mountCallbacks.forEach((cb) => {
|
|
135
|
+
cb()
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
document.dispatchEvent(new KeyboardEvent("keydown", { key: "a" }))
|
|
139
|
+
document.dispatchEvent(new KeyboardEvent("keydown", { key: "a" }))
|
|
140
|
+
document.dispatchEvent(new KeyboardEvent("keydown", { key: "a" }))
|
|
141
|
+
|
|
142
|
+
expect(handler).toHaveBeenCalledTimes(3)
|
|
143
|
+
})
|
|
144
|
+
})
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest"
|
|
2
|
+
import { useLatest } from "../useLatest"
|
|
3
|
+
|
|
4
|
+
describe("useLatest", () => {
|
|
5
|
+
it("returns a ref with the current value", () => {
|
|
6
|
+
const ref = useLatest(42)
|
|
7
|
+
expect(ref.current).toBe(42)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it("ref object can be manually updated", () => {
|
|
11
|
+
const ref = useLatest("a")
|
|
12
|
+
expect(ref.current).toBe("a")
|
|
13
|
+
|
|
14
|
+
// In Pyreon, since component runs once, the caller updates .current manually
|
|
15
|
+
;(ref as { current: string }).current = "b"
|
|
16
|
+
expect(ref.current).toBe("b")
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it("returns a stable ref identity", () => {
|
|
20
|
+
const ref = useLatest("hello")
|
|
21
|
+
const same = ref
|
|
22
|
+
expect(same).toBe(ref)
|
|
23
|
+
})
|
|
24
|
+
})
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest"
|
|
2
|
+
|
|
3
|
+
let mountCallbacks: Array<() => unknown> = []
|
|
4
|
+
let unmountCallbacks: Array<() => void> = []
|
|
5
|
+
|
|
6
|
+
vi.mock("@pyreon/core", () => ({
|
|
7
|
+
onMount: (fn: () => unknown) => {
|
|
8
|
+
mountCallbacks.push(fn)
|
|
9
|
+
},
|
|
10
|
+
onUnmount: (fn: () => void) => {
|
|
11
|
+
unmountCallbacks.push(fn)
|
|
12
|
+
},
|
|
13
|
+
}))
|
|
14
|
+
|
|
15
|
+
import { useMediaQuery } from "../useMediaQuery"
|
|
16
|
+
|
|
17
|
+
describe("useMediaQuery", () => {
|
|
18
|
+
let listeners: Map<string, (e: MediaQueryListEvent) => void>
|
|
19
|
+
let matchMediaMock: ReturnType<typeof vi.fn>
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
mountCallbacks = []
|
|
23
|
+
unmountCallbacks = []
|
|
24
|
+
listeners = new Map()
|
|
25
|
+
|
|
26
|
+
matchMediaMock = vi.fn((query: string) => {
|
|
27
|
+
const mql = {
|
|
28
|
+
matches: false,
|
|
29
|
+
media: query,
|
|
30
|
+
addEventListener: vi.fn((event: string, cb: (e: MediaQueryListEvent) => void) => {
|
|
31
|
+
if (event === "change") listeners.set(query, cb)
|
|
32
|
+
}),
|
|
33
|
+
removeEventListener: vi.fn((event: string) => {
|
|
34
|
+
if (event === "change") listeners.delete(query)
|
|
35
|
+
}),
|
|
36
|
+
dispatchEvent: vi.fn(),
|
|
37
|
+
onchange: null,
|
|
38
|
+
addListener: vi.fn(),
|
|
39
|
+
removeListener: vi.fn(),
|
|
40
|
+
}
|
|
41
|
+
return mql
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
Object.defineProperty(window, "matchMedia", {
|
|
45
|
+
writable: true,
|
|
46
|
+
value: matchMediaMock,
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it("returns false initially before mount", () => {
|
|
51
|
+
const matches = useMediaQuery("(min-width: 768px)")
|
|
52
|
+
expect(matches()).toBe(false)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it("returns the current match state after mount", () => {
|
|
56
|
+
matchMediaMock.mockReturnValue({
|
|
57
|
+
matches: true,
|
|
58
|
+
media: "(min-width: 768px)",
|
|
59
|
+
addEventListener: vi.fn(),
|
|
60
|
+
removeEventListener: vi.fn(),
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
const matches = useMediaQuery("(min-width: 768px)")
|
|
64
|
+
mountCallbacks.forEach((cb) => {
|
|
65
|
+
cb()
|
|
66
|
+
})
|
|
67
|
+
expect(matches()).toBe(true)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it("updates when media query changes", () => {
|
|
71
|
+
const mql = {
|
|
72
|
+
matches: false,
|
|
73
|
+
media: "(min-width: 768px)",
|
|
74
|
+
addEventListener: vi.fn((event: string, cb: (e: MediaQueryListEvent) => void) => {
|
|
75
|
+
if (event === "change") listeners.set("(min-width: 768px)", cb)
|
|
76
|
+
}),
|
|
77
|
+
removeEventListener: vi.fn(),
|
|
78
|
+
}
|
|
79
|
+
matchMediaMock.mockReturnValue(mql)
|
|
80
|
+
|
|
81
|
+
const matches = useMediaQuery("(min-width: 768px)")
|
|
82
|
+
mountCallbacks.forEach((cb) => {
|
|
83
|
+
cb()
|
|
84
|
+
})
|
|
85
|
+
expect(matches()).toBe(false)
|
|
86
|
+
|
|
87
|
+
// Simulate media query change
|
|
88
|
+
const changeListener = listeners.get("(min-width: 768px)")
|
|
89
|
+
changeListener?.({ matches: true } as MediaQueryListEvent)
|
|
90
|
+
expect(matches()).toBe(true)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it("removes listener on unmount", () => {
|
|
94
|
+
const removeEventListenerSpy = vi.fn()
|
|
95
|
+
const mql = {
|
|
96
|
+
matches: false,
|
|
97
|
+
media: "(min-width: 768px)",
|
|
98
|
+
addEventListener: vi.fn(),
|
|
99
|
+
removeEventListener: removeEventListenerSpy,
|
|
100
|
+
}
|
|
101
|
+
matchMediaMock.mockReturnValue(mql)
|
|
102
|
+
|
|
103
|
+
useMediaQuery("(min-width: 768px)")
|
|
104
|
+
mountCallbacks.forEach((cb) => {
|
|
105
|
+
cb()
|
|
106
|
+
})
|
|
107
|
+
unmountCallbacks.forEach((cb) => {
|
|
108
|
+
cb()
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
expect(removeEventListenerSpy).toHaveBeenCalledWith("change", expect.any(Function))
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it("passes the correct query string to matchMedia", () => {
|
|
115
|
+
useMediaQuery("(prefers-color-scheme: dark)")
|
|
116
|
+
mountCallbacks.forEach((cb) => {
|
|
117
|
+
cb()
|
|
118
|
+
})
|
|
119
|
+
expect(matchMediaMock).toHaveBeenCalledWith("(prefers-color-scheme: dark)")
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
it("updates from true to false on change", () => {
|
|
123
|
+
const mql = {
|
|
124
|
+
matches: true,
|
|
125
|
+
media: "(min-width: 768px)",
|
|
126
|
+
addEventListener: vi.fn((event: string, cb: (e: MediaQueryListEvent) => void) => {
|
|
127
|
+
if (event === "change") listeners.set("q", cb)
|
|
128
|
+
}),
|
|
129
|
+
removeEventListener: vi.fn(),
|
|
130
|
+
}
|
|
131
|
+
matchMediaMock.mockReturnValue(mql)
|
|
132
|
+
|
|
133
|
+
const matches = useMediaQuery("(min-width: 768px)")
|
|
134
|
+
mountCallbacks.forEach((cb) => {
|
|
135
|
+
cb()
|
|
136
|
+
})
|
|
137
|
+
expect(matches()).toBe(true)
|
|
138
|
+
|
|
139
|
+
const changeListener = listeners.get("q")
|
|
140
|
+
changeListener?.({ matches: false } as MediaQueryListEvent)
|
|
141
|
+
expect(matches()).toBe(false)
|
|
142
|
+
})
|
|
143
|
+
})
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest"
|
|
2
|
+
import { useMergedRef } from "../useMergedRef"
|
|
3
|
+
|
|
4
|
+
describe("useMergedRef", () => {
|
|
5
|
+
it("sets object refs", () => {
|
|
6
|
+
const ref1 = { current: null as HTMLDivElement | null }
|
|
7
|
+
const ref2 = { current: null as HTMLDivElement | null }
|
|
8
|
+
|
|
9
|
+
const merged = useMergedRef(ref1, ref2)
|
|
10
|
+
|
|
11
|
+
const node = document.createElement("div")
|
|
12
|
+
merged(node)
|
|
13
|
+
|
|
14
|
+
expect(ref1.current).toBe(node)
|
|
15
|
+
expect(ref2.current).toBe(node)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it("calls callback refs", () => {
|
|
19
|
+
const cb = vi.fn()
|
|
20
|
+
const objRef = { current: null as HTMLDivElement | null }
|
|
21
|
+
|
|
22
|
+
const merged = useMergedRef(cb, objRef)
|
|
23
|
+
|
|
24
|
+
const node = document.createElement("div")
|
|
25
|
+
merged(node)
|
|
26
|
+
|
|
27
|
+
expect(cb).toHaveBeenCalledWith(node)
|
|
28
|
+
expect(objRef.current).toBe(node)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it("skips undefined refs", () => {
|
|
32
|
+
const ref = { current: null as HTMLDivElement | null }
|
|
33
|
+
const merged = useMergedRef(undefined, ref)
|
|
34
|
+
|
|
35
|
+
const node = document.createElement("div")
|
|
36
|
+
merged(node)
|
|
37
|
+
|
|
38
|
+
expect(ref.current).toBe(node)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it("handles null node (unmount)", () => {
|
|
42
|
+
const cb = vi.fn()
|
|
43
|
+
const merged = useMergedRef(cb)
|
|
44
|
+
|
|
45
|
+
merged(null)
|
|
46
|
+
expect(cb).toHaveBeenCalledWith(null)
|
|
47
|
+
})
|
|
48
|
+
})
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { signal } from "@pyreon/reactivity"
|
|
2
|
+
import { describe, expect, it } from "vitest"
|
|
3
|
+
import { usePrevious } from "../usePrevious"
|
|
4
|
+
|
|
5
|
+
describe("usePrevious", () => {
|
|
6
|
+
it("returns undefined initially", () => {
|
|
7
|
+
const source = signal(1)
|
|
8
|
+
const prev = usePrevious(source)
|
|
9
|
+
expect(prev()).toBeUndefined()
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it("returns the previous value after source changes", () => {
|
|
13
|
+
const source = signal(1)
|
|
14
|
+
const prev = usePrevious(source)
|
|
15
|
+
expect(prev()).toBeUndefined()
|
|
16
|
+
|
|
17
|
+
source.set(2)
|
|
18
|
+
expect(prev()).toBe(1)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it("tracks multiple changes", () => {
|
|
22
|
+
const source = signal("a")
|
|
23
|
+
const prev = usePrevious(source)
|
|
24
|
+
expect(prev()).toBeUndefined()
|
|
25
|
+
|
|
26
|
+
source.set("b")
|
|
27
|
+
expect(prev()).toBe("a")
|
|
28
|
+
|
|
29
|
+
source.set("c")
|
|
30
|
+
expect(prev()).toBe("b")
|
|
31
|
+
|
|
32
|
+
source.set("d")
|
|
33
|
+
expect(prev()).toBe("c")
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it("works with number values", () => {
|
|
37
|
+
const source = signal(10)
|
|
38
|
+
const prev = usePrevious(source)
|
|
39
|
+
expect(prev()).toBeUndefined()
|
|
40
|
+
|
|
41
|
+
source.set(20)
|
|
42
|
+
expect(prev()).toBe(10)
|
|
43
|
+
|
|
44
|
+
source.set(30)
|
|
45
|
+
expect(prev()).toBe(20)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it("works with object values", () => {
|
|
49
|
+
const obj1 = { x: 1 }
|
|
50
|
+
const obj2 = { x: 2 }
|
|
51
|
+
const source = signal(obj1)
|
|
52
|
+
const prev = usePrevious(source)
|
|
53
|
+
expect(prev()).toBeUndefined()
|
|
54
|
+
|
|
55
|
+
source.set(obj2)
|
|
56
|
+
expect(prev()).toBe(obj1)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it("works with null values", () => {
|
|
60
|
+
const source = signal<string | null>("hello")
|
|
61
|
+
const prev = usePrevious(source)
|
|
62
|
+
expect(prev()).toBeUndefined()
|
|
63
|
+
|
|
64
|
+
source.set(null)
|
|
65
|
+
expect(prev()).toBe("hello")
|
|
66
|
+
|
|
67
|
+
source.set("world")
|
|
68
|
+
expect(prev()).toBeNull()
|
|
69
|
+
})
|
|
70
|
+
})
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest"
|
|
2
|
+
|
|
3
|
+
let mountCallbacks: Array<() => unknown> = []
|
|
4
|
+
let unmountCallbacks: Array<() => void> = []
|
|
5
|
+
|
|
6
|
+
vi.mock("@pyreon/core", () => ({
|
|
7
|
+
onMount: (fn: () => unknown) => {
|
|
8
|
+
mountCallbacks.push(fn)
|
|
9
|
+
},
|
|
10
|
+
onUnmount: (fn: () => void) => {
|
|
11
|
+
unmountCallbacks.push(fn)
|
|
12
|
+
},
|
|
13
|
+
}))
|
|
14
|
+
|
|
15
|
+
import { useReducedMotion } from "../useReducedMotion"
|
|
16
|
+
|
|
17
|
+
describe("useReducedMotion", () => {
|
|
18
|
+
let changeListeners: Map<string, (e: MediaQueryListEvent) => void>
|
|
19
|
+
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
mountCallbacks = []
|
|
22
|
+
unmountCallbacks = []
|
|
23
|
+
changeListeners = new Map()
|
|
24
|
+
|
|
25
|
+
Object.defineProperty(window, "matchMedia", {
|
|
26
|
+
writable: true,
|
|
27
|
+
value: vi.fn((query: string) => ({
|
|
28
|
+
matches: false,
|
|
29
|
+
media: query,
|
|
30
|
+
addEventListener: vi.fn((event: string, cb: (e: MediaQueryListEvent) => void) => {
|
|
31
|
+
if (event === "change") changeListeners.set(query, cb)
|
|
32
|
+
}),
|
|
33
|
+
removeEventListener: vi.fn(),
|
|
34
|
+
})),
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it("returns false when no motion preference", () => {
|
|
39
|
+
const reduced = useReducedMotion()
|
|
40
|
+
mountCallbacks.forEach((cb) => {
|
|
41
|
+
cb()
|
|
42
|
+
})
|
|
43
|
+
expect(reduced()).toBe(false)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it("returns true when reduced motion is preferred", () => {
|
|
47
|
+
Object.defineProperty(window, "matchMedia", {
|
|
48
|
+
writable: true,
|
|
49
|
+
value: vi.fn((query: string) => ({
|
|
50
|
+
matches: true,
|
|
51
|
+
media: query,
|
|
52
|
+
addEventListener: vi.fn((event: string, cb: (e: MediaQueryListEvent) => void) => {
|
|
53
|
+
if (event === "change") changeListeners.set(query, cb)
|
|
54
|
+
}),
|
|
55
|
+
removeEventListener: vi.fn(),
|
|
56
|
+
})),
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
const reduced = useReducedMotion()
|
|
60
|
+
mountCallbacks.forEach((cb) => {
|
|
61
|
+
cb()
|
|
62
|
+
})
|
|
63
|
+
expect(reduced()).toBe(true)
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it("updates when preference changes", () => {
|
|
67
|
+
const reduced = useReducedMotion()
|
|
68
|
+
mountCallbacks.forEach((cb) => {
|
|
69
|
+
cb()
|
|
70
|
+
})
|
|
71
|
+
expect(reduced()).toBe(false)
|
|
72
|
+
|
|
73
|
+
const listener = changeListeners.get("(prefers-reduced-motion: reduce)")
|
|
74
|
+
listener?.({ matches: true } as MediaQueryListEvent)
|
|
75
|
+
expect(reduced()).toBe(true)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it("queries the correct media string", () => {
|
|
79
|
+
const matchMediaSpy = vi.fn((query: string) => ({
|
|
80
|
+
matches: false,
|
|
81
|
+
media: query,
|
|
82
|
+
addEventListener: vi.fn(),
|
|
83
|
+
removeEventListener: vi.fn(),
|
|
84
|
+
}))
|
|
85
|
+
Object.defineProperty(window, "matchMedia", {
|
|
86
|
+
writable: true,
|
|
87
|
+
value: matchMediaSpy,
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
useReducedMotion()
|
|
91
|
+
mountCallbacks.forEach((cb) => {
|
|
92
|
+
cb()
|
|
93
|
+
})
|
|
94
|
+
expect(matchMediaSpy).toHaveBeenCalledWith("(prefers-reduced-motion: reduce)")
|
|
95
|
+
})
|
|
96
|
+
})
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest"
|
|
2
|
+
|
|
3
|
+
// Mock @pyreon/styler to provide theme values
|
|
4
|
+
vi.mock("@pyreon/styler", () => ({
|
|
5
|
+
useTheme: () => ({}),
|
|
6
|
+
}))
|
|
7
|
+
|
|
8
|
+
import { useRootSize } from "../useRootSize"
|
|
9
|
+
|
|
10
|
+
describe("useRootSize", () => {
|
|
11
|
+
it("defaults rootSize to 16", () => {
|
|
12
|
+
const result = useRootSize()
|
|
13
|
+
expect(result.rootSize).toBe(16)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it("pxToRem converts correctly with default rootSize", () => {
|
|
17
|
+
const result = useRootSize()
|
|
18
|
+
expect(result.pxToRem(32)).toBe("2rem")
|
|
19
|
+
expect(result.pxToRem(8)).toBe("0.5rem")
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it("remToPx converts correctly with default rootSize", () => {
|
|
23
|
+
const result = useRootSize()
|
|
24
|
+
expect(result.remToPx(2)).toBe(32)
|
|
25
|
+
expect(result.remToPx(0.5)).toBe(8)
|
|
26
|
+
})
|
|
27
|
+
})
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
|
|
2
|
+
|
|
3
|
+
vi.mock("@pyreon/core", () => ({
|
|
4
|
+
onMount: (_fn: () => unknown) => {
|
|
5
|
+
/* no-op */
|
|
6
|
+
},
|
|
7
|
+
onUnmount: (_fn: () => void) => {
|
|
8
|
+
/* no-op */
|
|
9
|
+
},
|
|
10
|
+
}))
|
|
11
|
+
|
|
12
|
+
describe("useScrollLock", () => {
|
|
13
|
+
beforeEach(async () => {
|
|
14
|
+
vi.resetModules()
|
|
15
|
+
document.body.style.overflow = ""
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
document.body.style.overflow = ""
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
async function getUseScrollLock() {
|
|
23
|
+
// Re-mock after resetModules
|
|
24
|
+
vi.doMock("@pyreon/core", () => ({
|
|
25
|
+
onMount: (_fn: () => unknown) => {
|
|
26
|
+
/* no-op */
|
|
27
|
+
},
|
|
28
|
+
onUnmount: (_fn: () => void) => {
|
|
29
|
+
/* no-op */
|
|
30
|
+
},
|
|
31
|
+
}))
|
|
32
|
+
const mod = await import("../useScrollLock")
|
|
33
|
+
return mod.useScrollLock
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
it("sets overflow to hidden when locked", async () => {
|
|
37
|
+
const useScrollLock = await getUseScrollLock()
|
|
38
|
+
const { lock } = useScrollLock()
|
|
39
|
+
lock()
|
|
40
|
+
expect(document.body.style.overflow).toBe("hidden")
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it("does not change overflow until lock is called", async () => {
|
|
44
|
+
const useScrollLock = await getUseScrollLock()
|
|
45
|
+
document.body.style.overflow = "auto"
|
|
46
|
+
useScrollLock()
|
|
47
|
+
expect(document.body.style.overflow).toBe("auto")
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it("restores overflow when unlocked", async () => {
|
|
51
|
+
const useScrollLock = await getUseScrollLock()
|
|
52
|
+
document.body.style.overflow = "auto"
|
|
53
|
+
const { lock, unlock } = useScrollLock()
|
|
54
|
+
lock()
|
|
55
|
+
expect(document.body.style.overflow).toBe("hidden")
|
|
56
|
+
unlock()
|
|
57
|
+
expect(document.body.style.overflow).toBe("auto")
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it("lock is idempotent", async () => {
|
|
61
|
+
const useScrollLock = await getUseScrollLock()
|
|
62
|
+
const { lock, unlock } = useScrollLock()
|
|
63
|
+
lock()
|
|
64
|
+
lock() // second call should be no-op
|
|
65
|
+
expect(document.body.style.overflow).toBe("hidden")
|
|
66
|
+
unlock()
|
|
67
|
+
expect(document.body.style.overflow).toBe("")
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it("unlock is idempotent", async () => {
|
|
71
|
+
const useScrollLock = await getUseScrollLock()
|
|
72
|
+
const { lock, unlock } = useScrollLock()
|
|
73
|
+
lock()
|
|
74
|
+
unlock()
|
|
75
|
+
unlock() // second call should be no-op
|
|
76
|
+
expect(document.body.style.overflow).toBe("")
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it("handles concurrent locks with reference counting", async () => {
|
|
80
|
+
const useScrollLock = await getUseScrollLock()
|
|
81
|
+
const lock1 = useScrollLock()
|
|
82
|
+
const lock2 = useScrollLock()
|
|
83
|
+
|
|
84
|
+
lock1.lock()
|
|
85
|
+
expect(document.body.style.overflow).toBe("hidden")
|
|
86
|
+
|
|
87
|
+
lock2.lock()
|
|
88
|
+
expect(document.body.style.overflow).toBe("hidden")
|
|
89
|
+
|
|
90
|
+
// Unlocking first should not restore (still 1 active)
|
|
91
|
+
lock1.unlock()
|
|
92
|
+
expect(document.body.style.overflow).toBe("hidden")
|
|
93
|
+
|
|
94
|
+
// Unlocking second should restore
|
|
95
|
+
lock2.unlock()
|
|
96
|
+
expect(document.body.style.overflow).toBe("")
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it("preserves original overflow across concurrent locks", async () => {
|
|
100
|
+
const useScrollLock = await getUseScrollLock()
|
|
101
|
+
document.body.style.overflow = "scroll"
|
|
102
|
+
|
|
103
|
+
const lock1 = useScrollLock()
|
|
104
|
+
const lock2 = useScrollLock()
|
|
105
|
+
|
|
106
|
+
lock1.lock()
|
|
107
|
+
lock2.lock()
|
|
108
|
+
|
|
109
|
+
lock1.unlock()
|
|
110
|
+
expect(document.body.style.overflow).toBe("hidden")
|
|
111
|
+
|
|
112
|
+
lock2.unlock()
|
|
113
|
+
expect(document.body.style.overflow).toBe("scroll")
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it("unlock without prior lock is no-op", async () => {
|
|
117
|
+
const useScrollLock = await getUseScrollLock()
|
|
118
|
+
document.body.style.overflow = "auto"
|
|
119
|
+
const { unlock } = useScrollLock()
|
|
120
|
+
unlock()
|
|
121
|
+
expect(document.body.style.overflow).toBe("auto")
|
|
122
|
+
})
|
|
123
|
+
})
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest"
|
|
2
|
+
|
|
3
|
+
// Mock @pyreon/styler to provide theme values
|
|
4
|
+
vi.mock("@pyreon/styler", () => ({
|
|
5
|
+
useTheme: () => ({}),
|
|
6
|
+
}))
|
|
7
|
+
|
|
8
|
+
import { useSpacing } from "../useSpacing"
|
|
9
|
+
|
|
10
|
+
describe("useSpacing", () => {
|
|
11
|
+
it("returns spacing function with default base (rootSize/2 = 8)", () => {
|
|
12
|
+
const spacing = useSpacing()
|
|
13
|
+
expect(spacing(1)).toBe("8px")
|
|
14
|
+
expect(spacing(2)).toBe("16px")
|
|
15
|
+
expect(spacing(0.5)).toBe("4px")
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it("accepts custom base unit", () => {
|
|
19
|
+
const spacing = useSpacing(4)
|
|
20
|
+
expect(spacing(1)).toBe("4px")
|
|
21
|
+
expect(spacing(3)).toBe("12px")
|
|
22
|
+
})
|
|
23
|
+
})
|