Skip to content

Commit d58d074

Browse files
committed
more unit tests for scroll-timeline-base functions
1 parent adfa3ad commit d58d074

File tree

6 files changed

+28
-25
lines changed

6 files changed

+28
-25
lines changed

.DS_Store

0 Bytes
Binary file not shown.

deploy.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
rm -rf build
55
mkdir build
66

7-
# recursively copy demo/ dir to www/
7+
# recursively copy demo/ dir to build/
88
cp -rf demo/ build/
99

1010
# copy demos root page that lists all demos

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta name="viewport"
66
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
77
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8-
<title>Local Dev Demos</title>
8+
<title>ScrollTimeline Demos</title>
99
</head>
1010
<body>
1111
<ul>

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"watch": "microbundle watch -f iife",
1010
"dev": "run-all \"serve\" \" npm run watch \" ",
1111
"deploy": "npm run build && ./deploy.sh && gh-pages build",
12-
"test:unit": "jest"
12+
"test:unit": "jest",
13+
"test:watch": "jest --watchAll"
1314
},
1415
"repository": {
1516
"type": "git",

src/scroll-timeline-base.js

+20-18
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import {parseLength} from './utils';
1616

17+
const ORIENTATIONS = new Set()
18+
1719
let scrollTimelineOptions = new WeakMap();
1820

1921
function scrollEventSource(scrollSource) {
@@ -22,8 +24,8 @@ function scrollEventSource(scrollSource) {
2224
return scrollSource;
2325
}
2426

25-
function calculateTargetEffectEnd(options) {
26-
if (options.iterationCount == Infinity)
27+
export function calculateTargetEffectEnd(options) {
28+
if (options.iterationCount === Infinity)
2729
return Infinity;
2830
return Math.max((options.startDelay || 0) + (options.duration || 0) * (options.iterationCount || 1) + (options.endDelay || 0), 0);
2931
}
@@ -34,62 +36,62 @@ export function installScrollOffsetExtension(parseFunction, evaluateFunction) {
3436
extensionScrollOffsetFunctions.push([parseFunction, evaluateFunction]);
3537
}
3638

37-
function calculateMaxScrollOffset(scrollSource, orientation) {
39+
export function calculateMaxScrollOffset(scrollSource, orientation) {
3840
// TODO: Support other writing directions.
39-
if (orientation == 'block')
41+
if (orientation === 'block')
4042
orientation = 'vertical';
41-
else if (orientation == 'inline')
43+
else if (orientation === 'inline')
4244
orientation = 'horizontal';
43-
if (orientation == 'vertical')
45+
if (orientation === 'vertical')
4446
return scrollSource.scrollHeight - scrollSource.clientHeight;
45-
else if (orientation == 'horizontal')
47+
else if (orientation === 'horizontal')
4648
return scrollSource.scrollWidth - scrollSource.clientWidth;
4749

4850
}
4951

5052
function calculateScrollOffset(autoValue, scrollSource, orientation, offset, fn) {
5153
if (fn)
52-
return fn(scrollSource, orientation, offset, autoValue == '0%' ? 'start' : 'end');
54+
return fn(scrollSource, orientation, offset, autoValue === '0%' ? 'start' : 'end');
5355
// TODO: Support other writing directions.
54-
if (orientation == 'block')
56+
if (orientation === 'block')
5557
orientation = 'vertical';
56-
else if (orientation == 'inline')
58+
else if (orientation === 'inline')
5759
orientation = 'horizontal';
5860

59-
let maxValue = orientation == 'vertical' ?
61+
let maxValue = orientation === 'vertical' ?
6062
scrollSource.scrollHeight - scrollSource.clientHeight :
6163
scrollSource.scrollWidth - scrollSource.clientWidth;
62-
let parsed = parseLength(offset == 'auto' ? autoValue : offset);
63-
if (parsed[2] == '%')
64+
let parsed = parseLength(offset === 'auto' ? autoValue : offset);
65+
if (parsed[2] === '%')
6466
return parseFloat(parsed[1]) * maxValue / 100;
6567
return parseFloat(parsed[1]);
6668
}
6769

6870
function calculateTimeRange(scrollTimeline) {
6971
let timeRange = scrollTimeline.timeRange;
70-
if (timeRange == 'auto') {
72+
if (timeRange === 'auto') {
7173
timeRange = 0;
7274
let options = scrollTimelineOptions.get(scrollTimeline).animationOptions;
7375
for (let i = 0; i < options.length; i++) {
7476
timeRange = Math.max(timeRange, calculateTargetEffectEnd(options[i]));
7577
}
76-
if (timeRange == Infinity)
78+
if (timeRange === Infinity)
7779
timeRange = 0;
7880
}
7981
return timeRange;
8082
}
8183

8284
function updateInternal() {
8385
let animations = scrollTimelineOptions.get(this).animations;
84-
if (animations.length == 0)
86+
if (animations.length === 0)
8587
return;
8688
let currentTime = this.currentTime;
8789
for (let i = 0; i < animations.length; i++) {
8890
// The web-animations spec says to throw a TypeError if you try to seek to
8991
// an unresolved time value from a resolved time value, so to polyfill the
9092
// expected behavior we cancel the underlying animation.
9193
if (currentTime == null) {
92-
if (animations[i].playState == 'paused')
94+
if (animations[i].playState === 'paused')
9395
animations[i].cancel();
9496
} else {
9597
animations[i].currentTime = currentTime;
@@ -108,7 +110,7 @@ function addAnimation(scrollTimeline, animation, options) {
108110
function removeAnimation(scrollTimeline, animation) {
109111
let animations = scrollTimelineOptions.get(scrollTimeline).animations;
110112
let index = animations.indexOf(animation);
111-
if (index == -1)
113+
if (index === -1)
112114
return;
113115
animations.splice(index, 1);
114116
scrollTimelineOptions.get(scrollTimeline).animationOptions.splice(index, 1);

tests/util.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
import {parseLength} from '../src/utils';
22

33
describe("parseLength util function", function () {
4-
test("parseLength should return an array or=f length 3 for valid input", function () {
4+
test("should return an array of length 3 for valid input", function () {
55
let offset = '100px';
66
expect(parseLength(offset).length).toEqual(3);
77
});
88

9-
test("parseLength should return an array or=f length 3 for valid input", function () {
9+
test("can understand px as valid unit", function () {
1010
let num = '100';
1111
let unit = 'px';
1212
let offset = num + unit;
1313
expect(parseLength(offset)[1]).toEqual(num);
1414
expect(parseLength(offset)[2]).toEqual(unit);
1515
});
1616

17-
test("parseLength should return an array or=f length 3 for valid input", function () {
17+
test("can understand % as valid input", function () {
1818
let num = '99999';
1919
let unit = '%';
2020
let offset = num + unit;
2121
expect(parseLength(offset)[1]).toEqual(num);
2222
expect(parseLength(offset)[2]).toEqual(unit);
2323
});
2424

25-
test("parseLength should return null for invalid input", function () {
25+
test("should null for invalid input", function () {
2626
let num = '100';
2727
let unit = '&';
2828
let offset = num + unit;

0 commit comments

Comments
 (0)