|
| 1 | +import { SparklesIcon } from "lucide-react" |
| 2 | +import { expect, userEvent, waitFor } from "storybook/test" |
| 3 | + |
| 4 | +import { |
| 5 | + AssistantDockControls, |
| 6 | + AssistantLayout, |
| 7 | + AssistantLayoutProvider, |
| 8 | +} from "./AssistantLayout" |
| 9 | + |
| 10 | +import type { AssistantDock } from "./dockLayout" |
| 11 | +import type { Meta, StoryObj } from "@storybook/react-vite" |
| 12 | + |
| 13 | +const meta: Meta<typeof AssistantLayout> = { |
| 14 | + title: "AI Elements/Layout Manager", |
| 15 | + component: AssistantLayout, |
| 16 | + parameters: { layout: "fullscreen" }, |
| 17 | + tags: ["autodocs"], |
| 18 | +} |
| 19 | + |
| 20 | +export default meta |
| 21 | +type Story = StoryObj<typeof AssistantLayout> |
| 22 | + |
| 23 | +function MockAssistant() { |
| 24 | + return ( |
| 25 | + <> |
| 26 | + <div className="flex shrink-0 items-center gap-2 border-b border-border px-3 py-2"> |
| 27 | + <SparklesIcon className="size-4 text-primary" /> |
| 28 | + <span className="text-sm font-medium">AI Assistant</span> |
| 29 | + </div> |
| 30 | + <div className="flex-1 space-y-3 overflow-auto p-3 text-sm"> |
| 31 | + <div className="ml-auto w-fit max-w-[80%] rounded-lg bg-primary px-3 py-2 text-primary-foreground"> |
| 32 | + Which leads have the best developability? |
| 33 | + </div> |
| 34 | + <div className="w-fit max-w-[85%] rounded-lg bg-muted px-3 py-2"> |
| 35 | + Leads 3, 7 and 12 score highest on aggregation and thermostability. Want me to filter the table to those? |
| 36 | + </div> |
| 37 | + </div> |
| 38 | + <div className="shrink-0 border-t border-border p-3"> |
| 39 | + <div className="flex h-9 items-center rounded-lg border border-input px-3 text-sm text-muted-foreground"> |
| 40 | + Ask anything… |
| 41 | + </div> |
| 42 | + </div> |
| 43 | + </> |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +function MockContent() { |
| 48 | + return ( |
| 49 | + <div className="flex-1 overflow-auto p-5"> |
| 50 | + <h2 className="text-lg font-semibold">Antibody Lead Selection</h2> |
| 51 | + <p className="mt-1 text-sm text-muted-foreground"> |
| 52 | + Main content area. Use the dock controls in the top bar to move the AI Assistant left, bottom, or right — |
| 53 | + or click the active icon again to hide it and reclaim the full width. |
| 54 | + </p> |
| 55 | + <div className="mt-4 grid grid-cols-3 gap-3"> |
| 56 | + {Array.from({ length: 9 }).map((_, i) => ( |
| 57 | + <div key={i} className="rounded-lg border border-border bg-background p-4 text-sm"> |
| 58 | + Lead {i + 1} |
| 59 | + </div> |
| 60 | + ))} |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + ) |
| 64 | +} |
| 65 | + |
| 66 | +function Demo({ defaultDock }: { defaultDock?: AssistantDock }) { |
| 67 | + return ( |
| 68 | + <AssistantLayoutProvider defaultDock={defaultDock} persist={false}> |
| 69 | + <div className="flex h-screen w-full flex-col gap-2 p-3"> |
| 70 | + <div className="flex shrink-0 items-center justify-between"> |
| 71 | + <span className="text-sm font-semibold">Workspace</span> |
| 72 | + <AssistantDockControls /> |
| 73 | + </div> |
| 74 | + <AssistantLayout assistant={<MockAssistant />}> |
| 75 | + <MockContent /> |
| 76 | + </AssistantLayout> |
| 77 | + </div> |
| 78 | + </AssistantLayoutProvider> |
| 79 | + ) |
| 80 | +} |
| 81 | + |
| 82 | +export const DockedLeft: Story = { |
| 83 | + render: () => <Demo defaultDock="left" />, |
| 84 | + play: async ({ canvasElement, step }) => { |
| 85 | + const body = () => canvasElement.querySelector('[data-slot="assistant-layout"]') as HTMLElement |
| 86 | + const assistant = () => |
| 87 | + canvasElement.querySelector('[data-slot="assistant-layout-assistant"]') as HTMLElement |
| 88 | + const content = () => |
| 89 | + canvasElement.querySelector('[data-slot="assistant-layout-content"]') as HTMLElement |
| 90 | + // The three dock buttons are the only elements with aria-pressed. |
| 91 | + const dockButtons = () => [...canvasElement.querySelectorAll<HTMLElement>("[aria-pressed]")] |
| 92 | + // True when `a` appears before `b` in document order. |
| 93 | + const precedes = (a: HTMLElement, b: HTMLElement) => |
| 94 | + Boolean(a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING) |
| 95 | + |
| 96 | + await step("Starts docked left (horizontal, assistant before content)", async () => { |
| 97 | + expect(getComputedStyle(body()).flexDirection).toBe("row") |
| 98 | + expect(assistant()).toBeVisible() |
| 99 | + expect(precedes(assistant(), content())).toBe(true) |
| 100 | + // The resizable handle is present while the assistant is visible. |
| 101 | + expect(canvasElement.querySelector('[role="separator"]')).not.toBeNull() |
| 102 | + }) |
| 103 | + |
| 104 | + await step("Dock bottom → vertical", async () => { |
| 105 | + await userEvent.click(dockButtons()[1]) // [left, bottom, right] |
| 106 | + expect(getComputedStyle(body()).flexDirection).toBe("column") |
| 107 | + expect(precedes(content(), assistant())).toBe(true) |
| 108 | + }) |
| 109 | + |
| 110 | + await step("Click the active dock again → hidden, no handle, none pressed", async () => { |
| 111 | + await userEvent.click(dockButtons()[1]) // now active → hides |
| 112 | + expect(canvasElement.querySelector('[role="separator"]')).toBeNull() |
| 113 | + expect(dockButtons().some((b) => b.getAttribute("aria-pressed") === "true")).toBe(false) |
| 114 | + expect(canvasElement.querySelector('[data-slot="assistant-layout-assistant"]')).toBeNull() |
| 115 | + }) |
| 116 | + |
| 117 | + await step("Re-dock right → visible again, content before assistant", async () => { |
| 118 | + await userEvent.click(dockButtons()[2]) |
| 119 | + expect(assistant()).toBeVisible() |
| 120 | + expect(getComputedStyle(body()).flexDirection).toBe("row") |
| 121 | + expect(precedes(content(), assistant())).toBe(true) |
| 122 | + }) |
| 123 | + }, |
| 124 | + parameters: { |
| 125 | + zephyr: { testCaseId: "SW-T5497" }, |
| 126 | + }, |
| 127 | +} |
| 128 | +export const DockedRight: Story = { render: () => <Demo defaultDock="right" />, |
| 129 | + parameters: { |
| 130 | + // Auto-generated by sync-storybook-zephyr - do not add manually |
| 131 | + zephyr: { testCaseId: "SW-T5498" }, |
| 132 | + } |
| 133 | +} |
| 134 | +export const DockedBottom: Story = { render: () => <Demo defaultDock="bottom" />, |
| 135 | + parameters: { |
| 136 | + // Auto-generated by sync-storybook-zephyr - do not add manually |
| 137 | + zephyr: { testCaseId: "SW-T5499" }, |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// testCaseId intentionally left blank — the zephyr_sync workflow backfills it. |
| 142 | +export const ResizeByKeyboard: Story = { |
| 143 | + parameters: { zephyr: { testCaseId: "SW-T5500" } }, |
| 144 | + render: () => <Demo defaultDock="right" />, |
| 145 | + play: async ({ canvasElement, step }) => { |
| 146 | + const separator = () => canvasElement.querySelector('[role="separator"]') as HTMLElement |
| 147 | + const assistant = () => |
| 148 | + canvasElement.querySelector('[data-slot="assistant-layout-assistant"]') as HTMLElement |
| 149 | + |
| 150 | + await step("Resize handle is present and keyboard-focusable", async () => { |
| 151 | + expect(separator()).not.toBeNull() |
| 152 | + expect(assistant()).toBeVisible() |
| 153 | + }) |
| 154 | + |
| 155 | + await step("Arrow keys resize the panels via the Resizable handle", async () => { |
| 156 | + const widthBefore = assistant().getBoundingClientRect().width |
| 157 | + separator().focus() |
| 158 | + await userEvent.keyboard("{ArrowLeft}{ArrowLeft}{ArrowLeft}{ArrowLeft}") |
| 159 | + await waitFor(() => |
| 160 | + expect(assistant().getBoundingClientRect().width).not.toBe(widthBefore) |
| 161 | + ) |
| 162 | + }) |
| 163 | + }, |
| 164 | +} |
0 commit comments