{"id":21697468,"url":"https://github.com/silvestrevivo/testing-counter","last_synced_at":"2026-04-14T00:05:36.856Z","repository":{"id":43583650,"uuid":"171757662","full_name":"silvestrevivo/testing-counter","owner":"silvestrevivo","description":"Unit-testing/React exercise developed with Jest + Enzyme, following the the Udemy tutorial from @flyrightsister","archived":false,"fork":false,"pushed_at":"2022-12-10T02:21:55.000Z","size":3318,"stargazers_count":0,"open_issues_count":22,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-25T14:23:17.585Z","etag":null,"topics":["enzyme","first","jest","reactjs","udemy-tutorial","unit-testing"],"latest_commit_sha":null,"homepage":"","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/silvestrevivo.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":"2019-02-20T22:14:36.000Z","updated_at":"2019-07-17T18:03:20.000Z","dependencies_parsed_at":"2023-01-25T20:16:45.673Z","dependency_job_id":null,"html_url":"https://github.com/silvestrevivo/testing-counter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silvestrevivo%2Ftesting-counter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silvestrevivo%2Ftesting-counter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silvestrevivo%2Ftesting-counter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silvestrevivo%2Ftesting-counter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/silvestrevivo","download_url":"https://codeload.github.com/silvestrevivo/testing-counter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244637101,"owners_count":20485446,"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":["enzyme","first","jest","reactjs","udemy-tutorial","unit-testing"],"created_at":"2024-11-25T19:27:55.063Z","updated_at":"2026-04-14T00:05:36.817Z","avatar_url":"https://github.com/silvestrevivo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# testing-counter\n\nUnit-testing/React exercises developed with Jest + Enzyme, following the the Udemy tutorial from @flyrightsister\n\n## First challenge\n\n### app.js\n```javascript\nimport React, { Component } from 'react';\nimport './App.css';\n\nclass App extends Component {\n  state = {\n    counter: 0\n  }\n\n  render() {\n    const { counter } = this.state\n    return (\n      \u003cdiv data-test=\"component-app\"\u003e\n        \u003ch1 data-test=\"counter-display\"\u003eThe counter is currently {counter}\u003c/h1\u003e\n        \u003cbutton\n          data-test=\"increment-button\"\n          onClick={() =\u003e this.setState({ counter: counter + 1 })}\u003eIncrement counter\u003c/button\u003e\n      \u003c/div\u003e\n    );\n  }\n}\n\nexport default App;\n```\n\n### app.test.js\n```jsx\nimport React from 'react';\nimport Enzyme, { shallow } from 'enzyme';\nimport EnzymeAdapter from 'enzyme-adapter-react-16';\nimport App from './App';\n\nEnzyme.configure({ adapter: new EnzymeAdapter() });\n\n/**\n * Factory function to create a ShallowWrapper for the App Component\n * @function setup\n * @param {object} props - Component props specific for this setup\n * @param {object} state - Initial state for setup\n * @returns {ShallowWrapper}\n */\n\nconst setup = (props = {}, state = null) =\u003e {\n  // =\u003e const wrapper = shallow(\u003cApp {...props} /\u003e)\n  if (state) wrapper.setState(state)\n  return wrapper;\n}\n\n/**\n * Return ShallowWrapper containing node with the given data-test value\n * @function findByTestAttr\n * @param {ShallowWrapper} wrapper - Enzyme shallow wrapper to searth within\n * @param {string} val - Value to data-test attribute for search\n * @returns {ShallowWrapper}\n */\n\nconst findByTestAttr = (wrapper, val) =\u003e {\n  return wrapper.find(`[data-test='${val}']`);\n}\n\ntest('renders without errors', () =\u003e {\n  const wrapper = setup();\n  const appComponent = findByTestAttr(wrapper, 'component-app');\n  expect(appComponent.length).toBe(1);\n})\n\ntest('renders increment button', () =\u003e {\n  const wrapper = setup();\n  const button = findByTestAttr(wrapper, 'increment-button');\n  expect(button.length).toBe(1);\n})\n\ntest('renders counter display', () =\u003e {\n  const wrapper = setup();\n  const counterDisplay = findByTestAttr(wrapper, 'counter-display');\n  expect(counterDisplay.length).toBe(1);\n})\n\ntest('counter starts at 0', () =\u003e {\n  const wrapper = setup();\n  let initialCounterState = wrapper.state('counter');\n  expect(initialCounterState).toBe(0)\n})\n\ntest('clicking button increments counter display', () =\u003e {\n  // define the counter at 7\n  const counter = 7;\n  const wrapper = setup(null, { counter });\n\n  // find button and click\n  const button = findByTestAttr(wrapper, 'increment-button');\n  button.simulate('click');\n  wrapper.update();\n\n  // find counterdisplay an equal to 8\n  const counterDisplay = findByTestAttr(wrapper, 'counter-display');\n  expect(counterDisplay.text()).toContain(counter + 1);\n})\n```\n\n## Second challenge\n\n```jsx\nimport React, { Component } from 'react';\nimport './App.css';\n\nclass App extends Component {\n  state = {\n    counter: 0,\n    warning: false\n  }\n\n  decrement = () =\u003e {\n    const { counter } = this.state\n    if (counter \u003e 0) {\n      this.setState({ counter: counter - 1, warning: false })\n    } else {\n      this.setState({ warning: true })\n    }\n  }\n\n  render() {\n    const { counter, warning } = this.state\n    return (\n      \u003cdiv data-test=\"component-app\"\u003e\n        \u003ch1 data-test=\"counter-display\"\u003eThe counter is currently {counter}\u003c/h1\u003e\n        \u003cbutton\n          data-test=\"increment-button\"\n          onClick={() =\u003e this.setState({ counter: counter + 1, warning: false })}\u003eIncrement counter\u003c/button\u003e\n        \u003cbutton\n          data-test=\"decrement-button\"\n          onClick={() =\u003e this.decrement()}\u003eDecrement counter\u003c/button\u003e\n        {warning \u0026\u0026 \u003cp data-test=\"warning-display\"\u003eIt can not be negative\u003c/p\u003e}\n      \u003c/div\u003e\n    );\n  }\n}\n\nexport default App;\n```\n```jsx\nimport React from 'react';\nimport Enzyme, { shallow } from 'enzyme';\nimport EnzymeAdapter from 'enzyme-adapter-react-16';\nimport App from './App';\n\nEnzyme.configure({ adapter: new EnzymeAdapter() });\n\n/**\n * Factory function to create a ShallowWrapper for the App Component\n * @function setup\n * @param {object} props - Component props specific for this setup\n * @param {object} state - Initial state for setup\n * @returns {ShallowWrapper}\n */\n\nconst setup = (props = {}, state = null) =\u003e {\n  // =\u003e const wrapper = shallow(\u003cApp {...props} /\u003e);\n  if (state) wrapper.setState(state)\n  return wrapper;\n}\n\n/**\n * Return ShallowWrapper containing node with the given data-test value\n * @function findByTestAttr\n * @param {ShallowWrapper} wrapper - Enzyme shallow wrapper to searth within\n * @param {string} val - Value to data-test attribute for search\n * @returns {ShallowWrapper}\n */\n\nconst findByTestAttr = (wrapper, val) =\u003e {\n  return wrapper.find(`[data-test='${val}']`);\n}\n\ntest('renders without errors', () =\u003e {\n  const wrapper = setup();\n  const appComponent = findByTestAttr(wrapper, 'component-app');\n  expect(appComponent.length).toBe(1);\n})\n\ntest('renders counter display', () =\u003e {\n  const wrapper = setup();\n  const counterDisplay = findByTestAttr(wrapper, 'counter-display');\n  expect(counterDisplay.length).toBe(1);\n})\n\ntest('counter starts at 0', () =\u003e {\n  const wrapper = setup();\n  let initialCounterState = wrapper.state('counter');\n  expect(initialCounterState).toBe(0)\n})\n\ndescribe('Increment', () =\u003e {\n  test('renders increment button', () =\u003e {\n    const wrapper = setup();\n    const button = findByTestAttr(wrapper, 'increment-button');\n    expect(button.length).toBe(1);\n  })\n\n  test('clicking button increments counter display', () =\u003e {\n    // define the counter at 7\n    const counter = 7;\n    const wrapper = setup(null, { counter });\n\n    // find button and click\n    const button = findByTestAttr(wrapper, 'increment-button');\n    button.simulate('click');\n    wrapper.update();\n\n    // find counterdisplay an equal to 8\n    const counterDisplay = findByTestAttr(wrapper, 'counter-display');\n    expect(counterDisplay.text()).toContain(counter + 1);\n  })\n})\n\ndescribe('Decrement', () =\u003e {\n  test('renders decrement button', () =\u003e {\n    const wrapper = setup();\n    const button = findByTestAttr(wrapper, 'decrement-button');\n    expect(button.length).toBe(1);\n  })\n\n  test('clicking button decrement counter display', () =\u003e {\n    // define the counter at 5, for example\n    const counter = 5;\n    const wrapper = setup(null, { counter });\n\n    // find a button to click\n    const button = findByTestAttr(wrapper, 'decrement-button');\n    button.simulate('click');\n    wrapper.update();\n\n    //find counterdisplay an equal to 4\n    const counterDisplay = findByTestAttr(wrapper, 'counter-display');\n    expect(counterDisplay.text()).toContain(counter - 1);\n  })\n\n  test('error does not show when not needed', () =\u003e {\n    const wrapper = setup();\n    const warningDisplay = findByTestAttr(wrapper, 'warning-display');\n    expect(warningDisplay.exists()).toBeFalsy();\n  })\n\n\n  describe('counter is 0', () =\u003e {\n    let counter;\n    let wrapper;\n    let counterDisplay;\n    beforeEach(() =\u003e {\n      counter = 0;\n      wrapper = setup(null, { counter });\n      counterDisplay = findByTestAttr(wrapper, 'counter-display');\n    })\n\n    test('clicking button decrement', () =\u003e {\n      // find a button to click\n      const button = findByTestAttr(wrapper, 'decrement-button');\n      button.simulate('click');\n      wrapper.update();\n\n      //find counterdisplay an equal to 0 and appears a warning message\n      const warningDisplay = findByTestAttr(wrapper, 'warning-display');\n      expect(wrapper.state().counter).toBeGreaterThanOrEqual(0)\n      expect(counterDisplay.text()).toContain(counter);\n      expect(warningDisplay.text()).toEqual('It can not be negative');\n    })\n\n    test('clicking button increment', () =\u003e {\n      const warningDisplay = findByTestAttr(wrapper, 'warning-display');\n      // find a button to click\n      const button = findByTestAttr(wrapper, 'increment-button');\n      button.simulate('click');\n      wrapper.update();\n\n      //find counterdisplay an equal to 1\n      const counterDisplay = findByTestAttr(wrapper, 'counter-display');\n      expect(counterDisplay.text()).toContain(counter + 1);\n      expect(warningDisplay.exists()).toBeFalsy();\n    })\n  })\n})\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsilvestrevivo%2Ftesting-counter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsilvestrevivo%2Ftesting-counter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsilvestrevivo%2Ftesting-counter/lists"}