mardi 24 juillet 2018

How to Stub a jquery selector in Sinon js

I'm doing some unit testing, and I stuck trying to stub this method. I'm also using materialize for dropdown, which I believe is related. Does someone faced this problem and can help?

My code goes

componentDidUpdate() {
    $('.dropdown-trigger-filter').dropdown({belowOrigin: true, alignment: 'right', closeOnClick: false});
    this.controller.isFirstLoginRedirect(this.props);
}

My test:

describe('Home Component', async () => {
    let container = props => { return <Home store={store} {...props} />;    };

    it('should render correctly', () => {
        const props = {};
        const wrapper = mount(shallow(container(props)).get(0));
        const tree = renderer.create(container(props)).toJSON();

        expect(tree).matchSnapshot();
    });

    it('should change state of selected tab to quotes ', () => {
        const props = {};
        const wrapper = mount(shallow(container(props)).get(0));
        const $ = sinon.stub();
        $.withArgs('.dropdown-trigger-filter').returns({});
        (wrapper.find('.tab').at(0)).simulate('click');
        expect(wrapper.state('selectedTab')).equal('##quotes');
        expect(wrapper.debug()).matchSnapshot();
    });
});

Error:

 TypeError: $(...).dropdown is not a function

Aucun commentaire:

Enregistrer un commentaire