|
1 | 1 | import { memoize } from "./memoize";
|
2 |
| -import { Provider } from "@aws-sdk/types"; |
3 | 2 |
|
4 | 3 | describe("memoize", () => {
|
| 4 | + let provider: jest.Mock; |
| 5 | + const mockReturn = "foo"; |
| 6 | + const repeatTimes = 10; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + provider = jest.fn().mockResolvedValue(mockReturn); |
| 10 | + }); |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + jest.clearAllMocks(); |
| 14 | + }); |
| 15 | + |
5 | 16 | describe("static memoization", () => {
|
6 | 17 | it("should cache the resolved provider", async () => {
|
7 |
| - const provider = jest.fn().mockResolvedValue("foo"); |
| 18 | + expect.assertions(repeatTimes * 2); |
| 19 | + |
8 | 20 | const memoized = memoize(provider);
|
9 | 21 |
|
10 |
| - expect(await memoized()).toEqual("foo"); |
11 |
| - expect(provider.mock.calls.length).toBe(1); |
12 |
| - expect(await memoized()).toEqual("foo"); |
13 |
| - expect(provider.mock.calls.length).toBe(1); |
| 22 | + for (const index in [...Array(repeatTimes).keys()]) { |
| 23 | + expect(await memoized()).toStrictEqual(mockReturn); |
| 24 | + expect(provider).toHaveBeenCalledTimes(1); |
| 25 | + } |
14 | 26 | });
|
15 | 27 |
|
16 | 28 | it("should always return the same promise", () => {
|
17 |
| - const provider = jest.fn().mockResolvedValue("foo"); |
| 29 | + expect.assertions(repeatTimes * 2); |
| 30 | + |
18 | 31 | const memoized = memoize(provider);
|
19 | 32 | const result = memoized();
|
20 | 33 |
|
21 |
| - expect(memoized()).toBe(result); |
| 34 | + for (const index in [...Array(repeatTimes).keys()]) { |
| 35 | + expect(memoized()).toStrictEqual(result); |
| 36 | + expect(provider).toHaveBeenCalledTimes(1); |
| 37 | + } |
22 | 38 | });
|
23 | 39 | });
|
24 | 40 |
|
25 | 41 | describe("refreshing memoization", () => {
|
26 |
| - it("should not reinvoke the underlying provider while isExpired returns `false`", async () => { |
27 |
| - const provider = jest.fn().mockResolvedValue("foo"); |
28 |
| - const isExpired = jest.fn().mockReturnValue(false); |
29 |
| - const memoized = memoize(provider, isExpired); |
30 |
| - |
31 |
| - const checkCount = 10; |
32 |
| - for (let i = 0; i < checkCount; i++) { |
33 |
| - expect(await memoized()).toBe("foo"); |
34 |
| - } |
| 42 | + let isExpired: jest.Mock; |
| 43 | + let requiresRefresh: jest.Mock; |
35 | 44 |
|
36 |
| - expect(isExpired.mock.calls.length).toBe(checkCount); |
37 |
| - expect(provider.mock.calls.length).toBe(1); |
| 45 | + beforeEach(() => { |
| 46 | + isExpired = jest.fn().mockReturnValue(true); |
| 47 | + requiresRefresh = jest.fn().mockReturnValue(false); |
38 | 48 | });
|
39 | 49 |
|
40 |
| - it("should reinvoke the underlying provider when isExpired returns `true`", async () => { |
41 |
| - const provider = jest.fn().mockResolvedValue("foo"); |
42 |
| - const isExpired = jest.fn().mockReturnValue(false); |
43 |
| - const memoized = memoize(provider, isExpired); |
44 |
| - |
45 |
| - const checkCount = 10; |
46 |
| - for (let i = 0; i < checkCount; i++) { |
47 |
| - expect(await memoized()).toBe("foo"); |
48 |
| - } |
49 |
| - |
50 |
| - expect(isExpired.mock.calls.length).toBe(checkCount); |
51 |
| - expect(provider.mock.calls.length).toBe(1); |
52 |
| - |
53 |
| - isExpired.mockReturnValueOnce(true); |
54 |
| - for (let i = 0; i < checkCount; i++) { |
55 |
| - expect(await memoized()).toBe("foo"); |
56 |
| - } |
57 |
| - |
58 |
| - expect(isExpired.mock.calls.length).toBe(checkCount * 2); |
59 |
| - expect(provider.mock.calls.length).toBe(2); |
| 50 | + describe("should not reinvoke the underlying provider while isExpired returns `false`", () => { |
| 51 | + const isExpiredFalseTest = async (requiresRefresh?: any) => { |
| 52 | + isExpired.mockReturnValue(false); |
| 53 | + const memoized = memoize(provider, isExpired, requiresRefresh); |
| 54 | + |
| 55 | + for (const index in [...Array(repeatTimes).keys()]) { |
| 56 | + expect(await memoized()).toEqual(mockReturn); |
| 57 | + } |
| 58 | + |
| 59 | + expect(isExpired).toHaveBeenCalledTimes(repeatTimes); |
| 60 | + if (requiresRefresh) { |
| 61 | + expect(requiresRefresh).toHaveBeenCalledTimes(repeatTimes); |
| 62 | + } |
| 63 | + expect(provider).toHaveBeenCalledTimes(1); |
| 64 | + }; |
| 65 | + |
| 66 | + it("when requiresRefresh is not passed", async () => { |
| 67 | + return isExpiredFalseTest(); |
| 68 | + }); |
| 69 | + |
| 70 | + it("when requiresRefresh returns true", () => { |
| 71 | + requiresRefresh.mockReturnValue(true); |
| 72 | + return isExpiredFalseTest(requiresRefresh); |
| 73 | + }); |
60 | 74 | });
|
61 | 75 |
|
62 |
| - it("should return the same promise for invocations 2-infinity if `requiresRefresh` returns `false`", async () => { |
63 |
| - const provider = jest.fn().mockResolvedValue("foo"); |
64 |
| - const isExpired = jest.fn().mockReturnValue(true); |
65 |
| - const requiresRefresh = jest.fn().mockReturnValue(false); |
66 |
| - |
67 |
| - const memoized = memoize(provider, isExpired, requiresRefresh); |
68 |
| - expect(await memoized()).toBe("foo"); |
69 |
| - const set = new Set<Promise<string>>(); |
70 |
| - |
71 |
| - const checkCount = 10; |
72 |
| - for (let i = 0; i < checkCount; i++) { |
73 |
| - set.add(memoized()); |
74 |
| - } |
| 76 | + describe("should reinvoke the underlying provider when isExpired returns `true`", () => { |
| 77 | + const isExpiredTrueTest = async (requiresRefresh?: any) => { |
| 78 | + const memoized = memoize(provider, isExpired, requiresRefresh); |
| 79 | + |
| 80 | + for (const index in [...Array(repeatTimes).keys()]) { |
| 81 | + expect(await memoized()).toEqual(mockReturn); |
| 82 | + } |
| 83 | + |
| 84 | + expect(isExpired).toHaveBeenCalledTimes(repeatTimes); |
| 85 | + if (requiresRefresh) { |
| 86 | + expect(requiresRefresh).toHaveBeenCalledTimes(repeatTimes); |
| 87 | + } |
| 88 | + expect(provider).toHaveBeenCalledTimes(repeatTimes + 1); |
| 89 | + }; |
| 90 | + |
| 91 | + it("when requiresRefresh is not passed", () => { |
| 92 | + return isExpiredTrueTest(); |
| 93 | + }); |
| 94 | + |
| 95 | + it("when requiresRefresh returns true", () => { |
| 96 | + requiresRefresh.mockReturnValue(true); |
| 97 | + return isExpiredTrueTest(requiresRefresh); |
| 98 | + }); |
| 99 | + }); |
75 | 100 |
|
76 |
| - expect(set.size).toBe(1); |
| 101 | + describe("should return the same promise for invocations 2-infinity if `requiresRefresh` returns `false`", () => { |
| 102 | + const requiresRefreshFalseTest = async () => { |
| 103 | + const memoized = memoize(provider, isExpired, requiresRefresh); |
| 104 | + const result = memoized(); |
| 105 | + expect(await result).toBe(mockReturn); |
| 106 | + |
| 107 | + for (const index in [...Array(repeatTimes).keys()]) { |
| 108 | + expect(memoized()).toStrictEqual(result); |
| 109 | + expect(provider).toHaveBeenCalledTimes(1); |
| 110 | + } |
| 111 | + |
| 112 | + expect(requiresRefresh).toHaveBeenCalledTimes(1); |
| 113 | + expect(isExpired).not.toHaveBeenCalled(); |
| 114 | + }; |
| 115 | + |
| 116 | + it("when isExpired returns true", () => { |
| 117 | + return requiresRefreshFalseTest(); |
| 118 | + }); |
| 119 | + |
| 120 | + it("when isExpired returns false", () => { |
| 121 | + isExpired.mockReturnValue(false); |
| 122 | + return requiresRefreshFalseTest(); |
| 123 | + }); |
77 | 124 | });
|
78 | 125 | });
|
79 | 126 | });
|
0 commit comments