forked from bots-garden/ori
| ✨ Workspace panel, selectors, previews, desktop app, sandbox template, resizable file tree, light/dark theme | 1 | import { beforeEach, describe, expect, it } from "vitest"; |
| 2 | import { | |
| 3 | DEFAULT_FILE_TREE_WIDTH, | |
| 4 | FILE_TREE_WIDTH_KEY, | |
| 5 | MAX_FILE_TREE_WIDTH, | |
| 6 | MIN_FILE_TREE_WIDTH, | |
| 7 | clampFileTreeWidth, | |
| 8 | openEditor, | |
| 9 | openPreview, | |
| 10 | resetFileTreeWidth, | |
| 11 | resetWorkspace, | |
| 12 | setFileTreeWidth, | |
| 13 | showTab, | |
| 14 | toggleWorkspace, | |
| 15 | useWorkspace, | |
| 16 | } from "./workspace"; | |
| 17 | ||
| 18 | describe("workspace store", () => { | |
| 19 | beforeEach(() => { | |
| 20 | localStorage.clear(); | |
| 21 | resetWorkspace(); | |
| 22 | }); | |
| 23 | ||
| 24 | it("starts closed on the preview tab", () => { | |
| 25 | const state = useWorkspace.getState(); | |
| 26 | expect(state.open).toBe(false); | |
| 27 | expect(state.tab).toBe("preview"); | |
| 28 | expect(state.previewPath).toBeNull(); | |
| 29 | expect(state.editorPath).toBeNull(); | |
| 30 | }); | |
| 31 | ||
| 32 | it("toggles the panel", () => { | |
| 33 | toggleWorkspace(); | |
| 34 | expect(useWorkspace.getState().open).toBe(true); | |
| 35 | toggleWorkspace(); | |
| 36 | expect(useWorkspace.getState().open).toBe(false); | |
| 37 | }); | |
| 38 | ||
| 39 | it("opens the panel when a tab is shown", () => { | |
| 40 | showTab("terminal"); | |
| 41 | const state = useWorkspace.getState(); | |
| 42 | expect(state.open).toBe(true); | |
| 43 | expect(state.tab).toBe("terminal"); | |
| 44 | }); | |
| 45 | ||
| 46 | it("openPreview targets the preview tab with the file", () => { | |
| 47 | openPreview("/work/readme.md"); | |
| 48 | const state = useWorkspace.getState(); | |
| 49 | expect(state).toMatchObject({ | |
| 50 | open: true, | |
| 51 | tab: "preview", | |
| 52 | previewPath: "/work/readme.md", | |
| 53 | }); | |
| 54 | }); | |
| 55 | ||
| 56 | it("openEditor targets the editor tab and keeps the preview file", () => { | |
| 57 | openPreview("/work/a.md"); | |
| 58 | openEditor("/work/b.go"); | |
| 59 | const state = useWorkspace.getState(); | |
| 60 | expect(state).toMatchObject({ | |
| 61 | open: true, | |
| 62 | tab: "editor", | |
| 63 | editorPath: "/work/b.go", | |
| 64 | previewPath: "/work/a.md", | |
| 65 | }); | |
| 66 | }); | |
| 67 | ||
| 68 | it("starts with the default file tree width", () => { | |
| 69 | expect(useWorkspace.getState().fileTreeWidth).toBe(DEFAULT_FILE_TREE_WIDTH); | |
| 70 | }); | |
| 71 | ||
| 72 | it("setFileTreeWidth applies the width and remembers it", () => { | |
| 73 | setFileTreeWidth(320); | |
| 74 | expect(useWorkspace.getState().fileTreeWidth).toBe(320); | |
| 75 | expect(localStorage.getItem(FILE_TREE_WIDTH_KEY)).toBe("320"); | |
| 76 | }); | |
| 77 | ||
| 78 | it("setFileTreeWidth keeps the width within the bounds", () => { | |
| 79 | setFileTreeWidth(10); | |
| 80 | expect(useWorkspace.getState().fileTreeWidth).toBe(MIN_FILE_TREE_WIDTH); | |
| 81 | setFileTreeWidth(5000); | |
| 82 | expect(useWorkspace.getState().fileTreeWidth).toBe(MAX_FILE_TREE_WIDTH); | |
| 83 | }); | |
| 84 | ||
| 85 | it("resetFileTreeWidth goes back to the default", () => { | |
| 86 | setFileTreeWidth(400); | |
| 87 | resetFileTreeWidth(); | |
| 88 | expect(useWorkspace.getState().fileTreeWidth).toBe(DEFAULT_FILE_TREE_WIDTH); | |
| 89 | expect(localStorage.getItem(FILE_TREE_WIDTH_KEY)).toBe( | |
| 90 | String(DEFAULT_FILE_TREE_WIDTH), | |
| 91 | ); | |
| 92 | }); | |
| 93 | }); | |
| 94 | ||
| 95 | describe("clampFileTreeWidth", () => { | |
| 96 | it("rounds and keeps in-range values", () => { | |
| 97 | expect(clampFileTreeWidth(300.6)).toBe(301); | |
| 98 | }); | |
| 99 | ||
| 100 | it("clamps below the minimum and above the maximum", () => { | |
| 101 | expect(clampFileTreeWidth(0)).toBe(MIN_FILE_TREE_WIDTH); | |
| 102 | expect(clampFileTreeWidth(-50)).toBe(MIN_FILE_TREE_WIDTH); | |
| 103 | expect(clampFileTreeWidth(10_000)).toBe(MAX_FILE_TREE_WIDTH); | |
| 104 | }); | |
| 105 | ||
| 106 | it("falls back to the default for NaN and infinities", () => { | |
| 107 | expect(clampFileTreeWidth(Number.NaN)).toBe(DEFAULT_FILE_TREE_WIDTH); | |
| 108 | expect(clampFileTreeWidth(Number.POSITIVE_INFINITY)).toBe( | |
| 109 | DEFAULT_FILE_TREE_WIDTH, | |
| 110 | ); | |
| 111 | }); | |
| 112 | }); |