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

Unit test "page-detect" module #148

Merged
merged 7 commits into from
Apr 15, 2016
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["transform-es2015-destructuring"]
}
4 changes: 1 addition & 3 deletions extension/page-detect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
window.pageDetect = (() => {
const isGistCheck = location.hostname === 'gist.github.com';

const isGist = () => isGistCheck;
const isGist = () => location.hostname === 'gist.github.com';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DrewML I did this like that, because isGist() is called for every isRepo() call which is called for every other check. But location.hostname can't change without invalidating the JS context, so it's safe to check it only once.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to change it if we can't test it otherwise though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I changed it for the purposes of making testing a tad bit easier. We technically can test without changing it, but it requires deleting a reference from node's require.cache and re-requiring it each time we need to make a change to location.hostname, which doesn't seem like a great thing to be doing in tests if it can be avoided.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hkdobrev Doesn't really matter. The overhead of one function is negligible.


const isDashboard = () => location.pathname === '/' || /^(\/orgs\/[^\/]+)?\/dashboard/.test(location.pathname);

Expand Down
16 changes: 15 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"scripts": {
"test": "xo"
"test": "xo && ava"
},
"devDependencies": {
"ava": "*",
"babel-plugin-transform-es2015-destructuring": "^6.6.5",
"babel-register": "^6.7.2",
"grunt": "^1.0.1",
"grunt-cli": "^1.2.0",
"grunt-webstore-upload": "^0.8.10",
Expand All @@ -18,5 +21,16 @@
"ignores": [
"extension/vendor/**"
]
},
"ava": {
"files": [
"test/*.js"
],
"source": [
"extension/*.js"
],
"require": [
"babel-register"
]
}
}
20 changes: 20 additions & 0 deletions test/fixtures/window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const url = require('url');

function WindowMock(initialURI) {
this._currentURI = initialURI;
}

WindowMock.prototype.location = {
set href(uri) {
const uriParts = url.parse(uri);
this.hostname = uriParts.hostname;
this.pathname = uriParts.pathname;
this._currentURI = uri;
},

get href() {
return this._currentURI;
}
};

module.exports = WindowMock;
208 changes: 208 additions & 0 deletions test/page-detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
import test from 'ava';
import Window from './fixtures/window';

global.window = new Window();
global.location = window.location;
require('../extension/page-detect.js');
const {pageDetect} = window;

const urlBulkTest = (shouldMatch, t, fn, testURLs = []) => {
for (const url of testURLs) {
location.href = url;
t[shouldMatch ? 'true' : 'false'](fn());
}
};
const urlsMatch = (...args) => urlBulkTest(true, ...args);
const urlsDontMatch = (...args) => urlBulkTest(false, ...args);

test('isGist', t => {
urlsMatch(t, pageDetect.isGist, [
'https://gist.github.com',
'http://gist.github.com',
'https://gist.github.com/sindresorhus/0ea3c2845718a0a0f0beb579ff14f064'
]);

urlsDontMatch(t, pageDetect.isGist, [
'https://github.com',
'https://help.github.com/'
]);
});

test('isDashboard', t => {
urlsMatch(t, pageDetect.isDashboard, [
'https://github.com/',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/dashboard should also match.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and https://github.com/dashboard and https://github.com.

'https://github.com',
'https://github.com/orgs/test/dashboard',
'https://github.com/dashboard'
]);

urlsDontMatch(t, pageDetect.isDashboard, [
'https://github.com/sindresorhus'
]);
});

test('isRepo', t => {
urlsMatch(t, pageDetect.isRepo, [
'http://github.com/sindresorhus/refined-github',
'https://github.com/sindresorhus/refined-github/issues/146',
'https://github.com/sindresorhus/refined-github/pull/145'
]);

urlsDontMatch(t, pageDetect.isRepo, [
'https://github.com/sindresorhus',
'https://github.com',
'https://github.com/stars'
]);
});

test('isIssueList', t => {
urlsMatch(t, pageDetect.isIssueList, [
'http://github.com/sindresorhus/ava/issues'
]);

urlsDontMatch(t, pageDetect.isIssueList, [
'http://github.com/sindresorhus/ava',
'https://github.com',
'https://github.com/sindresorhus/refined-github/issues/170'
]);
});

test('isIssue', t => {
urlsMatch(t, pageDetect.isIssue, [
'https://github.com/sindresorhus/refined-github/issues/146'
]);

urlsDontMatch(t, pageDetect.isIssue, [
'http://github.com/sindresorhus/ava',
'https://github.com',
'https://github.com/sindresorhus/refined-github/issues'
]);
});

test('isPRList', t => {
urlsMatch(t, pageDetect.isPRList, [
'https://github.com/sindresorhus/refined-github/pulls'
]);

urlsDontMatch(t, pageDetect.isPRList, [
'http://github.com/sindresorhus/ava',
'https://github.com',
'https://github.com/sindresorhus/refined-github/pull/148'
]);
});

test('isPR', t => {
urlsMatch(t, pageDetect.isPR, [
'https://github.com/sindresorhus/refined-github/pull/148'
]);

urlsDontMatch(t, pageDetect.isPR, [
'http://github.com/sindresorhus/ava',
'https://github.com',
'https://github.com/sindresorhus/refined-github/pulls'
]);
});

test('isPRFiles', t => {
urlsMatch(t, pageDetect.isPRFiles, [
'https://github.com/sindresorhus/refined-github/pull/148/files'
]);

urlsDontMatch(t, pageDetect.isPRFiles, [
'https://github.com/sindresorhus/refined-github/pull/148',
'https://github.com/sindresorhus/refined-github/pull/commits',
'https://github.com/sindresorhus/refined-github/pulls'
]);
});

test('isPRCommit', t => {
urlsMatch(t, pageDetect.isPRCommit, [
'https://github.com/sindresorhus/refined-github/pull/148/commits/0019603b83bd97c2f7ef240969f49e6126c5ec85',
'https://github.com/sindresorhus/refined-github/pull/148/commits/00196'
]);

urlsDontMatch(t, pageDetect.isPRCommit, [
'https://github.com/sindresorhus/refined-github/pull/148',
'https://github.com/sindresorhus/refined-github/pull/commits',
'https://github.com/sindresorhus/refined-github/pulls'
]);
});

test('isCommitList', t => {
urlsMatch(t, pageDetect.isCommitList, [
'https://github.com/sindresorhus/refined-github/commits/master?page=2',
'https://github.com/sindresorhus/refined-github/commits/test-branch',
'https://github.com/sindresorhus/refined-github/commits/0.13.0',
'https://github.com/sindresorhus/refined-github/commits/230c2',
'https://github.com/sindresorhus/refined-github/commits/230c2935fc5aea9a681174ddbeba6255ca040d63'
]);

urlsDontMatch(t, pageDetect.isCommitList, [
'https://github.com/sindresorhus/refined-github/pull/148',
'https://github.com/sindresorhus/refined-github/pull/commits',
'https://github.com/sindresorhus/refined-github/branches'
]);
});

test('isSingleCommit', t => {
urlsMatch(t, pageDetect.isSingleCommit, [
'https://github.com/sindresorhus/refined-github/commit/5b614b9035f2035b839f48b4db7bd5c3298d526f',
'https://github.com/sindresorhus/refined-github/commit/5b614'
]);

urlsDontMatch(t, pageDetect.isSingleCommit, [
'https://github.com/sindresorhus/refined-github/pull/148/commits',
'https://github.com/sindresorhus/refined-github/branches'
]);
});

test('isReleases', t => {
urlsMatch(t, pageDetect.isReleases, [
'https://github.com/sindresorhus/refined-github/releases'
]);

urlsDontMatch(t, pageDetect.isReleases, [
'https://github.com/sindresorhus/refined-github',
'https://github.com/sindresorhus/refined-github/graphs'
]);
});

test('isBlame', t => {
urlsMatch(t, pageDetect.isBlame, [
'https://github.com/sindresorhus/refined-github/blame/master/package.json'
]);

urlsDontMatch(t, pageDetect.isBlame, [
'https://github.com/sindresorhus/refined-github/blob/master/package.json'
]);
});

test('getOwnerAndRepo', t => {
const ownerAndRepo = {
'https://github.com/sindresorhus/refined-github/pull/148': {
ownerName: 'sindresorhus',
repoName: 'refined-github'
},
'https://github.com/DrewML/GifHub/blob/master/.gitignore': {
ownerName: 'DrewML',
repoName: 'GifHub'
}
};

Object.keys(ownerAndRepo).forEach(url => {
location.href = url;
t.deepEqual(ownerAndRepo[url], pageDetect.getOwnerAndRepo());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I updated ava to * in package.json in a recent commit - if t.deepEqual fails, run npm install to update to the latest version

});
});

test('isSingleFile', t => {
urlsMatch(t, pageDetect.isSingleFile, [
'https://github.com/sindresorhus/refined-github/blob/master/.gitattributes',
'https://github.com/sindresorhus/refined-github/blob/fix-narrow-diff/extension/custom.css'
]);

urlsDontMatch(t, pageDetect.isSingleFile, [
'https://github.com/sindresorhus/refined-github/pull/164/files',
'https://github.com/sindresorhus/refined-github/commit/57bf4'
]);
});