Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | const path = require('path'); |
| 4 | const helpers = require('yeoman-test'); |
| 5 | const assert = require('yeoman-assert'); |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 6 | const rimraf = require('rimraf'); |
| 7 | const mockery = require('mockery'); |
| 8 | const wiredep = require('wiredep'); |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 9 | |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 10 | const firstCharTouppercase = string => string.replace(/^./, string[0].toUpperCase()) |
| 11 | |
| 12 | // get bower deps installed in ngXosLib |
| 13 | let bowerDeps = wiredep({ |
| 14 | cwd: path.join(__dirname, '../../'), // pretending to be in the ngXosLib root |
| 15 | exclude: ['Chart.js'] |
| 16 | }); |
Matteo Scandolo | f2ec12b | 2016-05-16 10:31:32 -0700 | [diff] [blame] | 17 | bowerDeps = 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 Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 23 | |
| 24 | // test values |
| 25 | const viewName = 'testDashboard'; |
Matteo Scandolo | 4ec7c18 | 2016-05-11 12:10:17 -0700 | [diff] [blame] | 26 | const fileName = firstCharTouppercase(viewName); |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 27 | const testPath = path.join(__dirname, `../../../ngXosViews/${viewName}/`); |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 28 | |
| 29 | const getDefaultFiles = () => { |
| 30 | return [ |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 31 | '.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 Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 42 | const yeomanUserMock = { |
| 43 | git: { |
| 44 | name: () => 'Test User', |
| 45 | email: () => 'test@mail.org' |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | mockery.enable({ |
| 50 | warnOnReplace: false, |
| 51 | warnOnUnregistered: false, |
| 52 | useCleanCache: true, |
| 53 | }); |
| 54 | mockery.resetCache(); |
| 55 | mockery.registerMock('../node_modules/yeoman-generator/lib/actions/user', yeomanUserMock); |
| 56 | |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 57 | describe('Yeoman XOS generator', function () { |
| 58 | |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 59 | beforeEach(() => { |
| 60 | }); |
| 61 | |
| 62 | before(done => { |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 63 | this.generator = helpers |
| 64 | .run(require.resolve('../app')) |
| 65 | .inDir(testPath) |
| 66 | .withOptions({ 'skip-install': true }) |
| 67 | .withPrompts({ |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 68 | name: viewName, |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 69 | host: 'test-host', |
| 70 | token: 'test-token', |
| 71 | session: 'test-session' |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 72 | }) |
| 73 | .on('end', done); |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 74 | }); |
| 75 | |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 76 | |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 77 | 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 Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 95 | }); |
| 96 | }); |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 97 | |
| 98 | it('should set the right module name in all the files', () => { |
| 99 | assert.fileContent(`${testPath}src/index.html`, `ng-app="xos.${viewName}"`) |
Matteo Scandolo | 4ec7c18 | 2016-05-11 12:10:17 -0700 | [diff] [blame] | 100 | assert.fileContent(`${testPath}src/index.html`, `id="xos${fileName}"`) |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 101 | assert.fileContent(`${testPath}src/js/main.js`, `angular.module('xos.${viewName}', [`) |
Matteo Scandolo | 4ec7c18 | 2016-05-11 12:10:17 -0700 | [diff] [blame] | 102 | assert.fileContent(`${testPath}src/sass/main.scss`, `#xos${fileName}`) |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 103 | }); |
| 104 | |
Matteo Scandolo | 4ec7c18 | 2016-05-11 12:10:17 -0700 | [diff] [blame] | 105 | 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 Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 116 | }); |
| 117 | |
| 118 | after(done => { |
| 119 | // deleting the folder used for test |
| 120 | rimraf(testPath, {}, done) |
| 121 | }); |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 122 | }); |