Reactjs 上下文API HOC单元测试
我有一个HOC组件,它包装React的上下文API,如下所示Reactjs 上下文API HOC单元测试,reactjs,jestjs,enzyme,high-order-component,Reactjs,Jestjs,Enzyme,High Order Component,我有一个HOC组件,它包装React的上下文API,如下所示 import React from 'react'; import { AppContext } from './Context'; export function withAppContext(Component) { return function WrapperComponent(props) { return ( <AppContext.Consumer>
import React from 'react';
import { AppContext } from './Context';
export function withAppContext(Component) {
return function WrapperComponent(props) {
return (
<AppContext.Consumer>
{state => <Component {...props} context={state} />}
</AppContext.Consumer>
);
};
}
但它在这条线上失败了
const wrapper=mount()代码>
带着下面的信息
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your componentfrom the file it's defined in, or you might have mixed up default and named imports.
Check the render method of `WrapperComponent`.
46 | test('Verify state from Context API', ()=> {
47 | const Hello = getAppContext();
> 48 | const wrapper = mount(<Hello />); | ^
49 | expect(wrapper.find('li').hostNodes().length).toBe(2);
50 | expect(wrapper.html()).toBe("<ul><li>Name : Jane Doe</li><li>Age : 28</li></ul>")
51 | })
不变冲突:元素类型无效:需要字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。您可能忘记了从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。
检查“WrapperComponent”的呈现方法。
46 |测试('从上下文API验证状态',()=>{
47 | const Hello=getAppContext();
>48 | const wrapper=mount()|^
49 | expect(wrapper.find('li').hostNodes().length).toBe(2);
50 | expect(wrapper.html()).toBe(“- 姓名:Jane Doe
- 年龄:28
”)
51 | })
你能告诉我我做错了什么吗
谢谢在/Hello.js
中,默认导出是HOC
默认导出应该在getAppContext
中返回,因为在所需模块中“Hello”名称是undefined
return require('./Hello').default;
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your componentfrom the file it's defined in, or you might have mixed up default and named imports.
Check the render method of `WrapperComponent`.
46 | test('Verify state from Context API', ()=> {
47 | const Hello = getAppContext();
> 48 | const wrapper = mount(<Hello />); | ^
49 | expect(wrapper.find('li').hostNodes().length).toBe(2);
50 | expect(wrapper.html()).toBe("<ul><li>Name : Jane Doe</li><li>Age : 28</li></ul>")
51 | })
return require('./Hello').default;