This repository was archived by the owner on Sep 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathtransformers.test.ts
99 lines (90 loc) · 3.71 KB
/
transformers.test.ts
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
/**
* @file
* Test runner
*/
import * as path from 'path';
import * as fs from 'fs';
import * as ts from 'typescript';
import * as chalk from 'chalk';
import * as _ from 'lodash';
import {
reactJSMakePropsAndStateInterfaceTransformFactoryFactory,
reactStatelessFunctionMakePropsTransformFactoryFactory,
reactMovePropTypesToClassTransformFactoryFactory,
reactRemovePropTypesAssignmentTransformFactoryFactory,
reactRemoveStaticPropTypesMemberTransformFactoryFactory,
collapseIntersectionInterfacesTransformFactoryFactory,
reactRemovePropTypesImportTransformFactoryFactory,
allTransforms,
compile,
TransformFactoryFactory
} from '../src';
/** Map between a transform and its test folder */
const transformToFolderMap: [string, TransformFactoryFactory[]][] = [
['react-js-make-props-and-state-transform', [reactJSMakePropsAndStateInterfaceTransformFactoryFactory]],
['react-stateless-function-make-props-transform', [reactStatelessFunctionMakePropsTransformFactoryFactory]],
['react-remove-static-prop-types-member-transform', [reactRemoveStaticPropTypesMemberTransformFactoryFactory]],
['react-remove-prop-types-assignment-transform', [reactRemovePropTypesAssignmentTransformFactoryFactory]],
['collapse-intersection-interfaces-transform', [collapseIntersectionInterfacesTransformFactoryFactory]],
['react-move-prop-types-to-class-transform', [reactMovePropTypesToClassTransformFactoryFactory]],
['react-remove-prop-types-import', [reactRemovePropTypesImportTransformFactoryFactory]],
['end-to-end', allTransforms],
];
const isJestUpdateSnapshotEnabled = !!_.intersection(process.argv, ['-u', '--updateSnapshot']).length;
for (const [testFolderName, getFactory] of transformToFolderMap) {
describe(testFolderName.replace(/\-/g, ' ').replace('transform', ''), () => {
for (const folderName of fs.readdirSync(path.join(__dirname, testFolderName))) {
const folder = path.join(path.join(__dirname, testFolderName, folderName));
if (fs.statSync(folder).isDirectory()) {
it(`${testFolderName} ${folderName}`, async () => {
const inputPath = path.join(folder, 'input.tsx');
const outputPath = path.join(folder, 'output.tsx');
const result = compile(inputPath, getFactory, {
singleQuote: true,
printWidth: 120,
tabWidth: 4,
trailingComma: 'all',
});
if (isJestUpdateSnapshotEnabled) {
await writeFile(outputPath, result);
}
const output = await readFile(outputPath);
expect(stripEmptyLines(result)).toEqual(stripEmptyLines(output));
});
}
}
});
}
/**
* Remove extra empty lines
* @param s A file text
*/
function stripEmptyLines(s: string) {
const newLineRegex = /\n|\r\n/;
return s.split(newLineRegex).filter((l) => l.trim()).join('\n');
}
/**
* Read a string file
* @param pathToFile Path to a string file
*/
function readFile(pathToFile: string) {
return new Promise<string>((resolve, reject) => {
fs.readFile(pathToFile, (error, buffer) => {
if (error) { return reject(error); }
resolve(buffer.toString());
});
});
}
/**
* Read a file
* @param pathToFile Path to a string file
* @param contents Contents of the file
*/
function writeFile(pathToFile: string, contents: string) {
return new Promise<string>((resolve, reject) => {
fs.writeFile(pathToFile, contents, (error) => {
if (error) { return reject(error); }
resolve();
});
});
}