@strapi/typescript-utils 4.3.0-beta.1 → 4.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/__tests__/generators/schemas/attributes.test.js +703 -0
- package/lib/__tests__/generators/schemas/global.test.js +108 -0
- package/lib/__tests__/generators/schemas/imports.test.js +54 -0
- package/lib/__tests__/generators/schemas/utils.test.js +362 -0
- package/lib/generators/index.js +7 -0
- package/lib/generators/schemas/attributes.js +281 -0
- package/lib/generators/schemas/global.js +68 -0
- package/lib/generators/schemas/imports.js +33 -0
- package/lib/generators/schemas/index.js +185 -0
- package/lib/generators/schemas/schema.js +87 -0
- package/lib/generators/schemas/utils.js +160 -0
- package/lib/index.js +2 -0
- package/lib/utils/resolve-outdir.js +1 -1
- package/package.json +5 -2
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
jest.mock('../../../generators/schemas/utils', () => ({
|
|
4
|
+
getSchemaInterfaceName: jest.fn(),
|
|
5
|
+
}));
|
|
6
|
+
|
|
7
|
+
const ts = require('typescript');
|
|
8
|
+
const { get } = require('lodash/fp');
|
|
9
|
+
|
|
10
|
+
const { generateGlobalDefinition } = require('../../../generators/schemas/global');
|
|
11
|
+
const { getSchemaInterfaceName } = require('../../../generators/schemas/utils');
|
|
12
|
+
|
|
13
|
+
const getSchemasInterfaceNode = get('body.statements[0].body.statements[0]');
|
|
14
|
+
|
|
15
|
+
describe('Global', () => {
|
|
16
|
+
afterAll(() => {
|
|
17
|
+
jest.resetAllMocks();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const assertGlobalNodeStructure = node => {
|
|
21
|
+
// "declare global"
|
|
22
|
+
expect(node.kind).toBe(ts.SyntaxKind.ModuleDeclaration);
|
|
23
|
+
expect(node.modifiers).toHaveLength(1);
|
|
24
|
+
expect(node.modifiers[0].kind).toBe(ts.SyntaxKind.DeclareKeyword);
|
|
25
|
+
expect(node.name.originalKeywordKind).toBe(ts.SyntaxKind.GlobalKeyword);
|
|
26
|
+
expect(node.name.escapedText).toBe('global');
|
|
27
|
+
|
|
28
|
+
// "namespace Strapi"
|
|
29
|
+
const [strapiNamespace] = node.body.statements;
|
|
30
|
+
|
|
31
|
+
expect(strapiNamespace.kind).toBe(ts.SyntaxKind.ModuleDeclaration);
|
|
32
|
+
expect(strapiNamespace.name.kind).toBe(ts.SyntaxKind.Identifier);
|
|
33
|
+
expect(strapiNamespace.name.escapedText).toBe('Strapi');
|
|
34
|
+
|
|
35
|
+
// "interface Schemas"
|
|
36
|
+
const [schemasInterface] = strapiNamespace.body.statements;
|
|
37
|
+
|
|
38
|
+
expect(schemasInterface.kind).toBe(ts.SyntaxKind.InterfaceDeclaration);
|
|
39
|
+
expect(schemasInterface.name.escapedText).toBe('Schemas');
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
describe('Generate Global Definition', () => {
|
|
43
|
+
beforeEach(() => {
|
|
44
|
+
jest.resetAllMocks();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('With empty definition', () => {
|
|
48
|
+
const definitions = [];
|
|
49
|
+
|
|
50
|
+
const globalNode = generateGlobalDefinition(definitions);
|
|
51
|
+
|
|
52
|
+
assertGlobalNodeStructure(globalNode);
|
|
53
|
+
|
|
54
|
+
expect(getSchemaInterfaceName).not.toHaveBeenCalled();
|
|
55
|
+
|
|
56
|
+
const schemasNode = getSchemasInterfaceNode(globalNode);
|
|
57
|
+
|
|
58
|
+
expect(schemasNode.members).toHaveLength(0);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('With no definition', () => {
|
|
62
|
+
const globalNode = generateGlobalDefinition();
|
|
63
|
+
|
|
64
|
+
assertGlobalNodeStructure(globalNode);
|
|
65
|
+
|
|
66
|
+
expect(getSchemaInterfaceName).not.toHaveBeenCalled();
|
|
67
|
+
|
|
68
|
+
const schemasNode = getSchemasInterfaceNode(globalNode);
|
|
69
|
+
|
|
70
|
+
expect(schemasNode.members).toHaveLength(0);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('With multiple definitions', () => {
|
|
74
|
+
const definitions = [
|
|
75
|
+
{ schema: { uid: 'api::foo.foo' } },
|
|
76
|
+
{ schema: { uid: 'api::bar.bar' } },
|
|
77
|
+
{ schema: { uid: 'api::foobar.foobar' } },
|
|
78
|
+
{ schema: { uid: 'default.barfoo' } },
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
getSchemaInterfaceName.mockReturnValue('Placeholder');
|
|
82
|
+
|
|
83
|
+
const globalNode = generateGlobalDefinition(definitions);
|
|
84
|
+
|
|
85
|
+
assertGlobalNodeStructure(globalNode);
|
|
86
|
+
|
|
87
|
+
const schemasNode = getSchemasInterfaceNode(globalNode);
|
|
88
|
+
|
|
89
|
+
expect(schemasNode.members).toHaveLength(definitions.length);
|
|
90
|
+
|
|
91
|
+
definitions.forEach(({ schema }, index) => {
|
|
92
|
+
const { uid } = schema;
|
|
93
|
+
const node = schemasNode.members[index];
|
|
94
|
+
|
|
95
|
+
expect(node.kind).toBe(ts.SyntaxKind.PropertySignature);
|
|
96
|
+
|
|
97
|
+
expect(getSchemaInterfaceName).toHaveBeenCalledWith(uid);
|
|
98
|
+
|
|
99
|
+
expect(node.name.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
100
|
+
expect(node.name.text).toBe(uid);
|
|
101
|
+
expect(node.name.singleQuote).toBeTruthy();
|
|
102
|
+
|
|
103
|
+
expect(node.type.kind).toBe(ts.SyntaxKind.TypeReference);
|
|
104
|
+
expect(node.type.typeName.escapedText).toBe('Placeholder');
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const ts = require('typescript');
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
addImport,
|
|
7
|
+
generateImportDefinition,
|
|
8
|
+
getImports,
|
|
9
|
+
} = require('../../../generators/schemas/imports');
|
|
10
|
+
|
|
11
|
+
describe('Imports', () => {
|
|
12
|
+
test('When first loaded, the list of imports should be empty', () => {
|
|
13
|
+
expect(getImports()).toHaveLength(0);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test('Can add new imports to the list', () => {
|
|
17
|
+
addImport('foo');
|
|
18
|
+
addImport('bar');
|
|
19
|
+
|
|
20
|
+
expect(getImports()).toHaveLength(2);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('When adding an already registered import, ignore it', () => {
|
|
24
|
+
addImport('foo');
|
|
25
|
+
|
|
26
|
+
expect(getImports()).toHaveLength(2);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('Generate an import type definition containing the registered import', () => {
|
|
30
|
+
const def = generateImportDefinition();
|
|
31
|
+
|
|
32
|
+
expect(def.kind).toBe(ts.SyntaxKind.ImportDeclaration);
|
|
33
|
+
|
|
34
|
+
// Module specifier
|
|
35
|
+
expect(def.moduleSpecifier.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
36
|
+
expect(def.moduleSpecifier.text).toBe('@strapi/strapi');
|
|
37
|
+
|
|
38
|
+
// Import clause (should be named imports)
|
|
39
|
+
expect(def.importClause.kind).toBe(ts.SyntaxKind.ImportClause);
|
|
40
|
+
|
|
41
|
+
const { elements } = def.importClause.namedBindings;
|
|
42
|
+
|
|
43
|
+
expect(elements).toHaveLength(2);
|
|
44
|
+
|
|
45
|
+
// Import clauses
|
|
46
|
+
getImports().forEach((namedImport, index) => {
|
|
47
|
+
const element = elements[index];
|
|
48
|
+
|
|
49
|
+
expect(element.kind).toBe(ts.SyntaxKind.ImportSpecifier);
|
|
50
|
+
expect(element.name.kind).toBe(ts.SyntaxKind.Identifier);
|
|
51
|
+
expect(element.name.escapedText).toBe(namedImport);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const ts = require('typescript');
|
|
4
|
+
const { factory } = require('typescript');
|
|
5
|
+
|
|
6
|
+
const {
|
|
7
|
+
getAllStrapiSchemas,
|
|
8
|
+
getDefinitionAttributesCount,
|
|
9
|
+
getSchemaExtendsTypeName,
|
|
10
|
+
getSchemaInterfaceName,
|
|
11
|
+
getSchemaModelType,
|
|
12
|
+
getTypeNode,
|
|
13
|
+
toTypeLiteral,
|
|
14
|
+
} = require('../../../generators/schemas/utils');
|
|
15
|
+
|
|
16
|
+
describe('Utils', () => {
|
|
17
|
+
describe('Get All Strapi Schemas', () => {
|
|
18
|
+
test('Get both components and content types', () => {
|
|
19
|
+
const strapi = {
|
|
20
|
+
contentTypes: {
|
|
21
|
+
ctA: {},
|
|
22
|
+
ctB: {},
|
|
23
|
+
},
|
|
24
|
+
components: {
|
|
25
|
+
comp1: {},
|
|
26
|
+
comp2: {},
|
|
27
|
+
comp3: {},
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const schemas = getAllStrapiSchemas(strapi);
|
|
32
|
+
|
|
33
|
+
expect(schemas).toMatchObject({ ctA: {}, ctB: {}, comp1: {}, comp2: {}, comp3: {} });
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('Get only components if there is no content type', () => {
|
|
37
|
+
const strapi = {
|
|
38
|
+
contentTypes: {},
|
|
39
|
+
|
|
40
|
+
components: {
|
|
41
|
+
comp1: {},
|
|
42
|
+
comp2: {},
|
|
43
|
+
comp3: {},
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const schemas = getAllStrapiSchemas(strapi);
|
|
48
|
+
|
|
49
|
+
expect(schemas).toMatchObject({ comp1: {}, comp2: {}, comp3: {} });
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('Get only content types if there is no component', () => {
|
|
53
|
+
const strapi = {
|
|
54
|
+
contentTypes: {
|
|
55
|
+
ctA: {},
|
|
56
|
+
ctB: {},
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
components: {},
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const schemas = getAllStrapiSchemas(strapi);
|
|
63
|
+
|
|
64
|
+
expect(schemas).toMatchObject({ ctA: {}, ctB: {} });
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('Get Definition Attributes Count', () => {
|
|
69
|
+
const createMainNode = (members = []) => {
|
|
70
|
+
return factory.createInterfaceDeclaration(
|
|
71
|
+
undefined,
|
|
72
|
+
undefined,
|
|
73
|
+
factory.createIdentifier('Foo'),
|
|
74
|
+
undefined,
|
|
75
|
+
undefined,
|
|
76
|
+
members
|
|
77
|
+
);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const createPropertyDeclaration = (name, type) => {
|
|
81
|
+
return factory.createPropertyDeclaration(
|
|
82
|
+
undefined,
|
|
83
|
+
undefined,
|
|
84
|
+
factory.createIdentifier(name),
|
|
85
|
+
undefined,
|
|
86
|
+
type
|
|
87
|
+
);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
test('Returns null if there are no members in the parent node', () => {
|
|
91
|
+
const mainNode = createMainNode();
|
|
92
|
+
|
|
93
|
+
const count = getDefinitionAttributesCount(mainNode);
|
|
94
|
+
|
|
95
|
+
expect(count).toBeNull();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test('Returns null if there are members in the parent node, but none named "attributes"', () => {
|
|
99
|
+
const mainNode = createMainNode([
|
|
100
|
+
createPropertyDeclaration(
|
|
101
|
+
'bar',
|
|
102
|
+
factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
|
|
103
|
+
),
|
|
104
|
+
createPropertyDeclaration(
|
|
105
|
+
'foobar',
|
|
106
|
+
factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
|
|
107
|
+
),
|
|
108
|
+
]);
|
|
109
|
+
|
|
110
|
+
const count = getDefinitionAttributesCount(mainNode);
|
|
111
|
+
|
|
112
|
+
expect(count).toBeNull();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test('Returns the number of attributes if the property is present', () => {
|
|
116
|
+
const mainNode = createMainNode([
|
|
117
|
+
createPropertyDeclaration(
|
|
118
|
+
'bar',
|
|
119
|
+
factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
|
|
120
|
+
),
|
|
121
|
+
createPropertyDeclaration(
|
|
122
|
+
'attributes',
|
|
123
|
+
factory.createTypeLiteralNode([
|
|
124
|
+
createPropertyDeclaration(
|
|
125
|
+
'a',
|
|
126
|
+
factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
|
|
127
|
+
),
|
|
128
|
+
createPropertyDeclaration(
|
|
129
|
+
'b',
|
|
130
|
+
factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
|
|
131
|
+
),
|
|
132
|
+
createPropertyDeclaration(
|
|
133
|
+
'c',
|
|
134
|
+
factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword)
|
|
135
|
+
),
|
|
136
|
+
])
|
|
137
|
+
),
|
|
138
|
+
createPropertyDeclaration(
|
|
139
|
+
'foobar',
|
|
140
|
+
factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
|
|
141
|
+
),
|
|
142
|
+
]);
|
|
143
|
+
|
|
144
|
+
const count = getDefinitionAttributesCount(mainNode);
|
|
145
|
+
|
|
146
|
+
expect(count).toBe(3);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("Returns 0 if the attributes node is present but don't have any members", () => {
|
|
150
|
+
const mainNode = createMainNode([
|
|
151
|
+
createPropertyDeclaration('attributes', factory.createTypeLiteralNode()),
|
|
152
|
+
createPropertyDeclaration(
|
|
153
|
+
'foobar',
|
|
154
|
+
factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
|
|
155
|
+
),
|
|
156
|
+
]);
|
|
157
|
+
|
|
158
|
+
const count = getDefinitionAttributesCount(mainNode);
|
|
159
|
+
|
|
160
|
+
expect(count).toBe(0);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
describe('Get Schema Model Type', () => {
|
|
165
|
+
test.each([
|
|
166
|
+
[{ modelType: 'component', kind: null }, 'component'],
|
|
167
|
+
[{ modelType: 'contentType', kind: 'singleType' }, 'singleType'],
|
|
168
|
+
[{ modelType: 'contentType', kind: 'collectionType' }, 'collectionType'],
|
|
169
|
+
[{ modelType: 'invalidType', kind: 'foo' }, null],
|
|
170
|
+
])('%p to be evaluated to %p', (schema, expected) => {
|
|
171
|
+
expect(getSchemaModelType(schema)).toBe(expected);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
describe('Get Schema Extends Type Name', () => {
|
|
176
|
+
test.each([
|
|
177
|
+
[{ modelType: 'component', kind: null }, 'ComponentSchema'],
|
|
178
|
+
[{ modelType: 'contentType', kind: 'singleType' }, 'SingleTypeSchema'],
|
|
179
|
+
[{ modelType: 'contentType', kind: 'collectionType' }, 'CollectionTypeSchema'],
|
|
180
|
+
[{ modelType: 'invalidType', kind: 'foo' }, 'Schema'],
|
|
181
|
+
])("Expect %p to generate %p as the base type for a schema's interface", (schema, expected) => {
|
|
182
|
+
expect(getSchemaExtendsTypeName(schema)).toBe(expected);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe('Get Schema Interface Name', () => {
|
|
187
|
+
test.each([
|
|
188
|
+
['api::foo.foo', 'ApiFooFoo'],
|
|
189
|
+
['plugin::bar.foo', 'PluginBarFoo'],
|
|
190
|
+
['default.dish', 'DefaultDish'],
|
|
191
|
+
])('Should transform UID (%p) to interface name (%p)', (uid, interfaceName) => {
|
|
192
|
+
expect(getSchemaInterfaceName(uid)).toBe(interfaceName);
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
describe('Get Type Node', () => {
|
|
197
|
+
test('Create a valid type reference node based on the given generic parameters', () => {
|
|
198
|
+
const node = getTypeNode('FooBar', [
|
|
199
|
+
factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
|
|
200
|
+
factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
|
|
201
|
+
]);
|
|
202
|
+
|
|
203
|
+
expect(node.typeArguments).toHaveLength(2);
|
|
204
|
+
|
|
205
|
+
expect(node.typeArguments[0].kind).toBe(ts.SyntaxKind.StringKeyword);
|
|
206
|
+
expect(node.typeArguments[1].kind).toBe(ts.SyntaxKind.NumberKeyword);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test('Create a valid empty type reference node', () => {
|
|
210
|
+
const node = getTypeNode('FooBar');
|
|
211
|
+
|
|
212
|
+
expect(node.typeArguments).toBeUndefined();
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
describe('To Type Literal', () => {
|
|
217
|
+
test('String', () => {
|
|
218
|
+
const node = toTypeLiteral('foo');
|
|
219
|
+
|
|
220
|
+
expect(node.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
221
|
+
expect(node.text).toBe('foo');
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
test('Number', () => {
|
|
225
|
+
const node = toTypeLiteral(42);
|
|
226
|
+
|
|
227
|
+
expect(node.kind).toBe(ts.SyntaxKind.FirstLiteralToken);
|
|
228
|
+
expect(node.text).toBe('42');
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test('Boolean', () => {
|
|
232
|
+
const trueNode = toTypeLiteral(true);
|
|
233
|
+
const falseNode = toTypeLiteral(false);
|
|
234
|
+
|
|
235
|
+
expect(trueNode.kind).toBe(ts.SyntaxKind.TrueKeyword);
|
|
236
|
+
expect(falseNode.kind).toBe(ts.SyntaxKind.FalseKeyword);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
test('undefined', () => {
|
|
240
|
+
const node = toTypeLiteral(undefined);
|
|
241
|
+
|
|
242
|
+
expect(node.kind).toBe(ts.SyntaxKind.LiteralType);
|
|
243
|
+
expect(node.literal).toBe(ts.SyntaxKind.UndefinedKeyword);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
test('null', () => {
|
|
247
|
+
const node = toTypeLiteral(null);
|
|
248
|
+
|
|
249
|
+
expect(node.kind).toBe(ts.SyntaxKind.LiteralType);
|
|
250
|
+
expect(node.literal).toBe(ts.SyntaxKind.NullKeyword);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test('Array (empty)', () => {
|
|
254
|
+
const node = toTypeLiteral([]);
|
|
255
|
+
|
|
256
|
+
expect(node.kind).toBe(ts.SyntaxKind.TupleType);
|
|
257
|
+
expect(node.elements).toHaveLength(0);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
test('Array (with elements)', () => {
|
|
261
|
+
const node = toTypeLiteral(['foo', 2]);
|
|
262
|
+
|
|
263
|
+
expect(node.kind).toBe(ts.SyntaxKind.TupleType);
|
|
264
|
+
expect(node.elements).toHaveLength(2);
|
|
265
|
+
|
|
266
|
+
expect(node.elements[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
267
|
+
expect(node.elements[0].text).toBe('foo');
|
|
268
|
+
|
|
269
|
+
expect(node.elements[1].kind).toBe(ts.SyntaxKind.FirstLiteralToken);
|
|
270
|
+
expect(node.elements[1].text).toBe('2');
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
test('Array (nested)', () => {
|
|
274
|
+
const node = toTypeLiteral(['foo', ['bar', 'foobar']]);
|
|
275
|
+
|
|
276
|
+
expect(node.kind).toBe(ts.SyntaxKind.TupleType);
|
|
277
|
+
expect(node.elements).toHaveLength(2);
|
|
278
|
+
|
|
279
|
+
expect(node.elements[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
280
|
+
expect(node.elements[0].text).toBe('foo');
|
|
281
|
+
|
|
282
|
+
expect(node.elements[1].kind).toBe(ts.SyntaxKind.TupleType);
|
|
283
|
+
expect(node.elements[1].elements).toHaveLength(2);
|
|
284
|
+
|
|
285
|
+
expect(node.elements[1].elements[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
286
|
+
expect(node.elements[1].elements[0].text).toBe('bar');
|
|
287
|
+
|
|
288
|
+
expect(node.elements[1].elements[1].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
289
|
+
expect(node.elements[1].elements[1].text).toBe('foobar');
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test('Array (with object)', () => {
|
|
293
|
+
const node = toTypeLiteral([{ foo: 'bar', bar: true }]);
|
|
294
|
+
|
|
295
|
+
expect(node.kind).toBe(ts.SyntaxKind.TupleType);
|
|
296
|
+
expect(node.elements).toHaveLength(1);
|
|
297
|
+
|
|
298
|
+
const objectNode = node.elements[0];
|
|
299
|
+
|
|
300
|
+
expect(objectNode.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
301
|
+
expect(objectNode.members).toHaveLength(2);
|
|
302
|
+
|
|
303
|
+
expect(objectNode.members[0].kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
304
|
+
expect(objectNode.members[0].name.escapedText).toBe('foo');
|
|
305
|
+
expect(objectNode.members[0].type.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
306
|
+
expect(objectNode.members[0].type.text).toBe('bar');
|
|
307
|
+
|
|
308
|
+
expect(objectNode.members[1].kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
309
|
+
expect(objectNode.members[1].name.escapedText).toBe('bar');
|
|
310
|
+
expect(objectNode.members[1].type.kind).toBe(ts.SyntaxKind.TrueKeyword);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
test('Object', () => {
|
|
314
|
+
const node = toTypeLiteral({ foo: ['bar', true, 2], bar: null });
|
|
315
|
+
|
|
316
|
+
expect(node.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
317
|
+
expect(node.members).toHaveLength(2);
|
|
318
|
+
|
|
319
|
+
const [firstMember, secondMember] = node.members;
|
|
320
|
+
|
|
321
|
+
expect(firstMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
322
|
+
expect(firstMember.name.escapedText).toBe('foo');
|
|
323
|
+
expect(firstMember.type.kind).toBe(ts.SyntaxKind.TupleType);
|
|
324
|
+
expect(firstMember.type.elements).toHaveLength(3);
|
|
325
|
+
expect(firstMember.type.elements[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
326
|
+
expect(firstMember.type.elements[1].kind).toBe(ts.SyntaxKind.TrueKeyword);
|
|
327
|
+
expect(firstMember.type.elements[2].kind).toBe(ts.SyntaxKind.FirstLiteralToken);
|
|
328
|
+
|
|
329
|
+
expect(secondMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
330
|
+
expect(secondMember.name.escapedText).toBe('bar');
|
|
331
|
+
expect(secondMember.type.kind).toBe(ts.SyntaxKind.LiteralType);
|
|
332
|
+
expect(secondMember.type.literal).toBe(ts.SyntaxKind.NullKeyword);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
test('Object with complex keys', () => {
|
|
336
|
+
const node = toTypeLiteral({ 'foo-bar': 'foobar', foo: 'bar' });
|
|
337
|
+
|
|
338
|
+
expect(node.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
339
|
+
expect(node.members).toHaveLength(2);
|
|
340
|
+
|
|
341
|
+
const [firstMember, secondMember] = node.members;
|
|
342
|
+
|
|
343
|
+
expect(firstMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
344
|
+
expect(firstMember.name.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
345
|
+
expect(firstMember.name.text).toBe('foo-bar');
|
|
346
|
+
expect(firstMember.type.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
347
|
+
expect(firstMember.type.text).toBe('foobar');
|
|
348
|
+
|
|
349
|
+
expect(secondMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
350
|
+
expect(secondMember.name.kind).toBe(ts.SyntaxKind.Identifier);
|
|
351
|
+
expect(secondMember.name.escapedText).toBe('foo');
|
|
352
|
+
expect(secondMember.type.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
353
|
+
expect(secondMember.type.text).toBe('bar');
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
test('Invalid data type supplied (function)', () => {
|
|
357
|
+
expect(() => toTypeLiteral(() => {})).toThrowError(
|
|
358
|
+
'Cannot convert to object literal. Unknown type "function"'
|
|
359
|
+
);
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
});
|