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

Commit 08bc72e

Browse files
codymikolmmalerba
authored andcommitted
chore(tests): use beforeAll where possible (#11658)
setting up jasmine matchers and mockElementFocus don't need to be evaluated every time a test runs. Relates to #11489
1 parent 1063a92 commit 08bc72e

File tree

1 file changed

+96
-94
lines changed

1 file changed

+96
-94
lines changed

test/angular-material-spec.js

Lines changed: 96 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -30,100 +30,7 @@
3030

3131
var enableAnimations;
3232

33-
afterEach(function() {
34-
enableAnimations && enableAnimations();
35-
enableAnimations = null;
36-
});
37-
38-
beforeEach(function() {
39-
40-
/**
41-
* Before each test, require that the 'ngMaterial-mock' module is ready for injection
42-
* NOTE: assumes that angular-material-mocks.js has been loaded.
43-
*/
44-
45-
module('ngAnimate');
46-
module('ngMaterial-mock');
47-
48-
module(function() {
49-
return function($mdUtil, $rootElement, $document, $animate) {
50-
var DISABLE_ANIMATIONS = 'disable_animations';
51-
52-
// Create special animation 'stop' function used
53-
// to set 0ms durations for all animations and transitions
54-
55-
window.disableAnimations = function disableAnimations() {
56-
var body = angular.element($document[0].body);
57-
var head = angular.element($document[0].getElementsByTagName('head')[0]);
58-
var styleSheet = angular.element( buildStopTransitions() );
59-
60-
$animate.enabled(false);
61-
62-
head.prepend(styleSheet);
63-
body.addClass(DISABLE_ANIMATIONS);
64-
65-
// Prepare auto-restore
66-
enableAnimations = function() {
67-
body.removeClass(DISABLE_ANIMATIONS);
68-
styleSheet.remove();
69-
};
70-
};
71-
72-
/**
73-
* Build stylesheet to set all transition and animation
74-
* durations' to zero.
75-
*/
76-
function buildStopTransitions() {
77-
var style = "<style> .{0} * { {1} }</style>";
78-
79-
return $mdUtil.supplant(style,[ DISABLE_ANIMATIONS,
80-
"transition -webkit-transition animation -webkit-animation"
81-
.split(" ")
82-
.map(function(key){
83-
return $mdUtil.supplant("{0}: 0s none !important",[key]);
84-
})
85-
.join("; ")
86-
]);
87-
88-
}
89-
90-
};
91-
});
92-
93-
/**
94-
* Mocks the focus method from the HTMLElement prototype for the duration
95-
* of the running test.
96-
*
97-
* The mock will be automatically removed after the test finished.
98-
*
99-
* @example
100-
*
101-
* it('some focus test', inject(function($document)
102-
* {
103-
* jasmine.mockElementFocus(this); // 'this' is the test instance
104-
*
105-
* doSomething();
106-
* expect($document.activeElement).toBe(someElement[0]);
107-
*
108-
* }));
109-
*
110-
*/
111-
jasmine.mockElementFocus = function() {
112-
var _focusFn = HTMLElement.prototype.focus;
113-
114-
inject(function($document) {
115-
HTMLElement.prototype.focus = function() {
116-
$document.activeElement = this;
117-
};
118-
});
119-
120-
// Un-mock focus after the test is done
121-
afterEach(function() {
122-
HTMLElement.prototype.focus = _focusFn;
123-
});
124-
125-
};
126-
33+
beforeAll(function () {
12734
/**
12835
* Add special matchers used in the AngularJS-Material spec.
12936
*/
@@ -279,6 +186,40 @@
279186
}
280187
});
281188

189+
/**
190+
* Mocks the focus method from the HTMLElement prototype for the duration
191+
* of the running test.
192+
*
193+
* The mock will be automatically removed after the test finished.
194+
*
195+
* @example
196+
*
197+
* it('some focus test', inject(function($document)
198+
* {
199+
* jasmine.mockElementFocus(this); // 'this' is the test instance
200+
*
201+
* doSomething();
202+
* expect($document.activeElement).toBe(someElement[0]);
203+
*
204+
* }));
205+
*
206+
*/
207+
jasmine.mockElementFocus = function() {
208+
var _focusFn = HTMLElement.prototype.focus;
209+
210+
inject(function($document) {
211+
HTMLElement.prototype.focus = function() {
212+
$document.activeElement = this;
213+
};
214+
});
215+
216+
// Un-mock focus after the test is done
217+
afterEach(function() {
218+
HTMLElement.prototype.focus = _focusFn;
219+
});
220+
221+
};
222+
282223
/**
283224
* Returns the angular element associated with a css selector or element.
284225
* @param el {string|!angular.JQLite|!Element}
@@ -289,6 +230,67 @@
289230
document.querySelector(el) : el;
290231
return angular.element(queryResult);
291232
}
233+
});
234+
235+
afterEach(function() {
236+
enableAnimations && enableAnimations();
237+
enableAnimations = null;
238+
});
239+
240+
beforeEach(function() {
241+
242+
/**
243+
* Before each test, require that the 'ngMaterial-mock' module is ready for injection
244+
* NOTE: assumes that angular-material-mocks.js has been loaded.
245+
*/
246+
247+
module('ngAnimate');
248+
module('ngMaterial-mock');
249+
250+
module(function() {
251+
return function($mdUtil, $rootElement, $document, $animate) {
252+
var DISABLE_ANIMATIONS = 'disable_animations';
253+
254+
// Create special animation 'stop' function used
255+
// to set 0ms durations for all animations and transitions
256+
257+
window.disableAnimations = function disableAnimations() {
258+
var body = angular.element($document[0].body);
259+
var head = angular.element($document[0].getElementsByTagName('head')[0]);
260+
var styleSheet = angular.element( buildStopTransitions() );
261+
262+
$animate.enabled(false);
263+
264+
head.prepend(styleSheet);
265+
body.addClass(DISABLE_ANIMATIONS);
266+
267+
// Prepare auto-restore
268+
enableAnimations = function() {
269+
body.removeClass(DISABLE_ANIMATIONS);
270+
styleSheet.remove();
271+
};
272+
};
273+
274+
/**
275+
* Build stylesheet to set all transition and animation
276+
* durations' to zero.
277+
*/
278+
function buildStopTransitions() {
279+
var style = "<style> .{0} * { {1} }</style>";
280+
281+
return $mdUtil.supplant(style,[ DISABLE_ANIMATIONS,
282+
"transition -webkit-transition animation -webkit-animation"
283+
.split(" ")
284+
.map(function(key){
285+
return $mdUtil.supplant("{0}: 0s none !important",[key]);
286+
})
287+
.join("; ")
288+
]);
289+
290+
}
291+
292+
};
293+
});
292294

293295
});
294296

0 commit comments

Comments
 (0)