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

Passing in an existing value

Since this form will be used when modifying existing customers as well as adding new ones, we must set the text field's initial value to the existing first name if set:

it('includes the existing value for the first name', () => {
render(<CustomerForm firstName="Ashley" />);
const field = form('customer').elements.firstName;
expect(field.value).toEqual('Ashley');
});

To make this test pass, change the component definition to the following. We use a prop to pass in the previous first name value:

export const CustomerForm = ({ firstName }) => (
<form id="customer">
<input
type="text"
name="firstName"
value={firstName}
/>
</form>
);

Running tests again, you'll see that the test passes, but with a warning:

PASS test/CustomerForm.test.js
● Console
console.error node_modules/prop-types/checkPropTypes.js:19
Warning: Failed prop type: You provided a `value` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultValue`. Otherwise, set either `onChange` or `readOnly`.
in input (created by CustomerForm)
in form (created by CustomerForm)
in CustomerForm

To get rid of the warning, add the word readOnly to the input tag. You might be thinking: surely, we don't want a read-only field? You're right, but we need a further test, for modifying the input value, before we can avoid using the readOnly keyword. We'll add that test a little further on.

Always consider React warnings to be a test failure. Don't proceed without first fixing the warning.
主站蜘蛛池模板: 木里| 武乡县| 同心县| 永泰县| 丽江市| 喜德县| 印江| 汾西县| 安国市| 于田县| 宜兰县| 郯城县| 滨州市| 鄂托克前旗| 德庆县| 新密市| 新绛县| 大埔区| 武胜县| 宣城市| 应用必备| 庆阳市| 岳阳县| 兴宁市| 莫力| 东安县| 遂平县| 青铜峡市| 延吉市| 乌鲁木齐县| 玉溪市| 哈尔滨市| 芷江| 垣曲县| 北安市| 重庆市| 木里| 崇礼县| 来安县| 黔江区| 和硕县|