[英]How do I discover the DOM element that fired the event and behave accordingly
[英]How do I use event spies on angular element in the dom?
在這里,我將創建,編譯,鏈接和附加元素,然后設置事件間諜。
describe('Click Confirm', function() {
var eventSpy, element, scope;
beforeEach(module('app'));
beforeEach(inject(function ($location, $rootScope, $compile) {
var el, linkFn;
scope = $rootScope.$new();
el = angular.element('<a id="testClickConfirm" href="" ng-click="deleteFn()" click-confirm="Test confirmation message">Delete</a>');
// The $compile method returns the directive's link function
linkFn = $compile(el);
// The link function returns the resulting DOM object
element = linkFn(scope);
$('body').append(element);
eventSpy = spyOnEvent('#testClickConfirm', 'click');
});
這些通過並確認元素存在,在DOM中並且可見。
it('should have set up correctly', function() {
expect(element).toExist();
expect(element).toBeInDOM();
expect(element).toBeVisible();
expect(el).toExist();
expect(el).toBeInDOM();
expect(el).toBeVisible();
expect($('#testClickConfirm')).toExist();
expect($('#testClickConfirm')).toBeInDOM();
expect($('#testClickConfirm')).toBeVisible();
});
it('should have set up correctly', function() {
element.click();
expect(eventSpy).toHaveBeenTriggered(); // <- FAILS
expect('click').toHaveBeenTriggeredOn('#testClickConfirm'); // <- FAILS
});
});
我究竟做錯了什么? 為什么這些事件間諜說尚未觸發點擊。
我在我正在測試的指令中設置了element.bind('click', ...
並在其中添加了console.log
,當我模擬click事件時會觸發它。
當然,我立即意識到了罪魁禍首。
因此,毫不奇怪,茉莉花jQuery將不會處理其處理程序調用e.stopImmediatePropagation()
事件。
Jasmine-jquery確實提供了一個方法.toHaveBeenStopped()
但是由於我使用的是stopImmediatePropagation()
因此間諜似乎也被窒息了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.