|
| 1 | +import { Sticky } from "./sticky.js"; |
| 2 | + |
| 3 | +describe("Sticky Component", () => { |
| 4 | + let mockElement; |
| 5 | + let stickyInstance; |
| 6 | + let scrollEventListener; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + document.body.classList.add("nhsuk-frontend-supported"); |
| 10 | + |
| 11 | + // Create a mock DOM element |
| 12 | + document.body.innerHTML = `<div id="mock-element" style="position: sticky; top: 20px;"></div>`; |
| 13 | + mockElement = document.getElementById("mock-element"); |
| 14 | + |
| 15 | + // Mock getBoundingClientRect |
| 16 | + Object.defineProperty(mockElement, "getBoundingClientRect", { |
| 17 | + value: jest.fn(() => ({ top: 0 })), |
| 18 | + configurable: true, |
| 19 | + }); |
| 20 | + |
| 21 | + // Mock window.getComputedStyle |
| 22 | + Object.defineProperty(window, "getComputedStyle", { |
| 23 | + value: jest.fn(() => ({ |
| 24 | + top: "20px", |
| 25 | + })), |
| 26 | + writable: true, |
| 27 | + }); |
| 28 | + |
| 29 | + // Mock window.addEventListener and capture scroll listener |
| 30 | + const originalAddEventListener = window.addEventListener; |
| 31 | + window.addEventListener = jest.fn((event, listener) => { |
| 32 | + if (event === "scroll") { |
| 33 | + scrollEventListener = listener; |
| 34 | + } |
| 35 | + originalAddEventListener.call(window, event, listener); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + jest.clearAllMocks(); |
| 41 | + jest.restoreAllMocks(); |
| 42 | + }); |
| 43 | + |
| 44 | + describe("Initialization", () => { |
| 45 | + test("should initialize with correct properties", () => { |
| 46 | + stickyInstance = new Sticky(mockElement); |
| 47 | + |
| 48 | + expect(stickyInstance.stickyElement).toBe(mockElement); |
| 49 | + expect(stickyInstance.stickyElementTop).toBe(20); |
| 50 | + expect(window.getComputedStyle).toHaveBeenCalledWith(mockElement); |
| 51 | + expect(window.addEventListener).toHaveBeenCalledWith( |
| 52 | + "scroll", |
| 53 | + expect.any(Function), |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + test("should have correct moduleName", () => { |
| 58 | + expect(Sticky.moduleName).toBe("app-sticky"); |
| 59 | + }); |
| 60 | + |
| 61 | + test("should call determineStickyState on initialization", () => { |
| 62 | + const spy = jest.spyOn(Sticky.prototype, "determineStickyState"); |
| 63 | + stickyInstance = new Sticky(mockElement); |
| 64 | + expect(spy).toHaveBeenCalled(); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe("determineStickyState method", () => { |
| 69 | + beforeEach(() => { |
| 70 | + stickyInstance = new Sticky(mockElement); |
| 71 | + }); |
| 72 | + |
| 73 | + test("should set data-stuck to `true` when element is at or above threshold", () => { |
| 74 | + // Element is at the top (currentTop = 0, threshold = 20) |
| 75 | + mockElement.getBoundingClientRect.mockReturnValue({ top: 0 }); |
| 76 | + |
| 77 | + stickyInstance.determineStickyState(); |
| 78 | + |
| 79 | + expect(mockElement.dataset.stuck).toBe("true"); |
| 80 | + }); |
| 81 | + |
| 82 | + test("should set data-stuck to `true` when element above threshold", () => { |
| 83 | + mockElement.getBoundingClientRect.mockReturnValue({ top: 10 }); |
| 84 | + |
| 85 | + stickyInstance.determineStickyState(); |
| 86 | + |
| 87 | + expect(mockElement.dataset.stuck).toBe("true"); |
| 88 | + }); |
| 89 | + |
| 90 | + test("should set data-stuck to `true` when element at threshold", () => { |
| 91 | + mockElement.getBoundingClientRect.mockReturnValue({ top: 20 }); |
| 92 | + |
| 93 | + stickyInstance.determineStickyState(); |
| 94 | + |
| 95 | + expect(mockElement.dataset.stuck).toBe("true"); |
| 96 | + }); |
| 97 | + |
| 98 | + test("should set data-stuck to `false` when element below threshold", () => { |
| 99 | + mockElement.getBoundingClientRect.mockReturnValue({ top: 30 }); |
| 100 | + |
| 101 | + stickyInstance.determineStickyState(); |
| 102 | + |
| 103 | + expect(mockElement.dataset.stuck).toBe("false"); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe("Scroll behavior", () => { |
| 108 | + beforeEach(() => { |
| 109 | + jest.useFakeTimers(); |
| 110 | + |
| 111 | + // Clear any existing event listeners |
| 112 | + window.removeEventListener = jest.fn(); |
| 113 | + |
| 114 | + stickyInstance = new Sticky(mockElement); |
| 115 | + }); |
| 116 | + |
| 117 | + afterEach(() => { |
| 118 | + jest.useRealTimers(); |
| 119 | + }); |
| 120 | + |
| 121 | + test("should respond to scroll events", () => { |
| 122 | + mockElement.getBoundingClientRect.mockReturnValue({ top: 10 }); |
| 123 | + |
| 124 | + // Make sure we have the listener |
| 125 | + expect(scrollEventListener).toBeDefined(); |
| 126 | + |
| 127 | + // Trigger scroll event |
| 128 | + scrollEventListener(); |
| 129 | + |
| 130 | + expect(mockElement.dataset.stuck).toBe("true"); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe("Throttle functionality", () => { |
| 135 | + beforeEach(() => { |
| 136 | + jest.useFakeTimers(); |
| 137 | + stickyInstance = new Sticky(mockElement); |
| 138 | + }); |
| 139 | + |
| 140 | + afterEach(() => { |
| 141 | + jest.useRealTimers(); |
| 142 | + }); |
| 143 | + |
| 144 | + test("should throttle function calls", () => { |
| 145 | + const mockCallback = jest.fn(); |
| 146 | + const throttledCallback = stickyInstance.throttle(mockCallback, 100); |
| 147 | + |
| 148 | + // Call multiple times rapidly |
| 149 | + throttledCallback(); |
| 150 | + throttledCallback(); |
| 151 | + throttledCallback(); |
| 152 | + |
| 153 | + // Should only be called once |
| 154 | + expect(mockCallback).toHaveBeenCalledTimes(1); |
| 155 | + |
| 156 | + // Fast forward past throttle limit |
| 157 | + jest.advanceTimersByTime(100); |
| 158 | + |
| 159 | + // Now should allow another call |
| 160 | + throttledCallback(); |
| 161 | + expect(mockCallback).toHaveBeenCalledTimes(2); |
| 162 | + }); |
| 163 | + |
| 164 | + test("should preserve context and arguments in throttled function", () => { |
| 165 | + const mockCallback = jest.fn(); |
| 166 | + const throttledCallback = stickyInstance.throttle(mockCallback, 100); |
| 167 | + |
| 168 | + throttledCallback("arg1", "arg2"); |
| 169 | + |
| 170 | + expect(mockCallback).toHaveBeenCalledWith("arg1", "arg2"); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + describe("Integration with different CSS top values", () => { |
| 175 | + test("should handle different top values correctly", () => { |
| 176 | + // Mock different computed style top value |
| 177 | + window.getComputedStyle.mockReturnValue({ top: "50px" }); |
| 178 | + |
| 179 | + stickyInstance = new Sticky(mockElement); |
| 180 | + |
| 181 | + expect(stickyInstance.stickyElementTop).toBe(50); |
| 182 | + |
| 183 | + // Test with element above new threshold |
| 184 | + mockElement.getBoundingClientRect.mockReturnValue({ top: 30 }); |
| 185 | + stickyInstance.determineStickyState(); |
| 186 | + expect(mockElement.dataset.stuck).toBe("true"); |
| 187 | + |
| 188 | + // Test with element below new threshold |
| 189 | + mockElement.getBoundingClientRect.mockReturnValue({ top: 60 }); |
| 190 | + stickyInstance.determineStickyState(); |
| 191 | + expect(mockElement.dataset.stuck).toBe("false"); |
| 192 | + }); |
| 193 | + }); |
| 194 | +}); |
0 commit comments