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

Commit df9b9cd

Browse files
crisbetokara
authored andcommitted
test(colors): fix leaking elements during unit tests (#9645)
The unit tests for the `md-colors` directive were causing a leak since they were being compiled and added to the DOM, but were never being removed.
1 parent c912e9d commit df9b9cd

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

src/components/colors/colors.spec.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ describe('md-colors', function () {
22
var $compile, $rootScope;
33
var $mdColorPalette, $mdTheming;
44
var supplant, scope;
5+
var compiledElements = [];
56

67
beforeEach(module('material.components.colors', function ($mdThemingProvider) {
78
$mdThemingProvider.theme('myTheme')
@@ -18,6 +19,14 @@ describe('md-colors', function () {
1819
scope = $rootScope.$new();
1920
}));
2021

22+
afterEach(function() {
23+
compiledElements.forEach(function(element) {
24+
element.remove();
25+
});
26+
27+
compiledElements = [];
28+
});
29+
2130
// documentMode is an only-IE property, which confirms that the specs are currently running inside
2231
// of an Internet Explorer.
2332
var isIE = !!window.document.documentMode;
@@ -32,18 +41,25 @@ describe('md-colors', function () {
3241
}
3342
}
3443

44+
// Compiles a template and keeps track of the elements so they can be cleaned up properly.
45+
function compile(template) {
46+
var element = $compile(template)(scope);
47+
compiledElements.push(element);
48+
return element;
49+
}
50+
3551
describe('directive', function () {
3652

3753
function createElement(scope, options) {
3854
var style = supplant("{theme}-{palette}-{hue}-{opacity}", {
39-
attrs : options.attrs,
40-
palette : options.palette,
41-
theme : options.theme || 'default',
42-
hue : options.hue || (options.palette === 'accent' ? 'A200' : '500'),
43-
opacity : options.opacity || 1
44-
});
55+
attrs : options.attrs,
56+
palette : options.palette,
57+
theme : options.theme || 'default',
58+
hue : options.hue || (options.palette === 'accent' ? 'A200' : '500'),
59+
opacity : options.opacity || 1
60+
});
4561
var markup = supplant('<div md-colors="{background: \'{0}\'}" {1} ></div>', [style, options.attrs]);
46-
var element = $compile( markup )(scope);
62+
var element = compile(markup);
4763

4864
scope.$apply(function() {
4965
angular.element(document.body).append( element );
@@ -141,7 +157,7 @@ describe('md-colors', function () {
141157
});
142158

143159
});
144-
160+
145161
describe('themes', function () {
146162
/**
147163
* <div md-colors="{background: 'primary'}">
@@ -294,7 +310,7 @@ describe('md-colors', function () {
294310

295311

296312
var markup = '<div md-theme="myTheme"><div md-colors="{background: \'primary\'}" ></div></div>';
297-
var element = $compile( markup )(scope);
313+
var element = compile(markup);
298314

299315
expect(element.children()[0].style.background).toContain( expectedRGB );
300316
});
@@ -313,7 +329,7 @@ describe('md-colors', function () {
313329

314330
scope.theme = 'myTheme';
315331
var markup = '<div md-theme="{{theme}}"><div md-colors="{background: \'primary\'}" ></div></div>';
316-
var element = $compile( markup )(scope);
332+
var element = compile(markup);
317333

318334
expect(element.children()[0].style.background).toContain( expectedRGB );
319335

@@ -355,9 +371,9 @@ describe('md-colors', function () {
355371
/**
356372
* <div md-colors="{ background: color() }" >
357373
*/
358-
it('should accept function', inject(function ($compile) {
374+
it('should accept function', function() {
359375
var color = $mdColorPalette['light-blue']['200'].value;
360-
var element = $compile('<div md-colors="{background: color()}"></div>')(scope);
376+
var element = compile('<div md-colors="{background: color()}"></div>');
361377
var expectedRGBa = buildColor(color[0], color[1], color[2], 0.8);
362378

363379
scope.color = function () {
@@ -366,13 +382,13 @@ describe('md-colors', function () {
366382
scope.$apply();
367383

368384
expect(element[0].style.background).toContain( expectedRGBa );
369-
}));
385+
});
370386

371387
/**
372388
* <div md-colors="{ background: test ? 'red' : 'lightBlue' }" >
373389
*/
374-
it('should accept ternary value', inject(function ($compile, $timeout) {
375-
var element = $compile('<div md-colors="{background: \'{{test ? \'red\' : \'lightBlue\'}}\'}"></div>')(scope);
390+
it('should accept ternary value', inject(function($timeout) {
391+
var element = compile('<div md-colors="{background: \'{{test ? \'red\' : \'lightBlue\'}}\'}"></div>');
376392
var color = $mdColorPalette['light-blue']['500'].value;
377393
var red = $mdColorPalette['red']['500'].value;
378394
var expectedRGB = buildColor(color[0], color[1], color[2]);
@@ -458,7 +474,7 @@ describe('md-colors', function () {
458474
* <div md-colors="{background: '{{color}}' }" >
459475
*/
460476
it('should delete old colors when getting an empty object', function() {
461-
var element = $compile( '<div md-colors="{{color}}"></div>' )(scope);
477+
var element = compile('<div md-colors="{{color}}"></div>');
462478

463479
scope.color = '{background: \'red\'}';
464480
scope.$apply();
@@ -490,7 +506,7 @@ describe('md-colors', function () {
490506
* <div md-colors="" >
491507
*/
492508
it('should accept empty value and not color the element', function() {
493-
var element = $compile( '<div md-colors=""></div>' )(scope);
509+
var element = compile('<div md-colors=""></div>');
494510

495511
expect(element[0].style.background).toBe( '' );
496512
});
@@ -499,7 +515,7 @@ describe('md-colors', function () {
499515
* <div md-colors="{}" >
500516
*/
501517
it('should accept empty object and not color the element', function() {
502-
var element = $compile( '<div md-colors="{}"></div>' )(scope);
518+
var element = compile('<div md-colors="{}"></div>');
503519

504520
expect(element[0].style.background).toBe( '' );
505521
});

0 commit comments

Comments
 (0)