Commit 8ae72ed6 authored by chdorner's avatar chdorner

Add our own simpler implementation of the page npm package

We don't need all the client-side routing part, we only need to be able
to selectively run certain JavaScript code depending on which site the
user is at the moment.
This implementation is not as advanced as the one from the `page`
package, or what is possible with the `url-pattern` package. But we
don't need any fancy URL matching at the moment, once we do we can
revisit and maybe introduce something like `url-pattern` instead of just
a crude `document.location.pathname === path`.
parent aa0413b3
'use strict';
function page(path, callback) {
if (window.location.pathname === path) {
document.addEventListener('DOMContentLoaded', callback, false);
}
}
module.exports = page;
'use strict';
var page = require('../page');
// helper to dispatch a native event to an element
function sendEvent(element, eventType) {
// createEvent() used instead of Event constructor
// for PhantomJS compatibility
var event = document.createEvent('Event');
event.initEvent(eventType, true /* bubbles */, true /* cancelable */);
element.dispatchEvent(event);
return event;
}
describe('page', function () {
it('it adds the callback when the url path matches', function () {
var spy = sinon.spy();
page(document.location.pathname, spy);
sendEvent(document, 'DOMContentLoaded');
assert.calledOnce(spy);
});
it('it skips adding the callback when the url path does not match', function () {
var spy = sinon.spy();
page(document.location.pathname + '-foo', spy);
sendEvent(document, 'DOMContentLoaded');
assert.notCalled(spy);
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment