我想制作一个包装器函数,该函数可以将组件作为参数并以主题形式返回。像下面一样。但是它继续抛出错误包装器:
错误:只能在组件上调用ShallowWrapper :: dive()
beforeEach(() => {
component = shallowWithTheme(<SomeComponent value />);
});
test('The text should be something text', () => {
expect(component.text()).toBe('Something text');
});
包装器功能 shallowWithTheme
export const shallowWithTheme = (children, options) => {
const wrapper = shallow(<ThemeProvider theme={theme}>{children}</ThemeProvider>, options).dive();
return wrapper;
};
已编辑
export const shallowWithTheme = (children, options) => {
const wrapper = shallow(<ThemeProvider theme={theme}>{children}</ThemeProvider>, options);
const instance = wrapper.root().instance();
return wrapper.shallow({ context: instance.getChildContext() }).dive();
};
错误:
instance.getChildContext is not a function
更新:
经过数小时的查找,命中和尝试,我最终使用了以下解决方案,但我认为这不是正确的方法,因为您必须导入子组件才能找到它们并访问其属性。在以前的方法中,该方法不适用于当前版本。我能够从根级潜水一次。因此,如果我必须对两个子组件进行测试,则必须使用
expect(wrapper.find(SearchButton).dive().props().isDisabled).toBe(false);
});
expect(wrapper.find(SearchInput).dive().props().isDisabled).toBe(false);
});
util.js
export const shallowWithTheme = children => (
shallow(children, { theme })
);
test.js
it('should enable the search button while the input is not empty', () => {
const wrapper = shallowWithTheme(<HomepageSearch />);
wrapper.setState({ orderSearch: '111' });
expect(wrapper.find(SearchButton).dive().props().isDisabled).toBe(false);
});