Skip to content

Commit 03688bf

Browse files
author
Thomasr
committed
add test case
1 parent c158d44 commit 03688bf

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { specsToOptions, version2spec } from "./util";
2+
3+
describe('version2spec', () => {
4+
test('should return the spec for the given version', () => {
5+
const specs = {
6+
v1: 'spec for version 1',
7+
v2: 'spec for version 2',
8+
v3: 'spec for version 3'
9+
};
10+
11+
expect(version2spec(specs, 'v2')).toBe('spec for version 2');
12+
});
13+
14+
test('should return the first spec if version is undefined', () => {
15+
const specs = {
16+
v1: 'spec for version 1',
17+
v2: 'spec for version 2',
18+
v3: 'spec for version 3'
19+
};
20+
21+
expect(version2spec(specs, undefined)).toBe('spec for version 1');
22+
});
23+
24+
test('should return the first spec if version is an empty string', () => {
25+
const specs = {
26+
v1: 'spec for version 1',
27+
v2: 'spec for version 2',
28+
v3: 'spec for version 3'
29+
};
30+
31+
expect(version2spec(specs, "")).toBe('spec for version 1');
32+
});
33+
34+
test('should return undefined if specs is an empty object and version is undefined', () => {
35+
const specs = {};
36+
37+
expect(version2spec(specs, undefined)).toBeUndefined();
38+
});
39+
40+
test('should return undefined if specs is an empty object and version is an empty string', () => {
41+
const specs = {};
42+
43+
expect(version2spec(specs, "")).toBeUndefined();
44+
});
45+
46+
test('should return undefined if the specified version does not exist in specs', () => {
47+
const specs = {
48+
v1: 'spec for version 1',
49+
v2: 'spec for version 2',
50+
v3: 'spec for version 3'
51+
};
52+
53+
expect(version2spec(specs, 'v4')).toBeUndefined();
54+
});
55+
});
56+
57+
describe('specsToOptions', () => {
58+
test('should convert specs object to options array', () => {
59+
const specs = {
60+
color: 'red',
61+
size: 'large',
62+
weight: 'light'
63+
};
64+
65+
const expectedOptions = [
66+
{ value: 'color', label: 'color' },
67+
{ value: 'size', label: 'size' },
68+
{ value: 'weight', label: 'weight' }
69+
];
70+
71+
expect(specsToOptions(specs)).toEqual(expectedOptions);
72+
});
73+
74+
test('should return an empty array if specs object is empty', () => {
75+
const specs = {};
76+
const expectedOptions: any[] = [];
77+
78+
expect(specsToOptions(specs)).toEqual(expectedOptions);
79+
});
80+
81+
test('should handle specs object with non-string values', () => {
82+
const specs = {
83+
color: 'red',
84+
size: 42,
85+
available: true
86+
};
87+
88+
const expectedOptions = [
89+
{ value: 'color', label: 'color' },
90+
{ value: 'size', label: 'size' },
91+
{ value: 'available', label: 'available' }
92+
];
93+
94+
expect(specsToOptions(specs)).toEqual(expectedOptions);
95+
});
96+
97+
test('should handle specs object with numeric keys', () => {
98+
const specs = {
99+
1: 'one',
100+
2: 'two',
101+
3: 'three'
102+
};
103+
104+
const expectedOptions = [
105+
{ value: '1', label: '1' },
106+
{ value: '2', label: '2' },
107+
{ value: '3', label: '3' }
108+
];
109+
110+
expect(specsToOptions(specs)).toEqual(expectedOptions);
111+
});
112+
});

0 commit comments

Comments
 (0)