Skip to content

Commit d338fe8

Browse files
committed
improve: expand object-definition tests for better coverage
1 parent 7dc6300 commit d338fe8

File tree

1 file changed

+79
-14
lines changed

1 file changed

+79
-14
lines changed

test/components/object-definition.test.ts

Lines changed: 79 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,102 @@ import { expect, test, beforeAll } from 'vitest';
44
import { ComponentDefinition } from '../../src/components/interfaces';
55
import { buildProject } from './test-helpers';
66

7-
let codeEditor: ComponentDefinition;
7+
let simpleComponent: ComponentDefinition;
8+
let complexTypesComponents: ComponentDefinition[];
89

910
beforeAll(() => {
10-
const result = buildProject('simple');
11-
expect(result).toHaveLength(1);
12-
[codeEditor] = result;
11+
const simpleResult = buildProject('simple');
12+
expect(simpleResult).toHaveLength(1);
13+
[simpleComponent] = simpleResult;
14+
15+
complexTypesComponents = buildProject('complex-types');
16+
expect(complexTypesComponents.length).toBeGreaterThan(0);
1317
});
1418

1519
test('object definition should handle basic types', () => {
16-
// Test that the object definition functions work correctly
17-
expect(codeEditor.name).toBe('Simple');
18-
expect(codeEditor.properties).toBeDefined();
20+
expect(simpleComponent.name).toBe('Simple');
21+
expect(simpleComponent.properties).toBeDefined();
1922
});
2023

2124
test('object definition should handle union types correctly', () => {
22-
// Test basic union type handling
23-
const unionProp = codeEditor.properties.find(def => def.inlineType?.type === 'union');
25+
// Find a component with union types
26+
const componentWithUnions = complexTypesComponents.find(comp =>
27+
comp.properties.some(prop => prop.inlineType?.type === 'union')
28+
);
29+
30+
if (componentWithUnions) {
31+
const unionProp = componentWithUnions.properties.find(def => def.inlineType?.type === 'union');
2432

25-
if (unionProp?.inlineType?.type === 'union') {
26-
expect(unionProp.inlineType.values).toBeDefined();
27-
expect(Array.isArray(unionProp.inlineType.values)).toBe(true);
33+
if (unionProp?.inlineType?.type === 'union') {
34+
expect(unionProp.inlineType.values).toBeDefined();
35+
expect(Array.isArray(unionProp.inlineType.values)).toBe(true);
36+
expect(unionProp.inlineType.values.length).toBeGreaterThan(0);
37+
}
38+
}
39+
});
40+
41+
test('object definition should handle string literal unions', () => {
42+
// Test string literal union handling
43+
const componentWithStringUnions = complexTypesComponents.find(comp =>
44+
comp.properties.some(prop => prop.inlineType?.type === 'union' && prop.type === 'string')
45+
);
46+
47+
if (componentWithStringUnions) {
48+
const stringUnionProp = componentWithStringUnions.properties.find(
49+
prop => prop.inlineType?.type === 'union' && prop.type === 'string'
50+
);
51+
52+
if (stringUnionProp?.inlineType?.type === 'union') {
53+
expect(stringUnionProp.type).toBe('string');
54+
expect(stringUnionProp.inlineType.values).toBeDefined();
55+
// Should contain string values
56+
expect(stringUnionProp.inlineType.values.some(v => typeof v === 'string')).toBe(true);
57+
}
58+
}
59+
});
60+
61+
test('object definition should handle number literal unions', () => {
62+
// Test number literal union handling
63+
const componentWithNumberUnions = complexTypesComponents.find(comp =>
64+
comp.properties.some(prop => prop.inlineType?.type === 'union' && prop.type === 'number')
65+
);
66+
67+
if (componentWithNumberUnions) {
68+
const numberUnionProp = componentWithNumberUnions.properties.find(
69+
prop => prop.inlineType?.type === 'union' && prop.type === 'number'
70+
);
71+
72+
if (numberUnionProp?.inlineType?.type === 'union') {
73+
expect(numberUnionProp.type).toBe('number');
74+
expect(numberUnionProp.inlineType.values).toBeDefined();
75+
}
2876
}
2977
});
3078

3179
test('object definition should preserve type information', () => {
32-
// Test that type information is preserved correctly
33-
const props = codeEditor.properties;
80+
const props = simpleComponent.properties;
3481
expect(props.length).toBeGreaterThan(0);
3582

3683
props.forEach(prop => {
3784
expect(prop.name).toBeDefined();
3885
expect(prop.type).toBeDefined();
3986
});
4087
});
88+
89+
test('object definition should handle mixed union types', () => {
90+
// Test mixed union types (not primitive)
91+
const componentWithMixedUnions = complexTypesComponents.find(comp =>
92+
comp.properties.some(prop => prop.inlineType?.type === 'union' && prop.type !== 'string' && prop.type !== 'number')
93+
);
94+
95+
if (componentWithMixedUnions) {
96+
const mixedUnionProp = componentWithMixedUnions.properties.find(
97+
prop => prop.inlineType?.type === 'union' && prop.type !== 'string' && prop.type !== 'number'
98+
);
99+
100+
if (mixedUnionProp?.inlineType?.type === 'union') {
101+
expect(mixedUnionProp.inlineType.values).toBeDefined();
102+
expect(mixedUnionProp.inlineType.name).toBeDefined();
103+
}
104+
}
105+
});

0 commit comments

Comments
 (0)