Skip to content

Commit

Permalink
Cleanup (#9)
Browse files Browse the repository at this point in the history
* Remove global argument

* Update tests

* Use single selector instead of two queries

* Remove null argument from callback

* Fix jshint

* Add browser globals in test
  • Loading branch information
fregante authored and stefanbuck committed Jul 6, 2017
1 parent 5d01d77 commit 80ddc56
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"eqnull": true,
"node": true,
"globals": {
"document" : false,
"MutationObserver" : false,
"describe" : false,
"it" : false,
"before" : false,
Expand Down
23 changes: 7 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
'use strict';

var gitHubInjection = function (global, cb) {
if (!global) {
throw new Error('Missing argument global');
}

if (!global.document || !global.document.getElementById) {
throw new Error('The given argument global is not a valid window object');
}

var gitHubInjection = function (cb) {
if (!cb) {
throw new Error('Missing argument callback');
}
Expand All @@ -17,16 +9,15 @@ var gitHubInjection = function (global, cb) {
throw new Error('Callback is not a function');
}

var domElement = global.document.getElementById('js-repo-pjax-container') ||
global.document.getElementById('js-pjax-container');
if (!domElement || !global.MutationObserver) {
return cb(null);
var domElement = document.querySelector('#js-repo-pjax-container, #js-pjax-container');
if (!domElement || typeof MutationObserver === 'undefined') {
return cb();
}

var viewSpy = new global.MutationObserver(function (mutations) {
var viewSpy = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (mutation.addedNodes.length) {
cb(null);
cb();
}
});
});
Expand All @@ -35,7 +26,7 @@ var gitHubInjection = function (global, cb) {
childList: true
});

cb(null);
cb();
};

// Export the gitHubInjection function for **Node.js**, with
Expand Down
22 changes: 7 additions & 15 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,29 @@
'use strict';

var assert = require('assert');
var jsdom = require('jsdom').jsdom;
var helper = require('./helper');
var injection = require('..');

var fakeWindow = {
document: {
getElementById: function() {}
}
};

describe('GitHub-Injection', function() {

describe('constructor', function() {

it('require a window argument', function () {
assert.throws(injection.bind(null), 'Missing argument global');
before(function() {
global.document = jsdom();
global.window = document.defaultView;
});

it('require a callback argument', function () {
assert.throws(injection.bind(null, fakeWindow), 'Missing argument callback');
assert.throws(injection, 'Missing argument callback');
});

it('callback is not a function', function () {
assert.throws(injection.bind(null, fakeWindow, {}, {}), 'Callback is not a function');
});

it('window parameter is not valid', function () {
assert.throws(injection.bind(null, {}), 'The given argument global is not a valid window object');
assert.throws(injection.bind(null, {}, {}), 'Callback is not a function');
});

it('accept a callback argument', function (done) {
assert.doesNotThrow(injection.bind(null, fakeWindow, done));
assert.doesNotThrow(injection.bind(null, done));
});
});

Expand Down

0 comments on commit 80ddc56

Please sign in to comment.