Skip to content

Commit d842071

Browse files
committed
test: cover corner cases
Signed-off-by: Andres Correa Casablanca <castarco@coderspirit.xyz>
1 parent 13333f6 commit d842071

File tree

3 files changed

+210
-9
lines changed

3 files changed

+210
-9
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66
},
77
parser: '@typescript-eslint/parser',
88
parserOptions: {
9-
ecmaVersion: 2022,
9+
ecmaVersion: 2020,
1010
sourceType: 'module',
1111
tsConfigRootDir: __dirname,
1212
project: ['./tsconfig.json'],

src/core.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
export type TreeChild<T extends Tree = Tree> = {
22
node: T,
3-
edgeData: Record<string, unknown>,
3+
edgeData?: Record<string, unknown> | undefined,
44
}
55

66
export type Node = {
7-
data: Record<string, unknown>,
7+
data?: Record<string, unknown> | undefined,
88
}
99

1010
export type Tree = Node & {
11-
children?: TreeChild[]
11+
children?: TreeChild[] | undefined,
1212
}
1313

1414
export type TreeWithLayout = Node & {
15-
children?: Required<TreeChild<TreeWithLayout>>[]
15+
children?: Required<TreeChild<TreeWithLayout>>[] | undefined,
1616
layout: {
1717
plan: { x: number, y: number },
1818
}
@@ -35,7 +35,7 @@ const _computeLeftShiftLayout = (tree: Tree, depth: number = 0, layout?: Interna
3535
children: tree.children?.map(child => ({
3636
edgeData: child.edgeData,
3737
node: _computeLeftShiftLayout(child.node, depth + 1, layout),
38-
})) ?? [],
38+
})),
3939
layout: { plan: { x, y: depth } },
4040
} satisfies TreeWithLayout
4141

src/tests/core.test.ts

