forked from angular-fullstack/generator-angular-fullstack
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest-file-creation.js
507 lines (439 loc) · 14.5 KB
/
test-file-creation.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*global describe, beforeEach, it */
'use strict';
var path = require('path');
var fs = require('fs-extra');
var exec = require('child_process').exec;
var helpers = require('yeoman-generator').test;
var chai = require('chai');
var expect = chai.expect;
var recursiveReadDir = require('recursive-readdir');
describe('angular-fullstack generator', function () {
var gen, defaultOptions = {
script: 'js',
markup: 'html',
stylesheet: 'sass',
router: 'uirouter',
bootstrap: true,
uibootstrap: true,
mongoose: true,
auth: true,
oauth: [],
socketio: true
}, dependenciesInstalled = false;
function generatorTest(generatorType, name, mockPrompt, callback) {
gen.run({}, function () {
var afGenerator;
var deps = [path.join('../..', generatorType)];
afGenerator = helpers.createGenerator('angular-fullstack:' + generatorType, deps, [name]);
helpers.mockPrompt(afGenerator, mockPrompt);
afGenerator.run([], function () {
callback();
});
});
}
function assertOnlyFiles(expectedFiles, done, path, skip) {
path = path || './';
skip = skip || ['e2e', 'node_modules', 'client/bower_components'];
recursiveReadDir(path, skip, function(err, actualFiles) {
if (err) { return done(err); }
var files = actualFiles.concat();
expectedFiles.forEach(function(file, i) {
var index = files.indexOf(file);
if (index >= 0) {
files.splice(index, 1);
}
});
if (files.length !== 0) {
err = new Error('unexpected files found');
err.expected = '';
err.actual = files.join('\n');
return done(err);
}
done();
});
}
function runTest(cmd, self, cb) {
var args = Array.prototype.slice.call(arguments),
endpoint = (args[3] && typeof args[3] === 'string') ? args.splice(3, 1)[0] : null,
timeout = (args[3] && typeof args[3] === 'number') ? args.splice(3, 1)[0] : null;
self.timeout(timeout || 60000);
var execFn = function() {
exec(cmd, function(error, stdout, stderr) {
switch(cmd) {
case 'grunt test:client':
expect(stdout, 'Client tests failed \n' + stdout ).to.contain('Executed 1 of 1\u001b[32m SUCCESS\u001b');
break;
case 'grunt jshint':
expect(stdout).to.contain('Done, without errors.');
break;
case 'grunt test:server':
expect(stdout, 'Server tests failed (do you have mongoDB running?) \n' + stdout).to.contain('Done, without errors.');
break;
default:
expect(stderr).to.be.empty;
}
cb();
});
};
if (endpoint) {
generatorTest('endpoint', endpoint, {}, execFn);
} else {
gen.run({}, execFn);
}
}
function genFiles(ops) {
var mapping = {
stylesheet: {
sass: 'scss',
stylus: 'styl',
less: 'less',
css: 'css'
},
markup: {
jade: 'jade',
html: 'html'
},
script: {
js: 'js',
coffee: 'coffee'
}
},
files = [];
var oauthFiles = function(type) {
return [
'server/auth/' + type + '/index.js',
'server/auth/' + type + '/passport.js',
];
};
var script = mapping.script[ops.script],
markup = mapping.markup[ops.markup],
stylesheet = mapping.stylesheet[ops.stylesheet];
files = files.concat([
'client/.htaccess',
'client/.jshintrc',
'client/favicon.ico',
'client/robots.txt',
'client/index.html',
'client/app/app.' + script,
'client/app/app.' + stylesheet,
'client/app/main/main.' + script,
'client/app/main/main.' + markup,
'client/app/main/main.' + stylesheet,
'client/app/main/main.controller.' + script,
'client/app/main/main.controller.spec.' + script,
'client/assets/images/yeoman.png',
'client/components/navbar/navbar.' + markup,
'client/components/navbar/navbar.controller.' + script,
'server/.jshintrc',
'server/.jshintrc-spec',
'server/app.js',
'server/routes.js',
'server/api/thing/index.js',
'server/api/thing/index.spec.js',
'server/api/thing/thing.controller.js',
'server/api/thing/thing.e2e.js',
'server/components/errors/index.js',
'server/config/local.env.js',
'server/config/local.env.sample.js',
'server/config/express.js',
'server/config/environment/index.js',
'server/config/environment/development.js',
'server/config/environment/production.js',
'server/config/environment/test.js',
'server/views/404.' + markup,
'.bowerrc',
'.buildignore',
'.editorconfig',
'.gitattributes',
'.gitignore',
'.travis.yml',
'.yo-rc.json',
'Gruntfile.js',
'package.json',
'bower.json',
'karma.conf.js',
'mocha.conf.js',
'protractor.conf.js'
]);
if (ops.uibootstrap) {
files = files.concat([
'client/components/modal/modal.' + markup,
'client/components/modal/modal.' + stylesheet,
'client/components/modal/modal.service.' + script,
]);
}
if (ops.mongoose) {
files = files.concat([
'server/api/thing/thing.model.js',
'server/config/seed.js'
]);
}
if (ops.auth) {
files = files.concat([
'client/app/account/account.' + script,
'client/app/account/login/login.' + markup,
'client/app/account/login/login.' + stylesheet,
'client/app/account/login/login.controller.' + script,
'client/app/account/settings/settings.' + markup,
'client/app/account/settings/settings.controller.' + script,
'client/app/account/signup/signup.' + markup,
'client/app/account/signup/signup.controller.' + script,
'client/app/admin/admin.' + markup,
'client/app/admin/admin.' + stylesheet,
'client/app/admin/admin.' + script,
'client/app/admin/admin.controller.' + script,
'client/components/auth/auth.service.' + script,
'client/components/auth/user.service.' + script,
'client/components/mongoose-error/mongoose-error.directive.' + script,
'server/api/user/index.js',
'server/api/user/index.spec.js',
'server/api/user/user.controller.js',
'server/api/user/user.e2e.js',
'server/api/user/user.model.js',
'server/api/user/user.model.spec.js',
'server/auth/index.js',
'server/auth/auth.service.js',
'server/auth/local/index.js',
'server/auth/local/passport.js'
]);
}
if (ops.oauth) {
ops.oauth.forEach(function(type, i) {
files = files.concat(oauthFiles(type.replace('Auth', '')));
});
}
if (ops.socketio) {
files = files.concat([
'client/components/socket/socket.service.' + script,
'client/components/socket/socket.mock.' + script,
'server/api/thing/thing.socket.js',
'server/config/socketio.js'
]);
}
return files;
}
/**
* Generator tests
*/
beforeEach(function (done) {
this.timeout(10000);
var deps = [
'../../app',
[
helpers.createDummyGenerator(),
'ng-component:app'
]
];
helpers.testDirectory(path.join(__dirname, 'temp'), function (err) {
if (err) {
return done(err);
}
gen = helpers.createGenerator('angular-fullstack:app', deps);
gen.options['skip-install'] = true;
done();
}.bind(this));
});
describe('running app', function() {
beforeEach(function() {
this.timeout(20000);
fs.mkdirSync(__dirname + '/temp/client');
fs.symlinkSync(__dirname + '/fixtures/node_modules', __dirname + '/temp/node_modules');
fs.symlinkSync(__dirname +'/fixtures/bower_components', __dirname +'/temp/client/bower_components');
});
describe('with default options', function() {
beforeEach(function() {
helpers.mockPrompt(gen, defaultOptions);
});
it('should run client tests successfully', function(done) {
runTest('grunt test:client', this, done);
});
it('should pass lint', function(done) {
runTest('grunt jshint', this, done);
});
it('should run server tests successfully', function(done) {
runTest('grunt test:server', this, done);
});
it('should pass lint with generated endpoint', function(done) {
runTest('grunt jshint', this, done, 'foo');
});
it('should run server tests successfully with generated endpoint', function(done) {
runTest('grunt test:server', this, done, 'foo');
});
it('should pass lint with generated capitalized endpoint', function(done) {
runTest('grunt jshint', this, done, 'Foo');
});
it('should run server tests successfully with generated capitalized endpoint', function(done) {
runTest('grunt test:server', this, done, 'Foo');
});
it('should use existing config if available', function(done) {
this.timeout(60000);
fs.copySync(__dirname + '/fixtures/.yo-rc.json', __dirname + '/temp/.yo-rc.json');
var gen = helpers.createGenerator('angular-fullstack:app', [
'../../app',
[
helpers.createDummyGenerator(),
'ng-component:app'
]
]);
gen.options['skip-install'] = true;
helpers.mockPrompt(gen, {
skipConfig: true
});
gen.run({}, function () {
helpers.assertFile([
'client/app/main/main.less',
'client/app/main/main.coffee'
]);
done();
});
});
it('should generate expected files', function (done) {
gen.run({}, function () {
helpers.assertFile(genFiles(defaultOptions));
done();
});
});
it('should not generate unexpected files', function (done) {
gen.run({}, function () {
assertOnlyFiles(genFiles(defaultOptions), done);
});
});
// it('should run e2e tests successfully', function(done) {
// this.timeout(80000);
// gen.run({}, function () {
// exec('npm run update-webdriver', function (error, stdout, stderr) {
// exec('grunt test:e2e', function (error, stdout, stderr) {
// expect(stdout, 'Client tests failed \n' + stdout ).to.contain('Done, without errors.');
// done();
// });
// });
// })
// });
});
describe('with other preprocessors and oauth', function() {
var testOptions = {
script: 'coffee',
markup: 'jade',
stylesheet: 'less',
router: 'uirouter',
mongoose: true,
auth: true,
oauth: ['twitterAuth', 'facebookAuth', 'googleAuth'],
socketio: true,
bootstrap: true,
uibootstrap: true
};
beforeEach(function() {
helpers.mockPrompt(gen, testOptions);
});
it('should run client tests successfully', function(done) {
runTest('grunt test:client', this, done);
});
it('should pass lint', function(done) {
runTest('grunt jshint', this, done);
});
it('should run server tests successfully', function(done) {
runTest('grunt test:server', this, done);
});
it('should pass lint with generated snake-case endpoint', function(done) {
runTest('grunt jshint', this, done, 'foo-bar');
});
it('should run server tests successfully with generated snake-case endpoint', function(done) {
runTest('grunt test:server', this, done, 'foo-bar');
});
it('should generate expected files', function (done) {
gen.run({}, function () {
helpers.assertFile(genFiles(testOptions));
done();
});
});
it('should not generate unexpected files', function (done) {
gen.run({}, function () {
assertOnlyFiles(genFiles(testOptions), done);
});
});
});
describe('with other preprocessors and no server options', function() {
var testOptions = {
script: 'coffee',
markup: 'jade',
stylesheet: 'stylus',
router: 'ngroute',
mongoose: false,
auth: false,
oauth: [],
socketio: false,
bootstrap: false,
uibootstrap: false
};
beforeEach(function(done) {
helpers.mockPrompt(gen, testOptions);
done();
});
it('should run client tests successfully', function(done) {
runTest('grunt test:client', this, done);
});
it('should pass lint', function(done) {
runTest('grunt jshint', this, done);
});
it('should run server tests successfully', function(done) {
runTest('grunt test:server', this, done);
});
it('should pass lint with generated endpoint', function(done) {
runTest('grunt jshint', this, done, 'foo');
});
it('should run server tests successfully with generated endpoint', function(done) {
runTest('grunt test:server', this, done, 'foo');
});
it('should generate expected files', function (done) {
gen.run({}, function () {
helpers.assertFile(genFiles(testOptions));
done();
});
});
it('should not generate unexpected files', function (done) {
gen.run({}, function () {
assertOnlyFiles(genFiles(testOptions), done);
});
});
});
describe('with no preprocessors and no server options', function() {
var testOptions = {
script: 'js',
markup: 'html',
stylesheet: 'css',
router: 'ngroute',
mongoose: false,
auth: false,
oauth: [],
socketio: false,
bootstrap: true,
uibootstrap: true
};
beforeEach(function(done) {
helpers.mockPrompt(gen, testOptions);
done();
});
it('should run client tests successfully', function(done) {
runTest('grunt test:client', this, done);
});
it('should pass lint', function(done) {
runTest('grunt jshint', this, done);
});
it('should run server tests successfully', function(done) {
runTest('grunt test:server', this, done);
});
it('should generate expected files', function (done) {
gen.run({}, function () {
helpers.assertFile(genFiles(testOptions));
done();
});
});
it('should not generate unexpected files', function (done) {
gen.run({}, function () {
assertOnlyFiles(genFiles(testOptions), done);
});
});
});
});
});