- Mastering React Test:Driven Development
- Daniel Irvine
- 422字
- 2021-06-24 14:45:07
Adding a form element
Let's create our first form:
- Create a new file called test/CustomerForm.test.js and add the following:
import React from 'react';
import { createContainer } from './domManipulators';
import { CustomerForm } from '../src/CustomerForm';
describe('CustomerForm', () => {
let render, container;
beforeEach(() => {
({ render, container } = createContainer());
});
});
The call in the beforeEach block looks a little odd; it's a destructuring assignment where the variables have already been declared. The declaration and assignment are split in this way because the variables must be accessible within the scope of the describe block, but they must also be reassigned for each and every test. Each test gets its own container, independent of the other tests.
- Add the following test into the describe block:
it('renders a form', () => {
render(<CustomerForm />);
expect(
container.querySelector('form[id="customer"]')
).not.toBeNull();
});
- We have a complete test, so let's run it and see what happens:
FAIL test/CustomerForm.test.js
● Test suite failed to run
Cannot find module '../src/CustomerForm' from 'CustomerForm.test.js'
- Go ahead and create src/CustomerForm.js. Running your test again should give you the following output:
FAIL test/CustomerForm.test.js
● CustomerForm ? renders a label for the first name field
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
10 |
> 11 | render: component => ReactDOM.render(component, container),
| ^
12 | container
13 | };
- One issue with our domManipulators code is that Jest's stack trace points to a failure within our helper code, not the test itself. Thankfully, the error message is helpful enough; we need to add an export that matches the import we wrote at the top of our test file. Add the following line to src/CustomerForm.js:
export const CustomerForm = () => null;
- Running tests gives another failure:
Invariant Violation: CustomerForm(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.
- Fix that by making the component return something:
import React from 'react';
export const CustomerForm = () => <form id="customer" />;
This passes our tests.
Asserting on DOM IDs
We've given this form an ID and that our test looks specifically for that ID in its expectation. This protects us from brittle tests. For example, if our test just looked for any form, then it could break if we design our page to include two forms rather than just this one.
We've given this form an ID and that our test looks specifically for that ID in its expectation. This protects us from brittle tests. For example, if our test just looked for any form, then it could break if we design our page to include two forms rather than just this one.
推薦閱讀
- Spring 5.0 Microservices(Second Edition)
- R語(yǔ)言數(shù)據(jù)可視化之美:專業(yè)圖表繪制指南
- Learning Flask Framework
- Mastering Swift 2
- Hands-On Enterprise Automation with Python.
- PLC編程與調(diào)試技術(shù)(松下系列)
- 零基礎(chǔ)入門學(xué)習(xí)Python
- Flutter跨平臺(tái)開發(fā)入門與實(shí)戰(zhàn)
- 常用工具軟件立體化教程(微課版)
- Learning Docker Networking
- Natural Language Processing with Python Quick Start Guide
- Python Deep Learning
- Python+Office:輕松實(shí)現(xiàn)Python辦公自動(dòng)化
- Unity Android Game Development by Example Beginner's Guide
- Python機(jī)器學(xué)習(xí)與量化投資