How to Unit Test with Qunit a Knockout View Model that uses throttle on observables

后端 未结 1 806
星月不相逢
星月不相逢 2020-12-19 19:34

Supposed this is my view model

function VM()
{
    var self = this;

    this.Status = ko.observable(false);

    this.A = ko.observable();

    this.B = ko.         


        
相关标签:
1条回答
  • 2020-12-19 19:56

    If you only what to test that the Status toggles between true and false I would just subscribe on the Status change and check in the change callback that the first time I get the value true and the second time the value false.

    Writing your test this way you would need to have one start/stop pair, something like this:

    asyncTest("computed with ajax", function () {
        expect(2); // expecting two asserts, so the Status only changes twice
        var sut = new VM();
        // multiple calls to A to test the throttle behavior
        sut.A('something');
        sut.A('something2');
        sut.A('something3');
        stop();
        var callCount = 0;
        var expectedValues = [true, false];
        sut.Status.subscribe(function (value) {
            start();
            equal(sut.Status(), expectedValues[callCount]);
            callCount++;
        });
    });
    

    Demo JSFiddle.

    0 讨论(0)
提交回复
热议问题