{"library":"redux-waitfor-middleware","title":"Redux Waitfor Middleware","description":"Redux Waitfor Middleware (current stable version 1.0.1) is a lightweight utility designed to facilitate testing of Redux applications, particularly for asynchronous flows. It operates as a standard Redux middleware, recording dispatched actions and providing a `waitFor` method that allows tests to pause execution until a specific set of actions has been dispatched within a given timeout. The library is primarily used in testing environments to assert on the sequence and payload of actions after asynchronous operations complete, such as API calls. Its release cadence appears to be stable, with the current version being quite mature. A key differentiator is its simplicity and direct focus on action-based waiting, making it less verbose than complex mocking or promise-chaining in some test scenarios. It also handles the case where actions are already dispatched, immediately resolving the wait.","language":"javascript","status":"active","last_verified":"Thu Apr 23","install":{"commands":["npm install redux-waitfor-middleware"],"cli":null},"imports":["import createWaitForMiddleware from 'redux-waitfor-middleware';","const waitForMiddleware = createWaitForMiddleware();\nawait waitForMiddleware.waitFor(['ACTION_TYPE']);","waitForMiddleware.clean();"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import createWaitForMiddleware from 'redux-waitfor-middleware';\nimport { createStore, applyMiddleware } from 'redux';\n\n// A dummy reducer for demonstration\nconst exampleReducer = (state = { data: [] }, action) => {\n  switch (action.type) {\n    case 'FETCH_DATA_REQUEST':\n      return { ...state, loading: true };\n    case 'FETCH_DATA_SUCCESS':\n      return { ...state, loading: false, data: action.payload };\n    case 'FETCH_DATA_FAILURE':\n      return { ...state, loading: false, error: action.error };\n    default:\n      return state;\n  }\n};\n\nconst waitForMiddleware = createWaitForMiddleware();\nconst store = createStore(exampleReducer, applyMiddleware(waitForMiddleware));\n\nasync function simulateFetch() {\n  store.dispatch({ type: 'FETCH_DATA_REQUEST' });\n  console.log('Dispatched FETCH_DATA_REQUEST');\n\n  // Simulate an async operation\n  setTimeout(() => {\n    store.dispatch({ type: 'FETCH_DATA_SUCCESS', payload: ['item1', 'item2'] });\n    console.log('Dispatched FETCH_DATA_SUCCESS');\n  }, 100);\n}\n\nasync function runTest() {\n  console.log('Running test...');\n  simulateFetch();\n\n  try {\n    const successActions = await waitForMiddleware.waitFor(['FETCH_DATA_SUCCESS'], 500);\n    console.log('Test passed! Received actions:', successActions);\n    console.assert(successActions.length === 1, 'Expected one success action');\n    console.assert(successActions[0].payload.includes('item1'), 'Payload check');\n  } catch (error) {\n    console.error('Test failed:', error.message);\n  } finally {\n    // Always clean up after a test\n    waitForMiddleware.clean();\n    console.log('Cleaned recorded actions.');\n  }\n}\n\nrunTest();","lang":"javascript","description":"This quickstart demonstrates how to integrate `redux-waitfor-middleware` into a Redux store, simulate an asynchronous action flow, and then use `waitForMiddleware.waitFor()` to assert that a specific action ('FETCH_DATA_SUCCESS') is dispatched within a given timeout.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}