Skip to content

Commit fd81b71

Browse files
committed
feat(tests): add initial testing
1 parent fadf8b3 commit fd81b71

4 files changed

Lines changed: 2133 additions & 88 deletions

File tree

package.json

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
"jsnext:main": "dist/quicklink.mjs",
1111
"umd:main": "dist/quicklink.umd.js",
1212
"scripts": {
13-
"lint": "eslint src/*.mjs",
13+
"test": "eslint src/*.mjs && jest",
1414
"build": "microbundle src/index.mjs --no-sourcemap",
1515
"prepare": "npm run -s build",
16-
"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"
16+
"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"
1717
},
1818
"keywords": [
1919
"prefetch",
@@ -23,10 +23,25 @@
2323
"background",
2424
"speed"
2525
],
26+
"jest": {
27+
"testURL": "http://localhost/",
28+
"testMatch": [
29+
"<rootDir>/test/**/*.?(m)js?(x)"
30+
],
31+
"moduleFileExtensions": [
32+
"mjs",
33+
"js"
34+
],
35+
"transform": {
36+
"^.+\\.m?jsx?$": "babel-jest"
37+
}
38+
},
2639
"devDependencies": {
40+
"babel-preset-env": "^1.7.0",
2741
"cross-var": "^1.1.0",
2842
"eslint": "^5.9.0",
2943
"eslint-config-google": "^0.11.0",
44+
"jest": "^23.6.0",
3045
"microbundle": "^0.7.0"
3146
},
3247
"dependencies": {}

src/index.mjs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,24 @@ const prefetchURLs = function (urls, priority) {
6666
* @param {Array} options.urls - Array of URLs to prefetch (override)
6767
* @param {Object} options.el - DOM element to prefetch in-viewport links of
6868
* @param {string} options.priority - Attempt to fetch with higher priority (low or high)
69+
* @return {Object} Promise
6970
*/
7071
export default function (options) {
71-
options = options || {priority: 'low'};
72-
requestIdleCallback(() => {
73-
// Prefetch an array of URLs if supplied (as an override)
74-
if (options.urls !== undefined && options.urls.length > 0) {
75-
prefetchURLs(options.urls, options.priority);
76-
} else {
77-
// Element to extract in-viewport links for
78-
const el = options.el || document;
79-
extractInViewportLinks(el).then(urls => {
80-
prefetchURLs(urls, options.priority);
81-
});
82-
}
72+
return new Promise((resolve, reject) => {
73+
options = options || {priority: 'low'};
74+
requestIdleCallback(() => {
75+
// Prefetch an array of URLs if supplied (as an override)
76+
if (options.urls !== undefined && options.urls.length > 0) {
77+
prefetchURLs(options.urls, options.priority);
78+
resolve(options.urls);
79+
} else {
80+
// Element to extract in-viewport links for
81+
const el = options.el || document;
82+
extractInViewportLinks(el).then(urls => {
83+
prefetchURLs(urls, options.priority);
84+
resolve(urls);
85+
});
86+
}
87+
});
8388
});
8489
}

test/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import quicklink from '../src/index.mjs';
2+
import quicklinkDist from '../dist/quicklink.mjs';
3+
4+
describe('quicklink', () => {
5+
it('should be a function', () => {
6+
expect(quicklink).toEqual(expect.any(Function));
7+
});
8+
it('should be compiled correctly', () => {
9+
expect(quicklinkDist).toEqual(expect.any(Function));
10+
expect(quicklinkDist).toHaveLength(1);
11+
});
12+
describe('prefetching', () => {
13+
it('custom list of URLs', () => {
14+
const p = quicklinkDist({urls: ['1.html', '2.html']});
15+
p.then(r => {
16+
expect(r.toHaveLength(2));
17+
});
18+
});
19+
});
20+
});
21+
22+

0 commit comments

Comments
 (0)