-
Notifications
You must be signed in to change notification settings - Fork 0
/
prod.test.js
64 lines (57 loc) · 1.54 KB
/
prod.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
// @ts-check
import assert from "node:assert";
import { mock, test } from "node:test";
import memoizeeOne from "./index.js";
test("success", () => {
let calls = 0;
const myMock = (/** @type {number} */ a) => {
calls++;
return a + 1;
};
const memoized = memoizeeOne(myMock);
memoized(1);
memoized(1);
assert.strictEqual(calls, 1);
memoized(2);
assert.strictEqual(calls, 2);
assert.strictEqual(memoized(2), 3);
});
test("with different this", () => {
let calls = 0;
function fn(/** @type {number} */ a) {
calls++;
// @ts-expect-error
return [this, a + 1];
}
const memoized = memoizeeOne(fn);
const this1 = {};
memoized.call(this1, 1);
assert.strictEqual(memoized(1)[0], this1);
assert.strictEqual(memoized(1)[1], 2);
assert.strictEqual(calls, 1);
const this2 = {};
memoized.call(this2, 2);
assert.strictEqual(memoized(2)[0], this2);
assert.strictEqual(memoized(2)[1], 3);
assert.strictEqual(calls, 2);
memoized.clear();
memoized(2);
assert.strictEqual(memoized(2)[0], undefined);
assert.strictEqual(memoized(2)[1], 3);
assert.strictEqual(calls, 3);
});
test("errored", () => {
const error = new Error();
const myMock = mock.fn((/** @type {number} */ a) => {
if (a === 1) {
throw error;
}
return 42;
});
const memoized = memoizeeOne(myMock);
assert.throws(() => memoized(1), error);
assert.throws(() => memoized(1), error);
assert.strictEqual(myMock.mock.callCount(), 1);
assert.strictEqual(memoized(2), 42);
assert.strictEqual(myMock.mock.callCount(), 2);
});