作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 angularjs 实现“@用户功能”,除了编写单元测试外,我几乎完成了该功能。我有一个 Caret 模块,它可以帮助我获得文本区域中的插入符位置。
我认为最重要的是获得插入符位置,但我不知道如何在 Jasmine 中做到这一点。
我的指令
.directive('atUser', function (Caret) {
return {
restrict: 'A',
link: function (scope, element) {
element.bind('focus click keydown', function () {
scope.caretPos = Caret.getPos(element);
});
scope.$watch(function () {
return scope.caretPos;
}, function (nowCaretPos) {
/* do something here */
})
}
}
})
HTML
<textarea ng-model="message" at-user></textarea>
Jasmine
describe('test at user', function () {
/* some init code */
it('should get caret postion', function () {
textarea = element.find('textarea');
textarea.triggerHandler('focus');
except(textarea.scope().caretPos).toEqual(0);
/*
* then i want to simulate keydown event and type something
* and get the caret postion
* but i dont know how to do it
* /
})
})
还有一件事是我不想使用 jquery。
谁能帮帮我?
非常感谢!
最佳答案
我刚刚问了一个类似的问题,我需要在 Jasmine 测试中设置 textarea
的插入符号位置,我得到了一个有效的答案(using selectionStart on programmatically-created inputs),所以这是一个潜在的您可以使用 Jasmine 实现的解决方案:
describe('test at user', function () {
/* some init code */
it('should get caret postion', function () {
textarea = element.find('textarea');
textarea.triggerHandler('focus');
expect(textarea.scope().caretPos).toEqual(0);
/*
* then i want to simulate keydown event and type something
* and get the caret postion
* but i dont know how to do it
*/
document.body.appendChild(textarea[0]); // I discovered this to be the key to using the .selectionStart property successfully
textarea.val('some text');
textarea[0].selectionStart = 9; // you need to move the caret manually when doing things programmatically
textarea.triggerHandler('focus');
expect(textarea.scope().caretPos).toEqual(9);
})
})
关于unit-testing - 我可以在 Jasmine 的单元测试中获得 textarea 插入符位置吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18353694/
我是一名优秀的程序员,十分优秀!