Skip to content

Commit

Permalink
Add gulp and karma
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Kirda committed Apr 29, 2015
1 parent 40a8bfd commit 4ba392a
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 29 deletions.
74 changes: 74 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"excludeFiles": [
"node_modules/**",
"bower_components/**"
],

"requireCurlyBraces": [
"if",
"else",
"for",
"while",
"do",
"try",
"catch"
],
"requireOperatorBeforeLineBreak": true,
"requireCamelCaseOrUpperCaseIdentifiers": true,
"maximumLineLength": {
"value": 200,
"allowComments": true,
"allowRegex": true
},
"validateIndentation": 4,
"validateQuoteMarks": "'",

"disallowMultipleLineStrings": true,
"disallowMixedSpacesAndTabs": true,
"disallowTrailingWhitespace": true,
"disallowSpaceAfterPrefixUnaryOperators": true,
"disallowMultipleVarDecl": null,

"requireSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"return",
"try",
"catch"
],
"requireSpaceBeforeBinaryOperators": [
"=", "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=",
"&=", "|=", "^=", "+=",

"+", "-", "*", "/", "%", "<<", ">>", ">>>", "&",
"|", "^", "&&", "||", "===", "==", ">=",
"<=", "<", ">", "!=", "!=="
],
"requireSpaceAfterBinaryOperators": true,
"requireSpacesInConditionalExpression": true,
"requireSpaceBeforeBlockStatements": true,
"requireLineFeedAtFileEnd": true,
"disallowSpacesInsideObjectBrackets": "all",
"disallowSpacesInsideArrayBrackets": "all",
"disallowSpacesInsideParentheses": true,

"validateJSDoc": {
"checkParamNames": true,
"requireParamTypes": true
},

"disallowMultipleLineBreaks": true,

"disallowCommaBeforeLineBreak": null,
"disallowDanglingUnderscores": null,
"disallowEmptyBlocks": null,
"disallowTrailingComma": null,
"requireCommaBeforeLineBreak": null,
"requireDotNotation": null,
"requireMultipleVarDecl": null,
"requireParenthesesAroundIIFE": true
}
29 changes: 29 additions & 0 deletions gulp.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

module.exports = function () {
var rootDir = __dirname;

return {
rootDir: rootDir,
karma: {
default: {
configFile: rootDir + '/karma.config.js'
},
coverage: {
coverageReporter: {
dir: rootDir + '/coverage',
reporters: [
{ type: 'html', subdir: 'coverage' },
{ type: 'text-summary' }
]
},
preprocessors: {
'src/*.js': ['coverage']
},
reporters: ['mocha', 'coverage']
},
tdd: {
singleRun: false
}
}
};
};
71 changes: 71 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict';

// Gulp configuration:
var config = require('./gulp.config')();

// Imports:
var gulp = require('gulp');
var util = require('gulp-util');
var karma = require('karma').server;
var _ = require('lodash');

// Tasks:
gulp.task('tdd', tddTask);
gulp.task('karma', karmaTask);
gulp.task('coverage', coverageTask);

/**
* Test driven development task.
*/
function coverageTask(done) {
log('Starting a continuous "watch and test" cycle');

startKarma('coverage', done);
}

/**
* Test driven development task.
*/
function tddTask(done) {
log('Starting a continuous "watch and test" cycle');

startKarma('tdd', done);
}

/**
* Runs karma with default configuration.
*/
function karmaTask(done) {
startKarma('x', done);
}

/**
* Runs karma with given configuration type
*
* @param {string} configType
* @param callback
*/
function startKarma(configType, callback) {
var options = _.merge(
config.karma.default,
config.karma[configType] || {}
);

karma.start(options, callback);
}

/**
* Log a message or series of messages using chalk's blue color.
* Can pass in a string, object or array.
*/
function log(msg) {
if (typeof(msg) === 'object') {
for (var item in msg) {
if (msg.hasOwnProperty(item)) {
util.log($.util.colors.blue(msg[item]));
}
}
} else {
util.log(util.colors.blue(msg));
}
}
51 changes: 51 additions & 0 deletions karma.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: './',

// frameworks to use
// some available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine-ajax', 'jasmine'],

// list of files / patterns to load in the browser
files: [
'src/devbridge-autocomplete.js',
'tests/spec/*.js'
],

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

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

