|
| 1 | +import React, { useEffect } from 'react'; |
| 2 | +import { mount } from 'enzyme'; |
| 3 | +import { act } from 'react-dom/test-utils'; |
| 4 | +import { createStore, useStore } from '..'; |
| 5 | + |
| 6 | +describe('store memoization', () => { |
| 7 | + it('Should not update components if setState is called with the same previous state', (done) => { |
| 8 | + const store = createStore('store', 0); |
| 9 | + const componentRenderCount = jest.fn(); |
| 10 | + const anotherComponentRenderCount = jest.fn(); |
| 11 | + |
| 12 | + const Component = (props) => { |
| 13 | + const [state, setState] = useStore(store); |
| 14 | + |
| 15 | + componentRenderCount(); |
| 16 | + |
| 17 | + return ( |
| 18 | + <button onClick={() => setState(state+1)}> |
| 19 | + This button has been clicked {state} times |
| 20 | + </button> |
| 21 | + ); |
| 22 | + } |
| 23 | + |
| 24 | + const AnotherComponent = (props) => { |
| 25 | + const [state] = useStore(store); |
| 26 | + |
| 27 | + anotherComponentRenderCount(); |
| 28 | + |
| 29 | + return ( |
| 30 | + <div> |
| 31 | + {state} |
| 32 | + </div> |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + const renderedComponent = mount(<Component />); |
| 37 | + const renderedAnotherComponent = mount(<AnotherComponent />); |
| 38 | + |
| 39 | + requestAnimationFrame(() => { |
| 40 | + expect(componentRenderCount).toHaveBeenCalledTimes(1); |
| 41 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(1); |
| 42 | + |
| 43 | + renderedComponent.find('button').simulate('click'); |
| 44 | + expect(componentRenderCount).toHaveBeenCalledTimes(2); |
| 45 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(2); |
| 46 | + |
| 47 | + act(() => store.setState(3)); |
| 48 | + expect(componentRenderCount).toHaveBeenCalledTimes(3); |
| 49 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(3); |
| 50 | + |
| 51 | + act(() => store.setState(3)); |
| 52 | + expect(componentRenderCount).toHaveBeenCalledTimes(3); |
| 53 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(3); |
| 54 | + |
| 55 | + act(() => store.setState(3)); |
| 56 | + expect(componentRenderCount).toHaveBeenCalledTimes(3); |
| 57 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(3); |
| 58 | + |
| 59 | + act(() => store.setState(1)); |
| 60 | + expect(componentRenderCount).toHaveBeenCalledTimes(4); |
| 61 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(4); |
| 62 | + |
| 63 | + act(() => store.setState(1)); |
| 64 | + expect(componentRenderCount).toHaveBeenCalledTimes(4); |
| 65 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(4); |
| 66 | + |
| 67 | + expect(renderedComponent.text()).toBe('This button has been clicked 1 times') |
| 68 | + expect(renderedAnotherComponent.text()).toBe('1') |
| 69 | + |
| 70 | + done(); |
| 71 | + }) |
| 72 | + }); |
| 73 | + |
| 74 | + it('Should always update components if state is complex and no mapDeps is provided', (done) => { |
| 75 | + const store = createStore('complexState', { number: 0 }); |
| 76 | + const componentRenderCount = jest.fn(); |
| 77 | + const anotherComponentRenderCount = jest.fn(); |
| 78 | + |
| 79 | + const Component = (props) => { |
| 80 | + const [state, setState] = useStore(store); |
| 81 | + |
| 82 | + componentRenderCount(); |
| 83 | + |
| 84 | + return ( |
| 85 | + <button onClick={() => setState({ number: state.number + 1})}> |
| 86 | + This button has been clicked {state.number} times |
| 87 | + </button> |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + const AnotherComponent = (props) => { |
| 92 | + const [state] = useStore(store); |
| 93 | + |
| 94 | + anotherComponentRenderCount(); |
| 95 | + |
| 96 | + return ( |
| 97 | + <div> |
| 98 | + {state.number} |
| 99 | + </div> |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + const renderedComponent = mount(<Component />); |
| 104 | + const renderedAnotherComponent = mount(<AnotherComponent />); |
| 105 | + |
| 106 | + requestAnimationFrame(() => { |
| 107 | + expect(componentRenderCount).toHaveBeenCalledTimes(1); |
| 108 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(1); |
| 109 | + |
| 110 | + renderedComponent.find('button').simulate('click'); |
| 111 | + expect(componentRenderCount).toHaveBeenCalledTimes(2); |
| 112 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(2); |
| 113 | + |
| 114 | + act(() => store.setState({ number: 3 })); |
| 115 | + expect(componentRenderCount).toHaveBeenCalledTimes(3); |
| 116 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(3); |
| 117 | + |
| 118 | + act(() => store.setState({ number: 3 })); |
| 119 | + expect(componentRenderCount).toHaveBeenCalledTimes(4); |
| 120 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(4); |
| 121 | + |
| 122 | + act(() => store.setState({ number: 3 })); |
| 123 | + expect(componentRenderCount).toHaveBeenCalledTimes(5); |
| 124 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(5); |
| 125 | + |
| 126 | + act(() => store.setState({ number: 1 })); |
| 127 | + expect(componentRenderCount).toHaveBeenCalledTimes(6); |
| 128 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(6); |
| 129 | + |
| 130 | + act(() => store.setState({ number: 1 })); |
| 131 | + expect(componentRenderCount).toHaveBeenCalledTimes(7); |
| 132 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(7); |
| 133 | + |
| 134 | + expect(renderedComponent.text()).toBe('This button has been clicked 1 times') |
| 135 | + expect(renderedAnotherComponent.text()).toBe('1') |
| 136 | + |
| 137 | + done(); |
| 138 | + }) |
| 139 | + }); |
| 140 | + |
| 141 | + it('Should only update component if the field it wants is updated', (done) => { |
| 142 | + const store = createStore('complexStore2', {foo: { bar: 2 }, baz: 3}); |
| 143 | + |
| 144 | + const componentRenderCount = jest.fn(); |
| 145 | + const anotherComponentRenderCount = jest.fn(); |
| 146 | + |
| 147 | + const memoBar = (state) => state.foo && state.foo.bar; |
| 148 | + const memoBaz = (state) => state.baz; |
| 149 | + |
| 150 | + const Component = (props) => { |
| 151 | + const [state] = useStore(store, memoBar); |
| 152 | + |
| 153 | + componentRenderCount(); |
| 154 | + |
| 155 | + return ( |
| 156 | + <div> |
| 157 | + {state.foo && state.foo.bar} |
| 158 | + </div> |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + const AnotherComponent = (props) => { |
| 163 | + const [state] = useStore(store, memoBaz); |
| 164 | + |
| 165 | + anotherComponentRenderCount(); |
| 166 | + |
| 167 | + return ( |
| 168 | + <div> |
| 169 | + {state.baz} |
| 170 | + </div> |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + const renderedComponent = mount(<Component />); |
| 175 | + const renderedAnotherComponent = mount(<AnotherComponent />); |
| 176 | + |
| 177 | + requestAnimationFrame(() => { |
| 178 | + expect(componentRenderCount).toHaveBeenCalledTimes(1); |
| 179 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(1); |
| 180 | + |
| 181 | + act(() => store.setState({...store.getState(), baz: 2})); |
| 182 | + expect(componentRenderCount).toHaveBeenCalledTimes(1); |
| 183 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(2); |
| 184 | + |
| 185 | + act(() => store.setState({...store.getState(), foo: {bar: 2}})); |
| 186 | + expect(componentRenderCount).toHaveBeenCalledTimes(1); |
| 187 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(2); |
| 188 | + |
| 189 | + act(() => store.setState({...store.getState(), foo: {bar: 3}})); |
| 190 | + expect(componentRenderCount).toHaveBeenCalledTimes(2); |
| 191 | + |
| 192 | + act(() => store.setState({...store.getState()})); |
| 193 | + expect(componentRenderCount).toHaveBeenCalledTimes(2); |
| 194 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(2); |
| 195 | + |
| 196 | + act(() => store.setState({...store.getState(), foo: {}})); |
| 197 | + expect(componentRenderCount).toHaveBeenCalledTimes(3); |
| 198 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(2); |
| 199 | + |
| 200 | + act(() => store.setState({ foo: { bar: 3 }, baz: 10 })); |
| 201 | + expect(componentRenderCount).toHaveBeenCalledTimes(4); |
| 202 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(3); |
| 203 | + |
| 204 | + done(); |
| 205 | + }) |
| 206 | + }); |
| 207 | +}); |
| 208 | + |
| 209 | +describe('store memoization with dispatch', () => { |
| 210 | + it('Should not update components if setState is called with the same previous state', (done) => { |
| 211 | + const store = createStore('reduceredStore', 0, (state, payload) => payload); |
| 212 | + const componentRenderCount = jest.fn(); |
| 213 | + const anotherComponentRenderCount = jest.fn(); |
| 214 | + |
| 215 | + const Component = (props) => { |
| 216 | + const [state, dispatch] = useStore(store); |
| 217 | + |
| 218 | + componentRenderCount(); |
| 219 | + |
| 220 | + return ( |
| 221 | + <button onClick={() => dispatch(state + 1)}> |
| 222 | + This button has been clicked {state} times |
| 223 | + </button> |
| 224 | + ); |
| 225 | + } |
| 226 | + |
| 227 | + const AnotherComponent = (props) => { |
| 228 | + const [state] = useStore(store, ); |
| 229 | + |
| 230 | + anotherComponentRenderCount(); |
| 231 | + |
| 232 | + return ( |
| 233 | + <div> |
| 234 | + {state} |
| 235 | + </div> |
| 236 | + ); |
| 237 | + } |
| 238 | + |
| 239 | + const renderedComponent = mount(<Component />); |
| 240 | + const renderedAnotherComponent = mount(<AnotherComponent />); |
| 241 | + |
| 242 | + requestAnimationFrame(() => { |
| 243 | + expect(componentRenderCount).toHaveBeenCalledTimes(1); |
| 244 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(1); |
| 245 | + |
| 246 | + renderedComponent.find('button').simulate('click'); |
| 247 | + expect(componentRenderCount).toHaveBeenCalledTimes(2); |
| 248 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(2); |
| 249 | + |
| 250 | + act(() => store.dispatch(3)); |
| 251 | + expect(componentRenderCount).toHaveBeenCalledTimes(3); |
| 252 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(3); |
| 253 | + |
| 254 | + act(() => store.dispatch(3)); |
| 255 | + expect(componentRenderCount).toHaveBeenCalledTimes(3); |
| 256 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(3); |
| 257 | + |
| 258 | + act(() => store.dispatch(3)); |
| 259 | + expect(componentRenderCount).toHaveBeenCalledTimes(3); |
| 260 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(3); |
| 261 | + |
| 262 | + act(() => store.dispatch(1)); |
| 263 | + expect(componentRenderCount).toHaveBeenCalledTimes(4); |
| 264 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(4); |
| 265 | + |
| 266 | + act(() => store.dispatch(1)); |
| 267 | + expect(componentRenderCount).toHaveBeenCalledTimes(4); |
| 268 | + expect(anotherComponentRenderCount).toHaveBeenCalledTimes(4); |
| 269 | + |
| 270 | + expect(renderedComponent.text()).toBe('This button has been clicked 1 times') |
| 271 | + expect(renderedAnotherComponent.text()).toBe('1') |
| 272 | + |
| 273 | + done(); |
| 274 | + }) |
| 275 | + }); |
| 276 | +}); |
0 commit comments