Lines changed: 204 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('computeLeftShiftLayout', () => {
66
const resultWithoutChildren = computeLeftShiftLayout({
77
data: { v: 42 },
88
})
9-
expect(resultWithoutChildren).toMatchObject({
9+
expect(resultWithoutChildren).toEqual({
1010
data: { v: 42 },
1111
layout: { plan: { x: 0, y: 0 } },
1212
})
@@ -15,7 +15,7 @@ describe('computeLeftShiftLayout', () => {
1515
data: { v: 42 },
1616
children: [],
1717
})
18-
expect(resultWithEmptyChildren).toMatchObject({
18+
expect(resultWithEmptyChildren).toEqual({
1919
data: { v: 42 },
2020
children: [],
2121
layout: { plan: { x: 0, y: 0 } },
@@ -33,7 +33,7 @@ describe('computeLeftShiftLayout', () => {
3333
],
3434
})
3535

36-
expect(result).toMatchObject({
36+
expect(result).toEqual({
3737
data: { v: 42 },
3838
children: [
3939
{
@@ -47,4 +47,205 @@ describe('computeLeftShiftLayout', () => {
4747
layout: { plan: { x: 0, y: 0 } },
4848
})
4949
})
50+
51+
it('sets x=0,y=0 & x=0,y=1 & x=1,y=1 for tree with two children', () => {
52+
const result = computeLeftShiftLayout({
53+
data: { v: 42 },
54+
children: [
55+
{
56+
edgeData: {},
57+
node: { data: { v: 43 } },
58+
},
59+
{
60+
edgeData: {},
61+
node: { data: { v: 44 } },
62+
}
63+
],
64+
})
65+
66+
expect(result).toEqual({
67+
data: { v: 42 },
68+
children: [
69+
{
70+
edgeData: {},
71+
node: {
72+
data: { v: 43 },
73+
layout: { plan: { x: 0, y: 1 } },
74+
},
75+
},
76+
{
77+
edgeData: {},
78+
node: {
79+
data: { v: 44 },
80+
layout: { plan: { x: 1, y: 1 } },
81+
},
82+
}
83+
],
84+
layout: { plan: { x: 0, y: 0 } },
85+
})
86+
})
87+
88+
it('sets x for ((.,.),(.))', () => {
89+
const result = computeLeftShiftLayout({
90+
data: { v: 42 },
91+
children: [
92+
{
93+
edgeData: {},
94+
node: {
95+
data: { v: 43 },
96+
children: [
97+
{
98+
edgeData: {},
99+
node: { data: { v: 45 } },
100+
},
101+
{
102+
edgeData: {},
103+
node: { data: { v: 46 } },
104+
},
105+
]
106+
},
107+
},
108+
{
109+
edgeData: {},
110+
node: {
111+
data: { v: 44 },
112+
children: [
113+
{
114+
edgeData: {},
115+
node: { data: { v: 47 } },
116+
},
117+
]
118+
},
119+
}
120+
],
121+
})
122+
123+
expect(result).toEqual({
124+
data: { v: 42 },
125+
children: [
126+
{
127+
edgeData: {},
128+
node: {
129+
data: { v: 43 },
130+
children: [
131+
{
132+
edgeData: {},
133+
node: {
134+
data: { v: 45 },
135+
layout: { plan: { x: 0, y: 2 } },
136+
},
137+
},
138+
{
139+
edgeData: {},
140+
node: {
141+
data: { v: 46 },
142+
layout: { plan: { x: 1, y: 2 } },
143+
},
144+
},
145+
],
146+
layout: { plan: { x: 0, y: 1 } },
147+
},
148+
},
149+
{
150+
edgeData: {},
151+
node: {
152+
data: { v: 44 },
153+
children: [
154+
{
155+
edgeData: {},
156+
node: {
157+
data: { v: 47 },
158+
layout: { plan: { x: 2, y: 2 } },
159+
},
160+
},
161+
],
162+
layout: { plan: { x: 1, y: 1 } },
163+
},
164+
}
165+
],
166+
layout: { plan: { x: 0, y: 0 } },
167+
})
168+
})
169+
170+
it('sets x for ((.),(.,.))', () => {
171+
const result = computeLeftShiftLayout({
172+
data: { v: 42 },
173+
children: [
174+
{
175+
edgeData: {},
176+
node: {
177+
data: { v: 43 },
178+
children: [
179+
{
180+
edgeData: {},
181+
node: { data: { v: 45 } },
182+
}
183+
]
184+
},
185+
},
186+
{
187+
edgeData: {},
188+
node: {
189+
data: { v: 44 },
190+
children: [
191+
{
192+
edgeData: {},
193+
node: { data: { v: 46 } },
194+
},
195+
{
196+
edgeData: {},
197+
node: { data: { v: 47 } },
198+
},
199+
]
200+
},
201+
}
202+
],
203+
})
204+
205+
expect(result).toEqual({
206+
data: { v: 42 },
207+
children: [
208+
{
209+
edgeData: {},
210+
node: {
211+
data: { v: 43 },
212+
children: [
213+
{
214+
edgeData: {},
215+
node: {
216+
data: { v: 45 },
217+
layout: { plan: { x: 0, y: 2 } },
218+
},
219+
},
220+
],
221+
layout: { plan: { x: 0, y: 1 } },
222+
},
223+
},
224+
{
225+
edgeData: {},
226+
node: {
227+
data: { v: 44 },
228+
children: [
229+
{
230+
edgeData: {},
231+
node: {
232+
data: { v: 46 },
233+
layout: { plan: { x: 1, y: 2 } },
234+
},
235+
},
236+
{
237+
edgeData: {},
238+
node: {
239+
data: { v: 47 },
240+
layout: { plan: { x: 2, y: 2 } },
241+
},
242+
},
243+
],
244+
layout: { plan: { x: 1, y: 1 } },
245+
},
246+
}
247+
],
248+
layout: { plan: { x: 0, y: 0 } },
249+
})
250+
})
50251
})

0 commit comments

Comments
 (0)