Lauro Mansur
Front-End Developer at Hotmart
unit tests are low-level, focusing on a small part of the software system
Martin Fowler
Source: embeddedinsights.com
Improves code quality
Makes debugging easier
Provides greater security when refactoring
Provides documentation
Improves application architecture
Makes integrations easier
INPUTS
OUTPUTS
UNIT
User Interaction:
Props
State
Events / Functions
User Interface:
Component Tree Integrity
Snapshots
Decide inputs and outputs
Choose the function signature
Decide on a small aspect of functionality
Implement the test
Implement the code
<EmailForm />
<FormControl />
<EmailForm />
<FormControl />
SHALLOW
RENDERING
FULL
RENDERING
(MOUNT)
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`EmailForm renders component tree correctly (without enzyme) 1`] = `
<form
className="email__form"
>
<div
className="form__control"
>
<label
htmlFor="from"
>
From:
</label>
<input
autoComplete="off"
id="from"
name="from"
onChange={[Function]}
value=""
/>
</div>
<div
className="form__control"
>
<label
htmlFor="to"
>
To:
</label>
<input
autoComplete="off"
id="to"
name="to"
onChange={[Function]}
value=""
/>
</div>
<div
className="form__control"
>
<label
htmlFor="subject"
>
Subject:
</label>
<input
autoComplete="off"
id="subject"
name="subject"
onChange={[Function]}
value=""
/>
</div>
<div
className="form__control"
>
<label
htmlFor="message"
>
Message:
</label>
<textarea
autoComplete="off"
id="message"
name="message"
onChange={[Function]}
value=""
/>
</div>
<div
className="form__actions"
>
<button
className="button__icon"
onClick={[Function]}
type="submit"
>
Send
<svg>
paper-plane-solid.svg
</svg>
</button>
</div>
</form>
`;
test('sendEmail handler should be called on click event', () => {
const tree = shallow(<EmailForm />);
const spy = jest.spyOn(tree.instance(), 'sendEmail');
tree.instance().forceUpdate();
tree.find('button[type="submit"]').simulate('click', {
preventDefault: jest.fn()
});
expect(spy).toHaveBeenCalledTimes(1);
});
More options at https://jestjs.io/docs/en/cli
By Lauro Mansur
In this talk we will explore a little about the importance of unit testing in the development process and give an introduction on modern tools to write and maintain tests in a practical and efficient way.