This repository was archived by the owner on Aug 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathindex.spec.ts
179 lines (164 loc) · 7.68 KB
/
index.spec.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { getConvertedExternals, getEntryPathRegExp, getExternalsHandler } from './index';
const path = require("path");
describe('index', () => {
describe('getExternalsHandler', () => {
it('throws an error when the argument is not an array of regular expressions', () => {
expect(() => getExternalsHandler()).toThrowError();
expect(() => getExternalsHandler(32)).toThrowError();
expect(() => getExternalsHandler(null)).toThrowError();
expect(() => getExternalsHandler("asd")).toThrowError();
expect(() => getExternalsHandler([{}])).toThrowError();
expect(() => getExternalsHandler(/((node_modules\/)|^)nativescript-vue((\/.*)|$)/)).toThrowError();
});
const externalContextTestCases = [
{
name: 'request for another external module from node_modules',
input: 'nativescript-angular',
expectedIsExternal: true
},
{
name: 'request for the same module from node_modules',
input: 'nativescript-vue',
expectedIsExternal: true
},
{
name: 'request for a relative file in the same external module',
input: './vue-index',
expectedIsExternal: true
},
{
name: 'request for a relative file outside the external module',
input: '../another-non-external-module/another-index',
expectedIsExternal: false
},
{
name: 'request for an absolute local path',
input: '/nativescript-vue',
expectedIsExternal: false
}
];
externalContextTestCases.forEach(testCase => {
it(`returns handler which matches requests inside an external module - ${testCase.name}`, (done) => {
const handler = getExternalsHandler([/((node_modules\/)|^)nativescript-vue((\/.*)|$)/, /((node_modules\/)|^)nativescript-angular((\/.*)|$)/]);
handler("./node_modules/nativescript-vue/", testCase.input, (error, externalLink) => {
if (testCase.expectedIsExternal) {
expect(error).toBeNull();
expect(externalLink).toBeDefined();
} else {
expect(error).toBeUndefined();
expect(externalLink).toBeUndefined();
}
done();
});
});
});
const nonExternalContextTestCases = [
{
name: 'request for an external module from node_modules',
input: 'nativescript-vue',
expectedIsExternal: true
},
{
name: 'request for a relative file from an external module',
input: '../node_modules/nativescript-vue/vue-index',
expectedIsExternal: true
},
{
name: 'request for a relative file inside the app',
input: './app-module',
expectedIsExternal: false
},
{
name: 'request for a relative file from a non external module',
input: '../node_modules/another-non-external-module/another-index',
expectedIsExternal: false
},
{
name: 'request for an absolute local path',
input: '/nativescript-vue',
expectedIsExternal: false
}
];
nonExternalContextTestCases.forEach(testCase => {
it(`returns handler which matches requests from inside the app folder - ${testCase.name}`, (done) => {
const handler = getExternalsHandler([/((node_modules\/)|^)nativescript-vue((\/.*)|$)/]);
handler("./app/", testCase.input, (error, externalLink) => {
if (testCase.expectedIsExternal) {
expect(error).toBeNull();
expect(externalLink).toBeDefined();
} else {
expect(error).toBeUndefined();
expect(externalLink).toBeUndefined();
}
done();
});
});
});
});
describe('getConvertedExternals', () => {
it('returns empty array when nullable is passed', () => {
const actualResult = getConvertedExternals(null);
expect(actualResult).toEqual([]);
});
const testCases = [
{
input: ['nativescript-vue'],
expectedOutput: [/((node_modules\/)|^)nativescript-vue((\/.*)|$)/]
},
{
input: ['nativescript-vue', 'nativescript-angular'],
expectedOutput: [/((node_modules\/)|^)nativescript-vue((\/.*)|$)/, /((node_modules\/)|^)nativescript-angular((\/.*)|$)/]
}
];
testCases.forEach(testCase => {
it('converts passed strings to regular expressions', () => {
const actualResult = getConvertedExternals(testCase.input);
expect(actualResult).toEqual(testCase.expectedOutput);
});
it(`returns regular expressions which match expected modules and their subdirs, for input ${testCase.input}`, () => {
[
'nativescript-vue',
'nativescript-vue/subdir',
'./node_modules/nativescript-vue/subdir',
'nativescript-vue/subdir/subdir-2'
].forEach(testString => {
const regExpsExternals = getConvertedExternals(testCase.input);
const result = regExpsExternals.some((regExp: RegExp) => !!regExp.exec(testString));
expect(result).toBe(true, `String ${testString} does not match any of the regular expressions: ${regExpsExternals.join(', ')}`);
});
});
it(`returns regular expressions which does not match expected modules and their subdirs, for input ${testCase.input}`, () => {
[
'nativescript-facebook',
'nativescript-facebook/nativescript-vue',
'main-plugin/subdir/nativescript-vue',
'nativescript-vue-template-compiler',
'nativescript-vue-template-compiler/subdir'
].forEach(testString => {
const regExpsExternals = getConvertedExternals(testCase.input);
const result = regExpsExternals.some((regExp: RegExp) => !!regExp.exec(testString));
expect(result).toBe(false, `String ${testString} matches some of the regular expressions: ${regExpsExternals.join(', ')}`);
});
});
});
});
describe('getEntryPathRegExp', () => {
const originalPathJoin = path.join;
const entryModule = "index.js";
afterEach(() => {
path.join = originalPathJoin;
});
it('returns RegExp that matches Windows', () => {
const appPath = "D:\\Work\\app1\\app";
path.join = path.win32.join;
const regExp = getEntryPathRegExp(appPath, entryModule);
expect(!!regExp.exec(`${appPath}\\${entryModule}`)).toBe(true);
});
it('returns RegExp that works with POSIX paths', () => {
const appPath = "/usr/local/lib/app1/app";
path.join = path.posix.join;
const regExp = getEntryPathRegExp(appPath, entryModule);
expect(!!regExp.exec(`${appPath}/${entryModule}`)).toBe(true);
});
});
});