我很难理解测试函数的测试流程是什么,这些函数使用从对讲机加载的JavaScript库中的函数。
我的方法如下所示:
export const generateButton = (handleOnClick) => {
case "moo":
return <button onClick={() => Intercom('show')}>Sign Up</button>
default:
return 'moo'在运行此程序时,我遇到的错误是:
ReferenceError:未定义对讲机
发布于 2019-03-12 18:29:41
因此,我想出了办法,我需要添加一个新文件,并像这样在package.json上设置一个jest (添加的文件是mockObject)。
"setupFiles": [
"./config/jest/setupJest.js",
"./config/jest/mockObject.js"
],然后在文件本身中包含以下内容
global.Intercom = () => {
console.log('Intercom called')
}发布于 2019-03-12 16:58:10
如果我理解您想要做的事情,那么在测试中创建一个dummyFunction来替换内部通信。就像这样..。
const Intercom = jest.fn();
describe('button click', () => {
it('Intercom is called correctly', () => {
// whatever component contains the button should be mounted
const wrapper = mount(<YourComponentHere />);
// you may need to add a class to target the specific button
wrapper.find('button').simulate('click');
expect(dummyFunction).toHaveBeenCalledWith('show');
expect(dummyFunction).toHaveBeenCalledTimes(1);
});
});https://stackoverflow.com/questions/55126672
复制相似问题