-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from 6 commits
1e27d79
68ee8d5
e1aba6f
e3628fe
61cb60e
4ea33a4
9ec4a57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["transform-es2015-destructuring"] | ||
} |
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; |
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/', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and |
||
'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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: I updated ava to |
||
}); | ||
}); | ||
|
||
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' | ||
]); | ||
}); |
There was a problem hiding this comment.
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 everyisRepo()
call which is called for every other check. Butlocation.hostname
can't change without invalidating the JS context, so it's safe to check it only once.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 tolocation.hostname
, which doesn't seem like a great thing to be doing in tests if it can be avoided.There was a problem hiding this comment.
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.