gpt4 book ai didi

material-ui - 如何将上下文传递给Enzyme mount方法以测试包含Material UI组件的组件?

转载 作者:行者123 更新时间:2023-12-03 09:06:07 25 4
gpt4 key购买 nike

我正在尝试使用来自Enzyme的mount来测试我的组件,该组件中嵌套了多个Material UI组件。运行测试时出现此错误:
TypeError: Cannot read property 'prepareStyles' of undefined
经过一番挖掘后,I did found that a theme needs to be passed down in a context。我在测试中正在这样做,但仍然收到此错误。

我的测试:

import expect from  'expect';
import React, {PropTypes} from 'react';
import {mount} from 'enzyme';
import SearchBar from './SearchBar';
import getMuiTheme from 'material-ui/styles/getMuiTheme';

function setup() {
const muiTheme = getMuiTheme();

const props = {
closeSearchBar: () => {},
fetchSearchData: () => {},
data: [],
searching: false
};

return mount(<SearchBar {...props} />, {context: {muiTheme}});
}

describe('SearchBar Component', ()=> {

it('Renders search toolbar properly', () => {
const wrapper = setup();
expect(wrapper.find('.toolbar').length).toBe(1);
expect(wrapper.find('button').length).toBe(1);
});
});

我的搜索栏组件是无状态组件,因此在任何情况下我都不会拉。但是即使我在,我仍然会遇到同样的错误。

我究竟做错了什么?

最佳答案

尝试在childContextTypes选项中添加mount:

return mount(
<SearchBar {...props} />, {
context: {muiTheme},
childContextTypes: {muiTheme: React.PropTypes.object}
}
);

通过这样做,您可以设置 enzyme 包装器,以便通过上下文使 muiTheme对它的 child 可用。

关于material-ui - 如何将上下文传递给Enzyme mount方法以测试包含Material UI组件的组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38264715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com