@@ -3,6 +3,7 @@ describe('<md-tooltip> directive', function() {
3
3
var element ;
4
4
5
5
beforeEach ( module ( 'material.components.tooltip' ) ) ;
6
+ beforeEach ( module ( 'material.components.button' ) ) ;
6
7
beforeEach ( inject ( function ( _$compile_ , _$rootScope_ , _$animate_ , _$timeout_ ) {
7
8
$compile = _$compile_ ;
8
9
$rootScope = _$rootScope_ ;
@@ -19,7 +20,7 @@ describe('<md-tooltip> directive', function() {
19
20
buildTooltip (
20
21
'<md-button>' +
21
22
'Hello' +
22
- '<md-tooltip md-visible="isVisible">Tooltip</md-tooltip>' +
23
+ '<md-tooltip md-visible="testModel. isVisible">Tooltip</md-tooltip>' +
23
24
'</md-button>'
24
25
) ;
25
26
@@ -29,7 +30,7 @@ describe('<md-tooltip> directive', function() {
29
30
it ( 'should label parent' , function ( ) {
30
31
buildTooltip (
31
32
'<md-button>' +
32
- '<md-tooltip md-visible="isVisible">' +
33
+ '<md-tooltip md-visible="testModel. isVisible">' +
33
34
'Tooltip' +
34
35
'</md-tooltip>' +
35
36
'</md-button>'
@@ -46,38 +47,38 @@ describe('<md-tooltip> directive', function() {
46
47
buildTooltip (
47
48
'<outer>' +
48
49
'<inner>' +
49
- '<md-tooltip md-visible="isVisible">' +
50
+ '<md-tooltip md-visible="testModel. isVisible">' +
50
51
'Hello world' +
51
52
'</md-tooltip>' +
52
53
'</inner>' +
53
54
'</outer>'
54
55
) ;
55
56
56
57
triggerEvent ( 'mouseenter' , true ) ;
57
- expect ( $rootScope . isVisible ) . toBeUndefined ( ) ;
58
+ expect ( $rootScope . testModel . isVisible ) . toBeUndefined ( ) ;
58
59
59
60
} ) ) ;
60
61
61
62
it ( 'should show after tooltipDelay ms' , function ( ) {
62
63
buildTooltip (
63
64
'<md-button>' +
64
65
'Hello' +
65
- '<md-tooltip md-visible="isVisible" md-delay="99">' +
66
+ '<md-tooltip md-visible="testModel. isVisible" md-delay="99">' +
66
67
'Tooltip' +
67
68
'</md-tooltip>' +
68
69
'</md-button>'
69
70
) ;
70
71
71
72
triggerEvent ( 'focus' , true ) ;
72
- expect ( $rootScope . isVisible ) . toBeFalsy ( ) ;
73
+ expect ( $rootScope . testModel . isVisible ) . toBeFalsy ( ) ;
73
74
74
75
// Wait 1 below delay, nothing should happen
75
76
$timeout . flush ( 98 ) ;
76
- expect ( $rootScope . isVisible ) . toBeFalsy ( ) ;
77
+ expect ( $rootScope . testModel . isVisible ) . toBeFalsy ( ) ;
77
78
78
79
// Total 300 == tooltipDelay
79
80
$timeout . flush ( 1 ) ;
80
- expect ( $rootScope . isVisible ) . toBe ( true ) ;
81
+ expect ( $rootScope . testModel . isVisible ) . toBe ( true ) ;
81
82
82
83
} ) ;
83
84
@@ -90,7 +91,7 @@ describe('<md-tooltip> directive', function() {
90
91
buildTooltip (
91
92
'<md-button>' +
92
93
'Hello' +
93
- '<md-tooltip md-visible="isVisible">' +
94
+ '<md-tooltip md-visible="testModel. isVisible">' +
94
95
'Tooltip' +
95
96
'</md-tooltip>' +
96
97
'</md-button>'
@@ -111,84 +112,101 @@ describe('<md-tooltip> directive', function() {
111
112
buildTooltip (
112
113
'<md-button>' +
113
114
'Hello' +
114
- '<md-tooltip md-visible="isVisible">' +
115
+ '<md-tooltip md-visible="testModel. isVisible">' +
115
116
'Tooltip' +
116
117
'</md-tooltip>' +
117
118
'</md-button>'
118
119
) ;
119
120
120
121
triggerEvent ( 'mouseenter' ) ;
121
- expect ( $rootScope . isVisible ) . toBe ( true ) ;
122
+ expect ( $rootScope . testModel . isVisible ) . toBe ( true ) ;
122
123
123
124
triggerEvent ( 'mouseleave' ) ;
124
- expect ( $rootScope . isVisible ) . toBe ( false ) ;
125
+ expect ( $rootScope . testModel . isVisible ) . toBe ( false ) ;
125
126
} ) ;
126
127
127
128
it ( 'should set visible on focus and blur' , function ( ) {
128
129
buildTooltip (
129
130
'<md-button>' +
130
131
'Hello' +
131
- '<md-tooltip md-visible="isVisible">' +
132
+ '<md-tooltip md-visible="testModel. isVisible">' +
132
133
'Tooltip' +
133
134
'</md-tooltip>' +
134
135
'</md-button>'
135
136
) ;
136
137
137
138
triggerEvent ( 'focus' ) ;
138
- expect ( $rootScope . isVisible ) . toBe ( true ) ;
139
+ expect ( $rootScope . testModel . isVisible ) . toBe ( true ) ;
139
140
140
141
triggerEvent ( 'blur' ) ;
141
- expect ( $rootScope . isVisible ) . toBe ( false ) ;
142
+ expect ( $rootScope . testModel . isVisible ) . toBe ( false ) ;
142
143
} ) ;
143
144
144
145
it ( 'should set visible on touchstart and touchend' , function ( ) {
145
146
buildTooltip (
146
147
'<md-button>' +
147
148
'Hello' +
148
- '<md-tooltip md-visible="isVisible">' +
149
+ '<md-tooltip md-visible="testModel. isVisible">' +
149
150
'Tooltip' +
150
151
'</md-tooltip>' +
151
152
'</md-button>'
152
153
) ;
153
154
154
155
155
156
triggerEvent ( 'touchstart' ) ;
156
- expect ( $rootScope . isVisible ) . toBe ( true ) ;
157
+ expect ( $rootScope . testModel . isVisible ) . toBe ( true ) ;
157
158
158
159
triggerEvent ( 'touchend' ) ;
159
- expect ( $rootScope . isVisible ) . toBe ( false ) ;
160
+ expect ( $rootScope . testModel . isVisible ) . toBe ( false ) ;
160
161
161
162
} ) ;
162
163
163
- it ( 'should not be visible on mousedown and then mouseleave' , inject ( function ( $document ) {
164
- jasmine . mockElementFocus ( this ) ;
165
-
164
+ it ( 'should not be visible on mousedown and then mouseleave' , inject ( function ( $document ) {
166
165
buildTooltip (
167
166
'<md-button>' +
168
167
'Hello' +
169
- '<md-tooltip md-visible="isVisible">' +
168
+ '<md-tooltip md-visible="testModel. isVisible">' +
170
169
'Tooltip' +
171
170
'</md-tooltip>' +
172
171
'</md-button>'
173
172
) ;
174
173
175
- // this focus is needed to set `$document. activeElement`
176
- // and wouldn't be required if ` document.activeElement` was settable.
177
- element . focus ( ) ;
178
- triggerEvent ( 'focus, mousedown' ) ;
174
+ // Append element to DOM so it can be set as activeElement.
175
+ $ document[ 0 ] . body . appendChild ( element [ 0 ] ) ;
176
+ element [ 0 ] . focus ( ) ;
177
+ triggerEvent ( 'focus,mousedown' ) ;
179
178
180
- expect ( $document . activeElement ) . toBe ( element [ 0 ] ) ;
181
- expect ( $rootScope . isVisible ) . toBe ( true ) ;
179
+ expect ( $document [ 0 ] . activeElement ) . toBe ( element [ 0 ] ) ;
180
+ expect ( $rootScope . testModel . isVisible ) . toBe ( true ) ;
182
181
183
182
triggerEvent ( 'mouseleave' ) ;
183
+ expect ( $rootScope . testModel . isVisible ) . toBe ( false ) ;
184
+ } ) ) ;
184
185
185
- // very weak test since this is really always set to false because
186
- // we are not able to set `document.activeElement` to the parent
187
- // of `md-tooltip`. we compensate by testing `$document.activeElement`
188
- // which sort of mocks the behavior through `jasmine.mockElementFocus`
189
- // which should be replaced by a true `document.activeElement` check
190
- // if the problem gets fixed.
191
- expect ( $rootScope . isVisible ) . toBe ( false ) ;
186
+ it ( 'should not be visible when the window is refocused' , inject ( function ( $window , $document ) {
187
+ buildTooltip (
188
+ '<md-button>' +
189
+ 'Hello' +
190
+ '<md-tooltip md-visible="testModel.isVisible">' +
191
+ 'Tooltip' +
192
+ '</md-tooltip>' +
193
+ '</md-button>'
194
+ ) ;
195
+
196
+ // Append element to DOM so it can be set as activeElement.
197
+ $document [ 0 ] . body . appendChild ( element [ 0 ] ) ;
198
+ element [ 0 ] . focus ( ) ;
199
+ triggerEvent ( 'focus,mousedown' ) ;
200
+ expect ( document . activeElement ) . toBe ( element [ 0 ] ) ;
201
+
202
+ triggerEvent ( 'mouseleave' ) ;
203
+
204
+ // Simulate tabbing away.
205
+ angular . element ( $window ) . triggerHandler ( 'blur' ) ;
206
+
207
+ // Simulate focus event that occurs when tabbing back to the window.
208
+ triggerEvent ( 'focus' ) ;
209
+ expect ( $rootScope . testModel . isVisible ) . toBe ( false ) ;
192
210
} ) ) ;
193
211
} ) ;
194
212
@@ -199,6 +217,7 @@ describe('<md-tooltip> directive', function() {
199
217
function buildTooltip ( markup ) {
200
218
201
219
element = $compile ( markup ) ( $rootScope ) ;
220
+ $rootScope . testModel = { } ;
202
221
203
222
$rootScope . $apply ( ) ;
204
223
$animate . triggerCallbacks ( ) ;
@@ -209,7 +228,7 @@ describe('<md-tooltip> directive', function() {
209
228
function showTooltip ( isVisible ) {
210
229
if ( angular . isUndefined ( isVisible ) ) isVisible = true ;
211
230
212
- $rootScope . $apply ( 'isVisible = ' + ( isVisible ? 'true' : 'false' ) ) ;
231
+ $rootScope . $apply ( 'testModel. isVisible = ' + ( isVisible ? 'true' : 'false' ) ) ;
213
232
$animate . triggerCallbacks ( ) ;
214
233
}
215
234
0 commit comments