score:3
The culprit can be found in your reducer, specifically these lines:
state.id = state.id + 1;
state.list.push(state.id);
The reducer here is both mutating the state, and returning a new state. Mutating the state is bad practice, and can cause weird effects (like the one you're experiencing)
So the '2. has added once' test will increment the id to 0. This will stay in memory during the next test where initialStatus will have an id of 0, not -1. In our reducer, state points to initialStatus. So calling state.id = status.id + 1;
increments initialStatus's id field, and same for list. This is why it appears that the ADD action being called three times.
Moving away from mutations in the reducer can help fix this problem.
case "ADD": {
const newId = state.id + 1;
return {
id: newId,
list: state.list.concat([newId])
};
}
Remember, never mutate the state in a reducer, always return the new state instead! (http://redux.js.org/docs/Troubleshooting.html#never-mutate-reducer-arguments)
Source: stackoverflow.com
Related Query
- ReactJS: how to trigger reducer with action in Jest
- ReactJS + Redux: How to trigger a button to open up a Mac Mail/Windows Mail with property email?
- How can I test an action with jest if this action has thunk and axios?
- How to reuse reducer with same action using redux-subspace
- How to trigger animation on state change with styled-components in ReactJs
- how to jest test an async action with axios in react?
- How to unit test a Redux action in a component inside a connected Redux component with Jest
- how to jest test an async action with axios in react ? - part 2
- How to correctly create a test in ReactJs with Jest when the function uses an if statement
- How to get data back from reducer with a async action
- How to test with Jest whether action created by action creator (and not passed as prop) fires?
- How to write an integration test of an action with a reducer in react-redux-thunk
- How can I make an action be reusable? ReactJS with Redux
- how to match a returned Promise in using Jest with redux action
- reactJs redux: how to dispatch an action on a user event and link actions with the reducers and the store
- How to use single action type in different reducer function with createSlice method in redux-toolkit
- how to call action after every 2 minute in reactjs with await
- How to test onClose prop with Jest and ReactJs
- How can I trigger the right form action when the user hits enter, in a form with multiple buttons?
- How to use Jest to test higher order function for Redux action with nested function
- How to debug action.type issue with react-redux, single action type single reducer but it's not recognizing the type
- react redux how to trigger container function with action coming from non react component
- How to test a className with the Jest and React testing library
- How to use JQuery with ReactJS
- How to mock React component methods with jest and enzyme
- How to use jest with webpack?
- How to mock history.push with the new React Router Hooks using Jest
- reactjs axios interceptors how dispatch a logout action
- How to avoid 'children with same key' when replacing state in ReactJs component
- How to build a responsive website with ReactJs
More Query from same tag
- Return highest number and associated name from object in Javascript
- Having trouble displaying values retrieved from multiple APIs to Ag-Grid rows (React)
- Unable to delete item from an array in React
- React - nested objects and useState is mutating original state
- reactjs - Handling custom 404 page
- Configuring setupProxy.js using http-proxy-middleware
- Array of objects with values pass to useQuery in the custom hook and it become empty?
- how to create multiple fields, having their number?
- Early returns in React sub render function: return null, [], or React.Fragment?
- Put state from React context provider in const
- React Native _this2.refs.myinput.focus is not a function
- React Table Multiple Div
- Paginaton with ReactJS
- Laravel returns unauthorized after login with sanctum on a react app
- Create dynamic key value pairs for objects in list js
- Firestore - How can I add the Document ID and their data in my state?
- How to use the states of changed store right after dispatching an action in a component method?
- How to fetch data with multiple requests in Redux action creator and then send collected data to reducer?
- Delay loading json data from local file into React JS
- `No routes matched location "/" ` warning shown on the console
- I'm using react-hook-form to validate my input but I can't get it to work after extracting the input to its own component
- Dynamically Define ReactJS Routes from JSON
- Avoid infinite loop with useEffect hook
- Why doesn't my React-Django app detect static files?
- How to call useMap() outside of the file where <MapContainer> is NOT called?
- blitzjs form renders initial values while waiting for update mutation
- React-leaflet how to dynamically change Rectangle Color?
- Passing Data Between Route and Nested Route in React
- How to use multiple nested dynamic routes with getStaticPaths?
- How to wait for Fetch to finish grabbing data before continueing