Skip to content

Commit e246936

Browse files
Initial test suite added.
1 parent 1592259 commit e246936

File tree

5 files changed

+167
-45
lines changed

5 files changed

+167
-45
lines changed

Diff for: .eslintrc.json

+28-26
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
{
22
"env": {
33
"browser": true,
4-
"commonjs": true
4+
"commonjs": true,
5+
"es6": true,
6+
"node": true
57
},
68
"extends": "eslint:recommended",
9+
"parserOptions": {
10+
"sourceType": "module"
11+
},
712
"rules": {
813
"accessor-pairs": "error",
914
"array-bracket-newline": "error",
@@ -14,8 +19,17 @@
1419
"array-callback-return": "error",
1520
"array-element-newline": "off",
1621
"arrow-body-style": "error",
17-
"arrow-parens": "error",
18-
"arrow-spacing": "error",
22+
"arrow-parens": [
23+
"error",
24+
"as-needed"
25+
],
26+
"arrow-spacing": [
27+
"error",
28+
{
29+
"after": true,
30+
"before": true
31+
}
32+
],
1933
"block-scoped-var": "error",
2034
"block-spacing": "error",
2135
"brace-style": [
@@ -91,14 +105,7 @@
91105
"lines-around-comment": "error",
92106
"lines-around-directive": "error",
93107
"max-depth": "error",
94-
"max-len": [
95-
"error",
96-
{
97-
"code": 80,
98-
"tabWidth": 2,
99-
"ignoreStrings": true
100-
}
101-
],
108+
"max-len": "error",
102109
"max-lines": "off",
103110
"max-nested-callbacks": "error",
104111
"max-params": "error",
@@ -134,7 +141,7 @@
134141
"no-extend-native": "error",
135142
"no-extra-bind": "error",
136143
"no-extra-label": "error",
137-
"no-extra-parens": "off",
144+
"no-extra-parens": "error",
138145
"no-floating-decimal": "error",
139146
"no-implicit-coercion": "error",
140147
"no-implicit-globals": "error",
@@ -182,7 +189,7 @@
182189
"no-self-compare": "error",
183190
"no-sequences": "error",
184191
"no-shadow": "error",
185-
"no-shadow-restricted-names": "off",
192+
"no-shadow-restricted-names": "error",
186193
"no-spaced-func": "error",
187194
"no-sync": "error",
188195
"no-tabs": "error",
@@ -209,7 +216,7 @@
209216
"no-whitespace-before-property": "error",
210217
"no-with": "error",
211218
"nonblock-statement-body-position": "error",
212-
"object-curly-newline": "error",
219+
"object-curly-newline": "off",
213220
"object-curly-spacing": [
214221
"error",
215222
"always"
@@ -219,6 +226,10 @@
219226
"one-var": "off",
220227
"one-var-declaration-per-line": "error",
221228
"operator-assignment": "error",
229+
"operator-linebreak": [
230+
"error",
231+
null
232+
],
222233
"padded-blocks": "off",
223234
"padding-line-between-statements": "error",
224235
"prefer-arrow-callback": "error",
@@ -260,10 +271,7 @@
260271
"error",
261272
"always"
262273
],
263-
"strict": [
264-
"error",
265-
"global"
266-
],
274+
"strict": "off",
267275
"switch-colon-spacing": "error",
268276
"symbol-description": "error",
269277
"template-curly-spacing": "error",
@@ -272,19 +280,13 @@
272280
"error",
273281
"never"
274282
],
275-
"valid-jsdoc": [
276-
"error",
277-
{
278-
"requireReturnDescription": false,
279-
"requireParamDescription": false
280-
}
281-
],
282283
"vars-on-top": "off",
284+
"wrap-iife": "error",
283285
"wrap-regex": "error",
284286
"yield-star-spacing": "error",
285287
"yoda": [
286288
"error",
287289
"never"
288290
]
289291
}
290-
}
292+
}

Diff for: README.md

