|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
| 2 | +import React, { act, useRef } from "react"; |
| 3 | +import { createRoot, Root } from "react-dom/client"; |
| 4 | +import { useBreakpoint, type BreakpointName } from "../use-responsive"; |
| 5 | + |
| 6 | +let container: HTMLDivElement; |
| 7 | +let root: Root; |
| 8 | + |
| 9 | +function mount(element: React.ReactElement) { |
| 10 | + act(() => { |
| 11 | + root.render(element); |
| 12 | + }); |
| 13 | +} |
| 14 | + |
| 15 | +beforeEach(() => { |
| 16 | + container = document.createElement("div"); |
| 17 | + document.body.appendChild(container); |
| 18 | + root = createRoot(container); |
| 19 | +}); |
| 20 | + |
| 21 | +afterEach(() => { |
| 22 | + act(() => { |
| 23 | + root.unmount(); |
| 24 | + }); |
| 25 | + container.remove(); |
| 26 | + vi.unstubAllGlobals(); |
| 27 | + vi.restoreAllMocks(); |
| 28 | +}); |
| 29 | + |
| 30 | +function setViewportWidth(width: number) { |
| 31 | + Object.defineProperty(window, "innerWidth", { value: width, configurable: true, writable: true }); |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * A controllable ResizeObserver stand-in. happy-dom does no layout, so the real |
| 36 | + * observer never fires; this lets a test trigger the callback on demand. |
| 37 | + */ |
| 38 | +function stubResizeObserver() { |
| 39 | + const callbacks: ResizeObserverCallback[] = []; |
| 40 | + class FakeResizeObserver { |
| 41 | + constructor(cb: ResizeObserverCallback) { |
| 42 | + callbacks.push(cb); |
| 43 | + } |
| 44 | + observe() {} |
| 45 | + unobserve() {} |
| 46 | + disconnect() {} |
| 47 | + } |
| 48 | + vi.stubGlobal("ResizeObserver", FakeResizeObserver); |
| 49 | + return { |
| 50 | + fireAll: () => { |
| 51 | + act(() => { |
| 52 | + callbacks.forEach((cb) => cb([], {} as ResizeObserver)); |
| 53 | + }); |
| 54 | + }, |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +/** Pins clientWidth on the node the instant it attaches, before effects run. */ |
| 59 | +function pinWidth(width: number) { |
| 60 | + return (el: HTMLDivElement | null) => { |
| 61 | + if (el) Object.defineProperty(el, "clientWidth", { value: width, configurable: true }); |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +function Display({ width }: { width?: number }) { |
| 66 | + const ref = useRef<HTMLDivElement>(null); |
| 67 | + const bp = useBreakpoint(width !== undefined ? ref : undefined); |
| 68 | + return ( |
| 69 | + <div |
| 70 | + ref={(el) => { |
| 71 | + ref.current = el; |
| 72 | + if (width !== undefined) pinWidth(width)(el); |
| 73 | + }} |
| 74 | + data-testid="bp" |
| 75 | + > |
| 76 | + {bp} |
| 77 | + </div> |
| 78 | + ); |
| 79 | +} |
| 80 | + |
| 81 | +function readBp(): BreakpointName { |
| 82 | + return container.querySelector('[data-testid="bp"]')!.textContent as BreakpointName; |
| 83 | +} |
| 84 | + |
| 85 | +describe("useBreakpoint", () => { |
| 86 | + it("falls back to the viewport width when no container ref is supplied", () => { |
| 87 | + setViewportWidth(500); |
| 88 | + mount(<Display />); |
| 89 | + expect(readBp()).toBe("mobile"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("derives the breakpoint from the container width, not the viewport", () => { |
| 93 | + // Wide viewport, but a narrow content container (e.g. sidebar open) — the |
| 94 | + // breakpoint must follow the container so buttons collapse correctly. |
| 95 | + setViewportWidth(1920); |
| 96 | + stubResizeObserver(); |
| 97 | + mount(<Display width={700} />); |
| 98 | + expect(readBp()).toBe("tablet"); |
| 99 | + }); |
| 100 | + |
| 101 | + it("re-evaluates when the observed container changes width", async () => { |
| 102 | + setViewportWidth(1920); |
| 103 | + const { fireAll } = stubResizeObserver(); |
| 104 | + |
| 105 | + let measured = 1100; |
| 106 | + function Resizable() { |
| 107 | + // A getter lets the test mutate the reported width between observer fires. |
| 108 | + const setRef = (el: HTMLDivElement | null) => { |
| 109 | + if (el) |
| 110 | + Object.defineProperty(el, "clientWidth", { get: () => measured, configurable: true }); |
| 111 | + }; |
| 112 | + const ref = useRef<HTMLDivElement>(null); |
| 113 | + const bp = useBreakpoint(ref); |
| 114 | + return ( |
| 115 | + <div |
| 116 | + ref={(el) => { |
| 117 | + ref.current = el; |
| 118 | + setRef(el); |
| 119 | + }} |
| 120 | + data-testid="bp" |
| 121 | + > |
| 122 | + {bp} |
| 123 | + </div> |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + mount(<Resizable />); |
| 128 | + expect(readBp()).toBe("wide"); |
| 129 | + |
| 130 | + // Sidebar opens — content shrinks below the desktop band. The observer |
| 131 | + // callback is debounced by 100ms (RESIZE_DEBOUNCE_MS) before re-measuring. |
| 132 | + measured = 800; |
| 133 | + fireAll(); |
| 134 | + await act(async () => { |
| 135 | + await new Promise((r) => setTimeout(r, 150)); |
| 136 | + }); |
| 137 | + expect(readBp()).toBe("desktop"); |
| 138 | + }); |
| 139 | +}); |
0 commit comments