-
Notifications
You must be signed in to change notification settings - Fork 960
Expand file tree
/
Copy pathcollapsableWheel.test.ts
More file actions
87 lines (68 loc) · 2.65 KB
/
Copy pathcollapsableWheel.test.ts
File metadata and controls
87 lines (68 loc) · 2.65 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
import {createRoot} from 'solid-js';
import {useCollapsable} from '@hooks/useCollapsable';
// A wheel as Firefox dispatches it: the standard `deltaY` only. Chromium/WebKit additionally
// carry the legacy `wheelDeltaY` (roughly `-deltaY * 1.2`), which is what the hook used to read.
const wheel = (deltaY: number, withLegacyWheelDelta?: boolean) => {
const event = new WheelEvent('wheel', {deltaY, bubbles: true, cancelable: true});
if(withLegacyWheelDelta) {
Object.defineProperty(event, 'wheelDeltaY', {value: -deltaY * 1.2});
}
return event;
};
const setup = () => {
const listenWheelOn = document.createElement('div');
const container = document.createElement('div');
const scrollable = document.createElement('div');
document.body.append(listenWheelOn, container, scrollable);
let dispose: () => void;
const collapsable = createRoot((_dispose) => {
dispose = _dispose;
return useCollapsable({
scrollable: () => scrollable,
container: () => container,
listenWheelOn,
disableHoverWhenFolded: true
});
});
return {
...collapsable,
listenWheelOn,
container,
cleanup: () => {
dispose();
[listenWheelOn, container, scrollable].forEach((element) => element.remove());
}
};
};
describe('useCollapsable wheel direction', () => {
test('unfolds on a standard wheel-up without the non-standard wheelDeltaY (Firefox)', () => {
const {progress, STATE_FOLDED, STATE_UNFOLDED, listenWheelOn, cleanup} = setup();
expect(progress()).toBe(STATE_FOLDED);
listenWheelOn.dispatchEvent(wheel(-100));
expect(progress()).toBe(STATE_UNFOLDED);
cleanup();
});
test('still unfolds when the legacy wheelDeltaY is present (Chromium/WebKit)', () => {
const {progress, STATE_FOLDED, STATE_UNFOLDED, listenWheelOn, cleanup} = setup();
expect(progress()).toBe(STATE_FOLDED);
listenWheelOn.dispatchEvent(wheel(-100, true));
expect(progress()).toBe(STATE_UNFOLDED);
cleanup();
});
test('folds back on a wheel-down', () => {
const {progress, STATE_FOLDED, STATE_UNFOLDED, listenWheelOn, cleanup} = setup();
listenWheelOn.dispatchEvent(wheel(-100));
expect(progress()).toBe(STATE_UNFOLDED);
listenWheelOn.dispatchEvent(wheel(100));
expect(progress()).toBe(STATE_FOLDED);
cleanup();
});
test('starts folded and non-interactive, and a wheel-up gets it out of that state', () => {
const {folded, listenWheelOn, container, cleanup} = setup();
expect(folded()).toBe(true);
expect(container.classList.contains('disable-hover')).toBe(true);
listenWheelOn.dispatchEvent(wheel(-100));
expect(folded()).toBe(false);
cleanup();
});
});