-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathutils.test.ts
More file actions
100 lines (86 loc) · 3.02 KB
/
utils.test.ts
File metadata and controls
100 lines (86 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
92
93
94
95
96
97
98
99
100
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import {download, generate} from '@commerce-apps/raml-toolkit';
import {
registerHelpers,
registerPartials,
setupApis,
updateApis,
} from './utils';
const Handlebars = generate.HandlebarsWithAmfHelpers;
const API_DIRECTORY = `${__dirname}/../apis`;
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment
const pkg: {version: string} = require('../package.json');
describe('registerHelper', () => {
it('registers our custom helpers', () => {
expect(Object.keys(Handlebars.helpers)).not.toEqual(
expect.arrayContaining(['addNamespace', 'formatForTsDoc'])
);
registerHelpers();
expect(Object.keys(Handlebars.helpers)).toEqual(
expect.arrayContaining(['addNamespace', 'formatForTsDoc'])
);
});
});
describe('registerPartials', () => {
it('registers our partials', () => {
expect(Object.keys(Handlebars.partials)).not.toEqual(
expect.arrayContaining(['dtoPartial', 'operationsPartial'])
);
registerPartials();
expect(Object.keys(Handlebars.partials)).toEqual(
expect.arrayContaining(['dtoPartial', 'operationsPartial'])
);
});
});
describe('setupApis', () => {
it('loads our API modes', async () => {
// Don't need to perform the init, and doing so will cause timeout.
jest.spyOn(generate.ApiMetadata.prototype, 'init').mockResolvedValue();
jest.spyOn(generate.ApiModel.prototype, 'init').mockResolvedValue();
const apis = await setupApis(
API_DIRECTORY,
`${__dirname}/../renderedTemplates`
);
expect(apis.name.original).toEqual('apis');
expect(apis.metadata.sdkVersion).toContain(pkg.version);
const children = apis.children.map(child => child.name.original);
expect(children).toEqual(
expect.arrayContaining([
'shopper-baskets',
'shopper-customers',
'shopper-products',
'shopper-search',
])
);
});
});
describe('test updateApis script', () => {
it('throws error when no results', async () => {
await expect(
updateApis('noResults', /production/i, '/tmp')
).rejects.toThrow("No results in Exchange for 'noResults'");
});
it('throws error when no exact match', async () => {
await expect(updateApis('noMatch', /production/i, '/tmp')).rejects.toThrow(
"No exact match in Exchange for 'noMatch'"
);
});
it('downloads when exact match', async () => {
await expect(
updateApis('shopper-customers', /production/i, '/tmp')
).resolves.toBeUndefined();
});
it('throws error when download fails', async () => {
jest
.spyOn(download, 'downloadRestApis')
.mockRejectedValue(new Error('It failed.'));
await expect(
updateApis('shopper-customers', /production/i, '/tmp')
).rejects.toThrow('Failed to download shopper-customers: It failed.');
});
});