-
Notifications
You must be signed in to change notification settings - Fork 28
/
test.js
380 lines (369 loc) · 12.2 KB
/
test.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// @flow
import { extractReactTypes } from 'extract-react-types';
import convert, { converters } from './src';
const assembleERTAST = (propTypes, defaultProps, type = 'flow') => {
let file = `
class Component extends React.Component<${propTypes}> {
defaultProps = ${defaultProps}
}`;
let res = extractReactTypes(file, type);
return res.component.members;
};
const getSingleDefault = defaultPropVal => {
return assembleERTAST(`{ a: any }`, `{ a: ${defaultPropVal} }`)[0].default;
};
const getSingleProp = defaultPropType => {
const propTypes = assembleERTAST(`{ a: ${defaultPropType} }`, `{}`)[0];
return convert(propTypes.value);
};
const getSingleTSPropTypes = defaultPropType => {
const propTypes = assembleERTAST(`{ a: ${defaultPropType} }`, `{}`, 'typescript')[0];
return convert(propTypes);
};
const base = {
kind: 'memberExpression',
property: {
kind: 'id',
name: 'a'
}
};
const flatMemberExpressionId = {
...base,
object: {
kind: 'id',
name: 'testObject'
}
};
const flatMemberExpressionObject = {
...base,
object: {
kind: 'object',
members: [
{
kind: 'property',
key: {
kind: 'id',
name: 'redHerring'
},
value: {
kind: 'number',
value: NaN
}
},
{
kind: 'property',
key: {
kind: 'string',
value: 'a'
},
value: {
kind: 'number',
value: 34
}
}
]
}
};
const ErroneousMemberExpression = {
...base,
object: flatMemberExpressionObject,
property: {
kind: 'id',
name: 'badprop'
}
};
const nestedMemberExpressionId = {
...base,
object: flatMemberExpressionId
};
const nestedMemberExpressionObject = {
...base,
object: flatMemberExpressionObject
};
describe('kind 2 string tests', () => {
describe('converters', () => {
describe('memberExpression', () => {
describe('If the object property is of the type Obj', () => {
it('and the property does not exist, we should log an error and return an empty string', () => {
expect(convert(ErroneousMemberExpression)).toBe('undefined');
});
it('and the property does exist, we should return the value', () => {
expect(convert(flatMemberExpressionObject)).toBe('34');
});
});
describe('If the object property is of the type Id', () => {
it('should return the ObjectId and Property name as a string representation', () => {
expect(convert(flatMemberExpressionId)).toBe('testObject.a');
});
});
describe('If the object property is of the type MemberExpression', () => {
it('and the final type is of type object', () => {
expect(convert(nestedMemberExpressionObject)).toBe('34');
});
it('and the final type is of type id', () => {
expect(convert(nestedMemberExpressionId)).toBe('testObject.a.a');
});
});
});
describe('exists', () => {
it('return a string representation of the exist kind', () => {
const str = `*`;
const result = '*';
const final = getSingleProp(str);
expect(final).toBe(result);
});
});
describe('boolean', () => {
describe('If no value exists', () => {
it('return the string representation of the kind', () => {
const str = `{ c: boolean }`;
const result = `{ c: boolean }`;
const final = getSingleProp(str);
expect(final).toBe(result);
});
});
describe('If a value exists', () => {
it('return the string representatio of the value', () => {
const str = `{ c: true }`;
const result = `{ c: true }`;
const final = getSingleProp(str);
expect(final).toBe(result);
});
});
});
describe('number', () => {
describe('If no value exists', () => {
it('return the string representation of the kind', () => {
const str = `{ c: number }`;
const result = `{ c: number }`;
const final = getSingleProp(str);
expect(final).toBe(result);
});
it('return the string representatio of the value', () => {
const str = `{ c: 3 }`;
const result = `{ c: 3 }`;
const final = getSingleProp(str);
expect(final).toBe(result);
});
});
});
describe('string', () => {
describe('If no value exists', () => {
it('return the string representation of the kind', () => {
const str = `{ c: string }`;
const result = `{ c: string }`;
const final = getSingleProp(str);
expect(final).toBe(result);
});
it('return the string representatio of the value', () => {
const str = `{ c: "hello" }`;
const result = `{ c: "hello" }`;
const final = getSingleProp(str);
expect(final).toBe(result);
});
});
});
describe('templateLiteral', () => {
it('should resolve to same string', () => {
/* eslint-disable-next-line no-template-curly-in-string */
let str = '`abc${a}de`';
let reId = getSingleDefault(str);
let final = convert(reId);
expect(final).toBe(str);
});
it('should resolve excaped characters', () => {
/* eslint-disable-next-line no-template-curly-in-string */
let str = '`abc${a}de\n`';
let reId = getSingleDefault(str);
let final = convert(reId);
expect(final).toBe(str);
});
});
describe('any', () => {
it('should resolve to whatever type kind is passed in', () => {
let str = `any`;
let final = getSingleProp(str);
expect(final).toBe(str);
});
});
describe('object', () => {
it('should test a spread object', () => {
let defaults = `{ a: { ...something, b: "val" } }`;
let reId = getSingleDefault(defaults);
let final = convert(reId);
expect(final).toBe(defaults);
});
});
describe('function', () => {
it('should test a spread object', () => {
let defaults = `() => {}`;
let returnVal = `() => undefined`;
let reId = getSingleDefault(defaults);
let final = convert(reId);
expect(final).toBe(returnVal);
});
it('should test a default of a function type', () => {
let defaults = `(a = () => {}) => {}`;
let returnVal = `(a = () => undefined) => undefined`;
let reId = getSingleDefault(defaults);
let final = convert(reId);
expect(final).toBe(returnVal);
});
it('should handle a spread', () => {
let defaults = `({ ...res }) => {}`;
let returnVal = `({ ...res }) => undefined`;
let reId = getSingleDefault(defaults);
let final = convert(reId);
expect(final).toBe(returnVal);
});
});
describe('ObjectPattern', () => {
it('should', () => {
let defaults = `({ a, b }) => {}`;
let returnVal = `({ a, b }) => undefined`;
let reId = getSingleDefault(defaults);
let final = convert(reId);
expect(final).toBe(returnVal);
});
it('should handle assignment', () => {
let defaults = `({ a = 24, b = 3 }) => {}`;
let returnVal = `({ a = 24, b = 3 }) => undefined`;
let reId = getSingleDefault(defaults);
let final = convert(reId);
expect(final).toBe(returnVal);
});
});
describe('JSXElement', () => {
it('resolve to a self-closing JSXElement with attributes', () => {
let defaults = `<div a={3} b={4} />`;
let reId = getSingleDefault(defaults);
let final = convert(reId);
expect(final).toBe(defaults);
});
});
describe('intersection', () => {
it('should return a string representation of an intersection type', () => {
let prop = `true & false`;
let final = getSingleProp(prop);
expect(final).toBe(prop);
});
});
describe('nullable', () => {
it('should return a string representation of a nullable type value', () => {
let prop = `{ b: ?string }`;
let final = getSingleProp(prop);
expect(final).toBe(prop);
});
});
describe('typeof', () => {
it('if no name property is present it should return a string representation of a typeof invocation', () => {
let prop = `typeof 34`;
let result = 'number';
let final = getSingleProp(prop);
expect(final).toBe(result);
});
it('if a name property is present, it should return a string representation of the id of the type', () => {
let prop = 'typeof Foo';
let final = getSingleProp(prop);
expect(final).toBe(prop);
});
});
describe('generic', () => {
it('If type params exist it should return a string representation of a generic type with params', () => {
let prop = `Array<string>`;
let final = getSingleProp(prop);
expect(final).toBe(prop);
});
it('If type params do not exist, it should return the name of the type as a string', () => {
let prop = `Foo`;
let final = getSingleProp(prop);
expect(final).toBe(prop);
});
});
describe('tuples', () => {
it('Resolves down to a string representation of a tuple', () => {
let prop = `[string, number]`;
let final = getSingleTSPropTypes(prop);
expect(final).toBe(`a: [string, number]`);
});
});
describe('typeQuery', () => {
it('Resolves down to a string representation of a typeof invocation', () => {
const prop = `typeof Foo`;
const final = getSingleTSPropTypes(prop);
expect(final).toBe(`a: ${prop}`);
});
});
describe('type params', () => {
it('should return a string representation of a function type with type params', () => {
let prop = '(<T>) => <T><string>';
let file = `
type Foo<T> = (bar: T) => T;
class Component extends React.Component<{ a: Foo<string> }> {}
`;
let res = extractReactTypes(file, 'flow');
let final = convert(res.component.members[0].value);
expect(final).toBe(prop);
});
});
it('exposes converters object', () => {
expect(converters).not.toBeUndefined();
});
});
describe('arrayType', () => {
it('should convert the array syntax to a string', () => {
let prop = `number[]`;
let final = getSingleProp(prop);
expect(final).toBe('Array of number');
});
});
describe('import', () => {
it('Resolves down to a string representation of an external import', () => {
let file = `
import { Component } from 'react';
class Something extends React.Component<{ a: Component }> {}`;
let res = extractReactTypes(file, 'flow').component.members[0].value;
let converted = convert(res);
expect(converted).toBe('react.Component');
});
// eslint-disable-next-line jest/no-disabled-tests
it.skip('Resolves down to a string representation for namespaced external imports', () => {
let file = `
import { Component } from 'react';
import * as foo from 'bar';
class Something extends Component<{ a: foo }> {}
`;
let res = extractReactTypes(file, 'flow').classes[0].members[0].value;
let converted = convert(res);
expect(converted).toBe('foo');
});
});
describe('typecastExpression', () => {
let file = `
type Props = { bar: string }
class Component extends React.Component<Props> {
static defaultProps = {
bar: (ascii: string),
}
}
`;
let res = extractReactTypes(file, 'flow').component.value.members[0].default;
let converted = convert(res);
expect(converted).toBe('ascii');
});
describe('logicalExpression', () => {
it('should work', () => {
let file = `class Button extends React.Component<{ or: string }> {
static defaultProps = {
or: 'me' || 'you' || 'someone else' && 'impossible state',
}
}`;
let res = extractReactTypes(file, 'flow').component.members[0].default;
let converted = convert(res);
expect(converted).toBe('"me" || "you" || "someone else" && "impossible state"');
});
});
describe('utilities', () => {
describe('resolveLast', () => {});
});
});