admin管理员组文章数量:1310119
In my directive's link
function:
$document.on('click.sortColumnList', function () {
viewToggleController.closeSortColumnList();
scope.$apply();
});
In my Jasmine unit test:
describe('document click', function () {
beforeEach(function () {
this.$document.triggerHandler('click.sortColumnList');
});
it('should tell the controller to close the sort column list', function () {
expect(this.ctrlMock.closeSortColumnList).toHaveBeenCalled();
});
it('should trigger a digest cycle', function () {
expect(this.$scope.$apply).toHaveBeenCalled();
});
});
This is not actually triggering the event listener. The tests are failing with:
Expected spy closeSortColumnList to have been called.
The spies are set up correctly, the problem is that the event listener is not firing.
What do you have to do to trigger an event on $document
in a Jasmine Angular unit test?
In my directive's link
function:
$document.on('click.sortColumnList', function () {
viewToggleController.closeSortColumnList();
scope.$apply();
});
In my Jasmine unit test:
describe('document click', function () {
beforeEach(function () {
this.$document.triggerHandler('click.sortColumnList');
});
it('should tell the controller to close the sort column list', function () {
expect(this.ctrlMock.closeSortColumnList).toHaveBeenCalled();
});
it('should trigger a digest cycle', function () {
expect(this.$scope.$apply).toHaveBeenCalled();
});
});
This is not actually triggering the event listener. The tests are failing with:
Expected spy closeSortColumnList to have been called.
The spies are set up correctly, the problem is that the event listener is not firing.
What do you have to do to trigger an event on $document
in a Jasmine Angular unit test?
1 Answer
Reset to default 4You may need to add jasmine-jquery to your project they have specific functions to acplish what you need. Using this you can do something like the following:
var spyEvent = spyOnEvent('#some_element', 'click')
$('#some_element').click()
expect('click').toHaveBeenTriggeredOn('#some_element')
I hope this helps.
版权声明:本文标题:javascript - How do I trigger a click event in a Jasmine unit test for an Angular Directive? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741839612a2400418.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论