|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | +import scrollToPosition from './scrollToPosition.js' |
| 3 | + |
| 4 | +describe('scrollToPosition', () => { |
| 5 | + let mockHtmlNode: HTMLElement |
| 6 | + let mockBodyNode: HTMLElement |
| 7 | + let mockScrollingElement: HTMLElement |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + mockHtmlNode = { |
| 11 | + scrollTop: 0, |
| 12 | + get scrollHeight() { return 1000 }, |
| 13 | + get clientHeight() { return 500 }, |
| 14 | + } as unknown as HTMLElement |
| 15 | + |
| 16 | + mockBodyNode = { |
| 17 | + scrollTop: 0, |
| 18 | + get scrollHeight() { return 1000 }, |
| 19 | + get clientHeight() { return 500 }, |
| 20 | + } as unknown as HTMLElement |
| 21 | + |
| 22 | + mockScrollingElement = { |
| 23 | + scrollTop: 0, |
| 24 | + } as unknown as HTMLElement |
| 25 | + |
| 26 | + // Mock document |
| 27 | + global.document = { |
| 28 | + querySelector: vi.fn((selector) => { |
| 29 | + if (selector === 'html') {return mockHtmlNode} |
| 30 | + if (selector === 'body') {return mockBodyNode} |
| 31 | + return null |
| 32 | + }), |
| 33 | + scrollingElement: mockScrollingElement, |
| 34 | + documentElement: mockHtmlNode, |
| 35 | + } as unknown as Document |
| 36 | + }) |
| 37 | + |
| 38 | + it('should scroll html node when it is scrollable', () => { |
| 39 | + const yPosition = 100 |
| 40 | + scrollToPosition(yPosition) |
| 41 | + |
| 42 | + expect(mockHtmlNode.scrollTop).toBe(yPosition) |
| 43 | + }) |
| 44 | + |
| 45 | + it('should scroll body node when html is not scrollable', () => { |
| 46 | + Object.defineProperty(mockHtmlNode, 'scrollHeight', { value: 500 }) |
| 47 | + const yPosition = 100 |
| 48 | + scrollToPosition(yPosition) |
| 49 | + |
| 50 | + expect(mockBodyNode.scrollTop).toBe(yPosition) |
| 51 | + }) |
| 52 | + |
| 53 | + it('should scroll document.scrollingElement when neither html nor body is scrollable', () => { |
| 54 | + Object.defineProperty(mockHtmlNode, 'scrollHeight', { value: 500 }) |
| 55 | + Object.defineProperty(mockBodyNode, 'scrollHeight', { value: 500 }) |
| 56 | + const yPosition = 100 |
| 57 | + scrollToPosition(yPosition) |
| 58 | + |
| 59 | + expect(mockScrollingElement.scrollTop).toBe(yPosition) |
| 60 | + }) |
| 61 | + |
| 62 | + it('should fallback to documentElement when scrollingElement is not available', () => { |
| 63 | + Object.defineProperty(mockHtmlNode, 'scrollHeight', { value: 500 }) |
| 64 | + Object.defineProperty(mockBodyNode, 'scrollHeight', { value: 500 }) |
| 65 | + Object.defineProperty(global.document, 'scrollingElement', { value: null }) |
| 66 | + const yPosition = 100 |
| 67 | + scrollToPosition(yPosition) |
| 68 | + |
| 69 | + expect(mockHtmlNode.scrollTop).toBe(yPosition) |
| 70 | + }) |
| 71 | + |
| 72 | + it('should verify scroll position after scrolling html node', () => { |
| 73 | + const yPosition = 100 |
| 74 | + scrollToPosition(yPosition) |
| 75 | + |
| 76 | + expect(mockHtmlNode.scrollTop).toBe(yPosition) |
| 77 | + }) |
| 78 | + |
| 79 | + it('should verify scroll position after scrolling body node', () => { |
| 80 | + Object.defineProperty(mockHtmlNode, 'scrollHeight', { value: 500 }) |
| 81 | + const yPosition = 100 |
| 82 | + scrollToPosition(yPosition) |
| 83 | + |
| 84 | + expect(mockBodyNode.scrollTop).toBe(yPosition) |
| 85 | + }) |
| 86 | +}) |
0 commit comments