blob: dd49b03994528d5afd71e411a01be40deac3b32a [file] [log] [blame]
Matteo Scandolob8c76b12016-05-11 14:53:20 -07001'use strict';
2var path = require('path');
3var helpers = require('yeoman-test');
4var assert = require('yeoman-assert');
5var exec = require('child_process').exec;
6var fs = require('fs');
7const rimraf = require('rimraf');
8
9const getMillisec = min => min * 60 * 1000;
10const deleteFile = file => {
11 if(fs.existsSync(file)){
12 // console.log(`deleting: ${file}`);
13 fs.unlinkSync(file);
14 }
15}
16
17// source files
18const viewName = 'testDashboard';
19const fileName = viewName.replace(/^./, viewName[0].toUpperCase());
20const sourcePath = path.join(__dirname, `../../../ngXosViews/${viewName}/`);
21
22// dest files
23const basePath = '../../../../xos/core/xoslib';
24const destHtml = path.join(__dirname, basePath + '/dashboards/xosTestDashboard.html');
25const destJs = path.join(__dirname, basePath + '/static/js/xosTestDashboard.js');
26const destVendor = path.join(__dirname, basePath + '/static/js/vendor/xosTestDashboardVendor.js');
27const destCss = path.join(__dirname, basePath + '/static/css/xosTestDashboard.css');
28
29describe('The XOS Build script', function(){
30 const buildCmd = 'gulp build';
31
32 this.timeout(getMillisec(5));
33
34 before(done => {
35 console.log('Running generator');
36 this.generator = helpers
37 .run(require.resolve('../app'))
38 .inDir(sourcePath)
39 .withOptions({ 'skip-install': false })
40 .withPrompts({
41 name: viewName,
42 host: 'test-host',
43 token: 'test-token',
44 session: 'test-session'
45 })
46 .on('end', () => {
47 process.stdout.write('Installing Node Modules');
48 let npmInstall = setInterval(() => {
49 process.stdout.write('.');
50 }, 1000);
51 exec('npm install', {
52 cwd: sourcePath
53 }, (err) => {
54 clearInterval(npmInstall);
55 process.stdout.write('\nInstalling Bower Components');
56 let bowerInstall = setInterval(() => {
57 process.stdout.write('.');
58 }, 1000);
59 exec('bower install', {
60 cwd: sourcePath
61 }, (err) => {
62 clearInterval(bowerInstall);
63 done(err);
64 });
65 });
66 });
67 });
68
69 describe('when no styles or vendors are added', () => {
70
71 before((done) => {
72 process.stdout.write('\nBuilding App');
73 let appBuild = setInterval(() => {
74 process.stdout.write('.');
75 }, 1000);
76 exec(buildCmd, {
77 cwd: sourcePath
78 }, (err) => {
79 console.log(err);
80 clearInterval(appBuild);
81 done(err);
82 });
83 });
84
85 it('should have build the app', () => {
86 assert.file([destHtml, destJs]);
87 });
88
89 it('should include only minified files in the index', () => {
90 assert.fileContent(destHtml, `<script src="/static/js/xos${fileName}.js"></script>`);
91 assert.noFileContent(destHtml, `<!-- bower:css -->`);
92 assert.noFileContent(destHtml, `<!-- bower:js -->`);
93 });
94 });
95
96 describe('when a third party library is added', () => {
97 before((done) => {
98 process.stdout.write('\nInstalling 3rd party library');
99 let bowerInstall = setInterval(() => {
100 process.stdout.write('.');
101 }, 1000);
102 exec('bower install d3 --save', {
103 cwd: sourcePath
104 }, (err, out) => {
105 clearInterval(bowerInstall);
106 process.stdout.write('\nBuilding App');
107 let appBuild = setInterval(() => {
108 process.stdout.write('.');
109 }, 1000);
110 exec(buildCmd, {
111 cwd: sourcePath
112 }, (err) => {
113 console.log(err);
114 clearInterval(appBuild);
115 done(err);
116 });
117 });
118 });
119
120 it('should have build the app with a vendor file', () => {
121 assert.file([destHtml, destJs, destVendor]);
122 });
123
124 it('should include only minified files and minified deps in the index', () => {
125 assert.fileContent(destHtml, `<script src="/static/js/xos${fileName}.js"></script>`);
126 assert.fileContent(destHtml, `<script src="/static/js/vendor/xos${fileName}Vendor.js"></script>`);
127 assert.noFileContent(destHtml, `<!-- bower:css -->`);
128 assert.noFileContent(destHtml, `<!-- bower:js -->`);
129 });
130 });
131
132 describe('when some styles are added', () => {
133 before((done) => {
134 let styleContent = `
135 @import '../../../../style/sass/lib/_variables.scss';
136
137 #xosTestDashboard {
138 background: $brand-primary;
139 }
140 `;
141
142 fs.writeFile(`${sourcePath}src/sass/main.scss`, styleContent, function(err) {
143 process.stdout.write('\nBuilding the Application');
144 let appBuild = setInterval(() => {
145 process.stdout.write('.');
146 }, 1000);
147 exec('bower uninstall d3 --save', {
148 cwd: sourcePath
149 }, (err, out) => {
150 exec(buildCmd, {
151 cwd: sourcePath
152 }, (err, out) => {
153 clearInterval(appBuild);
154 done();
155 })
156 })
157 });
158 });
159
160 it('should have build the app with a css file', () => {
161 assert.file([destHtml, destJs, destCss]);
162 });
163
164 it('should include only minified files and minified deps in the index', () => {
165 assert.fileContent(destHtml, `<script src="/static/js/xos${fileName}.js"></script>`);
166 assert.fileContent(destHtml, `<link rel="stylesheet" href="/static/css/xos${fileName}.css">`);
167 assert.noFileContent(destHtml, `<!-- bower:css -->`);
168 assert.noFileContent(destHtml, `<!-- bower:js -->`);
169
170 assert.fileContent(destCss, `background:#337ab7`);
171 });
172 });
173
174 after(done => {
175 // deleting the folder used for test
176 deleteFile(destHtml);
177 deleteFile(destJs);
178 deleteFile(destVendor);
179 deleteFile(destCss);
180 rimraf(sourcePath, {}, done);
181 });
182});