-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
106 lines (90 loc) · 2.08 KB
/
test.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
const test = require('tape');
const {getterThrottle} = require('./index');
function computedFrom(...rest) {
return function(target, key, descriptor) {
descriptor.get.dependencies = rest;
return descriptor;
};
}
class A {
constructor() {
Object.defineProperties(this, {
fooCount: {
value: 0,
writable: true
},
barCount: {
value: 0,
writable: true
}
});
}
@getterThrottle()
@computedFrom('a', 'b')
get foo() {
this.fooCount += 1;
return 'foo';
}
@computedFrom('c', 'd')
@getterThrottle()
get foo2() {
}
@getterThrottle(100)
get bar() {
this.barCount += 1;
return 'bar';
}
}
test('getterThrottle preserves computed from', t => {
t.deepEqual(
Object.getOwnPropertyDescriptor(A.prototype, 'foo').get.dependencies,
['a', 'b'],
'when above computedFrom'
);
t.deepEqual(
Object.getOwnPropertyDescriptor(A.prototype, 'foo2').get.dependencies,
['c', 'd'],
'when below computedFrom'
);
t.end();
});
test('getterThrottle caches getter value in same run-loop by default', t => {
let a = new A();
let b = new A();
t.equal(a.foo, 'foo');
t.equal(a.foo, 'foo');
t.equal(Object.keys(a).length, 0);
t.equal(a.fooCount, 1);
t.equal(b.fooCount, 0);
setTimeout(() => {
t.equal(a.foo, 'foo');
t.equal(a.foo, 'foo');
t.equal(a.fooCount, 2);
t.equal(Object.keys(a).length, 0);
t.equal(b.foo, 'foo');
t.equal(b.foo, 'foo');
t.equal(b.fooCount, 1);
t.equal(Object.keys(b).length, 0);
t.end();
}, 20);
});
test('getterThrottle caches getter value within delay', t => {
let a = new A();
t.equal(a.bar, 'bar');
t.equal(a.bar, 'bar');
t.equal(Object.keys(a).length, 0);
t.equal(a.barCount, 1);
setTimeout(() => {
t.equal(a.bar, 'bar');
t.equal(a.bar, 'bar');
t.equal(Object.keys(a).length, 0);
t.equal(a.barCount, 1);
setTimeout(() => {
t.equal(a.bar, 'bar');
t.equal(a.bar, 'bar');
t.equal(Object.keys(a).length, 0);
t.equal(a.barCount, 2);
t.end();
}, 100);
}, 50);
});