/
browser-args_tests.js
447 lines (339 loc) · 13.2 KB
/
browser-args_tests.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
'use strict';
var browserArgs = require('../../lib/utils/browser-args');
var expect = require('chai').expect;
var log = require('npmlog');
var EventEmitter = require('events').EventEmitter;
var fakeStream = new EventEmitter();
fakeStream.write = function() {};
log.stream = fakeStream;
function createKnownBrowsers() {
return [{
name: 'Chrome',
args: function() {
return [
'--testem',
'http://localhost/'
];
}
}, {
name: 'Firefox',
args: [
'--testem',
'http://localhost/'
]
}];
}
describe('browserArgs', function() {
var config = {
get: function() {
return this.browser_args;
}
};
var knownBrowsers;
describe('methods', function() {
describe('addCustomArgs', function() {
afterEach(function() {
delete config.browser_args;
});
it('ignores no input', function() {
expect(browserArgs.addCustomArgs()).to.equal(undefined);
});
it('warns if browser_args is not an object', function(done) {
config.browser_args = 'invalid';
log.once('log.warn', function(warning) {
expect(warning.message).to.equal('Type error: browser_args should be an object');
done();
});
browserArgs.addCustomArgs(createKnownBrowsers(), config);
});
it('warns if args for a browser is an object but missing mode field', function(done) {
config.browser_args = {
chrome: {
args: '--fake'
}
};
log.once('log.warn', function(warning) {
expect(warning.message).to.equal('Type error: when using an object to specify browser_args for chrome you must specify a mode');
done();
});
browserArgs.addCustomArgs(createKnownBrowsers(), config);
});
it('warns if args for a browser is an object but missing args field', function(done) {
config.browser_args = {
chrome: {
mode: 'dev'
}
};
log.once('log.warn', function(warning) {
expect(warning.message).to.equal('Type error: when using an object to specify browser_args for chrome you must specify args');
done();
});
browserArgs.addCustomArgs(createKnownBrowsers(), config);
});
it('ignores args that specify a mode different from the current mode', function() {
// Get Chrome's default args
var defaultArgs = createKnownBrowsers()[0].args();
knownBrowsers = createKnownBrowsers();
config.appMode = 'ci';
config.browser_args = {
chrome: {
mode: 'dev',
args: '--fake'
}
};
browserArgs.addCustomArgs(knownBrowsers, config);
expect(knownBrowsers[0].args()).to.deep.equal(defaultArgs);
// Resets known browsers value
knownBrowsers = createKnownBrowsers();
});
it('adds args in object form when specifying a matching mode and args fields', function() {
// Get Chrome's default args
var defaultArgs = createKnownBrowsers()[0].args();
knownBrowsers = createKnownBrowsers();
config.appMode = 'dev';
config.browser_args = {
chrome: {
mode: 'dev',
args: '--fake'
}
};
browserArgs.addCustomArgs(knownBrowsers, config);
expect(knownBrowsers[0].args()).to.deep.equal([ '--fake' ].concat(defaultArgs));
// Resets known browsers value
knownBrowsers = createKnownBrowsers();
});
it('warns about deprecation of mode and args fields', function(done) {
knownBrowsers = createKnownBrowsers();
config.appMode = 'dev';
config.browser_args = {
chrome: {
mode: 'dev',
args: '--fake'
}
};
log.once('log.warn', function(warning) {
expect(warning.message).to.equal('[DEPRECATION] Specifying browser_args as a hash with "mode" and "args" properties has been deprecated. Mode should be a key with its arguments as a value.');
done();
});
browserArgs.addCustomArgs(knownBrowsers, config);
// Resets known browsers value
knownBrowsers = createKnownBrowsers();
});
// NOTE: knownBrowsers[0] is Chrome
it('adds args to browser regardless of key capitalization', function() {
// Get Chrome's default args
var defaultArgs = createKnownBrowsers()[0].args();
knownBrowsers = createKnownBrowsers();
// NOTE: Chrome is not capitalized here
config.browser_args = {
chrome: '--fake'
};
browserArgs.addCustomArgs(knownBrowsers, config);
expect(knownBrowsers[0].args()).to.deep.equal([ '--fake' ].concat(defaultArgs));
// Resets known browsers value
knownBrowsers = createKnownBrowsers();
});
describe('object form with mode as key', function() {
it('adds args for matching appMode', function() {
// Get Chrome's default args
var defaultArgs = createKnownBrowsers()[0].args();
knownBrowsers = createKnownBrowsers();
config.appMode = 'dev';
config.browser_args = {
chrome: {
dev: ['--fake'],
ci: ['--ci-only']
}
};
browserArgs.addCustomArgs(knownBrowsers, config);
expect(knownBrowsers[0].args()).to.deep.equal([ '--fake' ].concat(defaultArgs));
// Resets known browsers value
knownBrowsers = createKnownBrowsers();
});
it('adds args with "all" merged in', function() {
// Get Chrome's default args
var defaultArgs = createKnownBrowsers()[0].args();
knownBrowsers = createKnownBrowsers();
config.appMode = 'ci';
config.browser_args = {
chrome: {
all: '--all',
dev: ['--fake'],
ci: ['--ci-only']
}
};
browserArgs.addCustomArgs(knownBrowsers, config);
expect(knownBrowsers[0].args()).to.deep.equal([ '--all', '--ci-only' ].concat(defaultArgs));
// Resets known browsers value
knownBrowsers = createKnownBrowsers();
});
it('adds "all" args if arguments for the appMode are not set', function() {
// Get Chrome's default args
var defaultArgs = createKnownBrowsers()[0].args();
knownBrowsers = createKnownBrowsers();
config.appMode = 'ci';
config.browser_args = {
chrome: {
all: '--all',
dev: ['--fake']
}
};
browserArgs.addCustomArgs(knownBrowsers, config);
expect(knownBrowsers[0].args()).to.deep.equal([ '--all' ].concat(defaultArgs));
// Resets known browsers value
knownBrowsers = createKnownBrowsers();
});
});
});
describe('createValidation', function() {
it('creates an instance of Validation', function() {
var validation = browserArgs.createValidation();
expect(validation).to.have.property('knownBrowser', null);
expect(validation.messages).to.deep.equal([]);
expect(validation).to.have.property('valid', true);
});
});
describe('dedupeBrowserArgs', function() {
it('ignores no input', function() {
expect(browserArgs.dedupeBrowserArgs()).to.equal(undefined);
});
it('dedupes args', function(done) {
var args;
log.once('log.warn', function(warning) {
expect(warning.message).to.equal('Removed duplicate arg for Chrome: -b');
done();
});
args = browserArgs.dedupeBrowserArgs('Chrome', [ '-a', '-b', '-b' ]);
expect(args).to.deep.equal([ '-a', '-b' ]);
});
it('returns in tact args if no dupes', function() {
var argsIn = [ '-a', '-b', '-c' ];
var argsOut = browserArgs.dedupeBrowserArgs('Chrome', argsIn);
expect(argsOut).to.deep.equal(argsIn);
});
});
describe('parseArgs', function() {
beforeEach(function() {
knownBrowsers = createKnownBrowsers();
});
afterEach(function() {
knownBrowsers = createKnownBrowsers();
});
// NOTE: knownBrowsers[0] is Chrome
it('adds string args to browser with args method', function() {
// Get Chrome's default args
var defaultArgs = createKnownBrowsers()[0].args();
browserArgs.parseArgs('Chrome', '--fake', knownBrowsers);
expect(knownBrowsers[0].args()).to.deep.equal([ '--fake' ].concat(defaultArgs));
});
// NOTE: knownBrowsers[0] is Chrome
it('adds array args to browser with args method', function() {
// Get Chrome's default args
var defaultArgs = createKnownBrowsers()[0].args();
browserArgs.parseArgs('Chrome', [ '--fake' ], knownBrowsers);
expect(knownBrowsers[0].args()).to.deep.equal([ '--fake' ].concat(defaultArgs));
});
// NOTE: knownBrowsers[1] is Firefox
it('adds string args to browser with args array', function() {
// Get Firefox's default args
var defaultArgs = createKnownBrowsers()[1].args;
browserArgs.parseArgs('Firefox', '--fake', knownBrowsers);
expect(knownBrowsers[1].args).to.deep.equal([ '--fake' ].concat(defaultArgs));
});
// NOTE: knownBrowsers[1] is Firefox
it('adds array args to browser with args array', function() {
// Get Firefox's default args
var defaultArgs = createKnownBrowsers()[1].args;
browserArgs.parseArgs('Firefox', [ '--fake' ], knownBrowsers);
expect(knownBrowsers[1].args).to.deep.equal([ '--fake' ].concat(defaultArgs));
});
});
describe('validate', function() {
it('ignores invalid arguments', function() {
var badInputs = [
[],
[ 'Chrome' ],
[ 'Chrome', '--flag' ],
[ 'Chrome', '--flag', 'invalid' ]
];
badInputs.forEach(function(inputValues) {
expect(browserArgs.validate.apply(browserArgs, inputValues)).to.equal(undefined);
});
});
it('returns a validation object', function() {
var validation = browserArgs.validate('Fake', '--flag', knownBrowsers);
expect(validation).to.have.property('knownBrowser', null);
expect(validation.messages).to.be.an.instanceof(Array);
expect(validation).to.have.property('valid', false);
});
it('warns if unknown browser in args', function() {
var validation = browserArgs.validate('Fake', '--flag', knownBrowsers);
expect(validation.messages[0]).to.equal('Could not find "Fake" in known browsers');
});
});
describe('validateBrowserArgs', function() {
var validation;
beforeEach(function() {
validation = browserArgs.createValidation();
});
it('ignores invalid arguments', function() {
var badInputs = [
[],
[ 'Chrome' ],
[ 'Chrome', '--flag' ]
];
badInputs.forEach(function(inputs) {
expect(browserArgs.validateBrowserArgs.apply(browserArgs, inputs)).equal(undefined);
});
});
it('warns if args value not a string or array', function() {
browserArgs.validateBrowserArgs('Chrome', 0, validation);
expect(validation.messages[0]).to.equal('Type error: Chrome\'s "args" property should be a string or an array');
});
it('warns if string arg is empty', function() {
browserArgs.validateBrowserArgs('Chrome', '', validation);
expect(validation.messages[0]).to.equal('Bad value: Chrome\'s "args" property should not be empty');
});
it('warns if array arg is empty', function() {
browserArgs.validateBrowserArgs('Chrome', [], validation);
expect(validation.messages[0]).to.equal('Bad value: Chrome\'s "args" property should not be empty');
});
it('warns if array arg contains non-string values', function() {
browserArgs.validateBrowserArgs('Chrome', [ 0 ], validation);
expect(validation.messages[0]).to.equal('Bad value: Chrome\'s "args" may only contain strings');
});
it('warns if array arg contains empty strings', function() {
browserArgs.validateBrowserArgs('Chrome', [ ' ' ], validation);
expect(validation.messages[0]).to.equal('Bad value: Chrome\'s "args" may not contain empty strings');
});
});
});
describe('multiple warnings', function() {
afterEach(function() {
delete config.browser_args;
});
it('can log multiple validation warnings', function(done) {
var count = 1;
var warnHandler = function(warning) {
if (count === 1) {
expect(warning.message).to.equal('Could not find "Fake" in known browsers');
}
if (count === 2) {
expect(warning.message).to.equal('Bad value: Chrome\'s "args" may only contain strings');
log.removeListener('log.warn', warnHandler);
done();
}
count++;
};
config.browser_args = {
'Fake': '--testem',
'Chrome': [
'--testem',
12345
]
};
log.on('log.warn', warnHandler);
browserArgs.addCustomArgs(knownBrowsers, config);
});
});
});