简体   繁体   中英

Why mi jasmine spec fails TypeError: undefined is not a constructor (evaluating 'this.sortLayout.$('.toggle').removeProp('disabled')')?

When I testing this part of the code jasmine send me this error TypeError: undefined is not a constructor (evaluating 'this.sortLayout.$('.toggle').removeProp('disabled')')

 onResponsive: function() {
        var selectedServers,
            spanDetails,
            span;
        this.sortLayout.$('.toggle').removeProp('disabled');
        this.contractYear.$('.toggle').removeProp('disabled');
        selectedServers = parseInt(this.sortLayout.$('.toggle').find('span').text().match(/\d+/)[0], 10);
        span = $('div.wrapper').find('span');
        spanDetails = $('div#divider').find('span');

        span.empty();
        spanDetails.empty();
        if(selectedServers === 1){
            span.append('Selected '+selectedServers);
        }else{
            spanDetails.append('Selected'+selectedServers);
        }

    }

Spec

describe('#onResponsive', function() {
        it('enables sort filter menu', function() {
            this.view.onShow();
            spyOn($.fn, 'removeProp');
            spyOn($.fn, 'find').and.returnValue('4');
            this.view.onVizResponsive();
            //var result = $('.dropdown-toggle').find();
            //expect(result).toEqual('4');
            expect($.fn.removeProp).toHaveBeenCalledWith('disabled');
            expect($.fn.removeProp).toHaveBeenCalledWith('span');

        });
    });

Why I get this failure when this jQuery function don't return anything.

I needed append toggle to the DOM.

describe('#onResponsive', function() {
            it('enables the sort filter menu', function() {
                this.view.onShow();
                spyOn($.fn, 'removeProp');
                this.view.sortLayout.$el.append('<button class="toggle" disabled><span>Toggle option 1</span></button>');
                this.view.onVizResponsive();
                this.view.sortLayout.$el.find('.toggle').remove();
                this.view.sortLayout.$el.append('<button class="toggle" disabled><span>Toggle option 2</span></button>');
                this.view.onVizResponsive();
                expect($.fn.removeProp).toHaveBeenCalledWith('disabled');
            });
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM