forked from tylerlong/manate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoRun.spec.ts
43 lines (40 loc) · 1.1 KB
/
autoRun.spec.ts
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
import {debounce} from 'lodash';
import waitFor from 'wait-for-async';
import {useProxy, autoRun} from '../src';
describe('autoRun', () => {
test('default', () => {
class Store {
greeting = 'Hello';
}
const store = useProxy(new Store());
const greetings: string[] = [];
const autoRunner = autoRun(store, () => {
// this method auto runs when `store.greeting` changes
greetings.push(store.greeting);
});
autoRunner.start();
store.greeting = 'Hi';
autoRunner.stop();
expect(greetings).toEqual(['Hello', 'Hi']);
});
test('debounce', async () => {
class Store {
number = 0;
}
const store = useProxy(new Store());
const numbers: number[] = [];
const autoRunner = autoRun(
store,
() => numbers.push(store.number),
(func: () => void) => debounce(func, 10, {leading: true, trailing: true})
);
autoRunner.start();
store.number = 1;
store.number = 2;
store.number = 3;
store.number = 4;
expect(numbers).toEqual([0]);
await waitFor({interval: 20});
expect(numbers).toEqual([0, 4]);
});
});