|
| 1 | +import { beforeEach, describe, expect, it, jest } from "bun:test" |
| 2 | +import { getElementPosition } from "./dom" |
| 3 | + |
| 4 | +// Mock DOM methods |
| 5 | +const mockGetElementById = jest.fn() |
| 6 | +const mockGetComputedStyle = jest.fn() |
| 7 | +const mockGetBoundingClientRect = jest.fn() |
| 8 | + |
| 9 | +// Setup global mocks |
| 10 | +Object.defineProperty(global, "document", { |
| 11 | + value: { |
| 12 | + getElementById: mockGetElementById, |
| 13 | + }, |
| 14 | + writable: true, |
| 15 | +}) |
| 16 | + |
| 17 | +Object.defineProperty(global, "window", { |
| 18 | + value: { |
| 19 | + getComputedStyle: mockGetComputedStyle, |
| 20 | + scrollY: 0, |
| 21 | + }, |
| 22 | + writable: true, |
| 23 | +}) |
| 24 | + |
| 25 | +describe("getElementPosition", () => { |
| 26 | + beforeEach(() => { |
| 27 | + jest.clearAllMocks() |
| 28 | + // Reset window.scrollY |
| 29 | + Object.defineProperty(window, "scrollY", { |
| 30 | + value: 0, |
| 31 | + writable: true, |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + it("should return undefined when element is not found", () => { |
| 36 | + mockGetElementById.mockReturnValue(null) |
| 37 | + |
| 38 | + const result = getElementPosition("non-existent-id") |
| 39 | + |
| 40 | + expect(result).toBeUndefined() |
| 41 | + expect(mockGetElementById).toHaveBeenCalledWith("non-existent-id") |
| 42 | + }) |
| 43 | + |
| 44 | + it("should return undefined when id is empty string", () => { |
| 45 | + mockGetElementById.mockReturnValue(null) |
| 46 | + |
| 47 | + const result = getElementPosition("") |
| 48 | + |
| 49 | + expect(result).toBeUndefined() |
| 50 | + expect(mockGetElementById).toHaveBeenCalledWith("") |
| 51 | + }) |
| 52 | + |
| 53 | + it("should return undefined when id is undefined", () => { |
| 54 | + mockGetElementById.mockReturnValue(null) |
| 55 | + |
| 56 | + const result = getElementPosition(undefined) |
| 57 | + |
| 58 | + expect(result).toBeUndefined() |
| 59 | + expect(mockGetElementById).toHaveBeenCalledWith("") |
| 60 | + }) |
| 61 | + |
| 62 | + it("should calculate position correctly with no scroll margin", () => { |
| 63 | + const mockElement = { |
| 64 | + getBoundingClientRect: mockGetBoundingClientRect, |
| 65 | + } |
| 66 | + |
| 67 | + mockGetElementById.mockReturnValue(mockElement) |
| 68 | + mockGetBoundingClientRect.mockReturnValue({ top: 100 }) |
| 69 | + mockGetComputedStyle.mockReturnValue({ scrollMarginTop: "0px" }) |
| 70 | + |
| 71 | + // Set scroll position |
| 72 | + Object.defineProperty(window, "scrollY", { value: 50, writable: true }) |
| 73 | + |
| 74 | + const result = getElementPosition("test-id") |
| 75 | + |
| 76 | + expect(result).toEqual({ |
| 77 | + id: "test-id", |
| 78 | + top: 150, // 50 (scrollY) + 100 (getBoundingClientRect.top) - 0 (scrollMarginTop) |
| 79 | + }) |
| 80 | + expect(mockGetElementById).toHaveBeenCalledWith("test-id") |
| 81 | + expect(mockGetComputedStyle).toHaveBeenCalledWith(mockElement) |
| 82 | + }) |
| 83 | + |
| 84 | + it("should calculate position correctly with scroll margin", () => { |
| 85 | + const mockElement = { |
| 86 | + getBoundingClientRect: mockGetBoundingClientRect, |
| 87 | + } |
| 88 | + |
| 89 | + mockGetElementById.mockReturnValue(mockElement) |
| 90 | + mockGetBoundingClientRect.mockReturnValue({ top: 200 }) |
| 91 | + mockGetComputedStyle.mockReturnValue({ scrollMarginTop: "20px" }) |
| 92 | + |
| 93 | + // Set scroll position |
| 94 | + Object.defineProperty(window, "scrollY", { value: 100, writable: true }) |
| 95 | + |
| 96 | + const result = getElementPosition("test-id") |
| 97 | + |
| 98 | + expect(result).toEqual({ |
| 99 | + id: "test-id", |
| 100 | + top: 280, // 100 (scrollY) + 200 (getBoundingClientRect.top) - 20 (scrollMarginTop) |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + it("should handle fractional scroll margin values", () => { |
| 105 | + const mockElement = { |
| 106 | + getBoundingClientRect: mockGetBoundingClientRect, |
| 107 | + } |
| 108 | + |
| 109 | + mockGetElementById.mockReturnValue(mockElement) |
| 110 | + mockGetBoundingClientRect.mockReturnValue({ top: 150.5 }) |
| 111 | + mockGetComputedStyle.mockReturnValue({ scrollMarginTop: "10.7px" }) |
| 112 | + |
| 113 | + // Set scroll position |
| 114 | + Object.defineProperty(window, "scrollY", { value: 75.3, writable: true }) |
| 115 | + |
| 116 | + const result = getElementPosition("test-id") |
| 117 | + |
| 118 | + expect(result).toEqual({ |
| 119 | + id: "test-id", |
| 120 | + top: 215, // Math.floor(75.3 + 150.5 - 10.7) = Math.floor(215.1) = 215 |
| 121 | + }) |
| 122 | + }) |
| 123 | + |
| 124 | + it("should handle zero scroll position", () => { |
| 125 | + const mockElement = { |
| 126 | + getBoundingClientRect: mockGetBoundingClientRect, |
| 127 | + } |
| 128 | + |
| 129 | + mockGetElementById.mockReturnValue(mockElement) |
| 130 | + mockGetBoundingClientRect.mockReturnValue({ top: 300 }) |
| 131 | + mockGetComputedStyle.mockReturnValue({ scrollMarginTop: "10px" }) |
| 132 | + |
| 133 | + // window.scrollY is already 0 from beforeEach |
| 134 | + |
| 135 | + const result = getElementPosition("test-id") |
| 136 | + |
| 137 | + expect(result).toEqual({ |
| 138 | + id: "test-id", |
| 139 | + top: 290, // 0 + 300 - 10 |
| 140 | + }) |
| 141 | + }) |
| 142 | + |
| 143 | + it("should handle negative getBoundingClientRect top values", () => { |
| 144 | + const mockElement = { |
| 145 | + getBoundingClientRect: mockGetBoundingClientRect, |
| 146 | + } |
| 147 | + |
| 148 | + mockGetElementById.mockReturnValue(mockElement) |
| 149 | + mockGetBoundingClientRect.mockReturnValue({ top: -50 }) |
| 150 | + mockGetComputedStyle.mockReturnValue({ scrollMarginTop: "0px" }) |
| 151 | + |
| 152 | + Object.defineProperty(window, "scrollY", { value: 200, writable: true }) |
| 153 | + |
| 154 | + const result = getElementPosition("test-id") |
| 155 | + |
| 156 | + expect(result).toEqual({ |
| 157 | + id: "test-id", |
| 158 | + top: 150, // 200 + (-50) - 0 |
| 159 | + }) |
| 160 | + }) |
| 161 | +}) |
0 commit comments