|
| 1 | +/** @jest-environment jsdom */ |
| 2 | +/* eslint-disable react/prop-types */ |
| 3 | +/* eslint-disable no-undef */ |
| 4 | +/* eslint no-magic-numbers: "off" */ |
| 5 | +import React from 'react'; |
| 6 | +import { render } from 'react-dom'; |
| 7 | +import { act } from 'react-dom/test-utils'; |
| 8 | +import useMemoize from './useMemoAll'; |
| 9 | + |
| 10 | +const testHook = fun => { |
| 11 | + let state; |
| 12 | + const UseComponent = ({ useTest }) => { |
| 13 | + useTest(); |
| 14 | + return null; |
| 15 | + }; |
| 16 | + const TestComponent = () => { |
| 17 | + state = React.useState(); |
| 18 | + const [useTest] = state; |
| 19 | + if (useTest) { |
| 20 | + return <UseComponent useTest={useTest} />; |
| 21 | + } |
| 22 | + |
| 23 | + return <React.Fragment />; |
| 24 | + }; |
| 25 | + |
| 26 | + const root = document.createElement('div'); |
| 27 | + render(<TestComponent />, root); |
| 28 | + |
| 29 | + return (...args) => { |
| 30 | + const [_useTest, setTest] = state; |
| 31 | + return new Promise(resolve => { |
| 32 | + act(() => { |
| 33 | + setTest(() => () => resolve(fun(...args))); |
| 34 | + }); |
| 35 | + }); |
| 36 | + }; |
| 37 | +}; |
| 38 | + |
| 39 | +test('useMemoize should cache result across runs', async () => { |
| 40 | + const expensiveSum = jest.fn((x, y) => x + y); |
| 41 | + |
| 42 | + const render = testHook(doMemoChecks => { |
| 43 | + // Start a run, all calls to sum() will be cached. |
| 44 | + useMemoize(expensiveSum, sum => doMemoChecks(sum), []); |
| 45 | + }); |
| 46 | + |
| 47 | + await render(sum => { |
| 48 | + expect(sum(1, 2)).toBe(3); // Not cached, return 3. |
| 49 | + expect(sum(1, 2)).toBe(3); // Cached, return 3. |
| 50 | + expect(sum(2, 4)).toBe(6); // Not cached, return 6. |
| 51 | + expect(sum(1, 2)).toBe(3); // Cached, return 3. This is cached because it is inside the same run. |
| 52 | + }); |
| 53 | + expect(expensiveSum).toHaveBeenCalledTimes(2); |
| 54 | + |
| 55 | + expect(expensiveSum.mock.calls[0]).toEqual([1, 2]); |
| 56 | + expect(expensiveSum.mock.calls[1]).toEqual([2, 4]); |
| 57 | + |
| 58 | + // After the run, 1 + 2 = 3, and 2 + 4 = 6 is cached. |
| 59 | + |
| 60 | + // Start another run with previous cache |
| 61 | + await render(sum => { |
| 62 | + expect(sum(1, 2)).toBe(3); // Cached from previous run, return 3. |
| 63 | + expect(sum(3, 6)).toBe(9); // Not cached, return 9. |
| 64 | + }); |
| 65 | + |
| 66 | + expect(expensiveSum).toHaveBeenCalledTimes(3); |
| 67 | + expect(expensiveSum.mock.calls[2]).toEqual([3, 6]); |
| 68 | + |
| 69 | + // After the run, only 1 + 2 = 3 and 3 + 6 = 9 is cached. 2 + 4 is dropped. |
| 70 | + |
| 71 | + // Start another run with previous cache |
| 72 | + await render(sum => { |
| 73 | + expect(sum(2, 4)).toBe(6); // Not cached, return 6 |
| 74 | + }); |
| 75 | + |
| 76 | + expect(expensiveSum).toHaveBeenCalledTimes(4); |
| 77 | + expect(expensiveSum.mock.calls[3]).toEqual([2, 4]); |
| 78 | +}); |
| 79 | + |
| 80 | +test('useMemoize should cache result if deps change', async () => { |
| 81 | + const expensiveSum = jest.fn((x, y) => x + y); |
| 82 | + |
| 83 | + const render = testHook(doMemoChecks => { |
| 84 | + // Start a run, all calls to sum() will be cached. |
| 85 | + useMemoize(expensiveSum, sum => doMemoChecks(sum), [{}]); |
| 86 | + }); |
| 87 | + |
| 88 | + // Start a run, all calls to sum() will be cached. |
| 89 | + await render(sum => { |
| 90 | + expect(sum(1, 2)).toBe(3); // Not cached, return 3. |
| 91 | + expect(sum(1, 2)).toBe(3); // Cached, return 3. |
| 92 | + expect(sum(2, 4)).toBe(6); // Not cached, return 6. |
| 93 | + expect(sum(1, 2)).toBe(3); // Cached, return 3. This is cached because it is inside the same run. |
| 94 | + }); |
| 95 | + |
| 96 | + expect(expensiveSum).toHaveBeenCalledTimes(2); |
| 97 | + |
| 98 | + expect(expensiveSum.mock.calls[0]).toEqual([1, 2]); |
| 99 | + expect(expensiveSum.mock.calls[1]).toEqual([2, 4]); |
| 100 | + |
| 101 | + // After the run, 1 + 2 = 3, and 2 + 4 = 6 is cached. |
| 102 | + |
| 103 | + // Start another run with previous cache |
| 104 | + await render(sum => { |
| 105 | + expect(sum(1, 2)).toBe(3); // Cached from previous run, return 3. |
| 106 | + expect(sum(3, 6)).toBe(9); // Not cached, return 9. |
| 107 | + }); |
| 108 | + |
| 109 | + expect(expensiveSum).toHaveBeenCalledTimes(3); |
| 110 | + expect(expensiveSum.mock.calls[2]).toEqual([3, 6]); |
| 111 | + |
| 112 | + // After the run, only 1 + 2 = 3 and 3 + 6 = 9 is cached. 2 + 4 is dropped. |
| 113 | + |
| 114 | + // Start another run with previous cache |
| 115 | + await render(sum => { |
| 116 | + expect(sum(1, 2)).toBe(3); // Cached from previous run, return 3. |
| 117 | + expect(sum(2, 4)).toBe(6); // Not cached, return 6 |
| 118 | + }); |
| 119 | + |
| 120 | + expect(expensiveSum).toHaveBeenCalledTimes(4); |
| 121 | + expect(expensiveSum.mock.calls[3]).toEqual([2, 4]); |
| 122 | +}); |
| 123 | + |
| 124 | +test('useMemoize should not share cache across hooks', async () => { |
| 125 | + const expensiveSum = jest.fn((x, y) => x + y); |
| 126 | + |
| 127 | + const render = testHook(doMemoChecks => { |
| 128 | + // Start a run, all calls to sum() will be cached. |
| 129 | + useMemoize(expensiveSum, sum => doMemoChecks(sum), []); |
| 130 | + useMemoize(expensiveSum, sum => doMemoChecks(sum), []); |
| 131 | + }); |
| 132 | + |
| 133 | + // Start a run, all calls to sum() will be cached. |
| 134 | + await render(sum => { |
| 135 | + expect(sum(1, 2)).toBe(3); // Not cached, return 3. |
| 136 | + expect(sum(1, 2)).toBe(3); // Cached, return 3. |
| 137 | + expect(sum(2, 4)).toBe(6); // Not cached, return 6. |
| 138 | + expect(sum(1, 2)).toBe(3); // Cached, return 3. This is cached because it is inside the same run. |
| 139 | + }); |
| 140 | + |
| 141 | + expect(expensiveSum).toHaveBeenCalledTimes(4); |
| 142 | + |
| 143 | + expect(expensiveSum.mock.calls[0]).toEqual([1, 2]); |
| 144 | + expect(expensiveSum.mock.calls[1]).toEqual([2, 4]); |
| 145 | + expect(expensiveSum.mock.calls[2]).toEqual([1, 2]); |
| 146 | + expect(expensiveSum.mock.calls[3]).toEqual([2, 4]); |
| 147 | +}); |
0 commit comments