Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
Fix tests for IE11
Browse files Browse the repository at this point in the history
  • Loading branch information
devversion committed Aug 27, 2016
1 parent c536ce9 commit 06b4345
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 48 deletions.
66 changes: 32 additions & 34 deletions src/components/sidenav/sidenav.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<button>Trigger Element</button>');
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('<button>Toggle</button>');
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();
});

});
Expand Down
6 changes: 3 additions & 3 deletions src/core/services/interaction/interaction.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* @ngdoc module
* @name material.core.interaction
* @description
Expand All @@ -9,7 +9,7 @@ angular
.service('$mdInteraction', MdInteractionService);


/*
/**
* @ngdoc service
* @name $mdInteraction
* @module material.core.interaction
Expand Down Expand Up @@ -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;
Expand Down
23 changes: 12 additions & 11 deletions src/core/services/interaction/interaction.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Expand Down

0 comments on commit 06b4345

Please sign in to comment.