samedi 28 novembre 2015

Why the subscription code is not triggered in test cases?

I'm creating simple checker of available IDs. User puts in ID in input, ajax request checks availability and informs the user. The js app is in angular, i'm using requirejs to put it all together and unit-test everything in karma + jasmine and have completely no idea how to test it - i'm able to get some effects, but not enough. Lets say i have my checkUserId.js like this (i simplified as much as i could)

define([
    'rxjs'
], function (Rx) {
    return function () {
        var $input = $('#userId'),
            $info = $('#info');

        var keyups = Rx.Observable.fromEvent($input, 'keyup')
            .pluck('target', 'value')
            .filter(function (value) {
                if (value.length !== 0) {
                   return value;
                }
            });

        //some code here with debounces, $.ajax, promises etc

        atLast.subscribe(function (value) {
            $info.html(value); //taken or free
        });
    }
});

so checkUserIdSpec.js looks something like:

define([
    'checkUserId',
    'rxjs'
], function (checkUserId, Rx) {
    var onNext = Rx.ReactiveTest.onNext,
        onCompleted = Rx.ReactiveTest.onCompleted,
        subscribe = Rx.ReactiveTest.subscribe;

    beforeEach(function () {
        $('body').append($('<input type="text" id="userId" value="">' +
                '<div id="info"></div>'));
        checkUserId();
    });

    describe('checkUserId', function () {
        it('some good name for this test', function () {

            var scheduler = new Rx.TestScheduler();

            var input = scheduler.createHotObservable(
                onNext(201, 's'),
                onNext(202, 'o'),
                onNext(203, 'm'),
                onNext(204, 'e'),
                onNext(205, 'n'),
                onNext(206, 'a'),
                onNext(207, 'm'),
                onNext(208, 'e'),
                onCompleted(300)
            );

            scheduler.startScheduler(function () {
                    return input.map(function (b) {
                        $('#userId').trigger('keyup');
                    });
                },
                {
                    created: 100,
                    subscribed: 150,
                    disposed: 400
                }
            );

            expect($('#container').html()).toEqual("free");
        });
    });
});

I would like to be able to just get package with reactive code, put it into test-case and check if the final result is ok (theres not a lot of logic there, just some ajax needs to be mocked, so depending on the response the info will be displayed). But nothing get past first filter (i checked it with console logs) - when i trigger keyup in test case only some part of code in checkUserId.js is executed. Even if i put second filter it wont work over ther - its like the only one that reacts somehow to the event from testcase is the first one (not mentioning checking what will happen in subscribe functions).

Long story short - i just want to put some part of code into test case and know what do i have to trigger and provide so the whole path would be executed?

Please be gentle - this is my first time :)

Aucun commentaire:

Enregistrer un commentaire