【发布时间】:2019-02-05 18:41:30
【问题描述】:
我在使用 Enzyme 和 Jest 测试 Material UI 选项卡时遇到问题。 问题是模拟点击Tab组件
我已经尝试过材料浅层方法(createShallow)和酶浅层但结果是一样的
我在 Tab 组件上的 console.log 正在返回我的目标元素:
<WithStyles(Tab) label="Tab one" />
这是我的代码:
const setup = (newProps) => {
const props = {
selected: 0,
changeTab: jest.fn(),
...newProps
}
const wrapper = shallowUntilTarget(<DashboardTabs { ...props } />, Base)
return {
props,
wrapper
}
}
shallowUntilTarget 只是一个代码片段,用于在 HOC 中使用 .dive 递归查找组件 https://github.com/airbnb/enzyme/issues/539
it('Should call the onChange function', () => {
const { wrapper, props } = setup()
const tab = wrapper.find({ label: 'Tab One' })
tab.simulate('click')
wrapper.update()
console.log(wrapper.debug()) // I should see a differente content after click in this tab
expect(props.changeTab.mock.calls.length).toBe(1) // the mock function call return 0 on the length
})
什么也没发生 :(
【问题讨论】:
-
运行测试时遇到什么错误?
-
你在哪里对你的 comp 进行浅渲染?
-
不返回任何错误,我只是在模拟点击后没有得到预期的行为。而浅层在 jest 的一个 it 函数中
-
请出示你的笔试
-
我编辑了我的帖子并把整个“it”功能放了
标签: reactjs material-ui enzyme