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 | }); |
| 17 | bowerDeps = bowerDeps.js.map(d => d.match(/bower_components\/([a-zA-Z\-`.]+)\//)[1]); |
| 18 | |
| 19 | // test values |
| 20 | const viewName = 'testDashboard'; |
Matteo Scandolo | 4ec7c18 | 2016-05-11 12:10:17 -0700 | [diff] [blame] | 21 | const fileName = firstCharTouppercase(viewName); |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 22 | const testPath = path.join(__dirname, `../../../ngXosViews/${viewName}/`); |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 23 | |
| 24 | const getDefaultFiles = () => { |
| 25 | return [ |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 26 | '.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 Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 37 | const yeomanUserMock = { |
| 38 | git: { |
| 39 | name: () => 'Test User', |
| 40 | email: () => 'test@mail.org' |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | mockery.enable({ |
| 45 | warnOnReplace: false, |
| 46 | warnOnUnregistered: false, |
| 47 | useCleanCache: true, |
| 48 | }); |
| 49 | mockery.resetCache(); |
| 50 | mockery.registerMock('../node_modules/yeoman-generator/lib/actions/user', yeomanUserMock); |
| 51 | |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 52 | describe('Yeoman XOS generator', function () { |
| 53 | |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 54 | beforeEach(() => { |
| 55 | }); |
| 56 | |
| 57 | before(done => { |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 58 | this.generator = helpers |
| 59 | .run(require.resolve('../app')) |
| 60 | .inDir(testPath) |
| 61 | .withOptions({ 'skip-install': true }) |
| 62 | .withPrompts({ |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 63 | name: viewName, |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 64 | host: 'test-host', |
| 65 | token: 'test-token', |
| 66 | session: 'test-session' |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 67 | }) |
| 68 | .on('end', done); |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 69 | }); |
| 70 | |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 71 | |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 72 | 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 Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 90 | }); |
| 91 | }); |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 92 | |
| 93 | it('should set the right module name in all the files', () => { |
| 94 | assert.fileContent(`${testPath}src/index.html`, `ng-app="xos.${viewName}"`) |
Matteo Scandolo | 4ec7c18 | 2016-05-11 12:10:17 -0700 | [diff] [blame] | 95 | assert.fileContent(`${testPath}src/index.html`, `id="xos${fileName}"`) |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 96 | assert.fileContent(`${testPath}src/js/main.js`, `angular.module('xos.${viewName}', [`) |
Matteo Scandolo | 4ec7c18 | 2016-05-11 12:10:17 -0700 | [diff] [blame] | 97 | assert.fileContent(`${testPath}src/sass/main.scss`, `#xos${fileName}`) |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 98 | }); |
| 99 | |
Matteo Scandolo | 4ec7c18 | 2016-05-11 12:10:17 -0700 | [diff] [blame] | 100 | 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 Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame] | 111 | }); |
| 112 | |
| 113 | after(done => { |
| 114 | // deleting the folder used for test |
| 115 | rimraf(testPath, {}, done) |
| 116 | }); |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 117 | }); |