Skip to content

Commit

Permalink
feat(karma): add support for karma and a basic test
Browse files Browse the repository at this point in the history
  • Loading branch information
adamayres committed Jun 17, 2014
1 parent 85bb266 commit b2c390f
Show file tree
Hide file tree
Showing 7 changed files with 359 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.idea

coverage
test-reports
node_modules
bower_components
102 changes: 102 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
"asi" : false,
"bitwise" : false,
"boss" : true,
"browser" : true,
"camelcase" : false,
"couch" : false,
"curly" : false,
"debug" : true,
"devel" : true,
"dojo" : false,
"eqeqeq" : true,
"eqnull" : true,
"esnext" : false,
"evil" : true,
"expr" : true,
"forin" : true,
"funcscope" : false,
"globalstrict" : true,
"immed" : true,
"indent" : 4,
"iterator" : false,
"jquery" : true,
"lastsemic" : true,
"latedef" : true,
"laxbreak" : false,
"laxcomma" : false,
"loopfunc" : false,
"maxcomplexity" : 10,
"maxdepth" : 4,
"maxerr" : 100,
"maxlen" : 120,
"maxstatements" : 35,
"mootools" : false,
"multistr" : false,
"newcap" : true,
"noarg" : true,
"node" : true,
"noempty" : true,
"nomen" : false,
"nonstandard" : false,
"onecase" : false,
"onevar" : false,
"passfail" : false,
"plusplus" : false,
"predef" : [
"__dirname",
"element",
"browser",
"require",
"jasmine",
"describe",
"xdescribe",
"it",
"iit",
"angular",
"inject",
"xit",
"beforeEach",
"afterEach",
"expect",
"input",
"pause",
"spyOn",
"runs",
"waits",
"waitsFor",
"Benchmark",
"Raphael",
"Backbone",
"Modernizr",
"Handlebars",
"Ext",
"_gaq",
"module",
"exports",
"define",
"$",
"jQuery",
"grunt",
"phantom",
"WebPage"
],
"proto" : false,
"prototypejs" : false,
"quotmark" : "single",
"regexdash" : false,
"regexp" : false,
"rhino" : false,
"scripturl" : false,
"shadow" : false,
"smarttabs" : false,
"strict" : true,
"sub" : false,
"supernew" : false,
"trailing" : true,
"undef" : true,
"unused" : true,
"validthis" : false,
"white" : false,
"wsh" : false
}
6 changes: 5 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@
"bower_components",
"test",
"tests"
]
],
"devDependencies": {
"angular": "~1.2.18",
"angular-mocks": "~1.2.18"
}
}
69 changes: 69 additions & 0 deletions dist/js/mentio.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

describe('mentio-menu directive', function () {
var $compile, $rootScope;

beforeEach(module('mentio'));

beforeEach(inject(function (_$compile_, _$rootScope_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
}));

it('should search on items', function () {
var element, $renderScope, $scope, searchSpy, mockItems;

mockItems = [
{
id: 1
},
{
id: 2
}
];

$renderScope = $rootScope.$new();

$renderScope.search = function (term) {
if (term === 'foo') {
$renderScope.items = mockItems;
} else {
$renderScope.items = [];
}
};

searchSpy = spyOn($renderScope, 'search');
searchSpy.andCallThrough();

element = $compile('<div mentio-menu bind="content" items="items"' +
'search="search(term)" select="select(item)" ng-model="menuContent">')($renderScope);

$renderScope.$digest();

$scope = element.isolateScope();

// TODO: move getAtMentionInfo() into a service so it can be mocked
$scope.atVar = 'foo';
expect($scope.hide).toBeTruthy();
$scope.query();

$renderScope.$digest();

expect(searchSpy).toHaveBeenCalledWith('foo');
expect($scope.items).toEqual(mockItems);
expect($scope.hide).toBeFalsy();


/*
* The menu should not display when the search returns no items
*/
$scope.atVar = 'bar';
$scope.query();

$renderScope.$digest();

expect(searchSpy.callCount).toBe(2);
expect($scope.items).toEqual([]);
expect($scope.hide).toBeTruthy();
});
});
78 changes: 76 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

