ShoppingCart
组件现在渲染了一个子组件Items
。该Items
组件有一个默认quantity
prop,其值被设置为整数0
。通过传入数值10
来覆盖quantity
的默认 prop。
注意: 请记住,向组件添加 prop 的语法与添加 HTML 属性类似。但是,由于quantity
的值是整数,所以它不会加引号,但应该用花括号括起来,例如{100}
。这个语法告诉 JSX 直接将花括号中的值解释为 JavaScript。
ShoppingCart
组件。
testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(ShoppingCart)); return mockedComponent.find('ShoppingCart').length === 1; })());
- text: 应该渲染Items
组件。
testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(ShoppingCart)); return mockedComponent.find('Items').length === 1; })());
- text: 'Items
组件应该有一个{ quantity: 10 }
的prop,该 prop 是从ShoppingCart
组件传递过去的。'
testString: "getUserInput => assert((function() { const mockedComponent = Enzyme.mount(React.createElement(ShoppingCart)); return mockedComponent.find('Items').props().quantity == 10 && getUserInput('index').replace(/ /g,'').includes('