-
Notifications
You must be signed in to change notification settings - Fork 1
/
oscillation.spec.js
134 lines (108 loc) · 4 KB
/
oscillation.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { test as base } from "rollwright";
import { expect } from "@playwright/test";
let test = base.extend({
advance: async ({ execute }, use) => {
let advance = await execute(() => {
let timestamp = 0;
window.performance = { now: () => timestamp };
let callbacks = {};
let id = 0;
window.requestAnimationFrame = (cb) => ((callbacks[id++] = cb), id);
window.cancelAnimationFrame = (id) => delete callbacks[id];
function step(ms) {
let cbs = callbacks;
callbacks = {};
timestamp += ms;
for (let k in cbs) cbs[k](timestamp);
}
return (steps, ms) => {
for (let i = 0; i < steps; i++) step(ms);
};
});
await use((steps, ms) =>
execute((advance, steps, ms) => advance(steps, ms), advance, steps, ms),
);
},
});
test("interpolation with ideal timer condition", async ({ execute, advance }) => {
let snapshots = await execute(async () => {
let { motion, spring } = await import("./oscillation.js");
let snapshots = [];
let ctl = new AbortController();
motion(spring(0, 10), (x) => snapshots.push(x), { signal: ctl.signal });
return snapshots;
});
await advance(1, 0);
await advance(5, 1000 / 60);
expect(await snapshots.jsonValue()).toEqual([
0, 0.47241496285986795, 1.1901835641718526, 2.0130615470443205, 2.8566207208531145,
3.6720468997166984,
]);
});
test("eventual interpolation completeness", async ({ execute, advance }) => {
let snapshots = await execute(async () => {
let { motion, spring } = await import("./oscillation.js");
let snapshots = [];
let ctl = new AbortController();
motion(spring(0, 10), (x) => snapshots.push(x), { signal: ctl.signal });
return snapshots;
});
await advance(1, 0);
await advance(69, 1000 / 60);
expect((await snapshots.jsonValue()).at(-1)).toBe(10);
});
test("interpolation with inconsistent timer condition", async ({ execute, advance }) => {
let snapshots = await execute(async () => {
let { motion, spring } = await import("./oscillation.js");
let snapshots = [];
let ctl = new AbortController();
motion(spring(0, 10), (x) => snapshots.push(x), { signal: ctl.signal });
return snapshots;
});
await advance(1, 0);
expect(await snapshots.jsonValue()).toEqual([0]);
await advance(1, (1000 / 60) * 11);
expect(await snapshots.jsonValue()).toEqual([0]);
await advance(3, 0.1);
expect(await snapshots.jsonValue()).toEqual([
0, 0.0028344897771590463, 0.0056689795543180925, 0.008503469331477139,
]);
await advance(10, 999);
expect(await snapshots.jsonValue()).toEqual([
0, 0.0028344897771590463, 0.0056689795543180925, 0.008503469331477139,
]);
await advance(2, 36);
expect(await snapshots.jsonValue()).toEqual([
0, 0.0028344897771590463, 0.0056689795543180925, 0.008503469331477139, 1.3218440414314472,
3.117557098089461,
]);
});
test("explicit motion cancellation", async ({ execute, advance }) => {
let ctl = await execute(() => new AbortController());
let snapshots = await execute(async (ctl) => {
let { motion, spring } = await import("./oscillation.js");
let snapshots = [];
motion(spring(-10, -100), (x) => snapshots.push(x), { signal: ctl.signal });
return snapshots;
}, ctl);
await advance(1, 0);
await advance(3, 1000 / 60);
await execute((ctl) => ctl.abort(), ctl);
await advance(2, 1000 / 60);
expect(await snapshots.jsonValue()).toEqual([
-10, -14.251734665738812, -20.711652077546674, -28.117553923398887,
]);
});
test("prefers reduced motion", async ({ page, execute, advance }) => {
await page.emulateMedia({ reducedMotion: "reduce" });
let snapshots = await execute(async () => {
let { motion, spring } = await import("./oscillation.js");
let snapshots = [];
let ctl = new AbortController();
motion(spring(0, 10), (x) => snapshots.push(x), { signal: ctl.signal });
return snapshots;
});
await advance(1, 0);
await advance(5, 1000 / 60);
expect(await snapshots.jsonValue()).toEqual([10]);
});