How you restore $?

27 views
Skip to first unread message

Srdjan Dejanovic

unread,
May 30, 2017, 4:44:43 AM5/30/17
to Sinon.JS
Hi,

I have unit test:

function HTMLInjectorAlertHelperTestsModalNotification() {
    return '<html>' +
                '<body>' +
                    '<div class="modal.notification">' +
                        '<div class="modal-inner">' +
                            '<i class="modal-icon icon-check-in-circle"></i>' +
                            '<a href="javascript:;" class="modal-close do-close-overlay">' +
                                '<i class="icon-x"></i>' +
                            '</a>' +
                            '<div class="modal-row">' +
                                '<p><span><strong>Emergency contanct updated</strong></span></p>' +
                            '</div>' +
                         '</div>' +
                     '</div>' +
               '</body>' +
           '</html>';
}


QUnit.test('DisplayToastMessage - testing CalculateAdditionalHeight function call', function (assert) {
    var $alert = $($.parseHTML(HTMLInjectorAlertHelperTestsModalNotification())).clone();

    var mock = sinon.mock(AlertHelper);

    $ = sinon.stub();
    $.withArgs('body > .modal.small').returns({ "length": 42 });
    $.withArgs('.header').returns({ outerHeight: function() { } });
    $.withArgs('body > .notification-wrapper').returns({ append: function () { } });

    var expectationCalculateAdditionalHeight = mock.expects("CalculateAdditionalHeight");
    expectationCalculateAdditionalHeight.once();

    AlertHelper.DisplayToastMessage($alert);

    mock.verify();
    assert.ok(mock.verify(), "CalculateAdditionalHeight function is called once");
       
    mock.restore();
});


I added $.restore(); in the end of unit test and receive an error "$.restore is not a function". Do you know why?

Morgan Roderick

unread,
May 30, 2017, 6:51:33 AM5/30/17
to sin...@googlegroups.com
You're overwriting `$`, there is no way to restore it, because you didn't create a reference for it.

Instead, try this

var fake$= sinon.stub(window, '$');

// tests

fake$.restore();

---

I have closed the issue you created on github .... please don't cross post!

--
  Morgan Roderick

--
You received this message because you are subscribed to the Google Groups "Sinon.JS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinonjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Srdjan Dejanovic

unread,
May 30, 2017, 7:51:03 AM5/30/17
to Sinon.JS
You helped me a lot!
It works!
Thank you very much!
And sorry for posting the same question on github.

Srdjan
Reply all
Reply to author
Forward
0 new messages