-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-models.spec.ts
More file actions
177 lines (157 loc) · 6.13 KB
/
Copy pathcustom-models.spec.ts
File metadata and controls
177 lines (157 loc) · 6.13 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import assert from 'node:assert';
import { rm } from 'node:fs/promises';
import { beforeEach, describe, it } from 'node:test';
import { cli, CliExit, GlobalOptions } from '../src/cli';
import { generate } from '../src/generate';
import { getBoatsRc, getIndex } from '../src/templates/init';
import { getModel, getModels } from '../src/templates/model';
import { boats, mockConsoleLog } from './boats';
import { getAllFiles, getFile, toArgv } from './shared';
describe('custom-models.spec.ts', async () => {
beforeEach(async () => {
await rm('test/output/custom', { recursive: true, force: true });
});
await it('can load custom models', async () => {
await cli(
toArgv(`
path auth/verify --get
path auth/login --post
path auth/refresh-token --get
path albums/:albumId --list --get
path albums/:albumId/songs/:songId -crudl
model jwt
model artist
--quiet
--output test/output/custom
--templates test/fixtures/overrides
`),
);
const indexFile = await boats('test/output/custom/src/index.yml', 'test/output/custom/build/api.json');
assert.strictEqual(indexFile !== '', true, 'boats failed');
assert.strictEqual(await getFile(indexFile), await getFile('test/fixtures/spec/custom.json'), 'spec mismatch');
});
await it('can disable custom model output by returning a falsey value', async () => {
const globalOptions: GlobalOptions = {
quiet: true,
output: 'test/output/custom',
customTemplates: {
getModel: (): string => '',
getModels: (): string => '',
},
};
const files = await generate(
[
{
contents: (): string => getModels(globalOptions, `src/components/schemas/something/models.yml`, '#/components/schemas/Pagination'),
filename: `src/components/schemas/something/models.yml`,
},
{
contents: (): string => getModel(globalOptions, 'src/components/schemas/something/model.yml'),
filename: 'src/components/schemas/something/model.yml',
},
{ contents: (): string => getIndex(globalOptions, 'src/index.yml'), filename: 'src/index.yml' },
{ contents: (): string => getBoatsRc(globalOptions, '.boatsrc'), filename: '.boatsrc' },
],
globalOptions,
);
assert.deepStrictEqual(
JSON.stringify(Object.entries(files).sort(([a], [b]) => b.localeCompare(a))),
JSON.stringify([
['test/output/custom/src/index.yml', { filename: 'test/output/custom/src/index.yml' }],
[
'test/output/custom/src/components/schemas/something/models.yml',
{ filename: 'test/output/custom/src/components/schemas/something/models.yml', skipped: true },
],
[
'test/output/custom/src/components/schemas/something/model.yml',
{ filename: 'test/output/custom/src/components/schemas/something/model.yml', skipped: true },
],
['test/output/custom/src/components/schemas/index.yml', { filename: 'test/output/custom/src/components/schemas/index.yml' }],
['test/output/custom/.boatsrc', { filename: 'test/output/custom/.boatsrc' }],
]),
);
assert.deepStrictEqual(await getAllFiles('test/output/custom/'), [
'test/output/custom/.boatsrc',
'test/output/custom/src/components/schemas/index.yml',
'test/output/custom/src/index.yml',
]);
});
await it('can load all overrides from a module', async () => {
assert.deepStrictEqual(await getAllFiles('test/output/custom/').catch(() => []), []);
await cli(
toArgv(`
path users/:userId -crudl --put
model jwt
model userId --type query
--quiet
--output test/output/custom
-T test/fixtures/overrides/module.js
--templates test/fixtures/overrides
--templates test/fixtures/overrides/single-export/
`),
);
const indexFile = await boats('test/output/custom/src/index.yml', 'test/output/custom/build/api.json');
assert.strictEqual(indexFile !== '', true, 'boats failed');
assert.strictEqual(await getFile(indexFile), await getFile('test/fixtures/spec/custom-multi.json'), 'spec mismatch');
});
await it('adds and overwrites templates when invoked multiple times', async () => {
assert.deepStrictEqual(await getAllFiles('test/output/custom/').catch(() => []), []);
await cli(
toArgv(`
path users/:userId -crudl --put
model jwt
model userId --type query
--quiet
--output test/output/custom
-T test/fixtures/overrides/module.js
`),
);
});
await it('outputs a meaningful error message', async () => {
let log = mockConsoleLog();
let error = await cli(
toArgv(`
path users/:userId -crudl --put
model jwt
model userId --type query
--quiet
--output test/output/custom
-T something-that-doesnt-exist
`),
).catch((e: unknown) => e);
log.unmock();
assert.strictEqual(error instanceof CliExit, true);
assert.deepStrictEqual(log.logs, [['cannot load templates "something-that-doesnt-exist": not a module\n'], log.logs[1]]);
log = mockConsoleLog();
error = await cli(
toArgv(`
path users/:userId -crudl --put
model jwt
model userId --type query
--quiet
--output test/output/custom
-T test/fixtures/spec
`),
).catch((e: unknown) => e);
log.unmock();
assert.strictEqual(error instanceof CliExit, true);
assert.deepStrictEqual(log.logs, [['cannot load templates "test/fixtures/spec": template folder has no override files\n'], log.logs[1]]);
log = mockConsoleLog();
error = await cli(
toArgv(`
path users/:userId -crudl --put
model jwt
model userId --type query
--quiet
--output test/output/custom
-T test/fixtures/overrides/boats-rc.js
`),
).catch((e: unknown) => e);
log.unmock();
assert.strictEqual(error instanceof CliExit, true);
assert.deepStrictEqual(log.logs, [
['cannot load templates "test/fixtures/overrides/boats-rc.js": module has no override exports\n'],
log.logs[1],
]);
});
}).catch(console.error);