Jest测试pub / sub

时间:2017-09-26 23:41:15

标签: javascript jquery jestjs

我尝试使用jest.js测试pub / sub但是我遇到了失败。 如果我直接调用该函数,间谍就可以了。 我是否需要模拟我的发布,或者可以保持原样。

  expect(jest.fn()).toHaveBeenCalled()
  Expected mock function to have been called.

这是我的测试代码。

    // jest.dontMock('create/user-input');
    import userInput from 'create/user-input';
    jest.dontMock('create/payment-type-display');
    import paymentTypeDisplay from 'create/payment-type-display';
    jest.dontMock('shared/broadcaster');
    import {subscribe, publish} from 'shared/broadcaster';  

    test('dispatch pageHandler', ()=>{
        const spy = jest.spyOn(userInput, 'pageHandler');
        publish('DISPLAY_UPDATE', $('#verify_view') );
        expect(spy).toHaveBeenCalled();

    });

UserImput.js

class UserInput extends FieldApplicator{

    constructor( filter ) {
        super( filter ); // Inject Filters from ../create/display-filters
        this.pageHandler = this.pageHandler.bind(this);
        // Listen for broadcast from page-pagination :: displayViewById method.
        subscribe('DISPLAY_UPDATE', this.pageHandler);
    }

    // output results from FORM view to page. example data for (step3 & step 4)
    pageHandler( page ){
        super.output( page, this.result() );
    }

    result(){
        return formData.data( super.fieldNames() );
    }
}

let userInput = new UserInput( filter );

export default userInput;

0 个答案:

没有答案