Skip to content

Commit cffcef8

Browse files
author
duxinyue.dxy
committed
feat: add comprehensive unit tests
1 parent 56dcc56 commit cffcef8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+6290
-13
lines changed

__tests__/basic.test.ts

Lines changed: 900 additions & 0 deletions
Large diffs are not rendered by default.

__tests__/compact-box.test.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, it, expect } from 'vitest';
22
import { compactBox } from '../src/index';
3-
import { expectSVGToMatchSnapshot, collectNodes } from './utils/svg-generator';
3+
import { expectSVGToMatchSnapshot, expectStyledSVGToMatchSnapshot, collectNodes } from './utils/svg-generator';
4+
import complexTree from './fixtures/complex-tree.json';
45
import basicTree from './fixtures/basic-tree.json';
56

67
describe('CompactBox Layout', () => {
@@ -126,4 +127,44 @@ describe('CompactBox Layout', () => {
126127
expect(result.data.customProp).toBe('custom value');
127128
expect(result.children![0].data.anotherProp).toBe(123);
128129
});
130+
131+
it('complex styled snapshots (nodeType × colorScheme)', () => {
132+
const nodeTypes = ['rect', 'square', 'circle'] as const;
133+
const colorSchemes = ['depth', 'branch', 'single'] as const;
134+
135+
const getNodeWidth = (nodeType: string) => {
136+
switch (nodeType) {
137+
case 'circle':
138+
case 'square':
139+
return 20; // nodeSize*2
140+
case 'rect':
141+
default:
142+
return 100; // nodeSize*10
143+
}
144+
};
145+
const getNodeHeight = () => 20; // nodeSize*2
146+
147+
nodeTypes.forEach((nodeType) => {
148+
colorSchemes.forEach((colorScheme) => {
149+
const result = compactBox(complexTree as any, {
150+
direction: 'TB',
151+
getHGap: () => 50,
152+
getVGap: () => 50,
153+
getWidth: () => getNodeWidth(nodeType),
154+
getHeight: () => getNodeHeight(),
155+
} as any);
156+
157+
expect(result).toBeDefined();
158+
const filename = `compactBox-${nodeType}-${colorScheme}.svg`;
159+
expectStyledSVGToMatchSnapshot(result, filename, {
160+
nodeType: nodeType as any,
161+
colorScheme: colorScheme as any,
162+
nodeSize: 10,
163+
showLabels: true,
164+
width: 900,
165+
height: 700,
166+
});
167+
});
168+
});
169+
});
129170
});

__tests__/dendrogram.test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, it, expect } from 'vitest';
22
import { dendrogram } from '../src/index';
3-
import { expectSVGToMatchSnapshot, collectNodes } from './utils/svg-generator';
3+
import { expectSVGToMatchSnapshot, expectStyledSVGToMatchSnapshot, collectNodes } from './utils/svg-generator';
4+
import complexTree from './fixtures/complex-tree.json';
45
import basicTree from './fixtures/basic-tree.json';
56

67
describe('Dendrogram Layout', () => {
@@ -91,4 +92,45 @@ describe('Dendrogram Layout', () => {
9192

9293
expectSVGToMatchSnapshot(result, 'dendrogram-wide.svg');
9394
});
95+
96+
it('complex styled snapshots (nodeType × colorScheme)', () => {
97+
const nodeTypes = ['rect', 'square', 'circle'] as const;
98+
const colorSchemes = ['depth', 'branch', 'single'] as const;
99+
100+
const nodeSize = 10;
101+
const getNodeWidth = (nodeType: string) => {
102+
switch (nodeType) {
103+
case 'circle':
104+
case 'square':
105+
return nodeSize * 2;
106+
case 'rect':
107+
default:
108+
return nodeSize * 10;
109+
}
110+
};
111+
const getNodeHeight = () => nodeSize * 2;
112+
113+
nodeTypes.forEach((nodeType) => {
114+
colorSchemes.forEach((colorScheme) => {
115+
const result = dendrogram(complexTree as any, {
116+
direction: 'TB',
117+
getHGap: () => 50,
118+
getVGap: () => 50,
119+
getWidth: () => getNodeWidth(nodeType),
120+
getHeight: () => getNodeHeight(),
121+
} as any);
122+
123+
expect(result).toBeDefined();
124+
const filename = `dendrogram-${nodeType}-${colorScheme}.svg`;
125+
expectStyledSVGToMatchSnapshot(result, filename, {
126+
nodeType: nodeType as any,
127+
colorScheme: colorScheme as any,
128+
nodeSize,
129+
showLabels: true,
130+
width: 900,
131+
height: 700,
132+
});
133+
});
134+
});
135+
});
94136
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"id": "root",
3+
"children": [
4+
{
5+
"id": "branch-1",
6+
"children": [
7+
{
8+
"id": "leaf-1-1",
9+
"children": [
10+
{ "id": "leaf-1-1-1" },
11+
{ "id": "leaf-1-1-2" },
12+
{ "id": "leaf-1-1-3" }
13+
]
14+
},
15+
{ "id": "leaf-1-2" }
16+
]
17+
},
18+
{
19+
"id": "branch-2",
20+
"children": [
21+
{ "id": "leaf-2-1" },
22+
{ "id": "leaf-2-2" },
23+
{ "id": "leaf-2-3" },
24+
{ "id": "leaf-2-4" }
25+
]
26+
},
27+
{
28+
"id": "branch-3",
29+
"children": [
30+
{
31+
"id": "leaf-3-1",
32+
"children": [
33+
{ "id": "leaf-3-1-1" },
34+
{ "id": "leaf-3-1-2" }
35+
]
36+
}
37+
]
38+
}
39+
]
40+
}

__tests__/indented.test.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, it, expect } from 'vitest';
22
import { indented } from '../src/index';
3-
import { expectSVGToMatchSnapshot, collectNodes } from './utils/svg-generator';
3+
import { expectSVGToMatchSnapshot, expectStyledSVGToMatchSnapshot, collectNodes } from './utils/svg-generator';
4+
import complexTree from './fixtures/complex-tree.json';
45
import basicTree from './fixtures/basic-tree.json';
56

