Skip to content

Commit

Permalink
Merge 69026bc into 2c7f9fd
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp committed Jan 13, 2018
2 parents 2c7f9fd + 69026bc commit 825fef4
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ coverage.html
npm-debug.log
index-cov.js
.DS_Store
package-lock.json
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"browser": true,
"node":true,
"expr": true,
"eqnull": true,
"laxcomma": true,
"-W079": true,
"-W014": true,
Expand Down
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
language: node_js
node_js: ["0.10"]
node_js:
- 0.10
- node
before_script:
- npm run engine-deps
after_success:
- ./node_modules/.bin/jscoverage index.js index-cov.js
- PAGE_COV=1 ./node_modules/.bin/mocha test/tests.js -R mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js
Expand Down
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,6 @@
*/

function onclick(e) {

if (1 !== which(e)) return;

if (e.metaKey || e.ctrlKey || e.shiftKey) return;
Expand Down Expand Up @@ -632,7 +631,7 @@

function which(e) {
e = e || window.event;
return null === e.which ? e.button : e.which;
return null == e.which ? e.button : e.which;
}

/**
Expand Down
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
}
},
"scripts": {
"engine-deps": "install-engine-dependencies",
"test": "jshint index.js test/tests.js && mocha test/tests.js",
"serve": "serve test",
"test-cov": "jscoverage index.js index-cov.js; PAGE_COV=1 mocha test/tests.js -R html-cov > coverage.html",
Expand All @@ -26,16 +27,26 @@
"browserify": "^6.3.2",
"chai": "^1.10.0",
"coveralls": "^2.11.2",
"engine-dependencies": "^0.2.0",
"express": "^4.10.2",
"jade": "^1.7.0",
"jscoverage": "^0.5.9",
"jsdom": "^1.3.1",
"jsdom": "^11.5.1",
"jshint": "^2.5.10",
"mocha": "^2.0.1",
"mocha-lcov-reporter": "0.0.1",
"serve": "*",
"should": "*"
},
"engineDependencies": {
"node": {
"0.10.x": {
"devDependencies": {
"jsdom": "^1.3.1"
}
}
}
},
"files": [
"index.js",
"page.js"
Expand Down
47 changes: 31 additions & 16 deletions test/support/jsdom.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
before(function(next) {
require('jsdom').env({
html: '<!doctype html><html><head></head><body></body></html>',
done: function(errors, window) {
window.console = console;
window.history.replaceState(null, '', '/');
global.window = window;
global.location = window.location;
global.document = window.document;
global.history = window.history;
window._$jscoverage = global._$jscoverage;
if (errors) {
errors.forEach(console.log);
throw new Error(errors[0].data.error);
var jsdom = require('jsdom');
var html = '<!doctype html><html><head></head><body></body></html>';

function setupGlobals(window) {
window.console = console;
window.history.replaceState(null, '', '/');
global.window = window;
global.location = window.location;
global.document = window.document;
global.history = window.history;
window._$jscoverage = global._$jscoverage;
}

if(jsdom.env) {
jsdom.env({
html: html,
done: function(errors, window) {
setupGlobals(window);
if (errors) {
errors.forEach(console.log);
throw new Error(errors[0].data.error);
}
next();
}
next();
}
});
});
} else {
var dom = new jsdom.JSDOM(html, {
url: 'http://example.com'
});
setupGlobals(dom.window);
next();
}
});
70 changes: 54 additions & 16 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,28 @@
});

var fireEvent = function(node, eventName) {
var event;

var event = document.createEvent('MouseEvents');
if(typeof window.Event === 'function') {
event = new window.MouseEvent(eventName, {
bubbles: true,
button: 1
});
} else {
event = document.createEvent('MouseEvents');

// https://developer.mozilla.org/en-US/docs/Web/API/event.initMouseEvent
event.initEvent(
eventName, true, true, this, 0,
event.screenX, event.screenY, event.clientX, event.clientY,
false, false, false, false,
0, null);
// https://developer.mozilla.org/en-US/docs/Web/API/event.initMouseEvent
event.initEvent(
eventName, true, true, this, 0,
event.screenX, event.screenY, event.clientX, event.clientY,
false, false, false, false,
0, null);

event.button = 1;
event.which = null;
event.button = 1;
event.which = null;
}

node.dispatchEvent(event);

},
beforeTests = function(options) {
page.callbacks = [];
Expand Down Expand Up @@ -89,6 +96,28 @@
page(options);

},
replaceable = function(route) {
function realCallback(ctx) {
obj.callback(ctx);
}

var obj = {
callback: Function.prototype,
replace: function(cb){
obj.callback = cb;
},
once: function(cb){
obj.replace(function(ctx){
obj.callback = Function.prototype;
cb(ctx);
});
}
};

page(route, realCallback);

return obj;
},
tests = function() {
describe('on page load', function() {
it('should invoke the matching callback', function() {
Expand Down Expand Up @@ -173,18 +202,25 @@
});

describe('page.back', function() {
var first;

before(function() {
page('/first', function() {});
first = replaceable('/first', function(){});
page('/second', function() {});
page('/first');
page('/second');
});
it('should move back to history', function() {
it('should move back to history', function(done) {
first.once(function(){
var path = hashbang
? location.hash.replace('#!', '')
: location.pathname;
expect(path).to.be.equal('/first');
done();
});

page.back('/first');
var path = hashbang
? location.hash.replace('#!', '')
: location.pathname;
expect(path).to.be.equal('/first');

});
it('should decrement page.len on back()', function() {
var lenAtFirst = page.len;
Expand Down Expand Up @@ -287,9 +323,11 @@
describe('links dispatcher', function() {

it('should invoke the callback', function(done) {
//this.timeout(60000);
page('/about', function() {
done();
});

fireEvent($('.about'), 'click');
});

Expand Down

0 comments on commit 825fef4

Please sign in to comment.