diff --git a/src/components/sidenav/sidenav.spec.js b/src/components/sidenav/sidenav.spec.js index 1a11f2f414b..97a42783e49 100644 --- a/src/components/sidenav/sidenav.spec.js +++ b/src/components/sidenav/sidenav.spec.js @@ -243,77 +243,75 @@ describe('mdSidenav', function() { describe("focus", function() { var $material, $mdInteraction, $mdConstant; + var triggerElement; - beforeEach( inject(function(_$material_, _$mdInteraction_, _$mdConstant_) { - $material = _$material_; - $mdInteraction = _$mdInteraction_; - $mdConstant = _$mdConstant_ + beforeEach(inject(function($injector) { + $material = $injector.get('$material'); + $mdInteraction = $injector.get('$mdInteraction'); + $mdConstant = $injector.get('$mdInteraction'); + + triggerElement = angular.element(''); + document.body.appendChild(triggerElement[0]); })); + afterEach(function() { + triggerElement.remove(); + }); + + function dispatchEvent(eventName) { + angular.element(document.body).triggerHandler(eventName); + } + function flush() { $material.flushInterimElement(); } - function setupTrigger() { - var el; - inject(function($compile, $rootScope) { - var parent = angular.element(document.body); - el = angular.element(''); - parent.append(el); - $compile(parent)($rootScope); - $rootScope.$apply(); - }); - return el; + function blur() { + if ('documentMode' in document) { + document.body.focus(); + } else { + triggerElement.blur(); + } } - it("should restore after sidenav triggered by keyboard", function(done) { - var sidenavElement = setup(''); - var triggerElement = setupTrigger(); - var controller = sidenavElement.controller('mdSidenav'); + it("should restore after sidenav triggered by keyboard", function() { + var sidenavEl = setup(''); + var controller = sidenavEl.controller('mdSidenav'); triggerElement.focus(); - var keyboardEvent = document.createEvent("KeyboardEvent"); - keyboardEvent.initEvent("keydown", true, true, window, 0, 0, 0, 0, $mdConstant.KEY_CODE.ENTER, $mdConstant.KEY_CODE.ENTER); - triggerElement[0].dispatchEvent(keyboardEvent); + dispatchEvent('keydown'); controller.$toggleOpen(true); flush(); - triggerElement.blur(); + blur(); controller.$toggleOpen(false); flush(); expect($mdInteraction.getLastInteractionType()).toBe("keyboard"); expect(document.activeElement).toBe(triggerElement[0]); - done(); }); - it("should not restore after sidenav triggered by mouse", function(done) { - var sidenavElement = setup(''); - var triggerElement = setupTrigger(); - var controller = sidenavElement.controller('mdSidenav'); + it("should not restore after sidenav triggered by mouse", function() { + var sidenavEl = setup(''); + var controller = sidenavEl.controller('mdSidenav'); triggerElement.focus(); - var mouseEvent = document.createEvent("MouseEvent"); - mouseEvent.initMouseEvent("mousedown", true, true, window, null, 0, 0, 0, 0, false, false, false, false, 0, null); - triggerElement[0].dispatchEvent(mouseEvent); + dispatchEvent('mousedown'); controller.$toggleOpen(true); flush(); - expect(document.activeElement).toBe(triggerElement[0]); - - triggerElement.blur(); + blur(); controller.$toggleOpen(false); flush(); expect($mdInteraction.getLastInteractionType()).toBe("mouse"); expect(document.activeElement).not.toBe(triggerElement[0]); - done(); }); }); diff --git a/src/core/services/interaction/interaction.js b/src/core/services/interaction/interaction.js index 65997b0d491..e39211b7b80 100644 --- a/src/core/services/interaction/interaction.js +++ b/src/core/services/interaction/interaction.js @@ -1,4 +1,4 @@ -/* +/** * @ngdoc module * @name material.core.interaction * @description @@ -9,7 +9,7 @@ angular .service('$mdInteraction', MdInteractionService); -/* +/** * @ngdoc service * @name $mdInteraction * @module material.core.interaction @@ -125,7 +125,7 @@ MdInteractionService.prototype.onBufferInputEvent = function(event) { * @ngdoc method * @name $mdInteraction#getLastInteractionType * @description Retrieves the last interaction type triggered in body. - * @returns {'mouse'|'keyboard'|'touch'} Last interaction type. + * @returns {string|null} Last interaction type. */ MdInteractionService.prototype.getLastInteractionType = function() { return this.lastInteractionType; diff --git a/src/core/services/interaction/interaction.spec.js b/src/core/services/interaction/interaction.spec.js index ae9c6464027..0344efed340 100644 --- a/src/core/services/interaction/interaction.spec.js +++ b/src/core/services/interaction/interaction.spec.js @@ -3,27 +3,28 @@ describe("$mdInteraction service", function() { beforeEach(module('material.core')); - beforeEach(inject(function(_$mdInteraction_) { - $mdInteraction = _$mdInteraction_; + beforeEach(inject(function($injector) { + $mdInteraction = $injector.get('$mdInteraction'); })); describe("last interaction type", function() { - it("imitates a basic keyboard interaction and checks it", function() { + var bodyElement = null; - var event = document.createEvent('Event'); - event.keyCode = 37; - event.initEvent('keydown', false, true); - document.body.dispatchEvent(event); + beforeEach(function() { + bodyElement = angular.element(document.body); + }); + + it("should detect a keyboard interaction", function() { + + bodyElement.triggerHandler('keydown'); expect($mdInteraction.getLastInteractionType()).toBe('keyboard'); }); - it("dispatches a mousedown event on the document body and checks it", function() { + it("should detect a mouse interaction", function() { - var event = document.createEvent("MouseEvent"); - event.initMouseEvent("mousedown", true, true, window, null, 0, 0, 0, 0, false, false, false, false, 0, null); - document.body.dispatchEvent(event); + bodyElement.triggerHandler('mousedown'); expect($mdInteraction.getLastInteractionType()).toBe("mouse"); });