【问题标题】:How to test a method dispatching custom event如何测试调度自定义事件的方法
【发布时间】:2019-08-12 07:35:30
【问题描述】:

我有一个创建自定义事件并调度它的静态方法:

    class MyComponent extends {
       static refreshComponent() {
         const event = new Event('refreshComponent');
         document.dispatchEvent(event);
       }
       render() {
          MyComponent.refreshComponent();
       }
    }

我正在尝试如下测试:

describe('refreshComponent', () => {
    it('should dispatch an event', () => {
      const document = { dispatchEvent: jest.fn() };
      wrapper.refreshGoalsComponent();
      expect(document.dispatchEvent).toHaveBeenCalledWith('refreshComponent');
    });
  });

但是这里没有调用 dispatchEvent,因为没有模拟 'new Event()'。有没有办法嘲笑它?请帮忙

【问题讨论】:

    标签: javascript reactjs dom jestjs enzyme


    【解决方案1】:

    它看起来有点笨拙,但如果您想验证一个带有预期 type 的事件已被调度,这样的东西应该可以工作:

    describe('refreshComponent', () => {
      it('should dispatch an event', () => {
        const dispatchEventSpy = jest.spyOn(document, 'dispatchEvent');
    
        // Trigger component render here...
    
        expect(dispatchEventSpy).toHaveBeenCalledWith(expect.any(Event));
        expect(dispatchEventSpy.mock.calls[0][0].type).toBe('refreshComponent');
      });
    });
    

    【讨论】:

      【解决方案2】:

      您可以像这样测试调度事件:

      describe('refreshComponent', () => {
        it('should dispatch an event', () => {
          jest.spyOn(global, 'Event').mockImplementation((type: string, eventInit?: any) => ({ type, eventInit }));
          const mockDispatchEvent = jest.spyOn(document, 'dispatchEvent').mockImplementation(() => true);
      
          // render your component
      
          expect(mockDispatchEvent).toHaveBeenCalledWith({
            type: 'refreshComponent',
          });
        });
      });
      

      这种方式可以预期事件类型和事件初始值。如果您不想期待详细事件,我们不需要模拟事件并期待:

      expect(mockDispatchEvent).toHaveBeenCalledWith(expect.any(Event));
      

      【讨论】:

        【解决方案3】:

        你可以用 Jest 模拟全局变量:

        describe('your test', () => {
          let EventBak
          let documentBak
        
          beforeAll(() => {
            EventBak = global.Event
            documentBak = global.document
            global.Event = jest.fn()
            global.document = {
              ...global.document,
              dispatchEvent: jest.fn()
            }
          })
        
          afterAll(() => {
            global.Event = EventBak
            global.document = documentBak
          })
        
          it('...', () => {
            ...
            expect(global.document.dispatchEvent).toHaveBeenCalled()
          })
        })
        

        【讨论】:

        • Jest 现在抱怨要模拟 dispatchEvent:expect(jest.fn())[.not].toHaveBeenCalledWith() jest.fn() 值必须是模拟函数或间谍。收到:函数:[函数dispatchEvent]
        • 我们需要 JSDOM API 来模拟文档吗?
        • 取决于你的笑话设置
        • 如果你使用的是jsdom env,你不需要mock它:jestjs.io/docs/en/configuration.html#testenvironment-string
        • 但是你不能检查它是否已经被调用了
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-12
        • 2017-12-11
        • 1970-01-01
        • 1970-01-01
        • 2017-01-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多