{"id":21314454,"url":"https://github.com/polyconseil/redux-waitfor-middleware","last_synced_at":"2025-06-14T00:03:57.396Z","repository":{"id":57351868,"uuid":"68691113","full_name":"Polyconseil/redux-waitfor-middleware","owner":"Polyconseil","description":null,"archived":false,"fork":false,"pushed_at":"2016-12-23T09:53:24.000Z","size":8,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-06-14T00:03:55.266Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Polyconseil.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-20T08:29:49.000Z","updated_at":"2016-10-06T11:37:13.000Z","dependencies_parsed_at":"2022-08-29T16:31:03.858Z","dependency_job_id":null,"html_url":"https://github.com/Polyconseil/redux-waitfor-middleware","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/Polyconseil/redux-waitfor-middleware","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Polyconseil%2Fredux-waitfor-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Polyconseil%2Fredux-waitfor-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Polyconseil%2Fredux-waitfor-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Polyconseil%2Fredux-waitfor-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Polyconseil","download_url":"https://codeload.github.com/Polyconseil/redux-waitfor-middleware/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Polyconseil%2Fredux-waitfor-middleware/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259737916,"owners_count":22903870,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-21T18:13:03.603Z","updated_at":"2025-06-14T00:03:57.373Z","avatar_url":"https://github.com/Polyconseil.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# redux-waitfor-middleware\n\nLightweight Redux middleware to wait for specific actions to be dispatched. Very useful for testing purposes.\n\n## Installation\n\n```\nnpm install redux-waitfor-middleware\n```\n\n## Usage\n\nInject Waitfor Middleware in your modules (or React components) to start recording actions.\nThen, you can use it in tests to wait for a given action to be dispatched before a given delay.\n\n```javascript\nimport createWaitForMiddleware from 'redux-waitfor-middleware';\nimport {createStore, applyMiddleware} from 'redux';\n  \nconst reducers = [\n  /* import your reducers */\n];\nconst initialState = {\n  /* Initial state store */\n};\n\nconst waitForMiddleware = createWaitForMiddleware();\n\nconst store = applyMiddleware(waitForMiddleware)(createStore)(reducers, initialState);\n```\n\nThe waitFor middleware has started recording ! You can then use its `waitFor` method to wait asynchronously for some actions to be dispatched :\n\n```javascript\n/**\n* Allows to await FOO_ACTION and BAR_ACTION actions to be dispatched :\n*/\nasync function requiredActionsDispatched() {\n  await waitForMiddleware.waiFor([\n    'FOO_ACTION',\n    'BAR_ACTION'\n  ]);\n}\n```\n\n**Heads up !** If the actions have already been dispatched since the last call to `clean`, the method will immediately resolve with the payload of the actions found in its records.  \n\nThis kind of method is very useful in cases where you want to test that a part of your app that might take some time to respond, for example your back-end, replies accordingly.  \nCheck-list :\n- [x] you are able to fire a call to the back end in your test (you may be achieving this by simulating UI interaction)\n- [x] you are dispatching an action when a receiving a response from the back-end, whose payload is the content of the response\n\n```javascript\ndescribe('Potatoes list', () =\u003e {\n  it('should load a list of 5 potatoes', async () =\u003e {\n    // fire a request to the back-end, for example by simulating\n    // a click on the \"load potatoes\" button\n    \n    // This line will throw an Error if the POTATOES_RECEIVED action is not dispatched\n    // before 5000 ms, marking the test as failed :\n    const potaoesActions = await waitForMiddleware.waitFor(['POTATOES_RECEIVED'], 5000);\n    \n    // If the action is dispatched, you will now be able to make assertions on its payload :\n    \n    // waitFor resolves on an array of matching actions, but we only wait for one action to be dispatched, hence this assertion :\n    assert(potaoesActions.length === 1);\n    const potatoesList = potaoesActions[0].potatoesList;\n    assert(potatoesList.length === 5);\n  });\n});\n```\n\n## API\n\nThe object returned by the `createWaitForMiddleware` method exposes the following methods :\n\n### `async waitFor(actions, timeout)`\n\nWait asynchronously for `actions` to be dispatched.  \n**Heads up !** If the actions have already been dispatched since the last call to `clean`, the method will immediately resolve with the payload of the actions found in its records.   \n\n- `actions: Array\u003cString\u003e` : Actions types you are waiting for.\n- `timeout: number` : the timeout, in ms, after which waitFor will throw an Error. Default value : 2000.\n\n\n### `clean()`\n\nCleans the recorded actions history. This allows to avoid side effects between several test cases (see \"Heads up !\" above).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpolyconseil%2Fredux-waitfor-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpolyconseil%2Fredux-waitfor-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpolyconseil%2Fredux-waitfor-middleware/lists"}