How to test Reflux actions with Jest

后端 未结 1 1433
南笙
南笙 2020-12-30 16:11

I\'m having difficulty testing that Reflux actions are triggering correctly in my application, and in fact they do not seem to be working at all with Jest. I have this examp

相关标签:
1条回答
  • 2020-12-30 16:58

    I figured it out! I just needed to use Jest's own methods for fast-forwarding any timers. i.e. just add the line

    jest.runAllTimers();
    

    So the working version of my first example would be

    jest.autoMockOff();
    
    describe('Test', function () {
      it('Tests actions', function () {
        var Reflux = require('../node_modules/reflux/index');
    
        var action = Reflux.createAction('action');
        var mockFn = jest.genMockFn();
    
        var store = Reflux.createStore({
          init: function () {
            this.listenTo(action, this.onAction);
          },
          onAction: function () {
            mockFn();
          }
        });
    
        action('Hello World');
    
        jest.runAllTimers();
    
        expect(mockFn).toBeCalled();
      });
    });
    
    0 讨论(0)
提交回复
热议问题