Skip to content

Commit 6e9d4a6

Browse files
committed
Add custom serializers for groups, panels, and separators
1 parent 321100c commit 6e9d4a6

File tree

5 files changed

+178
-51
lines changed

5 files changed

+178
-51
lines changed

lib/state/MutableGroup.test.ts

Lines changed: 92 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -147,22 +147,32 @@ describe("MutableGroup", () => {
147147
groupDimensions: { width: 100, height: 50 },
148148
panels: [{ id: "left", defaultSize: 35, minSize: 35 }, { id: "right" }]
149149
});
150-
expect(group.layout).toMatchInlineSnapshot(`
151-
{
152-
"left": 35,
153-
"right": 65,
154-
}
150+
expect(group).toMatchInlineSnapshot(`
151+
Group: a
152+
Panel: left
153+
- default size 35px
154+
- range 35px - 100%
155+
Panel: right
156+
- range 0% - 100%
157+
Layout:
158+
- left: 35
159+
- right: 65
155160
`);
156161

157162
group.updateDimensions({
158163
width: 50,
159164
height: 50
160165
});
161-
expect(group.layout).toMatchInlineSnapshot(`
162-
{
163-
"left": 70,
164-
"right": 30,
165-
}
166+
expect(group).toMatchInlineSnapshot(`
167+
Group: a
168+
Panel: left
169+
- default size 35px
170+
- range 35px - 100%
171+
Panel: right
172+
- range 0% - 100%
173+
Layout:
174+
- left: 70
175+
- right: 30
166176
`);
167177
});
168178
});
@@ -173,11 +183,15 @@ describe("MutableGroup", () => {
173183
groupDimensions: { width: 100, height: 50 },
174184
panels: [{ id: "left" }, { id: "right" }]
175185
});
176-
expect(group.layout).toMatchInlineSnapshot(`
177-
{
178-
"left": 50,
179-
"right": 50,
180-
}
186+
expect(group).toMatchInlineSnapshot(`
187+
Group: a
188+
Panel: left
189+
- range 0% - 100%
190+
Panel: right
191+
- range 0% - 100%
192+
Layout:
193+
- left: 50
194+
- right: 50
181195
`);
182196

183197
group.panels[0].updateConstraints({
@@ -187,12 +201,15 @@ describe("MutableGroup", () => {
187201
minSize: "65%"
188202
});
189203
group.flushPendingValidation();
190-
191-
expect(group.layout).toMatchInlineSnapshot(`
192-
{
193-
"left": 65,
194-
"right": 35,
195-
}
204+
expect(group).toMatchInlineSnapshot(`
205+
Group: a
206+
Panel: left
207+
- range 65% - 100%
208+
Panel: right
209+
- range 0% - 100%
210+
Layout:
211+
- left: 65
212+
- right: 35
196213
`);
197214
});
198215
});
@@ -205,11 +222,15 @@ describe("MutableGroup", () => {
205222
{ id: "right", minSize: "25%" }
206223
]
207224
});
208-
expect(group.layout).toMatchInlineSnapshot(`
209-
{
210-
"left": 50,
211-
"right": 50,
212-
}
225+
expect(group).toMatchInlineSnapshot(`
226+
Group: a
227+
Panel: left
228+
- range 25% - 100%
229+
Panel: right
230+
- range 25% - 100%
231+
Layout:
232+
- left: 50
233+
- right: 50
213234
`);
214235

215236
// Invalid (account to constraints)
@@ -220,11 +241,15 @@ describe("MutableGroup", () => {
220241
right: 75
221242
})
222243
.endTransaction();
223-
expect(group.layout).toMatchInlineSnapshot(`
224-
{
225-
"left": 25,
226-
"right": 75,
227-
}
244+
expect(group).toMatchInlineSnapshot(`
245+
Group: a
246+
Panel: left
247+
- range 25% - 100%
248+
Panel: right
249+
- range 25% - 100%
250+
Layout:
251+
- left: 25
252+
- right: 75
228253
`);
229254
});
230255

@@ -240,11 +265,15 @@ describe("MutableGroup", () => {
240265
{ id: "right", minSize: "25%" }
241266
]
242267
});
243-
expect(group.layout).toMatchInlineSnapshot(`
244-
{
245-
"left": 25,
246-
"right": 75,
247-
}
268+
expect(group).toMatchInlineSnapshot(`
269+
Group: a
270+
Panel: left
271+
- range 25% - 100%
272+
Panel: right
273+
- range 25% - 100%
274+
Layout:
275+
- left: 25
276+
- right: 75
248277
`);
249278

250279
// Invalid (account to constraints)
@@ -255,11 +284,15 @@ describe("MutableGroup", () => {
255284
right: 100
256285
})
257286
.endTransaction();
258-
expect(group.layout).toMatchInlineSnapshot(`
259-
{
260-
"left": 25,
261-
"right": 75,
262-
}
287+
expect(group).toMatchInlineSnapshot(`
288+
Group: a
289+
Panel: left
290+
- range 25% - 100%
291+
Panel: right
292+
- range 25% - 100%
293+
Layout:
294+
- left: 25
295+
- right: 75
263296
`);
264297
});
265298

