-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy path10_basic.spec.ts
96 lines (86 loc) · 2.68 KB
/
10_basic.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
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
import { describe, expect, it } from 'vitest';
import { memoize } from 'proxy-memoize';
describe('basic spec', () => {
it('x.a', () => {
const fn = memoize((x: { a: number }) => ({ a: x.a }));
expect(fn({ a: 1 })).toEqual({ a: 1 });
expect(fn({ a: 2 })).toBe(fn({ a: 2 }));
});
it('x.a/b/c', () => {
const fn = memoize((x: { a: number; b: number; c?: number }) => ({
a: x.a,
b: x.b,
}));
expect(fn({ a: 1, b: 2 })).toEqual({ a: 1, b: 2 });
expect(fn({ a: 1, b: 2, c: 3 })).toEqual({ a: 1, b: 2 });
expect(fn({ a: 2, b: 3, c: 4 })).toBe(fn({ a: 2, b: 3, c: 5 }));
});
it('x.a.b', () => {
const fn = memoize((x: { a: { b: number } }) => ({ a: x.a }));
expect(fn({ a: { b: 1 } })).toEqual({ a: { b: 1 } });
const a = { b: 2 };
expect(fn({ a })).toBe(fn({ a }));
});
});
describe('circular object', () => {
it('simple array', () => {
const fn = memoize((x: { a: number }) => {
const arr: unknown[] = [x.a];
arr.push(arr);
return arr;
});
expect(fn({ a: 1 })).toBe(fn({ a: 1 }));
});
});
describe('returning objects args', () => {
it("x checked, x returned, don't memoize", () => {
const fn = memoize((x: { a: number; b: number }) => x);
expect(fn({ a: 1, b: 1 })).not.toBe(fn({ a: 1, b: 2 }));
});
it("x.a checked, x returned, don't memoize", () => {
const fn = memoize((x: { a: number; b: number }) => {
if (x.a) {
return x;
}
return null;
});
expect(fn({ a: 1, b: 1 })).not.toBe(fn({ a: 1, b: 2 }));
});
it("x.a checked, x.a returned, don't memoize", () => {
const fn = memoize((x: { a: { b: number; c: number } }) => {
if (x.a) {
return x.a;
}
return null;
});
expect(fn({ a: { b: 1, c: 1 } })).not.toBe(fn({ a: { b: 1, c: 2 } }));
});
it("x.a.b checked, x.a returned, don't memoize", () => {
const fn = memoize((x: { a: { b: number; c: number } }) => {
if (x.a.b) {
return x.a;
}
return null;
});
expect(fn({ a: { b: 1, c: 1 } })).not.toBe(fn({ a: { b: 1, c: 2 } }));
});
it("x.a.b checked, x.a returned and nested, don't memoize", () => {
const fn = memoize((x: { a: { b: number; c: number } }) => {
if (x.a.b) {
return { d: { e: x.a } };
}
return null;
});
expect(fn({ a: { b: 1, c: 1 } })).not.toBe(fn({ a: { b: 1, c: 2 } }));
});
it('x.a.b checked, x.a (same object) returned and nested, do memoize', () => {
const fn = memoize((x: { a: { b: number; c: number } }) => {
if (x.a.b) {
return { d: { e: x.a } };
}
return null;
});
const a = { b: 1, c: 1 };
expect(fn({ a })).toBe(fn({ a }));
});
});