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