jeudi 24 décembre 2015

Responding with a given fixture when requesting window.location.href with Teaspoon

I'm trying to write a test in Teaspoon for one of the public functions in my jQuery UI dialog wrapper module.

In this function we're able to create a dialog by requesting window.location.href which pulls back a cached version of the page from the browser, rather than going out and hitting the server:

createFromFragmentId = function (fragmentId, options) {
    var promise = $.ajax({
            url: window.location.href
        });

    promise.done(function (html) {
        var $html = $('#' + fragmentId, html);
        create($html, options); // the function that creates the dialog
    });
},

My problem is that I can't find a way to get Teaspoon to spoof a response from window.location.href, responding with the markup from a fixture file.

This was my last attempt, which didn't work, and began wreaking havoc on the tests that followed:

describe('createFromFragmentId', function () {
    it('creates a dialog from a given ID', function () {
        this.server = sinon.fakeServer.create();

        this.server.respondWith('GET', window.location.href, [
            200, { 'Content-Type': 'text/html; charset=utf-8' },
            $fixture[0].outerHTML
        ]);

        WEBLINC.dialog.createFromFragmentId('fragment-dialog');

        this.server.respond();

        expect(
            _.isEmpty(NAMESPACE.dialog.current().has('#content-5'))
        ).to.equal(false);

        this.server.restore();
    });
});

A nudge in the right direction would be most appreciated.

Aucun commentaire:

Enregistrer un commentaire