vendredi 31 juillet 2015

angular unit test stateful filter

I have a filter that depends upon a service to retrieve some configuration, and hence I have made the filter stateful. It works great! However, I'd like to add unit tests around it but can't work out how to implement them.

An example filter:

.filter("myFilter", function(myService) {
    var data = null, serviceInvoked = false;

    function realFilter(value) {
        return data + value;
    }

    filterStub.$stateful = true;
    function filterStub(value) {
        if( data === null ) {
            if( !serviceInvoked ) {
                serviceInvoked = true;
                myService.get().then(function(result){
                    data = result;
                });
            }
            return "-";
        }
        else {
            return realFilter(value);
        }
    }

    return filterStub;
});

I can test that a blank string is initially returned.

In my next test I have stubbed myService with sinonjs and return a promise. I want to resolve that promise with data and see that the filter updates correctly.

Aucun commentaire:

Enregistrer un commentaire