-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathtranspileHelpers.test.ts
156 lines (146 loc) · 3.86 KB
/
transpileHelpers.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
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
import fs from 'fs';
import path from 'path';
import {
IDiagnostic,
_getLineStarts,
_getErrorLineInfo,
_getErrorMessages,
_supportedPackageToGlobalMap,
} from './transpileHelpers';
import { SUPPORTED_PACKAGES } from '../utilities/defaultSupportedPackages';
// Real diagnostics copied from loading ./examples/class.txt in the editor while type checking wasn't set up
const exampleLines = fs
.readFileSync(path.join(__dirname, 'examples/class.txt'))
.toString()
.split(/\r?\n/g);
const example = exampleLines.join('\n');
const exampleCRLF = exampleLines.join('\r\n');
const diagnostics: IDiagnostic[] = [
{ start: 23, length: 7, messageText: "Cannot find module 'react'.", code: 2307, category: 1 },
{ start: 192, length: 3, messageText: "Cannot find namespace 'JSX'.", code: 2503, category: 1 },
{ start: 225, length: 32, messageText: "JSX element implicitly has type 'any'.", code: 7026, category: 1 },
];
const lineStarts = [
0,
32,
93,
94,
166,
199,
212,
251,
271,
298,
336,
354,
374,
393,
452,
511,
570,
581,
594,
601,
605,
607,
];
const lineStartsCRLF = [
0,
33,
95,
97,
170,
204,
218,
258,
279,
307,
346,
365,
386,
406,
466,
526,
586,
598,
612,
620,
625,
628,
];
describe('_getLineStarts', () => {
it('works with LF', () => {
expect(_getLineStarts(example)).toEqual(lineStarts);
});
it('works with CRLF', () => {
expect(_getLineStarts(exampleCRLF)).toEqual(lineStartsCRLF);
});
});
describe('_getErrorLineInfo', () => {
it('works', () => {
expect(_getErrorLineInfo(diagnostics[0], lineStarts)).toMatchInlineSnapshot(`
Object {
"col": 24,
"line": 1,
}
`);
expect(_getErrorLineInfo(diagnostics[1], lineStarts)).toMatchInlineSnapshot(`
Object {
"col": 27,
"line": 5,
}
`);
expect(_getErrorLineInfo(diagnostics[2], lineStarts)).toMatchInlineSnapshot(`
Object {
"col": 14,
"line": 7,
}
`);
});
it('works at last line of file', () => {
expect(_getErrorLineInfo({ start: 615, messageText: 'fake', code: 0, category: 1 }, lineStarts))
.toMatchInlineSnapshot(`
Object {
"col": 9,
"line": 22,
}
`);
});
});
describe('_getErrorMessages', () => {
it('works', () => {
expect(_getErrorMessages(diagnostics, example)).toEqual([
"Line 1 - Cannot find module 'react'. (TS2307)",
"Line 5 - Cannot find namespace 'JSX'. (TS2503)",
"Line 7 - JSX element implicitly has type 'any'. (TS7026)",
]);
});
it('works with object messageText', () => {
const { messageText, code, ...rest } = diagnostics[0];
const diagnostic = { ...rest, code: 0, messageText: { messageText: messageText as string, code } };
expect(_getErrorMessages([diagnostic], example)).toEqual(["Line 1 - Cannot find module 'react'. (TS2307)"]);
});
});
describe('_supportedPackageToGlobalMap', () => {
it('works', () => {
expect(_supportedPackageToGlobalMap(SUPPORTED_PACKAGES)).toEqual({
'@fluentui/react': 'FluentUIReact',
'@fluentui/date-time-utilities': 'FluentUIReact',
'@fluentui/react-focus': 'FluentUIReact',
'@fluentui/foundation-legacy': 'FluentUIReact',
'@uifabric/icons': 'FluentUIReact',
'@uifabric/merge-styles': 'FluentUIReact',
'@fluentui/style-utilities': 'FluentUIReact',
'@uifabric/utilities': 'FluentUIReact',
'@uifabric/react-hooks': 'FabricReactHooks',
'@uifabric/example-data': 'FabricExampleData',
});
});
it('is memoized', () => {
const result1 = _supportedPackageToGlobalMap(SUPPORTED_PACKAGES);
const result2 = _supportedPackageToGlobalMap(SUPPORTED_PACKAGES);
expect(result1).toBe(result2);
const result3 = _supportedPackageToGlobalMap([{ globalName: 'Foo', packages: [{ packageName: 'foo' }] }]);
expect(result3).not.toEqual(result2);
});
});