blob: 4ae160202e6f9f26b66c54bd73d41c078a1bbdef [file] [log] [blame]
Matteo Scandolo124bbfc2016-05-11 09:03:25 -07001'use strict';
2
3const path = require('path');
4const helpers = require('yeoman-test');
5const assert = require('yeoman-assert');
Matteo Scandolo07dd2762016-05-11 11:52:10 -07006const rimraf = require('rimraf');
7const mockery = require('mockery');
8const wiredep = require('wiredep');
Matteo Scandolo124bbfc2016-05-11 09:03:25 -07009
Matteo Scandolo07dd2762016-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});
Matteo Scandolodb20d5a2016-05-16 10:31:32 -070017bowerDeps = bowerDeps.js.map(d => {
18 let path = d.match(/bower_components\/([1-9a-zA-Z\-`.]+)\//);
19 if(path){
20 return path[1];
21 }
22});
Matteo Scandolo07dd2762016-05-11 11:52:10 -070023
24// test values
25const viewName = 'testDashboard';
Matteo Scandoloce6624c2016-05-11 12:10:17 -070026const fileName = firstCharTouppercase(viewName);
Matteo Scandolo07dd2762016-05-11 11:52:10 -070027const testPath = path.join(__dirname, `../../../ngXosViews/${viewName}/`);
Matteo Scandolo124bbfc2016-05-11 09:03:25 -070028
29const getDefaultFiles = () => {
30 return [
Matteo Scandolo07dd2762016-05-11 11:52:10 -070031 '.bowerrc',
32 '.eslintrc',
33 '.gitignore',
34 'bower.json',
35 'gulpfile.js',
36 'karma.conf.js',
37 'package.json',
38 'src/index.html',
39 ].map(i => `${testPath}${i}`);
Matteo Scandolo124bbfc2016-05-11 09:03:25 -070040};
41
Matteo Scandolo07dd2762016-05-11 11:52:10 -070042const yeomanUserMock = {
43 git: {
44 name: () => 'Test User',
45 email: () => 'test@mail.org'
46 }
47}
48
49mockery.enable({
50 warnOnReplace: false,
51 warnOnUnregistered: false,
52 useCleanCache: true,
53});
54mockery.resetCache();
55mockery.registerMock('../node_modules/yeoman-generator/lib/actions/user', yeomanUserMock);
56
Matteo Scandolo124bbfc2016-05-11 09:03:25 -070057describe('Yeoman XOS generator', function () {
58
Matteo Scandolo07dd2762016-05-11 11:52:10 -070059 beforeEach(() => {
60 });
61
62 before(done => {
Matteo Scandolo124bbfc2016-05-11 09:03:25 -070063 this.generator = helpers
64 .run(require.resolve('../app'))
65 .inDir(testPath)
66 .withOptions({ 'skip-install': true })
67 .withPrompts({
Matteo Scandolo07dd2762016-05-11 11:52:10 -070068 name: viewName,
Matteo Scandolo124bbfc2016-05-11 09:03:25 -070069 host: 'test-host',
70 token: 'test-token',
71 session: 'test-session'
Matteo Scandolo07dd2762016-05-11 11:52:10 -070072 })
73 .on('end', done);
Matteo Scandolo124bbfc2016-05-11 09:03:25 -070074 });
75
Matteo Scandolo124bbfc2016-05-11 09:03:25 -070076
Matteo Scandolo07dd2762016-05-11 11:52:10 -070077 it('should generate base files in the correct directory', () => {
78 assert.file(getDefaultFiles());
79 });
80
81 it('should create the env file with correct params', () => {
82 assert.fileContent(`${testPath}env/default.js`, 'host: \'test-host\'');
83 assert.fileContent(`${testPath}env/default.js`, 'xoscsrftoken: \'test-token\'');
84 assert.fileContent(`${testPath}env/default.js`, 'xossessionid: \'test-session\'');
85 });
86
87 it('should write username in package & bower json', () => {
88 assert.fileContent(`${testPath}package.json`, '"author": "Test User"');
89 assert.fileContent(`${testPath}bower.json`, '"Test User <test@mail.org>"')
90 });
91
92 it('should add all xosLib dependencies in the dev section of bower.json', () => {
93 bowerDeps.forEach(d => {
94 assert.fileContent(`${testPath}bower.json`, d);
Matteo Scandolo124bbfc2016-05-11 09:03:25 -070095 });
96 });
Matteo Scandolo07dd2762016-05-11 11:52:10 -070097
98 it('should set the right module name in all the files', () => {
99 assert.fileContent(`${testPath}src/index.html`, `ng-app="xos.${viewName}"`)
Matteo Scandoloce6624c2016-05-11 12:10:17 -0700100 assert.fileContent(`${testPath}src/index.html`, `id="xos${fileName}"`)
Matteo Scandolo07dd2762016-05-11 11:52:10 -0700101 assert.fileContent(`${testPath}src/js/main.js`, `angular.module('xos.${viewName}', [`)
Matteo Scandoloce6624c2016-05-11 12:10:17 -0700102 assert.fileContent(`${testPath}src/sass/main.scss`, `#xos${fileName}`)
Matteo Scandolo07dd2762016-05-11 11:52:10 -0700103 });
104
Matteo Scandoloce6624c2016-05-11 12:10:17 -0700105 it('should set correct paths in build file', () => {
106 assert.fileContent(`${testPath}gulp/build.js`, `angular.module('xos.${viewName}')`)
107 assert.fileContent(`${testPath}gulp/build.js`, `options.dashboards + 'xos${fileName}.html'`)
108 assert.fileContent(`${testPath}gulp/build.js`, `options.static + 'css/xos${fileName}.css'`)
109 assert.fileContent(`${testPath}gulp/build.js`, `.pipe(concat('xos${fileName}.css'))`)
110 assert.fileContent(`${testPath}gulp/build.js`, `.pipe(concat('xos${fileName}.js'))`)
111 assert.fileContent(`${testPath}gulp/build.js`, `module: 'xos.${viewName}'`)
112 assert.fileContent(`${testPath}gulp/build.js`, `options.static + 'js/vendor/xos${fileName}Vendor.js'`)
113 assert.fileContent(`${testPath}gulp/build.js`, `options.static + 'js/xos${fileName}.js'`)
114 assert.fileContent(`${testPath}gulp/build.js`, `options.static + 'css/xos${fileName}.css'`)
115 assert.fileContent(`${testPath}gulp/build.js`, `.pipe(concat('xos${fileName}Vendor.js'))`)
Matteo Scandolo07dd2762016-05-11 11:52:10 -0700116 });
117
118 after(done => {
119 // deleting the folder used for test
120 rimraf(testPath, {}, done)
121 });
Matteo Scandolo124bbfc2016-05-11 09:03:25 -0700122});