对于以下注册表单,我将第一个输入字段配置id="signUpName"
为在呈现表单时聚焦。Backbone-forms用于创建表单。
我想用Jasmine v.1.3.0测试加载视图时是否关注某个输入表单字段:
it("should focus the name field of the sign-up form", function() {
var signUpName = userController.loginView.signUpForm.$el.find("#signUpName");
userController.loginView.showSignUpForm();
expect(signUpName).toBeFocused();
// expect(signUpName.is(":focus")).toBe(true);
});
茉莉花提示:
Expected '<input id="signUpName" name="signUpName" type="text">' to be focused.
正如您从规范中看到的那样,我使用来自jasmine-jquerytoBeFocused()
的匹配器。我运行扩展的最新版本 1.5.93。我还发现了关于实现的讨论,并注意到他们同时将其更改为Ryan Greenberg 建议的替代实现。toBeFocused()
作为替代方案,我试图监视焦点事件 - 如果真的有一个?
it("should focus the name field of the sign-up form", function() {
var signUpName = userController.loginView.signUpForm.$el.find("#signUpName");
var spyEvent = spyOnEvent(signUpName, 'focus');
userController.loginView.showSignUpForm();;
expect('focus').toHaveBeenTriggeredOn(signUpName);
expect(spyEvent).toHaveBeenTriggered();
});
茉莉花提示:
Expected event focus to have been triggered on [object Object]