mercredi 6 juillet 2016

Unit Testing Knockout Binding Handlers with Jasmine

I have created a binding handler dependent on moment which is used for formatting the date. I wanted to unit test this binding handler using Jasmine.

Below is my binding handler code:

define(['knockout', 'moment'], function (ko, moment) {

    'use strict';
    ko.bindingHandlers.date = {
        update: function (element, dateValue, allBindings) {
            var date = ko.utils.unwrapObservable(dateValue()) || '-',
                format = allBindings.get('format'),
                formattedDate = function () {
                    return moment(date).format(format);
                };
            ko.bindingHandlers.text.update(element, formattedDate);
        }
    };

    return {
        dateBinding: ko.bindingHandlers.date
    };
});

I am creating my spec file as below:

define(['testUtils', 'jquery', 'knockout'], function (testUtils, $, ko) {

    'use strict';
    ddescribe('utils/date.binding', function () {

        var testee;

        beforeEach(function (done) {
            testUtils.loadWithCurrentStubs('utils/date.binding', function (dateUtils) {
                testee = dateUtils;
                done();
            });
        });

        afterEach(function () {
            testUtils.reset();
        });

        describe('ko.bindingHandlers.date', function () {
            var element = document.createElement();

            it('should be true', function () {
                expect(true).toBe(true);
            });
        });
    });
});

Not sure where to start testing and what parts needs to be tested.

Aucun commentaire:

Enregistrer un commentaire