React -测试组件propTypes -预期失败
React - Testing component propTypes - expecting a failure
我正在尝试测试一个组件,我一直在看文档,我没有一个很好的参考如何测试他们的失败。
我想测试一个场景,在这个场景中,组件没有传递必需的属性,因此它将无法挂载。
我当前的测试:
jest.dontMock('./Carousel.react.js');
jest.dontMock('react');
var React = require('react');
var ReactTestUtils = require('react-addons-test-utils');
var Carousel = require('./Carousel.react.js');
/*
Carousel propTypes
propTypes: {
choices: React.PropTypes.array.isRequired, // an array of image URLs
choice: React.PropTypes.string
},
*/
describe('Carousel', function () {
it('should fail when images array is not provided', function () {
var failed = false;
try {
var CarouselDom = ReactTestUtils.renderIntoDocument(<Carousel/>);
var wrapperDiv = ReactTestUtils.scryRenderedDOMComponentsWithTag(CarouselDom, 'div');
} catch(e) {
failed = true;
}
expect(failed).toBe(true);
});
});
现在,虽然测试成功了,但这感觉不是测试组件挂载失败的正确方法。正确的方法是什么?
使用Jest编写测试的一种稍微优雅的方法是:
describe('Carousel', () => {
it('throws when image array is not provided', () => {
expect(() => {
var CarouselDom = ReactTestUtils.renderIntoDocument(<Carousel/>);
}).toThrow();
});
});
相关文章:
- React重新渲染但未显示正确的组件
- 不能从angular2中的子组件指定父组件中的数组
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- yeoman generator聚合物的web组件测试失败
- 为什么Angular 1.5双向绑定在将作用域变量传递给组件绑定时失败
- 反应+通量:通知视图/组件操作失败
- React 组件流类型检查中的可选函数属性失败
- Firefox:drawImage(视频)失败,NS_ERROR_NOT_AVAILABLE:组件不可用
- React.js测试在查找组件时失败
- 多个聚合物Web组件在Firefox中失败
- FireFox中HTML/iframe错误-组件返回失败代码:0x80004005 (NS_ERROR_FAILURE)
- 在react native项目中导入我自己的组件失败了
- 组件返回失败码:0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIDOMEventTarg
- 随机失败的聚合物卷材组件测试
- React -测试组件propTypes -预期失败
- 简单的React组件渲染失败
- 组件didmount失败的propType-定义获取作为一个变量
- 组件返回失败码:0x80040111 (NS_ERROR_NOT_AVAILABLE)
- 组件初始化失败,导致NoProvider异常
- 如果元素在模板返回前被移除,则淘汰组件失败