|
1 | 1 | import '@testing-library/jest-dom';
|
2 | 2 |
|
3 |
| -import React from 'react'; |
| 3 | +import React, { useCallback } from 'react'; |
4 | 4 | import { act, queryByText, render, renderHook } from '@testing-library/react';
|
5 | 5 | import { atom, PrimitiveAtom, useAtomValue } from 'jotai';
|
6 | 6 | import { splitAtom } from 'jotai/utils';
|
7 | 7 |
|
8 | 8 | import { createAtomStore } from './createAtomStore';
|
9 | 9 |
|
10 | 10 | describe('createAtomStore', () => {
|
| 11 | + describe('no unnecessary rerender', () => { |
| 12 | + type MyTestStoreValue = { |
| 13 | + num: number; |
| 14 | + arr: string[]; |
| 15 | + }; |
| 16 | + |
| 17 | + const INITIAL_NUM = 0; |
| 18 | + const INITIAL_ARR = ['alice', 'bob']; |
| 19 | + |
| 20 | + const initialTestStoreValue: MyTestStoreValue = { |
| 21 | + num: INITIAL_NUM, |
| 22 | + arr: INITIAL_ARR, |
| 23 | + }; |
| 24 | + |
| 25 | + const { useMyTestStoreStore, MyTestStoreProvider } = createAtomStore( |
| 26 | + initialTestStoreValue, |
| 27 | + { name: 'myTestStore' as const } |
| 28 | + ); |
| 29 | + |
| 30 | + let numRenderCount = 0; |
| 31 | + const NumRenderer = () => { |
| 32 | + numRenderCount += 1; |
| 33 | + const num = useMyTestStoreStore().useNumValue(); |
| 34 | + return <div>{num}</div>; |
| 35 | + }; |
| 36 | + |
| 37 | + let arrRenderCount = 0; |
| 38 | + const ArrRenderer = () => { |
| 39 | + arrRenderCount += 1; |
| 40 | + const arr = useMyTestStoreStore().useArrValue(); |
| 41 | + return <div>{`[${arr.join(', ')}]`}</div>; |
| 42 | + }; |
| 43 | + |
| 44 | + let arrRendererWithShallowRenderCount = 0; |
| 45 | + const ArrRendererWithShallow = () => { |
| 46 | + arrRendererWithShallowRenderCount += 1; |
| 47 | + const equalityFn = useCallback((a: string[], b: string[]) => { |
| 48 | + if (a.length !== b.length) return false; |
| 49 | + for (let i = 0; i < a.length; i += 1) { |
| 50 | + if (a[i] !== b[i]) return false; |
| 51 | + } |
| 52 | + return true; |
| 53 | + }, []); |
| 54 | + const arr = useMyTestStoreStore().useArrValue(undefined, equalityFn); |
| 55 | + return <div>{`[${arr.join(', ')}]`}</div>; |
| 56 | + }; |
| 57 | + |
| 58 | + let arr0RenderCount = 0; |
| 59 | + const Arr0Renderer = () => { |
| 60 | + arr0RenderCount += 1; |
| 61 | + const select0 = useCallback((v: string[]) => v[0], []); |
| 62 | + const arr0 = useMyTestStoreStore().useArrValue(select0); |
| 63 | + return <div>{arr0}</div>; |
| 64 | + }; |
| 65 | + |
| 66 | + let arr1RenderCount = 0; |
| 67 | + const Arr1Renderer = () => { |
| 68 | + arr1RenderCount += 1; |
| 69 | + const select1 = useCallback((v: string[]) => v[1], []); |
| 70 | + const arr1 = useMyTestStoreStore().useArrValue(select1); |
| 71 | + return <div>{arr1}</div>; |
| 72 | + }; |
| 73 | + |
| 74 | + let arrNumRenderCount = 0; |
| 75 | + const ArrNumRenderer = () => { |
| 76 | + arrNumRenderCount += 1; |
| 77 | + const store = useMyTestStoreStore(); |
| 78 | + const num = store.useNumValue(); |
| 79 | + const selectArrNum = useCallback((v: string[]) => v[num], [num]); |
| 80 | + const arrNum = store.useArrValue(selectArrNum); |
| 81 | + return ( |
| 82 | + <div> |
| 83 | + <div>arrNum: {arrNum}</div> |
| 84 | + </div> |
| 85 | + ); |
| 86 | + }; |
| 87 | + |
| 88 | + let arrNumRenderWithDepsCount = 0; |
| 89 | + const ArrNumRendererWithDeps = () => { |
| 90 | + arrNumRenderWithDepsCount += 1; |
| 91 | + const store = useMyTestStoreStore(); |
| 92 | + const num = store.useNumValue(); |
| 93 | + const arrNum = store.useArrValue((v) => v[num], [num]); |
| 94 | + return ( |
| 95 | + <div> |
| 96 | + <div>arrNumWithDeps: {arrNum}</div> |
| 97 | + </div> |
| 98 | + ); |
| 99 | + }; |
| 100 | + |
| 101 | + const BadSelectorRenderer = () => { |
| 102 | + const arr0 = useMyTestStoreStore().useArrValue((v) => v[0]); |
| 103 | + return <div>{arr0}</div>; |
| 104 | + }; |
| 105 | + |
| 106 | + const Buttons = () => { |
| 107 | + const store = useMyTestStoreStore(); |
| 108 | + return ( |
| 109 | + <div> |
| 110 | + <button |
| 111 | + type="button" |
| 112 | + onClick={() => store.setNum(store.getNum() + 1)} |
| 113 | + > |
| 114 | + increment |
| 115 | + </button> |
| 116 | + <button |
| 117 | + type="button" |
| 118 | + onClick={() => store.setArr([...store.getArr(), 'charlie'])} |
| 119 | + > |
| 120 | + add one name |
| 121 | + </button> |
| 122 | + <button |
| 123 | + type="button" |
| 124 | + onClick={() => store.setArr([...store.getArr()])} |
| 125 | + > |
| 126 | + copy array |
| 127 | + </button> |
| 128 | + <button |
| 129 | + type="button" |
| 130 | + onClick={() => { |
| 131 | + store.setArr(['ava', ...store.getArr().slice(1)]); |
| 132 | + store.setNum(0); |
| 133 | + }} |
| 134 | + > |
| 135 | + modify arr0 |
| 136 | + </button> |
| 137 | + </div> |
| 138 | + ); |
| 139 | + }; |
| 140 | + |
| 141 | + it('does not rerender when unrelated state changes', () => { |
| 142 | + const { getByText } = render( |
| 143 | + <MyTestStoreProvider> |
| 144 | + <NumRenderer /> |
| 145 | + <ArrRenderer /> |
| 146 | + <ArrRendererWithShallow /> |
| 147 | + <Arr0Renderer /> |
| 148 | + <Arr1Renderer /> |
| 149 | + <ArrNumRenderer /> |
| 150 | + <ArrNumRendererWithDeps /> |
| 151 | + <Buttons /> |
| 152 | + </MyTestStoreProvider> |
| 153 | + ); |
| 154 | + |
| 155 | + // Why it's 2, not 1? Is React StrictMode causing this? |
| 156 | + expect(numRenderCount).toBe(2); |
| 157 | + expect(arrRenderCount).toBe(2); |
| 158 | + expect(arrRendererWithShallowRenderCount).toBe(2); |
| 159 | + expect(arr0RenderCount).toBe(2); |
| 160 | + expect(arr1RenderCount).toBe(2); |
| 161 | + expect(arrNumRenderCount).toBe(2); |
| 162 | + expect(arrNumRenderWithDepsCount).toBe(2); |
| 163 | + expect(getByText('arrNum: alice')).toBeInTheDocument(); |
| 164 | + expect(getByText('arrNumWithDeps: alice')).toBeInTheDocument(); |
| 165 | + |
| 166 | + act(() => getByText('increment').click()); |
| 167 | + expect(numRenderCount).toBe(3); |
| 168 | + expect(arrRenderCount).toBe(2); |
| 169 | + expect(arrRendererWithShallowRenderCount).toBe(2); |
| 170 | + expect(arr0RenderCount).toBe(2); |
| 171 | + expect(arr1RenderCount).toBe(2); |
| 172 | + expect(arrNumRenderCount).toBe(5); |
| 173 | + expect(arrNumRenderWithDepsCount).toBe(5); |
| 174 | + expect(getByText('arrNum: bob')).toBeInTheDocument(); |
| 175 | + expect(getByText('arrNumWithDeps: bob')).toBeInTheDocument(); |
| 176 | + |
| 177 | + act(() => getByText('add one name').click()); |
| 178 | + expect(numRenderCount).toBe(3); |
| 179 | + expect(arrRenderCount).toBe(3); |
| 180 | + expect(arrRendererWithShallowRenderCount).toBe(3); |
| 181 | + expect(arr0RenderCount).toBe(2); |
| 182 | + expect(arr1RenderCount).toBe(2); |
| 183 | + expect(arrNumRenderCount).toBe(5); |
| 184 | + expect(arrNumRenderWithDepsCount).toBe(5); |
| 185 | + expect(getByText('arrNum: bob')).toBeInTheDocument(); |
| 186 | + expect(getByText('arrNumWithDeps: bob')).toBeInTheDocument(); |
| 187 | + |
| 188 | + act(() => getByText('copy array').click()); |
| 189 | + expect(numRenderCount).toBe(3); |
| 190 | + expect(arrRenderCount).toBe(4); |
| 191 | + expect(arrRendererWithShallowRenderCount).toBe(3); |
| 192 | + expect(arr0RenderCount).toBe(2); |
| 193 | + expect(arr1RenderCount).toBe(2); |
| 194 | + expect(arrNumRenderCount).toBe(5); |
| 195 | + expect(arrNumRenderWithDepsCount).toBe(5); |
| 196 | + expect(getByText('arrNum: bob')).toBeInTheDocument(); |
| 197 | + expect(getByText('arrNumWithDeps: bob')).toBeInTheDocument(); |
| 198 | + |
| 199 | + act(() => getByText('modify arr0').click()); |
| 200 | + expect(numRenderCount).toBe(4); |
| 201 | + expect(arrRenderCount).toBe(5); |
| 202 | + expect(arrRendererWithShallowRenderCount).toBe(4); |
| 203 | + expect(arr0RenderCount).toBe(3); |
| 204 | + expect(arr1RenderCount).toBe(2); |
| 205 | + expect(arrNumRenderCount).toBe(8); |
| 206 | + expect(arrNumRenderWithDepsCount).toBe(8); |
| 207 | + expect(getByText('arrNum: ava')).toBeInTheDocument(); |
| 208 | + expect(getByText('arrNumWithDeps: ava')).toBeInTheDocument(); |
| 209 | + }); |
| 210 | + |
| 211 | + it('Throw error if user does not memoize selector', () => { |
| 212 | + expect(() => |
| 213 | + render( |
| 214 | + <MyTestStoreProvider> |
| 215 | + <BadSelectorRenderer /> |
| 216 | + </MyTestStoreProvider> |
| 217 | + ) |
| 218 | + ).toThrow(); |
| 219 | + }); |
| 220 | + }); |
| 221 | + |
11 | 222 | describe('single provider', () => {
|
12 | 223 | type MyTestStoreValue = {
|
13 | 224 | name: string;
|
|
0 commit comments