forked from sindresorhus/memoize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
246 lines (214 loc) · 5.45 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import test from 'ava';
import delay from 'delay';
import mem from '.';
test('memoize', t => {
let i = 0;
const fixture = () => i++;
const memoized = mem(fixture);
t.is(memoized(), 0);
t.is(memoized(), 0);
t.is(memoized(), 0);
t.is(memoized('foo'), 1);
t.is(memoized('foo'), 1);
t.is(memoized('foo'), 1);
t.is(memoized('foo', 'bar'), 2);
t.is(memoized('foo', 'bar'), 2);
t.is(memoized('foo', 'bar'), 2);
t.is(memoized(1), 3);
t.is(memoized(1), 3);
t.is(memoized(null), 4);
t.is(memoized(null), 4);
t.is(memoized(undefined), 5);
t.is(memoized(undefined), 5);
t.is(memoized(fixture), 6);
t.is(memoized(fixture), 6);
t.is(memoized(true), 7);
t.is(memoized(true), 7);
// Ensure that functions are stored by reference and not by "value" (e.g. their `.toString()` representation)
t.is(memoized(() => i++), 8);
t.is(memoized(() => i++), 9);
});
test('memoize with multiple non-primitive arguments', t => {
let i = 0;
const memoized = mem(() => i++);
t.is(memoized(), 0);
t.is(memoized(), 0);
t.is(memoized({foo: true}, {bar: false}), 1);
t.is(memoized({foo: true}, {bar: false}), 1);
t.is(memoized({foo: true}, {bar: false}, {baz: true}), 2);
t.is(memoized({foo: true}, {bar: false}, {baz: true}), 2);
});
test.failing('memoize with regexp arguments', t => {
let i = 0;
const memoized = mem(() => i++);
t.is(memoized(), 0);
t.is(memoized(), 0);
t.is(memoized(/Sindre Sorhus/), 1);
t.is(memoized(/Sindre Sorhus/), 1);
t.is(memoized(/Elvin Peng/), 2);
t.is(memoized(/Elvin Peng/), 2);
});
test.failing('memoize with Symbol arguments', t => {
let i = 0;
const argument1 = Symbol('fixture1');
const argument2 = Symbol('fixture2');
const memoized = mem(() => i++);
t.is(memoized(), 0);
t.is(memoized(), 0);
t.is(memoized(argument1), 1);
t.is(memoized(argument1), 1);
t.is(memoized(argument2), 2);
t.is(memoized(argument2), 2);
t.is(memoized({foo: argument1}), 3);
t.is(memoized({foo: argument1}), 3);
t.is(memoized({foo: argument2}), 4);
t.is(memoized({foo: argument2}), 4);
});
test('maxAge option', async t => {
let i = 0;
const fixture = () => i++;
const memoized = mem(fixture, {maxAge: 100});
t.is(memoized(1), 0);
t.is(memoized(1), 0);
await delay(50);
t.is(memoized(1), 0);
await delay(200);
t.is(memoized(1), 1);
});
test('maxAge option deletes old items', async t => {
let i = 0;
const fixture = () => i++;
const cache = new Map();
const deleted = [];
const remove = cache.delete.bind(cache);
cache.delete = item => {
deleted.push(item);
return remove(item);
};
const memoized = mem(fixture, {maxAge: 100, cache});
t.is(memoized(1), 0);
t.is(memoized(1), 0);
t.is(cache.has(1), true);
await delay(50);
t.is(memoized(1), 0);
t.is(deleted.length, 0);
await delay(200);
t.is(memoized(1), 1);
t.is(deleted.length, 1);
t.is(deleted[0], 1);
});
test('maxAge items are deleted even if function throws', async t => {
let i = 0;
const fixture = () => {
if (i === 1) {
throw new Error('failure');
}
return i++;
};
const cache = new Map();
const memoized = mem(fixture, {maxAge: 100, cache});
t.is(memoized(1), 0);
t.is(memoized(1), 0);
t.is(cache.size, 1);
await delay(50);
t.is(memoized(1), 0);
await delay(200);
t.throws(() => memoized(1), 'failure');
t.is(cache.size, 0);
});
test('cacheKey option', t => {
let i = 0;
const fixture = () => i++;
const memoized = mem(fixture, {cacheKey: x => x});
t.is(memoized(1), 0);
t.is(memoized(1), 0);
t.is(memoized(1, 2), 0);
t.is(memoized(2), 1);
t.is(memoized(2, 1), 1);
});
test('cache option', t => {
let i = 0;
const fixture = () => i++;
const memoized = mem(fixture, {
cache: new WeakMap(),
cacheKey: x => x
});
const foo = {};
const bar = {};
t.is(memoized(foo), 0);
t.is(memoized(foo), 0);
t.is(memoized(bar), 1);
t.is(memoized(bar), 1);
});
test('promise support', async t => {
let i = 0;
const memoized = mem(async () => i++);
t.is(await memoized(), 0);
t.is(await memoized(), 0);
t.is(await memoized(10), 1);
});
test('cachePromiseRejection option', async t => {
let i = 0;
const memoized = mem(async () => {
i++;
if (i === 1) {
throw new Error('foo bar');
}
return i;
}, {
cachePromiseRejection: false
});
await t.throwsAsync(memoized(), 'foo bar');
const first = memoized();
const second = memoized();
const third = memoized();
t.is(await first, 2);
t.is(await second, 2);
t.is(await third, 2);
});
test('cache rejected promises if enabled', async t => {
let i = 0;
const memoized = mem(async () => {
i++;
if (i === 1) {
throw new Error('foo bar');
}
return i;
}, {
cachePromiseRejection: true
});
await t.throwsAsync(memoized(), 'foo bar');
await t.throwsAsync(memoized(), 'foo bar');
await t.throwsAsync(memoized(), 'foo bar');
});
test('preserves the original function name', t => {
t.is(mem(function foo() {}).name, 'foo'); // eslint-disable-line func-names
});
test('.clear()', t => {
let i = 0;
const fixture = () => i++;
const memoized = mem(fixture);
t.is(memoized(), 0);
t.is(memoized(), 0);
mem.clear(memoized);
t.is(memoized(), 1);
t.is(memoized(), 1);
});
test('prototype support', t => {
const fixture = function () {
return this.i++;
};
const Unicorn = function () {
this.i = 0;
};
Unicorn.prototype.foo = mem(fixture);
const unicorn = new Unicorn();
t.is(unicorn.foo(), 0);
t.is(unicorn.foo(), 0);
t.is(unicorn.foo(), 0);
});
test('mem.clear() throws when called with a plain function', t => {
t.throws(() => {
mem.clear(() => {});
}, 'Can\'t clear a function that was not memoized!');
});