Skip to content

Commit 42609d3

Browse files
committed
some tests
1 parent 7415107 commit 42609d3

File tree

1 file changed

+172
-2
lines changed

1 file changed

+172
-2
lines changed

tests/index.test.ts

Lines changed: 172 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { oasConfig, oas } from '../src/types';
2-
import { getConfig, getPackage, filterPaths } from '../src/utils';
2+
import { getConfig, getPackage, filterPaths, merge } from '../src/utils';
33

44
describe('openapi config generation', () => {
55
afterAll(async () => {
@@ -71,10 +71,19 @@ describe('getPackage', () => {
7171
expect(test).toHaveProperty('version');
7272
expect(test).toHaveProperty('description');
7373
});
74+
test('should return empty object when package.json not found', async () => {
75+
const originalCwd = process.cwd();
76+
jest.spyOn(process, 'cwd').mockImplementation(() => {
77+
return '/nonexistent/path';
78+
});
79+
const test = await getPackage();
80+
expect(test).toEqual({});
81+
process.cwd = () => originalCwd;
82+
});
7483
});
7584

7685
describe('filterPaths', () => {
77-
test('should be valid', () => {
86+
test('should filter paths based on published tags', () => {
7887
const oasConfig: oasConfig = {
7988
docsPath: 'api-docs',
8089
info: {},
@@ -113,4 +122,165 @@ describe('filterPaths', () => {
113122
expect(oas.tags.length).toEqual(1);
114123
expect(oas.tags[0].name).toEqual('tag2');
115124
});
125+
126+
test('should handle empty publishedTags array', () => {
127+
const oasConfig: oasConfig = {
128+
docsPath: 'api-docs',
129+
info: {},
130+
useAuthentication: true,
131+
tags: [],
132+
components: {},
133+
publishedTags: [],
134+
paths: {},
135+
};
136+
const oas: oas = {
137+
info: {},
138+
tags: [{ name: 'tag1' }, { name: 'tag2' }],
139+
components: {},
140+
paths: {
141+
endpoint1: {
142+
get: { tags: ['tag1'] },
143+
},
144+
},
145+
};
146+
filterPaths(oasConfig, oas);
147+
expect(oas.paths.endpoint1?.get).toBeUndefined();
148+
expect(oas.tags.length).toEqual(0);
149+
});
150+
151+
test('should handle endpoints with multiple published tags', () => {
152+
const oasConfig: oasConfig = {
153+
docsPath: 'api-docs',
154+
info: {},
155+
useAuthentication: true,
156+
tags: [],
157+
components: {},
158+
publishedTags: ['tag1', 'tag2', 'tag3'],
159+
paths: {},
160+
};
161+
const oas: oas = {
162+
info: {},
163+
tags: [{ name: 'tag1' }, { name: 'tag2' }, { name: 'tag3' }],
164+
components: {},
165+
paths: {
166+
endpoint1: {
167+
get: { tags: ['tag1', 'tag2', 'tag3'] },
168+
},
169+
},
170+
};
171+
filterPaths(oasConfig, oas);
172+
expect(oas.paths.endpoint1).toHaveProperty('get');
173+
expect(oas.paths.endpoint1?.get?.tags.length).toEqual(3);
174+
expect(oas.tags.length).toEqual(3);
175+
});
176+
177+
test('should handle endpoints without tags', () => {
178+
const oasConfig: oasConfig = {
179+
docsPath: 'api-docs',
180+
info: {},
181+
useAuthentication: true,
182+
tags: [],
183+
components: {},
184+
publishedTags: ['tag1'],
185+
paths: {},
186+
};
187+
const oas: oas = {
188+
info: {},
189+
tags: [{ name: 'tag1' }],
190+
components: {},
191+
paths: {
192+
endpoint1: {
193+
get: { tags: [] },
194+
},
195+
},
196+
};
197+
filterPaths(oasConfig, oas);
198+
expect(oas.paths.endpoint1?.get).toBeUndefined();
199+
});
200+
});
201+
202+
describe('merge', () => {
203+
test('should deep merge two simple objects', () => {
204+
const obj1 = { a: 1, b: 2 };
205+
const obj2 = { b: 3, c: 4 };
206+
const result = merge(obj1, obj2);
207+
expect(result).toEqual({ a: 1, b: 3, c: 4 });
208+
});
209+
210+
test('should deep merge nested objects', () => {
211+
const obj1 = { a: { x: 1, y: 2 }, b: 3 };
212+
const obj2 = { a: { y: 3, z: 4 }, c: 5 };
213+
const result = merge(obj1, obj2);
214+
expect(result).toEqual({ a: { x: 1, y: 3, z: 4 }, b: 3, c: 5 });
215+
});
216+
217+
test('should merge arrays', () => {
218+
const obj1 = { arr: [1, 2] };
219+
const obj2 = { arr: [3, 4] };
220+
const result = merge(obj1, obj2);
221+
expect(result.arr).toEqual([3, 4]);
222+
});
223+
224+
test('should handle empty objects', () => {
225+
const obj1 = {};
226+
const obj2 = { a: 1, b: 2 };
227+
const result = merge(obj1, obj2);
228+
expect(result).toEqual({ a: 1, b: 2 });
229+
});
230+
231+
test('should merge complex nested structures', () => {
232+
const obj1 = {
233+
components: {
234+
schemas: { User: { type: 'object' } },
235+
responses: { 200: { description: 'OK' } },
236+
},
237+
};
238+
const obj2 = {
239+
components: {
240+
schemas: { Post: { type: 'object' } },
241+
parameters: { id: { in: 'path' } },
242+
},
243+
};
244+
const result = merge(obj1, obj2);
245+
expect(result.components.schemas).toHaveProperty('User');
246+
expect(result.components.schemas).toHaveProperty('Post');
247+
expect(result.components.responses).toHaveProperty('200');
248+
expect(result.components.parameters).toHaveProperty('id');
249+
});
250+
251+
test('should handle null and undefined values', () => {
252+
const obj1 = { a: 1, b: null };
253+
const obj2 = { b: 2, c: undefined };
254+
const result = merge(obj1, obj2);
255+
expect(result).toEqual({ a: 1, b: 2, c: undefined });
256+
});
257+
258+
test('should overwrite primitive values', () => {
259+
const obj1 = { a: 'hello', b: 10, c: true };
260+
const obj2 = { a: 'world', b: 20 };
261+
const result = merge(obj1, obj2);
262+
expect(result).toEqual({ a: 'world', b: 20, c: true });
263+
});
264+
});
265+
266+
describe('getConfig edge cases', () => {
267+
test('should return default config when no oasconfig exists', () => {
268+
jest.spyOn(process, 'cwd').mockImplementation(() => {
269+
return './tests/mocks/nonexistent';
270+
});
271+
const test = getConfig();
272+
expect(test.docsPath).toBe('api-docs');
273+
expect(test.useAuthentication).toBe(false);
274+
expect(test.tags).toEqual([]);
275+
expect(test.publishedTags).toEqual([]);
276+
});
277+
278+
test('should handle missing oas.yaml files gracefully', () => {
279+
jest.spyOn(process, 'cwd').mockImplementation(() => {
280+
return './tests/mocks/oasconfig';
281+
});
282+
const test = getConfig();
283+
expect(test).toBeDefined();
284+
expect(test.docsPath).toBeDefined();
285+
});
116286
});

0 commit comments

Comments
 (0)