state
, writing methods that set state
, and assigning click handlers to trigger these methods.
Counter
component keeps track of a count
value in state
. There are two buttons which call methods increment()
and decrement()
. Write these methods so the counter value is incremented or decremented by 1 when the appropriate button is clicked. Also, create a reset()
method so when the reset button is clicked, the count is set to 0.
Note: Make sure you don't modify the classNames
of the buttons. Also, remember to add the necessary bindings for the newly-created methods in the constructor.
Counter
should return a div
element which contains three buttons with text content in this order Increment!
, Decrement!
, Reset
.
testString: assert((() => { const mockedComponent = Enzyme.mount(React.createElement(Counter)); return (mockedComponent.find('.inc').text() === 'Increment!' && mockedComponent.find('.dec').text() === 'Decrement!' && mockedComponent.find('.reset').text() === 'Reset'); })());
- text: The state of Counter
should initialize with a count
property set to 0
.
testString: 'const mockedComponent = Enzyme.mount(React.createElement(Counter)); assert(mockedComponent.find("h1").text() === "Current Count: 0")'
- text: Clicking the increment button should increment the count by 1
.
testString: 'const mockedComponent = Enzyme.mount(React.createElement(Counter)); mockedComponent.find(".inc").simulate("click"); assert(mockedComponent.find("h1").text() === "Current Count: 1")'
- text: Clicking the decrement button should decrement the count by 1
.
testString: 'const mockedComponent = Enzyme.mount(React.createElement(Counter)); mockedComponent.find(".dec").simulate("click"); assert(mockedComponent.find("h1").text() === "Current Count: -1")'
- text: Clicking the reset button should reset the count to 0
.
testString: 'const mockedComponent = Enzyme.mount(React.createElement(Counter)); mockedComponent.setState({ count: 5 }); const currentCountElement = mockedComponent.find("h1"); assert(currentCountElement.text() === "Current Count: 5"); mockedComponent.find(".reset").simulate("click"); assert(currentCountElement.text() === "Current Count: 0");'
```