Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for modern browsers #10

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"esversion": 6,
"curly": true,
"eqeqeq": true,
"immed": true,
Expand Down
7 changes: 2 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
language: node_js
sudo: false
node_js:
- v4
- v5
after_script:
- npm test
- 6
- node
22 changes: 7 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

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

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

var viewSpy = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
const viewSpy = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.addedNodes.length) {
cb();
}
Expand All @@ -29,16 +29,8 @@ var gitHubInjection = function (cb) {
cb();
};

// Export the gitHubInjection function for **Node.js**, with
// backwards-compatibility for the old `require()` API. If we're in
// the browser, add `gitHubInjection` as a global object.
// Export the gitHubInjection function for **Node.js**
// Otherwise leave it as a global
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = gitHubInjection;
}
exports.gitHubInjection = gitHubInjection;
} else {
/*jshint -W040 */
this.gitHubInjection = gitHubInjection;
/*jshint +W040 */
}
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@
"devDependencies": {
"coveralls": "^2.11.1",
"gulp": "^3.8.8",
"gulp-bump": "^0.1.11",
"gulp-istanbul": "^0.3.0",
"gulp-jscs": "^1.1.2",
"gulp-jshint": "^1.8.4",
"gulp-load-plugins": "^0.6.0",
"gulp-mocha": "^1.1.0",
"gulp-plumber": "^0.6.5",
"gulp-bump": "^2.7.0",
"gulp-istanbul": "^1.1.2",
"gulp-jscs": "^4.0.0",
"gulp-jshint": "^2.0.4",
"gulp-load-plugins": "^1.5.0",
"gulp-mocha": "^4.3.1",
"gulp-plumber": "^1.1.0",
"gulp-util": "^3.0.1",
"jsdom": "^6.3.0",
"jshint-stylish": "^0.4.0"
"jsdom": "^11.1.0",
"jshint": "^2.9.5",
"jshint-stylish": "^2.2.1",
"webcomponentsjs": "^1.0.2"
},
"scripts": {
"test": "npm run test-local && npm run test-remote && cat coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
Expand Down
44 changes: 17 additions & 27 deletions test/helper.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
'use strict';

var util = require('util');
var fs = require('fs');
var path = require('path');
var env = require('jsdom').env;

module.exports = function(file, url, done) {
var content, baseUrl, filePath;
const util = require('util');
const fs = require('fs');
const path = require('path');
const {JSDOM} = require('jsdom');

module.exports = (file, url) => {
let content;
let baseUrl;
let filePath;
baseUrl = 'https://github.com/octo-linker/injection/';

if (typeof url === 'function') {
done = url;
if (typeof url === 'undefined') {
url = 'blob/master/test/fixtures/' + file;
}

Expand All @@ -21,24 +22,13 @@ module.exports = function(file, url, done) {
console.log(' remote tests');
} else {
console.log(' local tests');
filePath = util.format('./fixtures/%s', file);
filePath = path.resolve(__dirname, filePath);
content = fs.readFileSync(filePath, 'utf-8');
}

env(content, function(err, window) {
if (err) {
return done(err);
}

if (process.env.TEST_ENV !== 'remote') {
window.document.location.href = url;
}

if (process.env.TEST_ENV !== 'remote') {
window.document.location.href = url;
if (file) {
filePath = util.format('./fixtures/%s', file);
filePath = path.resolve(__dirname, filePath);
content = fs.readFileSync(filePath, 'utf-8');
}
}

done(null, window);
});
const {window} = new JSDOM(content, {url});
return window;
};
72 changes: 36 additions & 36 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,53 @@
'use strict';

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

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

describe('constructor', function() {

before(function() {
global.document = jsdom();
global.window = document.defaultView;
const assert = require('assert');
const {JSDOM} = require('jsdom');
const helper = require('./helper');
const injection = require('..');


function MutationObserverMock(cb) {
return {
observe() {
setTimeout(() => {
cb({
addedNodes: [{}]
})
}, 100);
}
};
}

describe('GitHub-Injection', () => {

describe('constructor', () => {

before(() => {
global.window = helper();
global.document = window.document;
global.MutationObserver = MutationObserverMock;
});

it('require a callback argument', function () {
assert.throws(injection, 'Missing argument callback');
it('require a callback argument', () => {
assert.throws(() => injection(), 'Missing argument callback');
});

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

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

describe('markup repo', function() {

this.timeout(4000);

before(function(done) {
before(function() {
this.$ = this.result = null;

helper('repo_browser.html', '/', function(err, window) {
if (err) {
return done(err);
}
this.window = window;
done();
}.bind(this));
this.window = helper('repo_browser.html', '/');
});

it('ajax container is present', function() {
Expand All @@ -52,16 +59,9 @@ describe('GitHub-Injection', function() {

this.timeout(4000);

before(function(done) {
before(function() {
this.$ = this.result = null;

helper('pr_browser.html', '/', function(err, window) {
if (err) {
return done(err);
}
this.window = window;
done();
}.bind(this));
this.window = helper('pr_browser.html', '/');
});

it('ajax container is present', function() {
Expand Down