jeudi 4 août 2016

Jasmine test for method call inside Ajax on Dom element

I am completely new to Jasmine and not sure if I am approaching this correctly. I want to test that the DataTable() method is called on my table dom element. This should occur on successful ajax responses. However, it is initialized inside an ajax call and I'm not sure how to test this. The javascript function that makes the ajax call is updateResults().

Here's what I've tried.

describe('#ajax', function () {
    var leaderboards, spyOnAjax;
    var formActionUrl = '/jasmine_test_url';

    beforeEach(function () {
        setFixtures('<div class="lb-container">' +
            '<form class="leaderboard" action="' + formActionUrl + '">' +
            '<input type="submit">' +
            '</form>' +
            '<div id="result-box">' +
            '<table class="table-leaderboard"></table> </div>' +
            '</div>');
        leaderboards = new LeaderBoards($('div.lb-container')[0]);
        spyOnAjax = spyOn($, 'ajax').and.callThrough();
        leaderboards.updateResults();
    });

    describe('when updateResults method is called on leaderboards object', function () {

        it('initializes DataTables', function () {
            var table = $(leaderboards.resultBox.tableLeaderboard);
            spyOn(table, 'DataTable');
            expect(table.DataTable).toHaveBeenCalled();
        });
    });
});

Jasmine tells me that it Expected spy DataTable to have been called. I don't appear to be getting the table element correctly. Any advice is appreciated.

Aucun commentaire:

Enregistrer un commentaire