Skip to content

Commit

Permalink
More tests, also fixed #5
Browse files Browse the repository at this point in the history
Type check before value is changed
  • Loading branch information
remy committed May 4, 2015
1 parent 1370f80 commit bd8ad7e
Show file tree
Hide file tree
Showing 12 changed files with 557 additions and 237 deletions.
1 change: 1 addition & 0 deletions dist/bind.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 79 additions & 12 deletions karma.conf.js
@@ -1,20 +1,87 @@
module.exports = function(config) {
module.exports = function (config) {
config.set({
frameworks: ['mocha', 'browserify', 'sinon', 'chai'],
// base path, that will be used to resolve files and exclude
basePath: '.',

frameworks: ['mocha', 'browserify'],

// list of files / patterns to load in the browser
files: [
'*.js',
'bind.js',
'test/spec/*.js',
'test/index.js',
'lib/**/*.js',
'test/**/*.html',
'test/**/*.test.js',
'test/**/*.browser.js',
],

client: {
mocha: {
reporter: 'html', // change Karma's debug.html to the mocha web reporter
ui: 'tdd',
},
preprocessors: {
'test/**/*.html': ['html2js'],
'lib/**/*.js': 'coverage',
'lib/**/*.js': ['browserify'],
'test/**/*.test.js': ['browserify'],
'test/**/*.browser.js': ['browserify'],
},

browserify: {
debug: true,
// transform: ['browserify-istanbul'],
},

// use dots reporter, as travis terminal does not support escaping sequences
// possible values: 'dots', 'progress'
// CLI --reporters progress
reporters: ['mocha', 'coverage'],
coverageReporter: {
type: 'lcov',
dir: 'coverage'
},
// web server port
// CLI --port 9876
port: 9876,

// 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: true,

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

// 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-mocha',
'karma-mocha-reporter',
'karma-coverage',
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-html2js-preprocessor',
'karma-browserify',
],
});
};
};
13 changes: 8 additions & 5 deletions bind.js → lib/bind.js
Expand Up @@ -57,8 +57,8 @@ var Bind = (function Bind(global) {
}

// this is a conditional because we're also supporting node environment
var noop = function () {};
var $ = global.document ? document.querySelectorAll.bind(document) : noop;
var $;
try { $ = document.querySelectorAll.bind(document); } catch (e) {}
var array = [];
var o = 'object';

Expand All @@ -68,7 +68,7 @@ var Bind = (function Bind(global) {
for (var i = 0; i < length; i++) {
fn(subject[i], i, subject);
}
};
}

function AugmentedArray(callback) {
var methods = 'pop push reverse shift sort splice unshift'.split(' ');
Expand Down Expand Up @@ -117,7 +117,6 @@ var Bind = (function Bind(global) {
// cache the matched elements. Note the :) is because qSA won't allow an
// empty (or undefined) string so I like the smilie.
var elements = $(selector || '☺');

if (elements.length === 0) {
console.warn('No elements found against "' + selector + '" selector');
}
Expand Down Expand Up @@ -146,7 +145,11 @@ var Bind = (function Bind(global) {
// if (this.type === 'checkbox') {
// FIXME can't handle multiple checkbox values
// } else {
target[key] = this.value;
if (typeof target[key] === 'number') {
target[key] = this.value * 1; // parseFloat...basically
} else {
target[key] = this.value;
}
// }
});
}
Expand Down
19 changes: 15 additions & 4 deletions package.json
Expand Up @@ -2,12 +2,14 @@
"name": "bind.js",
"version": "0.1.2",
"description": "Simple data binding for callbacks & HTML (also node.js compatible).",
"main": "bind.js",
"main": "lib/bind.js",
"directories": {
"test": "tests"
},
"scripts": {
"test": "karma start"
"test": "karma start",
"build": "uglifyjs --mangle --compress -- lib/bind.js > dist/bind.min.js",
"test-node": "mocha test/*.test.js test/**/*.test.js"
},
"repository": {
"type": "git",
Expand All @@ -18,15 +20,24 @@
"readmeFilename": "README.md",
"gitHead": "59b398e1007d41a41d8fb73a659b9fc4ba4fa20a",
"devDependencies": {
"browserify": "^10.0.0",
"browserify-istanbul": "^0.2.1",
"browserify-shim": "^3.8.6",
"chai": "^2.3.0",
"karma": "^0.12.31",
"karma-browserify": "^4.1.2",
"karma-chai": "^0.1.0",
"karma-coverage": "^0.3.1",
"karma-chrome-launcher": "^0.1.8",
"karma-cli": "0.0.4",
"karma-commonjs": "0.0.13",
"karma-coverage": "^0.2.6",
"karma-firefox-launcher": "^0.1.4",
"karma-html2js-preprocessor": "^0.1.0",
"karma-mocha": "^0.1.10",
"karma-mocha-reporter": "^1.0.2",
"karma-sinon": "^1.0.4",
"mocha": "^2.2.4",
"sinon": "^1.14.1"
"sinon": "^1.14.1",
"uglify-js": "^2.4.20"
}
}
65 changes: 65 additions & 0 deletions test/callbacks.test.js
@@ -0,0 +1,65 @@
/*globals describe:true, assert: true, beforeEach: true, Bind:true, sinon:true, it:true */
var sinon = require('sinon');
var assert = require('assert');
var Bind = require('../');

describe('numbers', function () {
'use strict';
var data, spy, spy2, players;

beforeEach(function () {
spy = new sinon.spy();
spy2 = new sinon.spy();
players = new sinon.spy();

data = new Bind({
score: 10,
players: [{
name: 'Remy',
highscore: 300,
}, {
name: 'Julie',
highscore: 350,
},],
}, {
score: spy,
'players.1.highscore': spy2,
players: players,
});
});

it('should allow raw value to change', function () {
data.score = 11;
assert(data.score === 11);
data.score++;
assert(data.score === 12);
});

it('should trigger callback on change', function () {
var callCount = spy.callCount;
data.score = 11;
assert(spy.callCount === (callCount + 1));
});

it('should trigger callback after init', function () {
assert(spy.withArgs(10, undefined).calledOnce);
});

it('should pass callback new value and old', function () {
assert(spy.withArgs(10, undefined).calledOnce);

data.score = 11;
assert(spy.withArgs(11, 10).calledOnce);
});

it('should trigger on deep object value change', function () {
data.players[1].highscore++;
assert(spy2.callCount === 2);
});

it('should trigger on array item change', function () {
var count = players.callCount; // because it's called on initial setup
data.players.push({ name: 'Ellis', highscore: 50 });
assert(players.callCount === count + 1, 'new callcount: ' + players.callCount);
});
});
29 changes: 29 additions & 0 deletions test/export.test.js
@@ -0,0 +1,29 @@
'use strict';
var sinon = require('sinon');
var assert = require('assert');
var Bind = require('../');

/*globals describe, assert, beforeEach, Bind, sinon, it */
describe('export', function () {
var data;

it('returns vanilla object', function () {
var spy = sinon.spy();
var o = {
name: 'remy',
complex: {
age: 20,
location: 'house',
},
};
var data = Bind(o, {
name: spy,
});

assert.ok(spy.calledWith('remy'), 'initial data set');
assert.ok(JSON.stringify(o) === JSON.stringify(data.__export()),
'export returns same object');
});


});
33 changes: 33 additions & 0 deletions test/html.js
@@ -0,0 +1,33 @@
(function () {
var ready = false;
var sandbox = null;

function setup() {
destroy();
sandbox.style.visibilty = 'hidden';
sandbox.style.position = 'absolute';
sandbox.style.top = '-999999px';
sandbox.style.zIndex = '-1';
document.body.appendChild(sandbox);
ready = true;
}


function insert(html) {
setup();
sandbox.innerHTML = html;
}

function destroy() {
if (sandbox) {
sandbox.innerHTML = '';
sandbox.parentNode.removeChild(sandbox);
}
sandbox = document.createElement('div');
ready = false;
};

if (typeof exports !== 'undefined') {
module.exports = insert;
}
})();

0 comments on commit bd8ad7e

Please sign in to comment.