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'; |
| 21 | const testPath = path.join(__dirname, `../../../ngXosViews/${viewName}/`); |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 22 | |
| 23 | const getDefaultFiles = () => { |
| 24 | return [ |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame^] | 25 | '.bowerrc', |
| 26 | '.eslintrc', |
| 27 | '.gitignore', |
| 28 | 'bower.json', |
| 29 | 'gulpfile.js', |
| 30 | 'karma.conf.js', |
| 31 | 'package.json', |
| 32 | 'src/index.html', |
| 33 | ].map(i => `${testPath}${i}`); |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame^] | 36 | const yeomanUserMock = { |
| 37 | git: { |
| 38 | name: () => 'Test User', |
| 39 | email: () => 'test@mail.org' |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | mockery.enable({ |
| 44 | warnOnReplace: false, |
| 45 | warnOnUnregistered: false, |
| 46 | useCleanCache: true, |
| 47 | }); |
| 48 | mockery.resetCache(); |
| 49 | mockery.registerMock('../node_modules/yeoman-generator/lib/actions/user', yeomanUserMock); |
| 50 | |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 51 | describe('Yeoman XOS generator', function () { |
| 52 | |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame^] | 53 | beforeEach(() => { |
| 54 | }); |
| 55 | |
| 56 | before(done => { |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 57 | this.generator = helpers |
| 58 | .run(require.resolve('../app')) |
| 59 | .inDir(testPath) |
| 60 | .withOptions({ 'skip-install': true }) |
| 61 | .withPrompts({ |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame^] | 62 | name: viewName, |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 63 | host: 'test-host', |
| 64 | token: 'test-token', |
| 65 | session: 'test-session' |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame^] | 66 | }) |
| 67 | .on('end', done); |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 68 | }); |
| 69 | |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 70 | |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame^] | 71 | it('should generate base files in the correct directory', () => { |
| 72 | assert.file(getDefaultFiles()); |
| 73 | }); |
| 74 | |
| 75 | it('should create the env file with correct params', () => { |
| 76 | assert.fileContent(`${testPath}env/default.js`, 'host: \'test-host\''); |
| 77 | assert.fileContent(`${testPath}env/default.js`, 'xoscsrftoken: \'test-token\''); |
| 78 | assert.fileContent(`${testPath}env/default.js`, 'xossessionid: \'test-session\''); |
| 79 | }); |
| 80 | |
| 81 | it('should write username in package & bower json', () => { |
| 82 | assert.fileContent(`${testPath}package.json`, '"author": "Test User"'); |
| 83 | assert.fileContent(`${testPath}bower.json`, '"Test User <test@mail.org>"') |
| 84 | }); |
| 85 | |
| 86 | it('should add all xosLib dependencies in the dev section of bower.json', () => { |
| 87 | bowerDeps.forEach(d => { |
| 88 | assert.fileContent(`${testPath}bower.json`, d); |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 89 | }); |
| 90 | }); |
Matteo Scandolo | c9c9739 | 2016-05-11 11:52:10 -0700 | [diff] [blame^] | 91 | |
| 92 | it('should set the right module name in all the files', () => { |
| 93 | assert.fileContent(`${testPath}src/index.html`, `ng-app="xos.${viewName}"`) |
| 94 | assert.fileContent(`${testPath}src/index.html`, `id="xos${firstCharTouppercase(viewName)}"`) |
| 95 | assert.fileContent(`${testPath}src/js/main.js`, `angular.module('xos.${viewName}', [`) |
| 96 | assert.fileContent(`${testPath}src/sass/main.scss`, `#xos${firstCharTouppercase(viewName)}`) |
| 97 | }); |
| 98 | |
| 99 | xit('should create a correct gulp build file', () => { |
| 100 | |
| 101 | }); |
| 102 | |
| 103 | after(done => { |
| 104 | // deleting the folder used for test |
| 105 | rimraf(testPath, {}, done) |
| 106 | }); |
Matteo Scandolo | 41d9f72 | 2016-05-11 09:03:25 -0700 | [diff] [blame] | 107 | }); |