forked from orval-labs/orval
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.ts
More file actions
91 lines (77 loc) · 3.02 KB
/
Copy pathindex.test.ts
File metadata and controls
91 lines (77 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { describe, expect, it } from 'vitest';
import { extractExistingHandlerBodies } from './index';
describe('extractExistingHandlerBodies', () => {
it('returns the body of each handler keyed by name', () => {
const source = `
import { createFactory } from 'hono/factory';
const factory = createFactory();
export const listPetsHandlers = factory.createHandlers(
async (c: ListPetsContext) => {
return c.json([]);
},
);
export const createPetsHandlers = factory.createHandlers(
zValidator('json', CreatePetsBody),
async (c: CreatePetsContext) => {},
);
`;
const bodies = extractExistingHandlerBodies(source);
expect([...bodies.keys()]).toEqual([
'listPetsHandlers',
'createPetsHandlers',
]);
expect(bodies.get('listPetsHandlers')).toContain('return c.json([]);');
expect(bodies.get('createPetsHandlers')).toBe('');
});
it('preserves nested syntax inside the handler body', () => {
const source = `export const listPetsHandlers = factory.createHandlers(
async (c: ListPetsContext) => {
const limit = Number(c.req.query('limit') ?? 10);
if (limit > 0) { return c.json({ items: pets.slice(0, limit) }); }
},
);`;
const body = extractExistingHandlerBodies(source).get('listPetsHandlers');
expect(body).toContain("Number(c.req.query('limit') ?? 10)");
expect(body).toContain('{ items: pets.slice(0, limit) }');
});
it('is not confused by parentheses or braces inside strings, templates, comments, and regex', () => {
const source = `export const trickyHandlers = factory.createHandlers(
async (c: TrickyContext) => {
const a = ")"; // a paren in a string — must not close the call
const b = '}';
const c1 = \`tpl ) { } \${'} )'} end\`;
const re = /\\)\\}\\(/g;
/* block comment with ) and } and ( */
// line comment with ) }
return c.text(a + b + c1);
},
);`;
const body = extractExistingHandlerBodies(source).get('trickyHandlers');
expect(body).toBeDefined();
expect(body).toContain('return c.text(a + b + c1);');
expect(body).toContain('/* block comment with ) and } and ( */');
});
it('preserves bodies containing nested arrow functions', () => {
const source = `export const listPetsHandlers = factory.createHandlers(
async (c: ListPetsContext) => {
const ids = pets.map((p) => p.id);
return c.json(ids);
},
);`;
const body = extractExistingHandlerBodies(source).get('listPetsHandlers');
expect(body).toContain('pets.map((p) => p.id)');
expect(body).toContain('return c.json(ids);');
});
it('preserves bodies containing regex literals after keywords', () => {
const source = `export const listPetsHandlers = factory.createHandlers(
async (c: ListPetsContext) => {
return /[)]/.test(c.req.query('q') ?? '');
},
);`;
const body = extractExistingHandlerBodies(source).get('listPetsHandlers');
expect(body).toContain('/[)]/.test');
});
it('returns an empty map when no handlers are present', () => {
expect(extractExistingHandlerBodies('// nothing here').size).toBe(0);
});
});