blob: f75f444a805734b5881006a5b639dbd235f53f2a [file] [log] [blame]
Matteo Scandolo41d9f722016-05-11 09:03:25 -07001'use strict';
2
3const path = require('path');
4const helpers = require('yeoman-test');
5const assert = require('yeoman-assert');
Matteo Scandoloc9c97392016-05-11 11:52:10 -07006const rimraf = require('rimraf');
7const mockery = require('mockery');
8const wiredep = require('wiredep');
Matteo Scandolo41d9f722016-05-11 09:03:25 -07009
Matteo Scandoloc9c97392016-05-11 11:52:10 -070010const firstCharTouppercase = string => string.replace(/^./, string[0].toUpperCase())
11
12// get bower deps installed in ngXosLib
13let bowerDeps = wiredep({
14 cwd: path.join(__dirname, '../../'), // pretending to be in the ngXosLib root
15 exclude: ['Chart.js']
16});
17bowerDeps = bowerDeps.js.map(d => d.match(/bower_components\/([a-zA-Z\-`.]+)\//)[1]);
18
19// test values
20const viewName = 'testDashboard';
Matteo Scandolo4ec7c182016-05-11 12:10:17 -070021const fileName = firstCharTouppercase(viewName);
Matteo Scandoloc9c97392016-05-11 11:52:10 -070022const testPath = path.join(__dirname, `../../../ngXosViews/${viewName}/`);
Matteo Scandolo41d9f722016-05-11 09:03:25 -070023
24const getDefaultFiles = () => {
25 return [
Matteo Scandoloc9c97392016-05-11 11:52:10 -070026 '.bowerrc',
27 '.eslintrc',
28 '.gitignore',
29 'bower.json',
30 'gulpfile.js',
31 'karma.conf.js',
32 'package.json',
33 'src/index.html',
34 ].map(i => `${testPath}${i}`);
Matteo Scandolo41d9f722016-05-11 09:03:25 -070035};
36
Matteo Scandoloc9c97392016-05-11 11:52:10 -070037const yeomanUserMock = {
38 git: {
39 name: () => 'Test User',
40 email: () => 'test@mail.org'
41 }
42}
43
44mockery.enable({
45 warnOnReplace: false,
46 warnOnUnregistered: false,
47 useCleanCache: true,
48});
49mockery.resetCache();
50mockery.registerMock('../node_modules/yeoman-generator/lib/actions/user', yeomanUserMock);
51
Matteo Scandolo41d9f722016-05-11 09:03:25 -070052describe('Yeoman XOS generator', function () {
53
Matteo Scandoloc9c97392016-05-11 11:52:10 -070054 beforeEach(() => {
55 });
56
57 before(done => {
Matteo Scandolo41d9f722016-05-11 09:03:25 -070058 this.generator = helpers
59 .run(require.resolve('../app'))
60 .inDir(testPath)
61 .withOptions({ 'skip-install': true })
62 .withPrompts({
Matteo Scandoloc9c97392016-05-11 11:52:10 -070063 name: viewName,
Matteo Scandolo41d9f722016-05-11 09:03:25 -070064 host: 'test-host',
65 token: 'test-token',
66 session: 'test-session'
Matteo Scandoloc9c97392016-05-11 11:52:10 -070067 })
68 .on('end', done);
Matteo Scandolo41d9f722016-05-11 09:03:25 -070069 });
70
Matteo Scandolo41d9f722016-05-11 09:03:25 -070071
Matteo Scandoloc9c97392016-05-11 11:52:10 -070072 it('should generate base files in the correct directory', () => {
73 assert.file(getDefaultFiles());
74 });
75
76 it('should create the env file with correct params', () => {
77 assert.fileContent(`${testPath}env/default.js`, 'host: \'test-host\'');
78 assert.fileContent(`${testPath}env/default.js`, 'xoscsrftoken: \'test-token\'');
79 assert.fileContent(`${testPath}env/default.js`, 'xossessionid: \'test-session\'');
80 });
81
82 it('should write username in package & bower json', () => {
83 assert.fileContent(`${testPath}package.json`, '"author": "Test User"');
84 assert.fileContent(`${testPath}bower.json`, '"Test User <test@mail.org>"')
85 });
86
87 it('should add all xosLib dependencies in the dev section of bower.json', () => {
88 bowerDeps.forEach(d => {
89 assert.fileContent(`${testPath}bower.json`, d);
Matteo Scandolo41d9f722016-05-11 09:03:25 -070090 });
91 });
Matteo Scandoloc9c97392016-05-11 11:52:10 -070092
93 it('should set the right module name in all the files', () => {
94 assert.fileContent(`${testPath}src/index.html`, `ng-app="xos.${viewName}"`)
Matteo Scandolo4ec7c182016-05-11 12:10:17 -070095 assert.fileContent(`${testPath}src/index.html`, `id="xos${fileName}"`)
Matteo Scandoloc9c97392016-05-11 11:52:10 -070096 assert.fileContent(`${testPath}src/js/main.js`, `angular.module('xos.${viewName}', [`)
Matteo Scandolo4ec7c182016-05-11 12:10:17 -070097 assert.fileContent(`${testPath}src/sass/main.scss`, `#xos${fileName}`)
Matteo Scandoloc9c97392016-05-11 11:52:10 -070098 });
99
Matteo Scandolo4ec7c182016-05-11 12:10:17 -0700100 it('should set correct paths in build file', () => {
101 assert.fileContent(`${testPath}gulp/build.js`, `angular.module('xos.${viewName}')`)
102 assert.fileContent(`${testPath}gulp/build.js`, `options.dashboards + 'xos${fileName}.html'`)
103 assert.fileContent(`${testPath}gulp/build.js`, `options.static + 'css/xos${fileName}.css'`)
104 assert.fileContent(`${testPath}gulp/build.js`, `.pipe(concat('xos${fileName}.css'))`)
105 assert.fileContent(`${testPath}gulp/build.js`, `.pipe(concat('xos${fileName}.js'))`)
106 assert.fileContent(`${testPath}gulp/build.js`, `module: 'xos.${viewName}'`)
107 assert.fileContent(`${testPath}gulp/build.js`, `options.static + 'js/vendor/xos${fileName}Vendor.js'`)
108 assert.fileContent(`${testPath}gulp/build.js`, `options.static + 'js/xos${fileName}.js'`)
109 assert.fileContent(`${testPath}gulp/build.js`, `options.static + 'css/xos${fileName}.css'`)
110 assert.fileContent(`${testPath}gulp/build.js`, `.pipe(concat('xos${fileName}Vendor.js'))`)
Matteo Scandoloc9c97392016-05-11 11:52:10 -0700111 });
112
113 after(done => {
114 // deleting the folder used for test
115 rimraf(testPath, {}, done)
116 });
Matteo Scandolo41d9f722016-05-11 09:03:25 -0700117});