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

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.
主站蜘蛛池模板: 潞城市| 青田县| 定南县| 申扎县| 渑池县| 多伦县| 库尔勒市| 晋城| 开封县| 曲沃县| 龙州县| 上饶县| 甘谷县| 噶尔县| 镇雄县| 安康市| 汤原县| 郁南县| 新昌县| 卢龙县| 芮城县| 双桥区| 闻喜县| 苗栗市| 兴城市| 安陆市| 三门峡市| 武山县| 津市市| 通海县| 六枝特区| 石楼县| 金阳县| 沅陵县| 河源市| 水城县| 汉川市| 五原县| 郁南县| 呼和浩特市| 英德市|