如何解决我的情况?以下函数的 Jest + Enzyme 测试返回
TypeError: window.getSelection is not a function
我的代码:
_addNewAgent () {
window.getSelection().removeAllRanges();
const newAgent = generateNewAgent();
const newDataBase = this.state.agentsDatabase;
newDataBase.push(newAgent);
this.setState({
currentAgentProfile: newAgent,
agentsDatabase: newDataBase,
infoDisplayContent: 'profile'
});
}
我的测试:
import React from 'react';
import { mount } from 'enzyme';
import App from '../containers/App';
const result = mount(
<App />
);
test('add agent', () => {
const agentsList = result.state().agentsDatabase.length;
result.find('#addNewAgent').simulate('click');
expect(result.state().agentsDatabase.length).toBe(agentsList + 1);
expect(result.state().currentAgentProfile)
.toEqual(result.state().agentsDatabase[agentsList]);
expect(result.state().infoDisplayContent).toBe('profile');
});
最佳答案
您必须删除 window.getSelection().removeAllRanges()
。我认为这会起作用:
before(() => {
window.getSelection = () => {
return {
removeAllRanges: () => {}
};
})
});
关于reactjs - enzyme Jest window.getSelection() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43677034/