- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
> MyComponent - Render', -6ren">
我正在尝试做什么:
我正在尝试使用shallow
按照以下模式从 enzyme 进行渲染,该模式适用于我的项目中的许多其他组件。
describe('>> MyComponent - Render', () => {
let wrapper;
beforeEach(() => {
wrapper = shallow(<MyComponent.WrappedComponent
actions={{}}
history={}
/>);
});
it("test something", () => { expect(wrapper).toEqual(1); });
});
我遇到了什么问题:
我收到一条错误消息“无法读取未定义的属性“contextTypes””,这意味着 wrapper
是 undefined
。但当我改变 <MyComponent.WrappedComponent />
到只是<MyComponent />
,测试成功。这是我的代码:
describe('>> Legends - render', () => {
let wrapper;
beforeEach(() => {
wrapper = shallow(<Legends textsAndColor={[]} />);
});
it('+++ render the component', () => {
expect(wrapper.length).toEqual(1);
});
it('+++ match snapshot', () => {
expect(wrapper).toMatchSnapshot();
});
});
我有问题:
.WrappedComponent
到底是什么?做 ?为什么 <MyComponent />
有效但无效<MyComponent.WrappedComponent />
?
最佳答案
通过使用.WrappedComponent
,您可以访问由redux的connect
函数包装的组件。我假设您的大多数组件都是 connect
的(因为使用 .WrappedComponent
没有问题),并且抛出所述错误的组件不是 connect
编辑。
我建议您阅读redux docs了解如何为此案例编写测试。简而言之,他们建议对连接组件进行默认导出,对原始组件进行非默认导出。然后仅导入原始组件用于测试目的,如下所示:
import { MyComponent } from './path/to/my/component`;
此后,您将能够挂载
(或浅
)您的原始组件,如下所示:
describe('>> MyComponent - Render', () => {
let wrapper;
beforeEach(() => {
wrapper = shallow(<MyComponent />);
}
});
关于reactjs - 编写 enzyme 测试时何时使用 "Component.WrappedComponent",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49740231/
下面的代码示例是组件的简化版本。我不明白该组件底部的代码,即 Case.wrappedComponent.propTypes 部分。我在互联网上也找不到有关wrappedComponent的相关文档。
我正在尝试做什么: 我正在尝试使用shallow按照以下模式从 enzyme 进行渲染,该模式适用于我的项目中的许多其他组件。 describe('>> MyComponent - Render',
我正在尝试测试我的组件“CBreadcrumb”,它使用“withRouter”HOC。运行测试用例时,所有测试用例都通过,但显示警告。 我已经多次尝试检查我的代码。但我无法找到警告的原因。我也尝试过
当我进入我的 React Router-dom 时,我将我的路由指向验证页面。无论目标是否登录,我都会将我的route(history) 推送到所需的页面,但我不断收到以下错误消息。 Error: O
我是一名优秀的程序员,十分优秀!