Skip to content

Commit

Permalink
Merge pull request #9 from DomoApps/feat/DAS-4146-test-framework-rest…
Browse files Browse the repository at this point in the history
…ructuring

Feat/das 4146 test framework restructuring
  • Loading branch information
jpumford authored Aug 17, 2016
2 parents 909a58a + b8d7fef commit ad38dfa
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 61 deletions.
69 changes: 26 additions & 43 deletions other/karma.conf.es6.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,46 @@
const path = require('path');
module.exports = config => {
// load external cdn dependencies
const pkg = require('../package.json');
const cdns = Object.values(pkg.cdnDependencies);

// load webpack config here for for webpack preprocessor
const webpackConfig = require('../webpack.config');
delete webpackConfig.devtool;
webpackConfig.cache = true;
// entry file that bundles all the test files
const testEntryFile = './other/tests.js';

let file;
const webpackConfig = require('../webpack.config');
const webpackLoaders = webpackConfig.module.loaders;

const cdns = Object.values(require('../package.json').cdnDependencies);

const entry = [
...cdns,
'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular-mocks.js',
];
const preprocessors = {};
for (const chunk in webpackConfig.entry) {
if ({}.hasOwnProperty.call(webpackConfig.entry, chunk)) {
file = path.resolve(webpackConfig.context, webpackConfig.entry[chunk]);
entry.push(file);
preprocessors[file] = ['webpack'];
}
}

module.exports = (config) => {
config.set({
basePath: './',
frameworks: ['mocha', 'chai', 'sinon'],
files: entry,
webpack: webpackConfig,

files: [
...cdns,
testEntryFile
],
preprocessors: {
[testEntryFile]: ['webpack', 'sourcemap']
},
webpack: {
module: {
loaders: webpackLoaders
},
devtool: 'inline-source-map'
},
webpackMiddleware: {
noInfo: true
},

// list of files to exclude
exclude: [
'src/switcher.js'
],

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: preprocessors,

// how the test success/failure status is reported:
reporters: ['dots'],
port: 9876,
colors: true,
autoWatch: true,
logLevel: config.LOG_ERROR,
browsers: ['PhantomJS', 'Chrome', 'Firefox', 'Safari'],
plugins: [
require('karma-webpack'),
'karma-coverage',
'karma-phantomjs-launcher',
'karma-sourcemap-loader',
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-safari-launcher',
'karma-mocha',
'karma-chai',
'karma-sinon',
],
logLevel: config.LOG_ERROR
'karma-webpack'
]
});
};
4 changes: 4 additions & 0 deletions other/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require('angular-mocks/ngMock');
const testFiles = require.context('../src', true, /\.spec\.js$/);
const ngModule = angular.module('da.test', []);
testFiles.keys().forEach(key => { testFiles(key)(ngModule); });
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"repository": "https://github.com/DomoApps/starter-kit",
"license": "SEE LICENSE IN LICENSE",
"devDependencies": {
"@domoinc/da-plop": "^3.0.0",
"@domoinc/da-plop": "^4.0.1",
"angular-mocks": "^1.5.8",
"autoprefixer": "^6.1.2",
"babel-core": "^6.3.17",
"babel-loader": "^6.2.0",
Expand Down Expand Up @@ -65,6 +66,7 @@
"karma-phantomjs-launcher": "^1.0.0",
"karma-safari-launcher": "^0.1.1",
"karma-sinon": "^1.0.4",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^1.7.0",
"lcov-filter": "0.1.1",
"lodash.kebabcase": "^3.1.1",
Expand Down
4 changes: 0 additions & 4 deletions src/common/services/da-events/da-events.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,5 @@ module.exports = ngModule => {

ngModule.factory('daEvents', daEvents);

if (ON_TEST) {
require('./da-events.factory.spec.js')(ngModule);
}

return ngModule;
};
30 changes: 17 additions & 13 deletions src/common/services/da-events/da-events.factory.spec.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
/**
* Here you can write tests for you service
* @param {Angular Module} ngModule The module with the service
*/
module.exports = ngModule => {
const factory = require('./da-events.factory.js');

//console.log(ngModule.name);
factory(ngModule);

describe('factory:daEvents', () => {
let daEvents;
let callbackSpy;

beforeEach(window.module(ngModule.name));

beforeEach(inject(_daEvents_ => {
daEvents = _daEvents_;
}));
beforeEach(() => {
callbackSpy = sinon.spy();
});

it('should exist emit registered events', () => {
const spy = sinon.spy();
daEvents.on('app:loaded', spy);
it('should emit registered events', () => {
callbackSpy = sinon.spy();
daEvents.on('app:loaded', callbackSpy);
daEvents.trigger('app:loaded');
expect(spy.calledOnce).to.equal(true);
expect(callbackSpy.calledOnce).to.equal(true);
});

it('should not allow a listener to be setup for event that is not in registry', () => {
const spy = sinon.spy();
daEvents.on('not:in:registry', spy);
daEvents.trigger('not:in:registry');
expect(spy.calledOnce).to.equal(false);
callbackSpy = sinon.spy();
expect(daEvents.on('not:in:registry', callbackSpy)).to.equal(null);
expect(daEvents.trigger('not:in:registry')).to.equal(null);
expect(callbackSpy.calledOnce).to.equal(false);
});
});
};

0 comments on commit ad38dfa

Please sign in to comment.