Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80668 views
1
/**
2
* Copyright 2013 Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*
16
* @emails [email protected] [email protected]
17
*/
18
19
describe('JSLoader', function() {
20
var path = require('path');
21
var JS = require('../lib/resource/JS');
22
var JSLoader = require('../lib/loader/JSLoader');
23
var ProjectConfiguration = require('../lib/resource/ProjectConfiguration');
24
var ResourceMap = require('../lib/ResourceMap');
25
var loadResouce = require('../lib/test_helpers/loadResource');
26
var MessageList = require('../lib/MessageList');
27
var waitsForCallback = require('../lib/test_helpers/waitsForCallback');
28
29
var testData = path.join(__dirname, '..', '__test_data__', 'JS');
30
31
32
it('should match package.json paths', function() {
33
var loader =new JSLoader();
34
expect(loader.matchPath('x.js')).toBe(true);
35
expect(loader.matchPath('a/x.js')).toBe(true);
36
expect(loader.matchPath('a/1.css')).toBe(false);
37
});
38
39
it('should parse old school components', function() {
40
loadResouce(
41
new JSLoader(),
42
path.join(testData, 'oldSchoolComponent.js'),
43
null,
44
function(errors, js) {
45
expect(js.isModule).toBe(false);
46
expect(js.id).toBe('oldSchoolComponent-tag');
47
expect(js.requiredLegacyComponents).toEqual(['foo', 'bar']);
48
expect(js.requiredCSS).toEqual(['foo-css']);
49
});
50
});
51
52
53
it('should parse modules with requires', function() {
54
loadResouce(
55
new JSLoader(),
56
path.join(testData, 'module.js'),
57
null,
58
function(errors, js) {
59
expect(js.isModule).toBe(true);
60
expect(js.id).toBe('module-tag');
61
expect(js.requiredModules).toEqual(['foo', 'bar']);
62
expect(js.requiredCSS).toEqual(['foo-css']);
63
});
64
});
65
66
67
it('should parse javelin', function() {
68
loadResouce(
69
new JSLoader(),
70
path.join(testData, 'javelin.js'),
71
null,
72
function(errors, js) {
73
expect(js.isModule).toBe(true);
74
expect(js.isJavelin).toBe(true);
75
expect(js.isRunWhenReady).toBe(true);
76
expect(js.id).toBe('JX.MSteps');
77
expect(js.requiredLegacyComponents)
78
.toEqual(['javelin-dom', 'javelin-install', 'javelin-stratcom']);
79
});
80
});
81
82
it('should exptract network size', function() {
83
loadResouce(
84
new JSLoader({ networkSize: true }),
85
path.join(testData, 'javelin.js'),
86
null,
87
function(errors, js) {
88
expect(js.networkSize > 0).toBe(true);
89
});
90
});
91
92
it('should exptract javelin symbols', function() {
93
loadResouce(
94
new JSLoader(),
95
path.join(testData, 'javelin.js'),
96
null,
97
function(errors, js) {
98
expect(js.definedJavelinSymbols).toEqual(['JX.MSteps']);
99
expect(js.requiredJavelinSymbols.sort())
100
.toEqual(['JX.URL', 'JX.install']);
101
});
102
});
103
104
it('should exptract javelin symbols and networkSize', function() {
105
loadResouce(
106
new JSLoader({
107
networkSize: true
108
}),
109
path.join(testData, 'javelin.js'),
110
null,
111
function(errors, js) {
112
expect(js.definedJavelinSymbols).toEqual(['JX.MSteps']);
113
expect(js.requiredJavelinSymbols.sort())
114
.toEqual(['JX.URL', 'JX.install']);
115
expect(js.networkSize > 0).toBe(true);
116
});
117
});
118
119
it('should resolve paths using configuration', function() {
120
loadResouce(
121
new JSLoader(),
122
path.join(testData, 'configured', 'a.js'),
123
new ProjectConfiguration(
124
path.join(testData, 'configured', 'package.json'),
125
{}),
126
function(errors, js) {
127
expect(js.id).toBe(path.join('configured','a.js'));
128
expect(js.requiredCSS).toEqual(['foo-css']);
129
});
130
});
131
132
133
it('should resolve commonJS "main" modules post process', function() {
134
var map;
135
136
waitsForCallback(
137
// test
138
function(callback) {
139
var loader = new JSLoader();
140
map = new ResourceMap([
141
// hasCustomMain dependency project
142
JS.fromObject({
143
id: 'hasCustomMain/folderWithMain/customMainModule.js',
144
path: path.join(
145
testData,
146
'hasCustomMain',
147
'folderWithMain',
148
'customMainModule.js'
149
),
150
requiredModules: []
151
}),
152
new ProjectConfiguration(
153
path.join(testData, 'hasCustomMain', 'package.json'), {
154
name: 'hasCustomMain',
155
main: 'folderWithMain/customMainModule.js'
156
}
157
),
158
159
// hasStandardIndex dependency project
160
JS.fromObject({
161
id: 'hasStandardIndex/index.js',
162
path: path.join(testData, 'hasStandardIndex', 'index.js'),
163
requiredModules: []
164
}),
165
new ProjectConfiguration(
166
path.join(testData, 'hasStandardIndex', 'package.json'),
167
{name: 'hasStandardIndex'} // Defaults main to index.js
168
),
169
170
171
JS.fromObject({
172
id: 'commonJSProject/dependsOnCustomMain.js',
173
path: path.join(
174
testData,
175
'commonJSProject',
176
'dependsOnCustomMain.js'
177
),
178
requiredModules: ['hasCustomMain']
179
}),
180
JS.fromObject({
181
id: 'commonJSProject/dependsOnStandardIndex.js',
182
path: path.join(
183
testData,
184
'commonJSProject',
185
'dependsOnStandardIndex.js'
186
),
187
requiredModules: ['hasStandardIndex']
188
}),
189
new ProjectConfiguration(
190
path.join(testData, 'commonJSProject', 'package.json'),
191
{name: 'commonJSProject'} // Must mirror what node will *actually* find
192
)
193
]);
194
195
loader.postProcess(map, map.getAllResourcesByType('JS'), callback);
196
},
197
198
// expectation
199
function(messages) {
200
expect(messages).toEqual(jasmine.any(MessageList));
201
expect(
202
map.getResource('JS', 'commonJSProject/dependsOnCustomMain.js')
203
.requiredModules
204
).toEqual(['hasCustomMain/folderWithMain/customMainModule.js']);
205
206
expect(
207
map.getResource('JS', 'commonJSProject/dependsOnCustomMain.js')
208
._requiredTextToResolvedID
209
).toEqual({
210
'hasCustomMain': 'hasCustomMain/folderWithMain/customMainModule.js'
211
});
212
213
expect(
214
map.getResource('JS', 'commonJSProject/dependsOnStandardIndex.js')
215
.requiredModules
216
).toEqual(['hasStandardIndex/index.js']);
217
expect(
218
map.getResource('JS', 'commonJSProject/dependsOnStandardIndex.js')
219
._requiredTextToResolvedID
220
).toEqual({'hasStandardIndex': 'hasStandardIndex/index.js'});
221
}
222
);
223
});
224
225
it('should resolve intern rel paths *with* package process', function() {
226
var map;
227
228
waitsForCallback(
229
// test
230
function(callback) {
231
var loader = new JSLoader();
232
map = new ResourceMap([
233
JS.fromObject({
234
id: 'configured/a.js',
235
path: path.join(testData, 'configured', 'a.js'),
236
requiredModules: ['./b'] // TODO: add more interesting things here
237
}),
238
JS.fromObject({
239
id: 'configured/b.js',
240
path: path.join(testData, 'configured', 'b.js')
241
}),
242
new ProjectConfiguration(
243
path.join(testData, 'configured', 'package.json'),
244
{name: 'configured'} // Must mirror what node will *actually* find
245
)
246
]);
247
248
loader.postProcess(map, map.getAllResourcesByType('JS'), callback);
249
},
250
251
// expectation
252
function(messages) {
253
expect(messages).toEqual(jasmine.any(MessageList));
254
expect(
255
map.getResource('JS', 'configured/a.js').requiredModules)
256
.toEqual(['configured/b.js']
257
);
258
expect(
259
map.getResource('JS', 'configured/a.js')._requiredTextToResolvedID
260
).toEqual({'./b': 'configured/b.js'});
261
}
262
);
263
});
264
265
it('should resolve local paths without package.json', function() {
266
var map;
267
268
waitsForCallback(
269
// test
270
function(callback) {
271
var jsLoader = new JSLoader();
272
map = new ResourceMap([
273
JS.fromObject({
274
id: 'configured/a.js',
275
path: path.join(testData, 'configured', 'a.js'),
276
requiredModules: ['./b']
277
}),
278
JS.fromObject({
279
id: 'configured/b.js',
280
path: path.join(testData, 'configured', 'b.js'),
281
requiredModules: []
282
})
283
]);
284
jsLoader.postProcess(map, map.getAllResources(), callback);
285
},
286
287
// expectation
288
function(messages) {
289
expect(messages).toEqual(jasmine.any(MessageList));
290
expect(
291
map.getResource('JS', 'configured/a.js').requiredModules
292
).toEqual(['configured/b.js']);
293
}
294
);
295
});
296
});
297
298