-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtest.js
418 lines (344 loc) · 12 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*global describe, it, beforeEach */
'use strict';
var path = require('path');
var assert = require('chai').assert;
var proxyquire = require('proxyquire');
var bowerUtil = require('./util/bower');
var EventEmitter = require('events').EventEmitter;
describe('google-cdn', function () {
beforeEach(function () {
this.mainPath = '';
this.googlecdn = proxyquire('./googlecdn', {
'./util/bower': {
resolveMainPath: function (supportedTypes, name, version, cb) {
var mainFiles = {};
var thatMainPath = (typeof(this.mainPath) === 'object' ? this.mainPath[name] : this.mainPath);
supportedTypes.types.forEach(function (type) {
if (supportedTypes.typesRegex[type].test(thatMainPath)) {
mainFiles[type] = thatMainPath;
}
});
cb(null, mainFiles);
}.bind(this),
joinComponent: bowerUtil.joinComponent
}
});
});
it('should load', function () {
assert(this.googlecdn !== undefined);
});
it('should replace jquery', function (cb) {
var source = '<script src="bower_components/jquery/jquery.js"></script>';
var bowerConfig = {
dependencies: { jquery: '~2.0.0' }
};
this.mainPath = 'jquery/jquery.js';
this.googlecdn(source, bowerConfig, function (err, result) {
if (err) {
return cb(err);
}
assert.equal(result, '<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>');
cb();
});
});
it('should replace jquery ui', function (cb) {
var source = '<script src="bower_components/jquery-ui/ui/jquery-ui.js"></script>';
var bowerConfig = {
dependencies: { 'jquery-ui': '~1.10.3' }
};
this.mainPath = 'jquery-ui/ui/jquery-ui.js';
this.googlecdn(source, bowerConfig, function (err, result) {
if (err) {
return cb(err);
}
assert.equal(result, '<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>');
cb();
});
});
it('should replace lodash, even though bower and cdnjs use different names', function (cb) {
var source = '<script src="bower_components/lodash/dist/lodash.compat.js"></script>';
var bowerConfig = {
dependencies: { lodash: '~3.9.0' }
};
this.mainPath = 'lodash/dist/lodash.compat.js';
this.googlecdn(source, bowerConfig, { cdn: 'cdnjs' }, function (err, result) {
if (err) {
return cb(err);
}
assert.equal(result, '<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/3.9.3/lodash.min.js"></script>');
cb();
});
});
it('should replace lodash as a devDependency', function (cb) {
var source = '<script src="bower_components/lodash/dist/lodash.compat.js"></script>';
var bowerConfig = {
devDependencies: { lodash: '~3.9.0' }
};
this.mainPath = 'lodash/dist/lodash.compat.js';
this.googlecdn(source, bowerConfig, { cdn: 'cdnjs' }, function (err, result) {
if (err) {
return cb(err);
}
assert.equal(result, '<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/3.9.3/lodash.min.js"></script>');
cb();
});
});
it('should throw without bowerJson', function () {
var source = '<script src="bower_components/jquery-ui/ui/jquery-ui.js"></script>';
var bowerConfig = null;
var fn = this.googlecdn.bind(this, source, bowerConfig, function () {
});
assert.throw(fn, /bowerJson.*missing/);
});
it('should support cdnjs', function (cb) {
var source = '<script src="bower_components/jquery/jquery.js"></script>';
var bowerConfig = {
dependencies: { 'jquery': '~2.0.1' }
};
this.mainPath = 'jquery/jquery.js';
this.googlecdn(source, bowerConfig, { cdn: 'cdnjs' }, function (err, result) {
if (err) {
return cb(err);
}
assert.equal(result, '<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>');
cb();
});
});
it('should support cdn object', function (cb) {
var source = '<script src="bower_components/jquery/jquery.js"></script>';
var bowerConfig = {
dependencies: { 'jquery': '~2.0.1' }
};
this.mainPath = 'jquery/jquery.js';
var cdnObject = {
jquery: {
versions: ['2.0.3', '2.0.2', '2.0.1', '2.0.0'],
url: function (version) {
return '//my.own.cdn/libs/jquery/' + version + '/jquery.min.js';
}
}
};
this.googlecdn(source, bowerConfig, { cdn: cdnObject }, function (err, result) {
if (err) {
return cb(err);
}
assert.equal(result, '<script src="//my.own.cdn/libs/jquery/2.0.3/jquery.min.js"></script>');
cb();
});
});
it('should support cdn array', function (cb) {
var source = '<script src="bower_components/jquery/jquery.js">' +
'</script><script src="bower_components/d3/d3.js"></script>';
var bowerConfig = {
dependencies: {
'jquery': '~2.0.1',
'd3': '~3.4.0'
}
};
this.mainPath = {
'jquery': 'jquery/jquery.js',
'd3': 'd3/d3.js',
};
this.googlecdn(source, bowerConfig, { cdn: ['google', 'cdnjs'] }, function (err, result) {
if (err) {
return cb(err);
}
assert.equal(result, '<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.4.13/d3.min.js"></script>');
cb();
});
});
it('should return an error if a requested cdn does not exist', function (cb) {
var source = '';
var bowerConfig = {};
this.googlecdn(source, bowerConfig, { cdn: ['google', 'does-not-exist', 'cdnjs'] }, function (err) {
assert.match(err, /CDN.*is not supported./);
cb();
});
});
it('should allow cdn data to include all files in the bower component', function (cb) {
var source = '<script src="bower_components/angular-i18n/angular-locale_he.js"></script>';
var bowerConfig = {
dependencies: { 'angular-i18n': '~1.2.0' }
};
var cdnObject = {
'angular-i18n': {
all: true,
versions: ['1.2.6', '1.2.10', '1.2.14'],
url: function (version) {
return '//my.own.cdn/libs/angularjs/' + version + '/i18n';
}
}
};
this.googlecdn(source, bowerConfig, { cdn: cdnObject }, function (err, result) {
if (err) {
return cb(err);
}
assert.equal(result, '<script src="//my.own.cdn/libs/angularjs/1.2.14/i18n/angular-locale_he.js"></script>');
cb();
});
});
it('should strip leading slashes', function (cb) {
var source = '<script src="/bower_components/jquery/jquery.js"></script>';
var bowerConfig = {
dependencies: { 'jquery': '2.0.3' }
};
this.mainPath = 'jquery/jquery.js';
this.googlecdn(source, bowerConfig, function (err, result) {
if (err) {
return cb(err);
}
assert.equal(result, '<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>');
cb();
});
});
it('should print jquery replacement information', function (cb) {
var source = '<script src="bower_components/jquery/jquery.js"></script>';
var bowerConfig = {
dependencies: { jquery: '~2.0.0' }
};
this.mainPath = 'jquery/jquery.js';
this.googlecdn(source, bowerConfig, function (err, result, replacementInfo) {
if (err) {
return cb(err);
}
assert.equal(replacementInfo[0].from, 'bower_components/jquery/jquery.js');
assert.equal(replacementInfo[0].to, '//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js');
cb();
});
});
it('should print jquery-ui replacement information', function (cb) {
var source = '<script src="bower_components/jquery-ui/ui/jquery-ui.js"></script>';
var bowerConfig = {
dependencies: {
'jquery-ui': '~1.10.3'
}
};
this.mainPath = 'jquery-ui/ui/jquery-ui.js';
this.googlecdn(source, bowerConfig, function (err, result, replacementInfo) {
if (err) {
return cb(err);
}
assert.equal(replacementInfo[0].from, 'bower_components/jquery-ui/ui/jquery-ui.js');
assert.equal(replacementInfo[0].to, '//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js');
cb();
});
});
});
describe('util/bower', function () {
beforeEach(function () {
this.pathMock = {
join: path.join,
sep: '/' // Always test the unix way by default.
};
var supportedTypes = {
types: ['js'],
typesRegex: {}
};
supportedTypes.types.forEach(function (type) {
supportedTypes.typesRegex[type] = new RegExp('\\.' + type + '$', 'i');
});
this.supportedTypes = supportedTypes;
this.queryResult = '';
this.bowerInfoMock = function bowerInfoMock() {
var query = new EventEmitter();
process.nextTick(function () {
query.emit('end', this.queryResult);
}.bind(this));
return query;
}.bind(this);
this.util = proxyquire('./util/bower', {
path: this.pathMock,
bower: {
commands: {
info: this.bowerInfoMock
}
}
});
});
it('joinComponent', function () {
var result = this.util.joinComponent('bower_components', 'jquery/jquery-2.0.js');
assert.equal(result, 'bower_components/jquery/jquery-2.0.js');
});
it('should join on w32', function () {
this.pathMock.sep = '\\';
var result = this.util.joinComponent('bower_components', 'jquery\\jquery-2.0.js');
assert.equal(result, 'bower_components/jquery/jquery-2.0.js');
});
it('should resolve jquery to a main path', function (cb) {
this.queryResult = 'jquery.js';
this.util.resolveMainPath(this.supportedTypes, 'jquery', '2.0.0', function (err, path) {
if (err) {
cb(err);
}
assert.deepEqual(path, { js: 'jquery/jquery.js' });
cb();
});
});
it('should resolve jquery-ui to a main path', function (cb) {
this.queryResult = ['ui/jquery-ui.js'];
this.util.resolveMainPath(this.supportedTypes, 'jquery-ui', '1.10.3', function (err, path) {
if (err) {
cb(err);
}
assert.deepEqual(path, { js: 'jquery-ui/ui/jquery-ui.js' });
cb();
});
});
it('should resolve lodash to a main path', function (cb) {
this.queryResult = 'dist/lodash.compat.js';
this.util.resolveMainPath(this.supportedTypes, 'lodash', '2.4.1', function (err, path) {
if (err) {
cb(err);
}
assert.deepEqual(path, { js: 'lodash/dist/lodash.compat.js' });
cb();
});
});
it('should resolve angular to a main path', function (cb) {
this.queryResult = './angular.js';
this.util.resolveMainPath(this.supportedTypes, 'angular', '1.2.6', function (err, path) {
if (err) {
cb(err);
}
assert.deepEqual(path, { js: 'angular/angular.js' });
cb();
});
});
it('should resolve just js for multiple main files', function (cb) {
this.queryResult = ['css/multiple.css', 'lib/multiple.js'];
this.util.resolveMainPath(this.supportedTypes, 'multiple-main', '1.10.3', function (err, path) {
if (err) {
cb(err);
}
assert.deepEqual(path, { js: 'multiple-main/lib/multiple.js' });
cb();
});
});
it('should resolve both css and js for multiple main files', function (cb) {
var supportedTypes = {
types: ['js', 'css'],
typesRegex: {}
};
supportedTypes.types.forEach(function (type) {
supportedTypes.typesRegex[type] = new RegExp('\\.' + type + '$', 'i');
});
this.queryResult = ['css/multiple.css', 'lib/multiple.js'];
this.util.resolveMainPath(supportedTypes, 'multiple-main', '1.10.3', function (err, path) {
if (err) {
cb(err);
}
assert.deepEqual(path, { js: 'multiple-main/lib/multiple.js', css: 'multiple-main/css/multiple.css' });
cb();
});
});
it('should fall fall back to name w/o main', function (cb) {
this.queryResult = undefined;
this.util.resolveMainPath(this.supportedTypes, 'no-main', '1.0', function (err, path) {
if (err) {
cb(err);
}
assert.deepEqual(path, { js: 'no-main/no-main.js' });
cb();
});
});
});