簡體   English   中英

如何在dom中的角度元素上使用事件間諜?

[英]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事件時會觸發它。

e.stopImmediatePropagation();

當然,我立即意識到了罪魁禍首。

因此,毫不奇怪,茉莉花jQuery將不會處理其處理程序調用e.stopImmediatePropagation()事件。

Jasmine-jquery確實提供了一個方法.toHaveBeenStopped()但是由於我使用的是stopImmediatePropagation()因此間諜似乎也被窒息了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM