|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { afterEach, beforeEach, describe, it } from 'node:test'; |
| 3 | + |
| 4 | +import { renderHook, act } from '@testing-library/react'; |
| 5 | + |
| 6 | +import useScrollDirection from '#site/hooks/client/useScrollDirection.js'; |
| 7 | + |
| 8 | +describe('useScrollDirection', () => { |
| 9 | + let scrollY; |
| 10 | + let originalRAF; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + scrollY = 0; |
| 14 | + |
| 15 | + Object.defineProperty(window, 'scrollY', { |
| 16 | + get: () => scrollY, |
| 17 | + configurable: true, |
| 18 | + }); |
| 19 | + |
| 20 | + originalRAF = window.requestAnimationFrame; |
| 21 | + Object.defineProperty(window, 'requestAnimationFrame', { |
| 22 | + value: cb => { |
| 23 | + cb(); |
| 24 | + return 1; |
| 25 | + }, |
| 26 | + writable: true, |
| 27 | + configurable: true, |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + window.requestAnimationFrame = originalRAF; |
| 33 | + }); |
| 34 | + |
| 35 | + it('should return null initially (at top of page)', () => { |
| 36 | + const { result } = renderHook(() => useScrollDirection()); |
| 37 | + assert.equal(result.current, null); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should return "down" when scrolling down past threshold', () => { |
| 41 | + const { result } = renderHook(() => useScrollDirection()); |
| 42 | + |
| 43 | + act(() => { |
| 44 | + scrollY = 100; |
| 45 | + window.dispatchEvent(new Event('scroll')); |
| 46 | + }); |
| 47 | + |
| 48 | + assert.equal(result.current, 'down'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should return "up" when scrolling up past threshold', () => { |
| 52 | + const { result } = renderHook(() => useScrollDirection()); |
| 53 | + |
| 54 | + act(() => { |
| 55 | + scrollY = 100; |
| 56 | + window.dispatchEvent(new Event('scroll')); |
| 57 | + }); |
| 58 | + |
| 59 | + act(() => { |
| 60 | + scrollY = 50; |
| 61 | + window.dispatchEvent(new Event('scroll')); |
| 62 | + }); |
| 63 | + |
| 64 | + assert.equal(result.current, 'up'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should not change direction for scroll less than threshold', () => { |
| 68 | + const { result } = renderHook(() => useScrollDirection()); |
| 69 | + |
| 70 | + act(() => { |
| 71 | + scrollY = 5; |
| 72 | + window.dispatchEvent(new Event('scroll')); |
| 73 | + }); |
| 74 | + |
| 75 | + assert.equal(result.current, null); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should return null when scrolling back to top', () => { |
| 79 | + const { result } = renderHook(() => useScrollDirection()); |
| 80 | + |
| 81 | + act(() => { |
| 82 | + scrollY = 100; |
| 83 | + window.dispatchEvent(new Event('scroll')); |
| 84 | + }); |
| 85 | + |
| 86 | + assert.equal(result.current, 'down'); |
| 87 | + |
| 88 | + act(() => { |
| 89 | + scrollY = 0; |
| 90 | + window.dispatchEvent(new Event('scroll')); |
| 91 | + }); |
| 92 | + |
| 93 | + assert.equal(result.current, null); |
| 94 | + }); |
| 95 | +}); |
0 commit comments