|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import React, { act } from "react"; |
| 3 | +import { createRoot, Root } from "react-dom/client"; |
| 4 | +import { DataTableOption } from "../DataTableOption"; |
| 5 | +import { Filter } from "lucide-react"; |
| 6 | +import * as BreakpointContext from "@/hooks/use-breakpoint-context"; |
| 7 | + |
| 8 | +let container: HTMLDivElement; |
| 9 | +let root: Root; |
| 10 | + |
| 11 | +function mount(element: React.ReactElement) { |
| 12 | + act(() => { |
| 13 | + root.render(element); |
| 14 | + }); |
| 15 | +} |
| 16 | + |
| 17 | +beforeEach(() => { |
| 18 | + container = document.createElement("div"); |
| 19 | + document.body.appendChild(container); |
| 20 | + root = createRoot(container); |
| 21 | +}); |
| 22 | + |
| 23 | +afterEach(() => { |
| 24 | + act(() => { |
| 25 | + root.unmount(); |
| 26 | + }); |
| 27 | + container.remove(); |
| 28 | + vi.restoreAllMocks(); |
| 29 | +}); |
| 30 | + |
| 31 | +describe("DataTableOption responsive behavior", () => { |
| 32 | + it("renders inline (not popover) and fills available width on mobile breakpoint", () => { |
| 33 | + vi.spyOn(BreakpointContext, "useCurrentBreakpoint").mockReturnValue("mobile"); |
| 34 | + |
| 35 | + mount( |
| 36 | + <DataTableOption |
| 37 | + icon={Filter} |
| 38 | + label="Filter" |
| 39 | + displayMode="inline" |
| 40 | + inlineDirection="right" |
| 41 | + defaultExpanded={true} |
| 42 | + > |
| 43 | + <div data-testid="filter-content">Filter content</div> |
| 44 | + </DataTableOption>, |
| 45 | + ); |
| 46 | + |
| 47 | + const button = container.querySelector("button"); |
| 48 | + expect(button).toBeTruthy(); |
| 49 | + expect(button?.textContent).toContain("Filter"); |
| 50 | + |
| 51 | + // Behavior stays inline on mobile — the content is rendered inline (not |
| 52 | + // hidden behind a popover trigger). |
| 53 | + const filterContent = container.querySelector('[data-testid="filter-content"]'); |
| 54 | + expect(filterContent).toBeTruthy(); |
| 55 | + |
| 56 | + // On compact screens the container goes full-width and the expanded panel |
| 57 | + // flexes to fill the remaining space rather than using a fixed 450px width. |
| 58 | + const containerEl = container.querySelector(".w-full"); |
| 59 | + expect(containerEl).toBeTruthy(); |
| 60 | + const flexPanel = container.querySelector(".flex-1"); |
| 61 | + expect(flexPanel).toBeTruthy(); |
| 62 | + // No fixed desktop width on mobile. |
| 63 | + expect(container.querySelector(".w-\\[450px\\]")).toBeFalsy(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("renders inline with fixed width on desktop breakpoint", () => { |
| 67 | + vi.spyOn(BreakpointContext, "useCurrentBreakpoint").mockReturnValue("desktop"); |
| 68 | + |
| 69 | + mount( |
| 70 | + <DataTableOption |
| 71 | + icon={Filter} |
| 72 | + label="Filter" |
| 73 | + displayMode="inline" |
| 74 | + inlineDirection="right" |
| 75 | + defaultExpanded={true} |
| 76 | + > |
| 77 | + <div data-testid="filter-content">Filter content</div> |
| 78 | + </DataTableOption>, |
| 79 | + ); |
| 80 | + |
| 81 | + // On desktop with expanded state, should render inline expansion |
| 82 | + const button = container.querySelector("button"); |
| 83 | + expect(button).toBeTruthy(); |
| 84 | + |
| 85 | + // Content should be visible |
| 86 | + const filterContent = container.querySelector('[data-testid="filter-content"]'); |
| 87 | + expect(filterContent).toBeTruthy(); |
| 88 | + |
| 89 | + // Check that the expansion container uses desktop width (w-[450px]) |
| 90 | + const expansionContainer = container.querySelector(".w-\\[450px\\]"); |
| 91 | + expect(expansionContainer).toBeTruthy(); |
| 92 | + }); |
| 93 | + |
| 94 | + it("renders inline and fills available width on tablet breakpoint", () => { |
| 95 | + vi.spyOn(BreakpointContext, "useCurrentBreakpoint").mockReturnValue("tablet"); |
| 96 | + |
| 97 | + mount( |
| 98 | + <DataTableOption |
| 99 | + icon={Filter} |
| 100 | + label="Filter" |
| 101 | + displayMode="inline" |
| 102 | + inlineDirection="right" |
| 103 | + defaultExpanded={true} |
| 104 | + > |
| 105 | + <div data-testid="filter-content">Filter content</div> |
| 106 | + </DataTableOption>, |
| 107 | + ); |
| 108 | + |
| 109 | + // On tablet with expanded state, should render inline expansion |
| 110 | + const button = container.querySelector("button"); |
| 111 | + expect(button).toBeTruthy(); |
| 112 | + |
| 113 | + // Content should be visible |
| 114 | + const filterContent = container.querySelector('[data-testid="filter-content"]'); |
| 115 | + expect(filterContent).toBeTruthy(); |
| 116 | + |
| 117 | + // Tablet is also treated as compact: full-width container + flexing panel, |
| 118 | + // no fixed desktop width. |
| 119 | + expect(container.querySelector(".w-full")).toBeTruthy(); |
| 120 | + expect(container.querySelector(".flex-1")).toBeTruthy(); |
| 121 | + expect(container.querySelector(".w-\\[450px\\]")).toBeFalsy(); |
| 122 | + }); |
| 123 | + |
| 124 | + it("respects explicit popover displayMode on desktop", () => { |
| 125 | + vi.spyOn(BreakpointContext, "useCurrentBreakpoint").mockReturnValue("desktop"); |
| 126 | + |
| 127 | + mount( |
| 128 | + <DataTableOption icon={Filter} label="Filter" displayMode="popover"> |
| 129 | + <div data-testid="filter-content">Filter content</div> |
| 130 | + </DataTableOption>, |
| 131 | + ); |
| 132 | + |
| 133 | + // Even on desktop, if displayMode is explicitly "popover", it should render as popover |
| 134 | + const button = container.querySelector("button"); |
| 135 | + expect(button).toBeTruthy(); |
| 136 | + |
| 137 | + // The popover content should not be visible initially |
| 138 | + const filterContent = container.querySelector('[data-testid="filter-content"]'); |
| 139 | + expect(filterContent).toBeFalsy(); |
| 140 | + }); |
| 141 | +}); |
0 commit comments