72 lines
2.5 KiB
TypeScript
72 lines
2.5 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { useLayoutEffect } from "react";
|
|
import { render } from "@testing-library/react";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { AppShellSidebarContext, useRecommendedSidebarCollapse } from "./AppShellSidebarContext";
|
|
|
|
function BoardWorkspace({ workspaceKey }: { workspaceKey: string | null }) {
|
|
useRecommendedSidebarCollapse(workspaceKey);
|
|
return null;
|
|
}
|
|
|
|
function BoardWorkspaceWithLayoutProbe({ onLayout }: { onLayout: () => void }) {
|
|
useRecommendedSidebarCollapse("projects:pipeline-a");
|
|
useLayoutEffect(() => {
|
|
onLayout();
|
|
}, [onLayout]);
|
|
return null;
|
|
}
|
|
|
|
describe("useRecommendedSidebarCollapse", () => {
|
|
it("requests a collapse when a board opens and whenever its board identity changes", () => {
|
|
const collapseSidebarIfPreferred = vi.fn();
|
|
const value = { collapseSidebar: vi.fn(), collapseSidebarIfPreferred };
|
|
const { rerender } = render(
|
|
<AppShellSidebarContext.Provider value={value}>
|
|
<BoardWorkspace workspaceKey="projects:pipeline-a" />
|
|
</AppShellSidebarContext.Provider>
|
|
);
|
|
|
|
expect(collapseSidebarIfPreferred).toHaveBeenCalledOnce();
|
|
|
|
rerender(
|
|
<AppShellSidebarContext.Provider value={value}>
|
|
<BoardWorkspace workspaceKey="projects:pipeline-a" />
|
|
</AppShellSidebarContext.Provider>
|
|
);
|
|
expect(collapseSidebarIfPreferred).toHaveBeenCalledOnce();
|
|
|
|
rerender(
|
|
<AppShellSidebarContext.Provider value={value}>
|
|
<BoardWorkspace workspaceKey="projects:pipeline-b" />
|
|
</AppShellSidebarContext.Provider>
|
|
);
|
|
expect(collapseSidebarIfPreferred).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it("does nothing when no board is available", () => {
|
|
const collapseSidebarIfPreferred = vi.fn();
|
|
render(
|
|
<AppShellSidebarContext.Provider value={{ collapseSidebar: vi.fn(), collapseSidebarIfPreferred }}>
|
|
<BoardWorkspace workspaceKey={null} />
|
|
</AppShellSidebarContext.Provider>
|
|
);
|
|
|
|
expect(collapseSidebarIfPreferred).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("requests the collapse during layout before the board can paint", () => {
|
|
const order: string[] = [];
|
|
const collapseSidebarIfPreferred = vi.fn(() => order.push("collapse"));
|
|
|
|
render(
|
|
<AppShellSidebarContext.Provider value={{ collapseSidebar: vi.fn(), collapseSidebarIfPreferred }}>
|
|
<BoardWorkspaceWithLayoutProbe onLayout={() => order.push("layout-probe")} />
|
|
</AppShellSidebarContext.Provider>
|
|
);
|
|
|
|
expect(order).toEqual(["collapse", "layout-probe"]);
|
|
});
|
|
});
|