-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
88 lines (71 loc) · 1.86 KB
/
Copy pathtest.js
File metadata and controls
88 lines (71 loc) · 1.86 KB
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
import test from 'ava';
import now from 'performance-now';
import nextFrame, { loop, delay, when, until, sequence, wait, throttle } from './dist/nextframe';
test('call next frame with argument', async (t) => {
const value = await nextFrame('check');
t.is(value, 'check');
});
test('resolve when fn returns a truthy value.', async (t) => {
let x = 0;
wait(10).then(() => { x = 1; });
const value = await when(() => x >= 1);
t.is(value, true);
});
test('until fn returns a truthy value do not resolve.', async (t) => {
let x = 0;
wait(10).then(() => { x = 1; });
const value = await until(() => x >= 1);
t.is(value, false);
});
test('delay 1s with args', async (t) => {
const start = now();
const value = await delay(1000, 'check');
const duration = now() - start;
t.is(value, 'check');
t.truthy(duration >= 1000 && duration <= 1100);
});
test('sequence', async (t) => {
const values = [1, 2, 3, 4];
const increment = val => val + 1;
const result = await sequence(values, increment);
t.deepEqual(result, [2, 3, 4, 5]);
});
test('loop', async (t) => {
const p = new Promise((resolve) => {
let i = 0;
const cancel = loop(() => {
++i;
if (i >= 20) {
cancel();
resolve(20);
}
});
});
const result = await p;
t.is(result, 20);
});
test('wait 50 frames', async (t) => {
let i = 0;
const cancel = loop(() => ++i);
const result = await wait(50);
cancel();
t.is(result, 50);
t.is(i, 50);
});
test('throttle frames', async (t) => {
let i = 0;
let throttleCount = 0;
const p = new Promise((resolve) => {
const cancel = loop(() => ++i);
const cancelThrottle = throttle(() => {
if (++throttleCount >= 10) {
cancelThrottle();
cancel();
resolve(throttleCount);
}
}, 10);
});
const result = await p;
t.is(result, 10);
t.is(i, 100);
});