【问题标题】:How can I call a Meteor template event?如何调用 Meteor 模板事件?
【发布时间】:2015-09-10 13:55:48
【问题描述】:

功能:用户必须在输入字段中输入讲座代码才能进入讲座。在登录页面上,当用户键入讲座代码时,我想检查代码是否正确,如果正确,将“btn-enter-lecture”涂成绿色。

'keyup #lecture-code-input' : function() {
    var possibleLectureID = $('#lecture-code-input').val();
    var possibleLecture = Lectures.findOne({lectureCode: possibleLectureID});
    if(possibleLecture){
        $('#btn-enter-lecture').addClass('btn-success');
        $('#btn-enter-lecture').removeClass('disabled');
    }
    else {
        $('#btn-enter-lecture').removeClass('btn-success');
        $('#btn-enter-lecture').addClass('disabled');
    }
}

为了使用 jasmine 测试此功能,我将讲座代码粘贴到输入字段中,并尝试使用 jquery 触发事件。但这不会称为流星事件。

describe("'Enter Class' button", function() {
    it("turns green when there is a lecture with this lecture code", function(done) {
        $('#lecture-code-input').val(lectureCode);
        $('#lecture-code-input').trigger('keyup');
        var interval = setInterval(function() {
            if(!$('button#btn-enter-class').hasClass('disabled')){
                clearInterval(interval);
                expect($('button#btn-enter-class').hasClass('btn-success')).toBe(true);
                done();
            }
        },5);
    });
});

如何触发这个流星模板事件来测试按钮之后是否变为绿色?

代码:https://github.com/minden/rewind/commit/cac61ecc3da3014548ad4ec9d1ceb2fd49bb265c

【问题讨论】:

    标签: javascript jquery meteor jasmine meteor-velocity


    【解决方案1】:

    查看本教程:https://doctorllama.wordpress.com/2014/09/22/bullet-proof-internationalised-meteor-applications-with-velocity-unit-testing-integration-testing-and-jasmine/

    一直到:“.registerForTutorial”

    你应该看到这个:

    var data = new Tutorial();
    
    spyOn(data, "registerStudent");
    spyOn(Blaze, "getData").and.returnValue(data);
    
    Template.tutorials.__eventMaps[0]["click .registerForTutorial"].call({templateInstance: function() {}}, {preventDefault : function() {}});
    
    expect(data.registerStudent).toHaveBeenCalled();
    });
    

    我认为这就是您要寻找的。您需要指定模板。

    【讨论】:

      【解决方案2】:

      你也可以看样例Leaderboard Tests

      这是来自链接的sn-p

      Tinytest.add('Template.leaderboard [click input.inc] event', function (test) {
      
        //updates the player score by 5 when input.inc is clicked
        Session.set('selected_player', 1234);
        Players.update = function (selector, options) {
            test.equal(selector, 1234);
            test.equal(options.$inc.score, 5);
        };
        Template.leaderboard.fireEvent('click input.inc');
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-02
        • 2015-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多