forked from sindresorhus/p-memoize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
117 lines (93 loc) · 2.54 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
107
108
109
110
111
112
113
114
115
116
117
import test from 'ava';
import delay from 'delay';
import pMemoize, {pMemoizeClear} from './index.js';
test('main', async t => {
let index = 0;
const memoized = pMemoize(async () => index++);
t.is(await memoized(), 0);
t.is(await memoized(), 0);
t.is(await memoized(10), 1);
});
test('does memoize consecutive calls', async t => {
let index = 0;
const memoized = pMemoize(async () => index++);
const firstCall = memoized();
const secondCall = memoized();
await Promise.all([firstCall, secondCall]);
t.is(await firstCall, 0);
t.is(await secondCall, 0);
});
test('does not memoize rejected promise', async t => {
let index = 0;
const memoized = pMemoize(async () => {
index++;
if (index === 2) {
throw new Error('fixture');
}
return index;
});
t.is(await memoized(), 1);
t.is(await memoized(), 1);
await t.throwsAsync(memoized(10), {message: 'fixture'});
await t.notThrowsAsync(memoized(10));
t.is(await memoized(10), 3);
t.is(await memoized(10), 3);
t.is(await memoized(100), 4);
});
test('can memoize rejected promise', async t => {
let index = 0;
const memoized = pMemoize(async () => {
index++;
if (index === 2) {
throw new Error('fixture');
}
return index;
}, {
cachePromiseRejection: true,
});
t.is(await memoized(), 1);
t.is(await memoized(), 1);
await t.throwsAsync(memoized(10), {message: 'fixture'});
await t.throwsAsync(memoized(10), {message: 'fixture'});
t.is(await memoized(100), 3);
});
test('preserves the original function name', t => {
t.is(pMemoize(async function foo() {}).name, 'foo'); // eslint-disable-line func-names
});
test('pMemoizeClear()', async t => {
let index = 0;
const fixture = async () => index++;
const memoized = pMemoize(fixture);
t.is(await memoized(), 0);
t.is(await memoized(), 0);
pMemoizeClear(memoized);
t.is(await memoized(), 1);
t.is(await memoized(), 1);
});
test('always returns async function', async t => {
let index = 0;
const fixture = () => index++;
const memoized = pMemoize(fixture);
t.is(await memoized(), 0);
t.is(await memoized(), 0);
});
test('pMemoizeClear() throws when called with a plain function', t => {
t.throws(() => {
pMemoizeClear(() => {});
}, {
message: 'Cannot clear a function that was not memoized!',
});
});
test('maxAge starts on promise settlement', async t => {
let index = 0;
const fixture = async () => {
await delay(40);
return index++;
};
const memoized = pMemoize(fixture, {maxAge: 40});
t.is(await memoized(), 0);
await delay(20);
t.is(await memoized(), 0);
await delay(20);
t.is(await memoized(), 1);
});