{"library":"redux-mock-store","title":"Redux Mock Store","description":"redux-mock-store is a utility package designed to facilitate testing of Redux asynchronous action creators and middleware by providing a mocked Redux store. Its primary function is to capture a log of all dispatched actions, which can then be asserted against in tests. Unlike a real Redux store, `redux-mock-store` does not process actions through reducers, meaning its internal state never updates. This library is currently at version 1.5.5. However, since this version, `redux-mock-store` has been officially deprecated by the Redux team. The recommended testing practice now involves using a real Redux store to test the combined behavior of action creators, reducers, and selectors, asserting on observable state changes rather than just dispatched actions. This package is therefore in a deprecated status, with an implied end of active development and maintenance.","language":"javascript","status":"deprecated","last_verified":"Wed Apr 22","install":{"commands":["npm install redux-mock-store"],"cli":null},"imports":["import configureStore from 'redux-mock-store'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import configureStore from 'redux-mock-store';\nimport thunk from 'redux-thunk';\n\n// Mock fetch for demonstration purposes in a Node.js environment\nglobal.fetch = async (url) => {\n  if (url === '/users.json') {\n    return {\n      json: async () => ([{ id: 1, name: 'Test User' }]),\n      ok: true,\n      status: 200\n    };\n  }\n  throw new Error('Unknown URL: ' + url);\n};\n\nconst middlewares = [thunk]; // Add your Redux middleware\nconst mockStore = configureStore(middlewares);\n\n// Define a simple action creator (would be imported in a real app)\nconst FETCH_DATA_SUCCESS = 'FETCH_DATA_SUCCESS';\nfunction success(data) {\n  return {\n    type: FETCH_DATA_SUCCESS,\n    payload: data\n  };\n}\n\n// Define an async action creator (e.g., using redux-thunk)\nfunction fetchData() {\n  return (dispatch) => {\n    // Simulate an async operation, e.g., an API call\n    return fetch('/users.json')\n      .then(response => response.json())\n      .then(data => dispatch(success(data)));\n  };\n}\n\n// Example test using a common testing framework (like Jest)\ndescribe('redux-mock-store with async actions', () => {\n  it('should dispatch a success action after fetching data', async () => {\n    const store = mockStore({}); // Initialize mock store with an empty initial state\n\n    // Dispatch the async action and await its completion\n    await store.dispatch(fetchData());\n\n    // Retrieve all dispatched actions from the mock store\n    const actions = store.getActions();\n\n    // Assertions on the dispatched actions\n    expect(actions.length).toEqual(1);\n    expect(actions[0].type).toEqual(FETCH_DATA_SUCCESS);\n    expect(actions[0].payload).toEqual([{ id: 1, name: 'Test User' }]);\n\n    console.log('Successfully tested async action dispatch:', actions);\n  });\n});","lang":"javascript","description":"Demonstrates how to test asynchronous Redux actions and middleware (like `redux-thunk`) by initializing a mock store and asserting on the captured dispatched actions.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}