开玩笑:对讲机测试API方法

时间:2019-03-12 16:45:00

标签: javascript reactjs jestjs

我无法理解使用从对讲机的 JavaScript 库加载的功能的测试功能的测试流程。

我的方法如下:

 export const generateButton = (handleOnClick) => {
    case "moo":
        return <button onClick={() => Intercom('show')}>Sign Up</button>
    default: 
        return 'moo'

运行此命令时出现的错误是:

  

ReferenceError:未定义对讲机

1 个答案:

答案 0 :(得分:0)

如果我了解您要执行的操作,请创建一个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);
    });
});
相关问题