forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransitionsDev.test.ts
More file actions
95 lines (85 loc) · 2.94 KB
/
Copy pathtransitionsDev.test.ts
File metadata and controls
95 lines (85 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { prefersReducedMotion, readDurationToken, replayErrorShake } from '@/lib/transitionsDev';
describe('transitionsDev helpers', () => {
beforeEach(() => {
document.documentElement.style.setProperty('--text-swap-dur', '150ms');
document.documentElement.style.setProperty('--shake-dur-a', '80ms');
document.documentElement.style.setProperty('--shake-dur-b', '60ms');
vi.stubGlobal(
'matchMedia',
vi.fn(() => ({
matches: false,
media: '(prefers-reduced-motion: reduce)',
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
addListener: vi.fn(),
removeListener: vi.fn(),
dispatchEvent: vi.fn(),
onchange: null,
})),
);
});
afterEach(() => {
vi.unstubAllGlobals();
vi.useRealTimers();
});
it('reads duration tokens from :root', () => {
document.documentElement.style.setProperty('--text-swap-dur', '180ms');
expect(readDurationToken('--text-swap-dur', 200)).toBe(180);
expect(readDurationToken('--missing-token', 42)).toBe(42);
});
it('detects prefers-reduced-motion', () => {
vi.stubGlobal(
'matchMedia',
vi.fn((query: string) => ({
matches: query.includes('prefers-reduced-motion'),
media: query,
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
addListener: vi.fn(),
removeListener: vi.fn(),
dispatchEvent: vi.fn(),
onchange: null,
})),
);
expect(prefersReducedMotion()).toBe(true);
});
it('replays the error shake class through a reflow', () => {
vi.useFakeTimers();
const input = document.createElement('div');
input.classList.add('t-input');
document.body.appendChild(input);
replayErrorShake(input);
expect(input.classList.contains('is-shaking')).toBe(true);
vi.advanceTimersByTime(300);
expect(input.classList.contains('is-shaking')).toBe(false);
replayErrorShake(input);
expect(input.classList.contains('is-shaking')).toBe(true);
input.remove();
});
it('cancels an in-flight error shake', () => {
const input = document.createElement('div');
const stop = replayErrorShake(input);
expect(input.classList.contains('is-shaking')).toBe(true);
stop();
expect(input.classList.contains('is-shaking')).toBe(false);
});
it('skips the error shake when reduced motion is requested', () => {
vi.stubGlobal(
'matchMedia',
vi.fn((query: string) => ({
matches: query.includes('prefers-reduced-motion'),
media: query,
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
addListener: vi.fn(),
removeListener: vi.fn(),
dispatchEvent: vi.fn(),
onchange: null,
})),
);
const input = document.createElement('div');
replayErrorShake(input);
expect(input.classList.contains('is-shaking')).toBe(false);
});
});