|
| 1 | +import type { Action, Dispatch } from "@reduxjs/toolkit" |
| 2 | +import { configureStore } from "@reduxjs/toolkit" |
1 | 3 | import { screen, waitFor } from "@testing-library/react-native"
|
| 4 | +import { Component, PureComponent, type PropsWithChildren } from "react" |
| 5 | +import type { TextStyle } from "react-native" |
| 6 | +import { Button, Text, View } from "react-native" |
| 7 | +import { connect, Provider } from "react-redux" |
2 | 8 | import { App } from "./App"
|
3 | 9 | import { renderWithProviders } from "./src/utils/test-utils"
|
4 | 10 |
|
@@ -103,3 +109,207 @@ test("Add If Odd should work as expected", async () => {
|
103 | 109 | await user.press(screen.getByText("Add If Odd"))
|
104 | 110 | expect(screen.getByLabelText("Count")).toHaveTextContent("4")
|
105 | 111 | })
|
| 112 | + |
| 113 | +test("React-Redux issue #2150: Nested component updates should be properly batched when using connect", async () => { |
| 114 | + // Original Issue: https://github.com/reduxjs/react-redux/issues/2150 |
| 115 | + // Solution: https://github.com/reduxjs/react-redux/pull/2156 |
| 116 | + |
| 117 | + // Actions |
| 118 | + const ADD = "ADD" |
| 119 | + const DATE = "DATE" |
| 120 | + |
| 121 | + // Action types |
| 122 | + interface AddAction extends Action<string> {} |
| 123 | + interface DateAction extends Action<string> { |
| 124 | + payload?: { date: number } |
| 125 | + } |
| 126 | + |
| 127 | + // Reducer states |
| 128 | + interface DateState { |
| 129 | + date: number | null |
| 130 | + } |
| 131 | + |
| 132 | + interface CounterState { |
| 133 | + count: number |
| 134 | + } |
| 135 | + |
| 136 | + // Reducers |
| 137 | + const dateReducer = ( |
| 138 | + state: DateState = { date: null }, |
| 139 | + action: DateAction, |
| 140 | + ) => { |
| 141 | + switch (action.type) { |
| 142 | + case DATE: |
| 143 | + return { |
| 144 | + ...state, |
| 145 | + date: action.payload?.date ?? null, |
| 146 | + } |
| 147 | + default: |
| 148 | + return state |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + const counterReducer = ( |
| 153 | + state: CounterState = { count: 0 }, |
| 154 | + action: AddAction, |
| 155 | + ) => { |
| 156 | + switch (action.type) { |
| 157 | + case ADD: |
| 158 | + return { |
| 159 | + ...state, |
| 160 | + count: state.count + 1, |
| 161 | + } |
| 162 | + default: |
| 163 | + return state |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + // Store |
| 168 | + const store = configureStore({ |
| 169 | + reducer: { |
| 170 | + counter: counterReducer, |
| 171 | + dates: dateReducer, |
| 172 | + }, |
| 173 | + }) |
| 174 | + |
| 175 | + // ======== COMPONENTS ========= |
| 176 | + interface CounterProps { |
| 177 | + count?: number |
| 178 | + date?: number | null |
| 179 | + dispatch: Dispatch<AddAction | DateAction> |
| 180 | + testID?: string |
| 181 | + } |
| 182 | + |
| 183 | + class CounterRaw extends PureComponent<CounterProps> { |
| 184 | + handleIncrement = () => { |
| 185 | + this.props.dispatch({ type: ADD }) |
| 186 | + } |
| 187 | + |
| 188 | + handleDate = () => { |
| 189 | + this.props.dispatch({ type: DATE, payload: { date: Date.now() } }) |
| 190 | + } |
| 191 | + |
| 192 | + render() { |
| 193 | + return ( |
| 194 | + <View style={{ paddingVertical: 20 }}> |
| 195 | + <Text testID={`${this.props.testID}-child`}> |
| 196 | + Counter Value: {this.props.count} |
| 197 | + </Text> |
| 198 | + <Text>date Value: {this.props.date}</Text> |
| 199 | + </View> |
| 200 | + ) |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + class ButtonsRaw extends PureComponent<CounterProps> { |
| 205 | + handleIncrement = () => { |
| 206 | + this.props.dispatch({ type: ADD }) |
| 207 | + } |
| 208 | + |
| 209 | + handleDate = () => { |
| 210 | + this.props.dispatch({ type: DATE, payload: { date: Date.now() } }) |
| 211 | + } |
| 212 | + |
| 213 | + render() { |
| 214 | + return ( |
| 215 | + <View> |
| 216 | + <Button title="Update Date" onPress={this.handleDate} /> |
| 217 | + <View style={{ height: 20 }} /> |
| 218 | + <Button title="Increment Counter" onPress={this.handleIncrement} /> |
| 219 | + </View> |
| 220 | + ) |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + const mapStateToProps = (state: { |
| 225 | + counter: CounterState |
| 226 | + dates: DateState |
| 227 | + }) => { |
| 228 | + return { count: state.counter.count, date: state.dates.date } |
| 229 | + } |
| 230 | + |
| 231 | + const mapDispatchToProps = (dispatch: Dispatch<AddAction | DateAction>) => ({ |
| 232 | + dispatch, |
| 233 | + }) |
| 234 | + |
| 235 | + const Buttons = connect(null, mapDispatchToProps)(ButtonsRaw) |
| 236 | + const Counter = connect(mapStateToProps, mapDispatchToProps)(CounterRaw) |
| 237 | + |
| 238 | + class Container extends PureComponent<PropsWithChildren> { |
| 239 | + render() { |
| 240 | + return this.props.children |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + const mapStateToPropsBreaking = (_state: any) => ({}) |
| 245 | + |
| 246 | + const ContainerBad = connect(mapStateToPropsBreaking, null)(Container) |
| 247 | + |
| 248 | + const mapStateToPropsNonBlocking1 = (state: { counter: CounterState }) => ({ |
| 249 | + count: state.counter.count, |
| 250 | + }) |
| 251 | + |
| 252 | + const ContainerNonBlocking1 = connect( |
| 253 | + mapStateToPropsNonBlocking1, |
| 254 | + null, |
| 255 | + )(Container) |
| 256 | + |
| 257 | + const mapStateToPropsNonBlocking2 = (state: any) => ({ state }) |
| 258 | + |
| 259 | + const ContainerNonBlocking2 = connect( |
| 260 | + mapStateToPropsNonBlocking2, |
| 261 | + null, |
| 262 | + )(Container) |
| 263 | + |
| 264 | + class MainApp extends Component { |
| 265 | + render() { |
| 266 | + const $H1: TextStyle = { fontSize: 20 } |
| 267 | + return ( |
| 268 | + <Provider store={store}> |
| 269 | + <Buttons /> |
| 270 | + <Text style={$H1}>=Expected=</Text> |
| 271 | + <View> |
| 272 | + <Text> |
| 273 | + I don't have a parent blocking state updates so I should behave as |
| 274 | + expected |
| 275 | + </Text> |
| 276 | + <Counter /> |
| 277 | + </View> |
| 278 | + |
| 279 | + <Text style={$H1}>=Undesired behavior with react-redux 9.x=</Text> |
| 280 | + <ContainerBad> |
| 281 | + <Text>All redux state updates blocked</Text> |
| 282 | + <Counter testID="undesired" /> |
| 283 | + </ContainerBad> |
| 284 | + |
| 285 | + <Text style={$H1}>=Partially working in 9.x=</Text> |
| 286 | + <ContainerNonBlocking1> |
| 287 | + <Text> |
| 288 | + I'm inconsistent, if date updates first I don't see it, but once |
| 289 | + count updates I rerender with count or date changes |
| 290 | + </Text> |
| 291 | + <Counter testID="inconsistent" /> |
| 292 | + </ContainerNonBlocking1> |
| 293 | + |
| 294 | + <Text style={$H1}>=Poor workaround for 9.x?=</Text> |
| 295 | + <ContainerNonBlocking2> |
| 296 | + <Text>I see all state changes</Text> |
| 297 | + <Counter /> |
| 298 | + </ContainerNonBlocking2> |
| 299 | + </Provider> |
| 300 | + ) |
| 301 | + } |
| 302 | + } |
| 303 | + |
| 304 | + const { user, getByTestId, getByText } = renderWithProviders(<MainApp />) |
| 305 | + |
| 306 | + expect(getByTestId("undesired-child")).toHaveTextContent("Counter Value: 0") |
| 307 | + |
| 308 | + await user.press(getByText("Increment Counter")) |
| 309 | + |
| 310 | + expect(getByTestId("inconsistent-child")).toHaveTextContent( |
| 311 | + "Counter Value: 1", |
| 312 | + ) |
| 313 | + |
| 314 | + expect(getByTestId("undesired-child")).toHaveTextContent("Counter Value: 1") |
| 315 | +}) |
0 commit comments