var gulp = require('gulp');
var gutil = require('gulp-util');
var fs = require('fs');

var port = gutil.env.port || 3000;
var covport = gutil.env.covport || 3001;
var lrport = gutil.env.lrport || 35729;
var browser = gutil.env.browser;
var openBrowser = gutil.env.browser;

/*
* Default task is to start the examples
Expand All @@ -31,8 +33,80 @@ gulp.task('examples', function () {
});

// open the browser
require('open')('http://localhost:' + port, browser);
require('open')('http://localhost:' + port, openBrowser);

console.log('Example app started on port [%s]', port);
});
});


function testTask (params) {
var karma = require('gulp-karma');

var karmaConfig = {
configFile: './karma.conf.js',
action: params.isWatch ? 'watch' : 'run'
};

if (params.coverageReporter) {
karmaConfig.coverageReporter = params.coverageReporter;
}

if (params.reporters) {
karmaConfig.reporters = params.reporters;
}

return gulp.src('DO_NOT_MATCH') //use the files in the karma.conf.js
.pipe(karma(karmaConfig));
}

/**
* Run the karma spec tests
*/
gulp.task('test', function () {
testTask({
isWatch: gutil.env.hasOwnProperty('watch')
});
});

gulp.task('coverage', function () {
var express = require('express');
var app = express();
var coverageFile;
var karmaHtmlFile;

function getTestFile (path) {
if (fs.existsSync(path)) {
var files = fs.readdirSync(path);

if (files) {
for (var i = 0; i < files.length; i++) {
if (fs.lstatSync(path + '/' + files[i]).isDirectory()) {
return files[i];
} else {
return files[i];
}
}
}
}
}

testTask({
isWatch: gutil.env.hasOwnProperty('watch'),
reporters: ['progress', 'coverage', 'threshold']
});

setTimeout(function () {
coverageFile = getTestFile('coverage');
karmaHtmlFile = getTestFile('karma_html');

app.use(express.static('./'));

app.listen(covport, function openPage () {
if (coverageFile) {
require('open')('http://localhost:' + covport + '/coverage/' + coverageFile);
}
});
}, 3000);

});
94 changes: 94 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
'use strict';

module.exports = function(config) {
config.set({
frameworks: ['jasmine'],

// list of files / patterns to load in the browser
files: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'./dist/**/*.js'
],

// list of files to exclude
exclude: [],

preprocessors: {
'./dist/**/!(*.spec).js': 'coverage'
},

// use dots reporter, as travis terminal does not support escaping sequences
// possible values: 'dots', 'progress'
// CLI --reporters progress
reporters: ['junit','progress','coverage','threshold'],

junitReporter: {
outputFile: 'test-reports/junit.xml',
suite: 'ment.io'
},

thresholdReporter: {
statements: 0,
branches: 0,
functions: 0,
lines: 0
},

coverageReporter: {
type : 'html',
dir : 'coverage/'
},

// web server port
// CLI --port 9876
port: 8085,

// enable / disable colors in the output (reporters and logs)
// CLI --colors --no-colors
colors: true,

// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
// CLI --log-level debug
logLevel: config.LOG_INFO,

// enable / disable watching file and executing tests whenever any file changes
// CLI --auto-watch --no-auto-watch
autoWatch: false,

// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
// CLI --browsers Chrome,Firefox,Safari
browsers: ['PhantomJS'],

// If browser does not capture in given timeout [ms], kill it
// CLI --capture-timeout 5000
captureTimeout: 20000,

// Auto run tests on start (when browsers are captured) and exit
// CLI --single-run --no-single-run
singleRun: true,

// report which specs are slower than 500ms
// CLI --report-slower-than 500
reportSlowerThan: 500,

plugins: [
'karma-jasmine',
'karma-coverage',
'karma-phantomjs-launcher',
'karma-chrome-launcher',
'karma-junit-reporter',
'karma-threshold-reporter',
'karma-chrome-launcher'
]
});
};

Loading

0 comments on commit b2c390f

Please sign in to comment.