// test results reporter to use
// possible values: 'dots', 'progress', 'coverage'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['mocha'],

// web server port
port: 9876,

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

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

// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,

// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
// browsers: ['Chrome', 'ChromeCanary', 'FirefoxAurora', 'Safari', 'PhantomJS'],
browsers: ['PhantomJS'],

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true
});
};
60 changes: 33 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
{
"name": "devbridge-vanilla-autocomplete",
"version": "1.0.0",
"description": "Flexible JavaScript library for implementing Autocomplete features.",
"main": "dist/devbridge-vanilla-autocomplete.js",
"scripts": {
"test": "./node_modules/.bin/karma start karma-conf.js"
},
"keywords": [
"devbridge",
"autocomplete"
],
"author": "Tomas Kirda",
"license": "MIT",
"repository": {
"type": "git",
"url": "git://github.com/devbridge/Vanilla-Autocomplete.git"
},
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-uglify": "^0.8.0",
"karma": "^0.12.31",
"karma-coverage": "~0.1.0",
"karma-jasmine": "^0.3.3",
"karma-jasmine-ajax": "^0.1.12",
"karma-phantomjs-launcher": "^0.1.4"
}
}
"name": "devbridge-vanilla-autocomplete",
"version": "1.0.0",
"description": "Flexible JavaScript library for implementing Autocomplete features.",
"main": "dist/devbridge-vanilla-autocomplete.js",
"scripts": {
"test": "./node_modules/.bin/karma start karma-conf.js"
},
"keywords": [
"devbridge",
"autocomplete"
],
"author": "Tomas Kirda",
"license": "MIT",
"repository": {
"type": "git",
"url": "git://github.com/devbridge/Vanilla-Autocomplete.git"
},
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-uglify": "^0.8.0",
"gulp": "^3.8.11",
"gulp-uglify": "^1.1.0",
"karma": "^0.12.31",
"karma-coverage": "~0.1.0",
"karma-jasmine": "^0.3.3",
"karma-jasmine-ajax": "^0.1.12",
"karma-mocha": "^0.1.10",
"karma-mocha-reporter": "^1.0.2",
"karma-phantomjs-launcher": "^0.1.4",
"lodash": "^3.6.0",
"mocha": "^2.2.1"
}
}
50 changes: 48 additions & 2 deletions tests/spec/autocomplete-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,13 @@ describe("Vanilla Autocomplete", function () {
});

describe("By default", function () {
var instance, input, selectedSuggestion;
var instance;
var input;
var selectedSuggestion;

beforeEach(function () {
jasmine.Ajax.install();

selectedSuggestion = null;
input = document.createElement('input');

Expand All @@ -225,6 +229,7 @@ describe("Vanilla Autocomplete", function () {

}
};

instance = new VanillaAutocomplete(input, options);
});

Expand All @@ -233,6 +238,47 @@ describe("Vanilla Autocomplete", function () {
input.parentNode.removeChild(input);
});

it ("#isBadQuery() should always return false when preventBadQueries is set to false", function () {
instance.setOptions({
preventBadQueries: false,
lookup: null,
serviceUrl: '/test'
});

var query = 'BadValue';

// Do it twice:
instance.changeValue(query);

query = query + 'X';
instance.changeValue(query);

expect(instance.isBadQuery(query)).toEqual(false);
});

it ("#isBadQuery() should always true when preventBadQueries is set to true", function () {
instance.setOptions({
preventBadQueries: true,
lookup: null,
serviceUrl: '/test'
});

var query = 'BadValue';

// Do it twice:
instance.changeValue(query);

jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
responseText: '{ "suggestions": [] }'
});

query = query + 'X';
instance.changeValue(query);

expect(instance.isBadQuery(query)).toEqual(true);
});

it ("should set autocomplete attribute to 'off'", function () {
expect(input.getAttribute('autocomplete')).toEqual('off');
});
Expand Down Expand Up @@ -773,7 +819,7 @@ describe("Vanilla Autocomplete", function () {
document.body.appendChild(input);

var options = {
lookup: ['AA1', 'AA2', 'BB1', 'BB2'],
lookup: ['AA1', 'AA2', 'BB1', 'BB2']
};

instance = new VanillaAutocomplete(input, options);
Expand Down

0 comments on commit 4ba392a

Please sign in to comment.