Jasmine测试Durandal事件

时间:2015-05-05 19:52:00

标签: jasmine durandal durandal-2.0

我很惊讶我在文档或博客文章中没有找到任何帮助。

我试图添加测试以确保正确设置事件并保持这种状态。 来自Require的durandal app和events对象为null。因此,在我们不知道之前必须有一个设置步骤。

我有这个Knockout视图模型:

define([
    'knockout',
    'durandal/events',
    'durandal/app',
    'Common/Modules/KoComponents/slideInEventListKeys'
],
function (ko, durandalEvents, durandalApp, slideInEventListKeys) {
 function SlideInVm(params) {
    // listen for anyone broadcasting this specific event.
    durandalEvents.includeIn(this);
    var _this = this;
    durandalApp.on(slideInEventListKeys.open).then(function () {
        _this.isShowing(true);
    });

    durandalApp.on(slideInEventListKeys.close).then(function () {
        _this.isShowing(false);
    });
  }
   SlideInVm.prototype.isShowing = ko.observable(false);
});

在Jasmine:

define([
        'knockout',
        'Common/Modules/slideInVm',
        'durandal/events',
        'durandal/app',
        'Common/Modules/slideInEventListKeys'
],
function (ko, SlideInVm, durandalEvents, durandalApp, slideInEventListKeys) {
    'use strict';
    describe('Given the Slide In component', function () {
        var vm;
        beforeEach(function(){
          vm =  new SlideInVm();
        });
        it('Should subscribe for open events', function () {
                debugger;
                // events and app are null!
                // how to check this?
        });
        it('Should subscribe for close events', function () {

        });

        describe('When opening', function(){
            beforeEach(function(){
                // how to trigger this if app is null?
                app.trigger(slideInEventListKeys.open);
            });
            it('Should show the component', function(){
                expect(vm.isShowing()).toBeTruthy();
            });
        });
    });
});

0 个答案:

没有答案