0

我刚刚编写了这个服务来提醒用户在关闭窗口时他们是未保存的数据:

    module.factory('autosaver',
        ['$rootScope', '$window', 'editor', 'doc', 'saveInterval', '$timeout',
        function ($rootScope, $window, editor, doc, saveInterval, $timeout) {

        var scope = $rootScope.$new(true);
        scope.doc = doc;
        scope.confirmOnLeave = function(e) {
        var msg = "You have unsaved data.";

        // For IE and Firefox
        e = e || window.event;
        if (e) {e.returnValue = msg;}

        // For Chrome and Safari
        return msg;
    };
    scope.$watch('doc.dirty', function (newValue, oldValue) {
        if(newValue !== oldValue) {
            newValue ? $window.addEventListener('beforeunload', scope.confirmOnLeave) : $window.removeEventListener('beforeunload', scope.confirmOnLeave);
        }
    });

    return scope;
}]);

现在我想写规范。我不知道如何模拟 beforeunload 事件并监视调用。我试过这段代码:

    it('should listen to beforeunload event', inject(function ($window, autosaver) {
        autosaver.confirmOnLeave = jasmine.createSpy('confirmOnLeave');

        var evt = document.createEvent("beforeunloadEvents");
        evt.initMouseEvent("beforeunload", true, true, $window,
            0, 0, 0, 0, 0, false, false, false, false, 0, null);

        $window.dispatchEvent(evt);
    }));

但我得到这个错误:

NotSupportedError: NotSupportedError: DOM Exception 9
    Error: The implementation did not support the requested type of object or operation.

谢谢你的帮助。

4

1 回答 1

1

与其直接使用 DOM,不如直接模拟$window.addEventListener并立即调用回调函数。

jasmine.spyOn($window, 'addEventListener');

调用你的函数,然后触发事件:

var msg = $window.addEventListener.mostRecentCall. args[1]({}) 
expect(msg).toBeEqual("You have unsaved data.")
于 2013-04-15T12:23:09.617 回答