+12
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ Are you done? Awesome, submit a pull request explaining your changes.
5353
_This is a polyfill, not library, so make sure the behavior you are introducing is in the spec._
5454

5555

56+
### Requirements
57+
58+
- Node version required: 7.10.0
59+
60+
_On tests files we are using ES2015, but the polyfill is written in ES5 for browser compatibility._
61+
62+
63+
### Watch tests
64+
65+
If you want to watch tests as you write your code run `npm test -- --watch`.
66+
67+
5668
## Browser Support
5769

5870
Successfully working in:

Diff for: package.json

+8-5
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,32 @@
1717
],
1818
"dependencies": {},
1919
"devDependencies": {
20+
"ava": "^0.25.0",
2021
"eslint": "^4.7.1",
2122
"husky": "^0.14.3",
2223
"lint-staged": "^6.0.1",
24+
"lodash.clone": "^4.5.0",
25+
"lodash.clonedeep": "^4.5.0",
2326
"prettier": "^1.10.2",
2427
"rollup": "^0.50.0",
2528
"uglify-js": "^3.1.1"
2629
},
2730
"main": "dist/smoothscroll.js",
2831
"scripts": {
29-
"lint": "eslint src/**/*.js",
32+
"lint": "eslint {src,test}/**/*.js",
3033
"pretest": "npm run lint",
31-
"test": "echo 'Sorry, no tests yet, just linting' && exit 0",
34+
"test": "ava test/smoothscroll.test.js --verbose",
3235
"precommit": "lint-staged",
3336
"prebuild": "npm test",
3437
"build": "rollup -c rollup.config.js",
3538
"preminify": "npm run build",
3639
"minify": "uglifyjs dist/smoothscroll.js -c -m -o dist/smoothscroll.min.js --verbose",
37-
"listdiff": "prettier --list-different src/**/*.js",
38-
"format": "prettier --write src/**/*.js"
40+
"listdiff": "prettier --list-different {src,test}/**/*.js",
41+
"format": "prettier --write {src,test}/**/*.js"
3942
},
4043
"lint-staged": {
4144
"linters": {
42-
"src/**/*.js": [
45+
"{src,test}/**/*.js": [
4346
"prettier --list-different",
4447
"git add"
4548
]

Diff for: src/smoothscroll.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ function polyfill() {
1818
var Element = w.HTMLElement || w.Element;
1919
var SCROLL_TIME = 468;
2020

21+
// object gathering original scroll methods
22+
var original = {
23+
scroll: w.scroll || w.scrollTo,
24+
scrollBy: w.scrollBy,
25+
elementScroll: Element.prototype.scroll || scrollElement,
26+
scrollIntoView: Element.prototype.scrollIntoView
27+
};
28+
29+
// define timing method
30+
var now =
31+
w.performance && w.performance.now
32+
? w.performance.now.bind(w.performance)
33+
: Date.now;
34+
2135
/**
2236
* indicates if a the current browser is made by Microsoft
2337
* @method isMicrosoftBrowser
@@ -37,20 +51,6 @@ function polyfill() {
3751
*/
3852
var ROUNDING_TOLERANCE = isMicrosoftBrowser(w.navigator.userAgent) ? 1 : 0;
3953

40-
// object gathering original scroll methods
41-
var original = {
42-
scroll: w.scroll || w.scrollTo,
43-
scrollBy: w.scrollBy,
44-
elementScroll: Element.prototype.scroll || scrollElement,
45-
scrollIntoView: Element.prototype.scrollIntoView
46-
};
47-
48-
// define timing method
49-
var now =
50-
w.performance && w.performance.now
51-
? w.performance.now.bind(w.performance)
52-
: Date.now;
53-
5454
/**
5555
* changes scroll position inside an element
5656
* @method scrollElement

Diff for: test/smoothscroll.test.js

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import clone from 'lodash.clonedeep';
2+
import smoothscroll from '../src/smoothscroll';
3+
import test from 'ava';
4+
5+
// mock window object
6+
const mockedWindow = {
7+
Element: {
8+
prototype: {
9+
scroll: f => f,
10+
scrollBy: f => f,
11+
scrollIntoView: f => f
12+
}
13+
},
14+
scroll: f => f,
15+
scrollBy: f => f,
16+
navigator: {
17+
userAgent: 'test'
18+
},
19+
performance: {
20+
now: f => f
21+
},
22+
requestAnimationFrame: f => f
23+
};
24+
25+
// mock document object
26+
const mockedDocument = {
27+
documentElement: {
28+
style: {}
29+
}
30+
};
31+
32+
// expose clean browser objects as global
33+
test.beforeEach(() => {
34+
global.window = clone(mockedWindow);
35+
global.document = clone(mockedDocument);
36+
});
37+
38+
// clean browser objects after each test
39+
test.afterEach.always(() => {
40+
global.window = null;
41+
global.document = null;
42+
});
43+
44+
test('polyfill is available as method', t => {
45+
t.truthy(typeof smoothscroll.polyfill === 'function');
46+
});
47+
48+
test('polyfill overrides native methods when polyfill is not supported', t => {
49+
const originalWindowScroll = window.scroll;
50+
const originalWindowScrollBy = window.scrollBy;
51+
const originalElScroll = window.Element.prototype.scroll;
52+
const originalElScrollBy = window.Element.prototype.scrollBy;
53+
const originalElScrollIntoView = window.Element.prototype.scrollIntoView;
54+
55+
smoothscroll.polyfill();
56+
57+
// global methods were replaced
58+
t.not(window.scroll, originalWindowScroll);
59+
t.not(window.scrollBy, originalWindowScrollBy);
60+
t.not(window.Element.prototype.scroll, originalElScroll);
61+
t.not(window.Element.prototype.scrollBy, originalElScrollBy);
62+
t.not(window.Element.prototype.scrollIntoView, originalElScrollIntoView);
63+
});
64+
65+
test('polyfill bails out when polyfill is supported', t => {
66+
const originalWindowScroll = window.scroll;
67+
const originalWindowScrollBy = window.scrollBy;
68+
const originalElScroll = window.Element.prototype.scroll;
69+
const originalElScrollBy = window.Element.prototype.scrollBy;
70+
const originalElScrollIntoView = window.Element.prototype.scrollIntoView;
71+
72+
// global methods remained untouched
73+
document.documentElement.style.scrollBehavior = 'auto';
74+
75+
smoothscroll.polyfill();
76+
77+
t.is(window.scroll, originalWindowScroll);
78+
t.is(window.scrollBy, originalWindowScrollBy);
79+
t.is(window.Element.prototype.scroll, originalElScroll);
80+
t.is(window.Element.prototype.scrollBy, originalElScrollBy);
81+
t.is(window.Element.prototype.scrollIntoView, originalElScrollIntoView);
82+
});
83+
84+
test('polyfill overrides native methods when implementation is forced', t => {
85+
const originalWindowScroll = window.scroll;
86+
const originalWindowScrollBy = window.scrollBy;
87+
const originalElScroll = window.Element.prototype.scroll;
88+
const originalElScrollBy = window.Element.prototype.scrollBy;
89+
const originalElScrollIntoView = window.Element.prototype.scrollIntoView;
90+
91+
// simulate smooth scroll spec native support
92+
document.documentElement.style.scrollBehavior = 'auto';
93+
94+
// force implementation
95+
window.__forceSmoothScrollPolyfill__ = true;
96+
97+
smoothscroll.polyfill();
98+
99+
// global methods were forcefully replaced
100+
t.not(window.scroll, originalWindowScroll);
101+
t.not(window.scrollBy, originalWindowScrollBy);
102+
t.not(window.Element.prototype.scroll, originalElScroll);
103+
t.not(window.Element.prototype.scrollBy, originalElScrollBy);
104+
t.not(window.Element.prototype.scrollIntoView, originalElScrollIntoView);
105+
});

0 commit comments

Comments
 (0)