67
describe('Indented Layout', () => {
@@ -125,4 +126,46 @@ describe('Indented Layout', () => {
125126
expect(result).toBeDefined();
126127
expectSVGToMatchSnapshot(result, 'indented-custom-sizes.svg');
127128
});
129+
130+
it('complex styled snapshots (nodeType × colorScheme)', () => {
131+
const nodeTypes = ['rect', 'square', 'circle'] as const;
132+
const colorSchemes = ['depth', 'branch', 'single'] as const;
133+
134+
const nodeSize = 10;
135+
const getNodeWidth = (nodeType: string) => {
136+
switch (nodeType) {
137+
case 'circle':
138+
case 'square':
139+
return nodeSize * 2;
140+
case 'rect':
141+
default:
142+
return nodeSize * 10;
143+
}
144+
};
145+
const getNodeHeight = () => nodeSize * 2;
146+
147+
nodeTypes.forEach((nodeType) => {
148+
colorSchemes.forEach((colorScheme) => {
149+
const result = indented(complexTree as any, {
150+
direction: 'LR',
151+
indent: 50,
152+
getHGap: () => 50,
153+
getVGap: () => 50,
154+
getWidth: () => getNodeWidth(nodeType),
155+
getHeight: () => getNodeHeight(),
156+
} as any);
157+
158+
expect(result).toBeDefined();
159+
const filename = `indented-${nodeType}-${colorScheme}.svg`;
160+
expectStyledSVGToMatchSnapshot(result, filename, {
161+
nodeType: nodeType as any,
162+
colorScheme: colorScheme as any,
163+
nodeSize,
164+
showLabels: true,
165+
width: 900,
166+
height: 700,
167+
});
168+
});
169+
});
170+
});
128171
});

__tests__/mindmap.test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, it, expect } from 'vitest';
22
import { mindmap } from '../src/index';
3-
import { expectSVGToMatchSnapshot, collectNodes } from './utils/svg-generator';
3+
import { expectSVGToMatchSnapshot, expectStyledSVGToMatchSnapshot, collectNodes } from './utils/svg-generator';
4+
import complexTree from './fixtures/complex-tree.json';
45
import mindmapTree from './fixtures/mindmap-tree.json';
56
import basicTree from './fixtures/basic-tree.json';
67

@@ -138,4 +139,45 @@ describe('Mindmap Layout', () => {
138139

139140
expectSVGToMatchSnapshot(result, 'mindmap-auto-sides.svg');
140141
});
142+
143+
it('complex styled snapshots (nodeType × colorScheme)', () => {
144+
const nodeTypes = ['rect', 'square', 'circle'] as const;
145+
const colorSchemes = ['depth', 'branch', 'single'] as const;
146+
147+
const nodeSize = 10;
148+
const getNodeWidth = (nodeType: string) => {
149+
switch (nodeType) {
150+
case 'circle':
151+
case 'square':
152+
return nodeSize * 2;
153+
case 'rect':
154+
default:
155+
return nodeSize * 10;
156+
}
157+
};
158+
const getNodeHeight = () => nodeSize * 2;
159+
160+
nodeTypes.forEach((nodeType) => {
161+
colorSchemes.forEach((colorScheme) => {
162+
const result = mindmap(complexTree as any, {
163+
direction: 'H',
164+
getHGap: () => 50,
165+
getVGap: () => 50,
166+
getWidth: () => getNodeWidth(nodeType),
167+
getHeight: () => getNodeHeight(),
168+
} as any);
169+
170+
expect(result).toBeDefined();
171+
const filename = `mindmap-${nodeType}-${colorScheme}.svg`;
172+
expectStyledSVGToMatchSnapshot(result, filename, {
173+
nodeType: nodeType as any,
174+
colorScheme: colorScheme as any,
175+
nodeSize,
176+
showLabels: true,
177+
width: 900,
178+
height: 700,
179+
});
180+
});
181+
});
182+
});
141183
});
Lines changed: 55 additions & 0 deletions
Loading
Lines changed: 55 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)