{"id":21039424,"url":"https://github.com/arjunu/react-redux-testing-cheatsheet","last_synced_at":"2026-04-13T09:31:18.574Z","repository":{"id":81629315,"uuid":"106182492","full_name":"arjunu/react-redux-testing-cheatsheet","owner":"arjunu","description":null,"archived":false,"fork":false,"pushed_at":"2018-07-17T14:10:36.000Z","size":15,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-30T16:18:34.429Z","etag":null,"topics":["cheatsheet","enzyme","jest","react","redux","testing","unit-testing"],"latest_commit_sha":null,"homepage":null,"language":null,"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/arjunu.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-08T14:26:26.000Z","updated_at":"2023-03-08T04:07:19.000Z","dependencies_parsed_at":null,"dependency_job_id":"e0f2acdf-f248-41bd-b877-2b55bb691bfc","html_url":"https://github.com/arjunu/react-redux-testing-cheatsheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/arjunu/react-redux-testing-cheatsheet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arjunu%2Freact-redux-testing-cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arjunu%2Freact-redux-testing-cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arjunu%2Freact-redux-testing-cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arjunu%2Freact-redux-testing-cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arjunu","download_url":"https://codeload.github.com/arjunu/react-redux-testing-cheatsheet/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arjunu%2Freact-redux-testing-cheatsheet/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31746290,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-13T09:16:15.125Z","status":"ssl_error","status_checked_at":"2026-04-13T09:16:05.023Z","response_time":93,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["cheatsheet","enzyme","jest","react","redux","testing","unit-testing"],"created_at":"2024-11-19T13:41:18.618Z","updated_at":"2026-04-13T09:31:18.554Z","avatar_url":"https://github.com/arjunu.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# React Redux Testing Cheatsheet\n\nThis cheatsheet uses the following stack\n1. [React JS](https://github.com/facebook/react)\n2. [Redux](https://github.com/reactjs/redux)\n3. [Immutable JS](https://github.com/facebook/immutable-js)\n4. [Jest](https://github.com/facebook/jest)\n5. [Enzyme](https://github.com/airbnb/enzyme) \n\n# Imports\n\n```javascript\nimport React from 'react';\nimport { shallow } from 'enzyme';\nimport renderer from 'react-test-renderer'; // for jest snapshot\nimport toJson from 'enzyme-to-json'; // for enzyme snapshot\n```\n\n# Table of Contents\n- [Events](https://github.com/arjunu/react-redux-testing-cheatsheet#events)\n- [DOM](https://github.com/arjunu/react-redux-testing-cheatsheet#dom)\n- [Mocks](https://github.com/arjunu/react-redux-testing-cheatsheet#mocks)\n- [Props](https://github.com/arjunu/react-redux-testing-cheatsheet#props)\n- [Reducer](https://github.com/arjunu/react-redux-testing-cheatsheet#reducer)\n- [Snapshot](https://github.com/arjunu/react-redux-testing-cheatsheet#snapshot)\n- [State](https://github.com/arjunu/react-redux-testing-cheatsheet#state)\n- [Timers](https://github.com/arjunu/react-redux-testing-cheatsheet#timers)\n\n# Events\n\n### Simulate click event\n\n```javascript\nconst wrapper = shallow(\u003cSomeComponent /\u003e);\n\n//mock event if event is accessed\nconst event = { preventDefault: jest.fn() };\nwrapper.find('button').simulate('click', event);\nexpect(event.preventDefault.mock.calls.length).toBe(1);\n```\n### Simulate on change event\n\n```javascript\nconst wrapper = shallow(\u003cSomeComponent /\u003e);\n\nconst event = { target: { value: 'user123' } };\nwrapper.find(`input[name='username']`).simulate('change', event);\n```\n\n# DOM\n\n### Check for an element/component\n\n```javascript\nconst wrapper = shallow(\u003cSomeComponent /\u003e);\nexpect(wrapper.find('ChildElementOrComponent').length).toBe(1);\n\n//attribute selector\nexpect(wrapper.find(`input[type='submit']`).length).toBe(1);\n```\n\n### Check for focus\n\n```javascript\nconst wrapper = mount(\u003cSomeComponent /\u003e);\nconst element = wrapper.instance().componentInstanceVariableHoldingTheRef;\nspyOn(element, 'focus');\n// call code that triggers focus\n// ...\nexpect(element.focus).toHaveBeenCalledTimes(1);\n```\n# Mocks\n\n### Mock named import\n\n```javascript\n// foosModule.js\nexport const foo = () =\u003e {...}\n\n// fooConsumerModule.js\nimport { foo } from './foosModule.js';\nexport const fooConsumer = () =\u003e foo();\n\n// test.js \nimport fooConsumer from './fooConsumerModule.js';\n// import and mock the implementation\nconst module = require('./foosModule.js');\nmodule.foo = () =\u003e 42;\nexpect(fooConsumer()).toEqual(42);\n```\n\n### Mock module\n\n```javascript\njest.mock('./foo');\nconst foo = require('./foo');\nfoo.mockImplementation(() =\u003e 42);\n```\n\n# Props\n\n### Check if prop function was called\n\n```javascript\n//mock the function\nconst propFunction = jest.fn();\nconst wrapper = shallow(\u003cSomeComponent propFunction={propFunction} /\u003e);\n\n//propFunction was called once\nexpect(propFunction.mock.calls.length).toBe(1);\n\n//propFunction was called with argument 'arg'\nexpect(propFunction.mock.calls[0][0]).toEqual(arg);\n```\n### Update prop\n\n```javascript\nwrapper.setProps({ name: 'bar' });\n```\n\nhttp://airbnb.io/enzyme/docs/api/ShallowWrapper/setProps.html\n\n### Call component function\n\n```javascript\nconst wrapper = shallow(\u003cSomeComponent /\u003e);\nwrapper.instance().componentFunction();\n```\n\n# Reducer\n\n### Test reducer (action effect on store)\n\n```javascript\nconst state = { ... };\nconst expectedState = { ... };\nconst payload = { ... };\nconst action = actionCreator(payload);\nexpect(reducer(state, action).toEqual(expectedState);\n```\n# Snapshot\n\n### Jest snapshot (shallow)\n\n```javascript\nconst wrapper = shallow(\u003cComponent {...props}\u003e\u003c/Component\u003e);\nexpect(toJson(wrapper)).toMatchSnapshot();\n```\n\n### Deep Snapshot \n\n```javascript\nconst tree = renderer.create(\u003cComponent {...props}/\u003e).toJSON();\nexpect(tree).toMatchSnapshot();\n```\n\n# State\n\n### Check state \n\n```javascript\nconst wrapper = shallow(\u003cSomeComponent /\u003e);\nexpect(wrapper.state().fruit).toEqual('apple');\n```\n\n### Check asynchronously changed state \n\n```javascript\ntest('', done =\u003e {\n  const wrapper = shallow(\u003cSomeComponent /\u003e);\n  // do something that changes state in an async method\n  process.nextTick(() =\u003e {\n    wrapper.update();\n    expect(wrapper.state().fruit).toEqual('apple');\n    done();\n  });\n});\n```\n\n### Set state\n\n```javascript\nconst wrapper = shallow(\u003cSomeComponent /\u003e);\nwrapper.instance().setState({fruit: 'orange'}));\n```\n\n# Timers\n\n### Post timeout execution \n\n```javascript\nsetInterval(() =\u003e this.setState({ flag: true }), 100);\n\njest.useFakeTimers();\ntest('', () =\u003e {\n  jest.runTimersToTime(100);\n  wrapper.update();\n  expect(wrapper.state().flag).toEqual(true);\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farjunu%2Freact-redux-testing-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farjunu%2Freact-redux-testing-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farjunu%2Freact-redux-testing-cheatsheet/lists"}