官术网_书友最值得收藏!

Adding a form element

Let's create our first form:

  1. 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.
  1. Add the following test into the describe block:
it('renders a form', () => {
render(<CustomerForm />);
expect(
container.querySelector('form[id="customer"]')
).not.toBeNull();
});
  1. 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'
  1. 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 | };
  1. 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;
  1. 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.
  1. 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.
主站蜘蛛池模板: 栖霞市| 莱芜市| 广宗县| 吐鲁番市| 新昌县| 雅安市| 芒康县| 华坪县| 会东县| 江华| 连州市| 峨眉山市| 洪雅县| 尉氏县| 通城县| 永康市| 基隆市| 栖霞市| 司法| 赤水市| 柳州市| 双辽市| 类乌齐县| 伊宁市| 郓城县| 阜康市| 昭觉县| 潮州市| 当涂县| 汉寿县| 孝昌县| 宁都县| 汉川市| 留坝县| 通山县| 筠连县| 霍林郭勒市| 江西省| 贵阳市| 津市市| 蒙山县|