This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
interaction.spec.js
116 lines (78 loc) · 3.06 KB
/
interaction.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
describe("$mdInteraction service", function() {
var $mdInteraction = null;
var $rootScope = null;
var bodyElement = null;
var $timeout = null;
beforeEach(module('material.core'));
beforeEach(inject(function($injector) {
$mdInteraction = $injector.get('$mdInteraction');
$rootScope = $injector.get('$rootScope');
$timeout = $injector.get('$timeout');
bodyElement = angular.element(document.body);
}));
describe("last interaction type", function() {
it("should detect a keyboard interaction", function() {
bodyElement.triggerHandler('keydown');
expect($mdInteraction.getLastInteractionType()).toBe('keyboard');
});
it("should detect a mouse interaction", function() {
bodyElement.triggerHandler('mousedown');
expect($mdInteraction.getLastInteractionType()).toBe("mouse");
});
});
describe('isUserInvoked', function() {
var element = null;
beforeEach(function() {
element = angular.element('<button>Click</button>');
bodyElement.append(element);
});
afterEach(function() {
element.remove();
});
it('should be true when programmatically focusing an element', function() {
element.focus();
expect($mdInteraction.isUserInvoked()).toBe(false);
});
it('should be false when focusing an element through keyboard', function() {
// Fake a focus event triggered by a keyboard interaction.
bodyElement.triggerHandler('keydown');
element.focus();
expect($mdInteraction.isUserInvoked()).toBe(true);
});
it('should allow passing a custom check delay', function(done) {
bodyElement.triggerHandler('keydown');
// The keyboard interaction is still in the same tick, so the interaction happened earlier than 15ms (as default)
expect($mdInteraction.isUserInvoked()).toBe(true);
setTimeout(function() {
// Expect the keyboard interaction to be older than 5ms (safer than exactly 10ms) as check time.
expect($mdInteraction.isUserInvoked(5)).toBe(false);
done();
}, 10);
});
});
describe('when $rootScope is destroyed', function () {
var _initialTouchStartEvent = document.documentElement.ontouchstart;
beforeAll(function () {
document.documentElement.ontouchstart = function () {};
});
beforeEach(function () {
$mdInteraction.lastInteractionType = 'initial';
$rootScope.$destroy();
});
afterAll(function () {
document.documentElement.ontouchstart = _initialTouchStartEvent;
});
it('should remove mousedown events', function () {
bodyElement.triggerHandler('mousedown');
expect($mdInteraction.getLastInteractionType()).toEqual('initial');
});
it('should remove keydown events', function () {
bodyElement.triggerHandler('keydown');
expect($mdInteraction.getLastInteractionType()).toEqual('initial');
});
it('should remove touchstart events', function () {
bodyElement.triggerHandler('touchstart');
expect($mdInteraction.getLastInteractionType()).toEqual('initial');
});
});
});