Skip to content

Commit

Permalink
feat(tests): add initial testing
Browse files Browse the repository at this point in the history
  • Loading branch information
addyosmani committed Nov 25, 2018
1 parent fadf8b3 commit fd81b71
Show file tree
Hide file tree
Showing 4 changed files with 2,133 additions and 88 deletions.
19 changes: 17 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"jsnext:main": "dist/quicklink.mjs",
"umd:main": "dist/quicklink.umd.js",
"scripts": {
"lint": "eslint src/*.mjs",
"test": "eslint src/*.mjs && jest",
"build": "microbundle src/index.mjs --no-sourcemap",
"prepare": "npm run -s build",
"release": "cross-var npm run build -s && cross-var git commit -am $npm_package_version && cross-var git tag $npm_package_version && git push && git push --tags && npm publish"
"release": "cross-var npm run build -s && cross-var git commit -am $npm_package_version && cross-var git tag $npm_package_version && git push && git push --tags && npm publish --access public"
},
"keywords": [
"prefetch",
Expand All @@ -23,10 +23,25 @@
"background",
"speed"
],
"jest": {
"testURL": "http://localhost/",
"testMatch": [
"<rootDir>/test/**/*.?(m)js?(x)"
],
"moduleFileExtensions": [
"mjs",
"js"
],
"transform": {
"^.+\\.m?jsx?$": "babel-jest"
}
},
"devDependencies": {
"babel-preset-env": "^1.7.0",
"cross-var": "^1.1.0",
"eslint": "^5.9.0",
"eslint-config-google": "^0.11.0",
"jest": "^23.6.0",
"microbundle": "^0.7.0"
},
"dependencies": {}
Expand Down
29 changes: 17 additions & 12 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,24 @@ const prefetchURLs = function (urls, priority) {
* @param {Array} options.urls - Array of URLs to prefetch (override)
* @param {Object} options.el - DOM element to prefetch in-viewport links of
* @param {string} options.priority - Attempt to fetch with higher priority (low or high)
* @return {Object} Promise
*/
export default function (options) {
options = options || {priority: 'low'};
requestIdleCallback(() => {
// Prefetch an array of URLs if supplied (as an override)
if (options.urls !== undefined && options.urls.length > 0) {
prefetchURLs(options.urls, options.priority);
} else {
// Element to extract in-viewport links for
const el = options.el || document;
extractInViewportLinks(el).then(urls => {
prefetchURLs(urls, options.priority);
});
}
return new Promise((resolve, reject) => {
options = options || {priority: 'low'};
requestIdleCallback(() => {
// Prefetch an array of URLs if supplied (as an override)
if (options.urls !== undefined && options.urls.length > 0) {
prefetchURLs(options.urls, options.priority);
resolve(options.urls);
} else {
// Element to extract in-viewport links for
const el = options.el || document;
extractInViewportLinks(el).then(urls => {
prefetchURLs(urls, options.priority);
resolve(urls);
});
}
});
});
}
22 changes: 22 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import quicklink from '../src/index.mjs';
import quicklinkDist from '../dist/quicklink.mjs';

describe('quicklink', () => {
it('should be a function', () => {
expect(quicklink).toEqual(expect.any(Function));
});
it('should be compiled correctly', () => {
expect(quicklinkDist).toEqual(expect.any(Function));
expect(quicklinkDist).toHaveLength(1);
});
describe('prefetching', () => {
it('custom list of URLs', () => {
const p = quicklinkDist({urls: ['1.html', '2.html']});
p.then(r => {
expect(r.toHaveLength(2));
});
});
});
});


Loading

0 comments on commit fd81b71

Please sign in to comment.