git clone <repo>
# install dependencies
npm i
# run and view application ui in browser
npm run start
# run and view test output
npm run test
buttonis a DOM node and you can assert any of its attributes such asdisabledqueryByTextwill returnnullwhen element is not presentgetByTextwill throw an error when element is not presentrerendercan be used to test with different propsjest.fncan be used to mock functionsfireEvent.clickcan be used to simulate click events- Before setting up your test suites, you should use
afterEach(cleanup)(after imports) in order to unmount any React trees that were mounted with render
Set up a default props object containing the button's text and an onClick even with a mock function value.
Create two tests for the Button component.
-
The first test should handle rendering the button with the proper text.
- Set a rendered
Buttoncomponent with all of its available properties to a deconstructedqueryByTextandrerender. - Ensure that the text "Submit" is present when queried.
- The third assertion should handle changing the button text property upon rerendering.
- Verify the changed text is as expected.
- Set a rendered
-
The second test should test that the button calls the correct function upon clicking.
- Set a variable called
onClickto the onClick from the default props object. - Set a rendered
Buttoncomponent with all of its available properties to a deconstructedgetByText. - Simulate a click event on the button's text as provided in the defaultProps.
- Evaluate that the
onClickevent occured.
- Set a variable called