触发功能输入 - 茉莉花测试

Fire function onInput - Jasmine test

本文关键字:茉莉花 测试 输入 功能      更新时间:2023-09-26

我有以下Jasmine测试,它试图触发一个函数onInput但它根本没有触发它。

问题

使用

夹具,我可以像在标准 html 中那样使用并期望它在包含的脚本中找到函数吗?

这就是我正在尝试的:

// check an input cannot accept more than x characters
describe("when the page is loaded", function(){
    var fixture;
    beforeEach(function () {
        fixture += "<input id='textInput' onInput='limitLength2(this,2)'>";
        setFixtures(fixture);
    });
    it("an input should only allow x number of characters", function(){
        $('#textInput').trigger("focus").val($('#textInput').val() + '1');
        $('#textInput').trigger("focus").val($('#textInput').val() + '1');
        $('#textInput').trigger("focus").val($('#textInput').val() + '1');
        expect($('#textInput').val().length).toEqual(2);
    });
    afterEach(function () {
        fixture = "";
        fixture = null;
    });
})

测试失败,就像Expected 3 to equal 2.

我还在limitLength2()中添加了一个console.log(),可以确认,这没有被触发。

可以这样做吗?

如果使用 jQuery 更改值,则事件 'onchange' 不会触发,也不会触发 'oninput。

您需要在.val(.... + 1).trigger('input'),然后它应该按预期工作:-)