-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathuseScrollDirection.test.mjs
More file actions
95 lines (72 loc) · 2.22 KB
/
useScrollDirection.test.mjs
File metadata and controls
95 lines (72 loc) · 2.22 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 assert from 'node:assert/strict';
import { afterEach, beforeEach, describe, it } from 'node:test';
import { renderHook, act } from '@testing-library/react';
import useScrollDirection from '#site/hooks/client/useScrollDirection.js';
describe('useScrollDirection', () => {
let scrollY;
let originalRAF;
beforeEach(() => {
scrollY = 0;
Object.defineProperty(window, 'scrollY', {
get: () => scrollY,
configurable: true,
});
originalRAF = window.requestAnimationFrame;
Object.defineProperty(window, 'requestAnimationFrame', {
value: cb => {
cb();
return 1;
},
writable: true,
configurable: true,
});
});
afterEach(() => {
window.requestAnimationFrame = originalRAF;
});
it('should return null initially (at top of page)', () => {
const { result } = renderHook(() => useScrollDirection());
assert.equal(result.current, null);
});
it('should return "down" when scrolling down past threshold', () => {
const { result } = renderHook(() => useScrollDirection());
act(() => {
scrollY = 100;
window.dispatchEvent(new Event('scroll'));
});
assert.equal(result.current, 'down');
});
it('should return "up" when scrolling up past threshold', () => {
const { result } = renderHook(() => useScrollDirection());
act(() => {
scrollY = 100;
window.dispatchEvent(new Event('scroll'));
});
act(() => {
scrollY = 50;
window.dispatchEvent(new Event('scroll'));
});
assert.equal(result.current, 'up');
});
it('should not change direction for scroll less than threshold', () => {
const { result } = renderHook(() => useScrollDirection());
act(() => {
scrollY = 5;
window.dispatchEvent(new Event('scroll'));
});
assert.equal(result.current, null);
});
it('should return null when scrolling back to top', () => {
const { result } = renderHook(() => useScrollDirection());
act(() => {
scrollY = 100;
window.dispatchEvent(new Event('scroll'));
});
assert.equal(result.current, 'down');
act(() => {
scrollY = 0;
window.dispatchEvent(new Event('scroll'));
});
assert.equal(result.current, null);
});
});