@@ -287,11 +320,15 @@ describe("MutableGroup", () => {
287320
})
288321
.endTransaction();
289322
}).toLogError("Invalid 2 panel layout keys: left, center, right");
290-
expect(group.layout).toMatchInlineSnapshot(`
291-
{
292-
"left": 25,
293-
"right": 75,
294-
}
323+
expect(group).toMatchInlineSnapshot(`
324+
Group: a
325+
Panel: left
326+
- range 25% - 100%
327+
Panel: right
328+
- range 25% - 100%
329+
Layout:
330+
- left: 25
331+
- right: 75
295332
`);
296333
});
297334

@@ -318,11 +355,15 @@ describe("MutableGroup", () => {
318355
})
319356
.endTransaction();
320357
}).toLogError("Invalid 2 panel layout keys: top, bottom");
321-
expect(group.layout).toMatchInlineSnapshot(`
322-
{
323-
"left": 25,
324-
"right": 75,
325-
}
358+
expect(group).toMatchInlineSnapshot(`
359+
Group: a
360+
Panel: left
361+
- range 25% - 100%
362+
Panel: right
363+
- range 25% - 100%
364+
Layout:
365+
- left: 25
366+
- right: 75
326367
`);
327368
});
328369

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { MutableGroup } from "../MutableGroup";
2+
import { serializeMutablePanel } from "./serializeMutablePanel";
3+
import { serializeMutableSeparator } from "./serializeMutableSeparator";
4+
5+
export function serializeMutableGroup(
6+
group: MutableGroup,
7+
indentation: string
8+
) {
9+
const strings = [`Group: ${group.id}`];
10+
group.panels.forEach((panel) => {
11+
strings.push(serializeMutablePanel(panel, indentation + " "));
12+
});
13+
group.separators.forEach((separator) => {
14+
strings.push(serializeMutableSeparator(separator, indentation + " "));
15+
});
16+
strings.push(" Layout:");
17+
Object.keys(group.layout).forEach((key) => {
18+
strings.push(` - ${key}: ${group.layout[key]}`);
19+
});
20+
21+
return strings.map((line) => `${indentation}${line}`).join("\n");
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { MutablePanel } from "../MutablePanel";
2+
3+
export function serializeMutablePanel(
4+
panel: MutablePanel,
5+
indentation: string
6+
) {
7+
const strings = [`Panel: ${panel.id}`];
8+
if (panel.collapsible) {
9+
strings.push(
10+
` - collapsible to ${panel.collapsedSize}${panel.collapsedUnit}`
11+
);
12+
}
13+
if (panel.defaultSize !== undefined) {
14+
strings.push(` - default size ${panel.defaultSize}${panel.defaultUnit}`);
15+
}
16+
strings.push(
17+
` - range ${panel.minSize}${panel.minUnit} - ${panel.maxSize}${panel.maxUnit}`
18+
);
19+
20+
return strings.map((line) => `${indentation}${line}`).join("\n");
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { MutableSeparator } from "../MutableSeparator";
2+
3+
export function serializeMutableSeparator(
4+
separator: MutableSeparator,
5+
indentation: string
6+
) {
7+
const strings = [`Separator: ${separator.id}`];
8+
9+
return strings.map((line) => `${indentation}${line}`).join("\n");
10+
}

vitest.setup.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { cleanup } from "@testing-library/react";
33
import { afterAll, afterEach, beforeAll, beforeEach, expect, vi } from "vitest";
44
import failOnConsole from "vitest-fail-on-console";
55
import { resetMockGroupIdCounter } from "./lib/global/test/mockGroup";
6+
import { MutableGroup } from "./lib/state/MutableGroup";
7+
import { MutablePanel } from "./lib/state/MutablePanel";
8+
import { MutableSeparator } from "./lib/state/MutableSeparator";
9+
import { serializeMutableGroup } from "./lib/state/test-utils/serializeMutableGroup";
10+
import { serializeMutablePanel } from "./lib/state/test-utils/serializeMutablePanel";
11+
import { serializeMutableSeparator } from "./lib/state/test-utils/serializeMutableSeparator";
612
import { EventEmitter } from "./lib/utils/EventEmitter";
713
import { mockBoundingClientRect } from "./lib/utils/test/mockBoundingClientRect";
814
import { mockResizeObserver } from "./lib/utils/test/mockResizeObserver";
@@ -38,6 +44,33 @@ expect.addSnapshotSerializer({
3844
}
3945
});
4046

47+
expect.addSnapshotSerializer({
48+
serialize(value, _, indentation) {
49+
return serializeMutableGroup(value as MutableGroup, indentation);
50+
},
51+
test(value) {
52+
return value instanceof MutableGroup;
53+
}
54+
});
55+
56+
expect.addSnapshotSerializer({
57+
serialize(value, _, indentation) {
58+
return serializeMutablePanel(value as MutablePanel, indentation);
59+
},
60+
test(value) {
61+
return value instanceof MutablePanel;
62+
}
63+
});
64+
65+
expect.addSnapshotSerializer({
66+
serialize(value, _, indentation) {
67+
return serializeMutableSeparator(value as MutableSeparator, indentation);
68+
},
69+
test(value) {
70+
return value instanceof MutableSeparator;
71+
}
72+
});
73+
4174
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4275
type GenericEventEmitter = EventEmitter<any>;
4376

0 commit comments

Comments
 (0)