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

Commit 906fdad

Browse files
committed
fix(mocks): remove usage of $animate.flushNext in favour of queing
The flushNext method of testing is difficult and highly coupled with the behavior of ngAnimate's $animate workflow. It is much better instead to just queue all $animate animation calls into a queue collection which is available on the $animate service when mock.animate is included as a module within test code.
1 parent a8c1d9c commit 906fdad

9 files changed

+205
-182
lines changed

src/ngMock/angular-mocks.js

+4-21
Original file line numberDiff line numberDiff line change
@@ -790,37 +790,20 @@ if(animateLoaded) {
790790
angular.mock.animate = angular.module('mock.animate', ['ng'])
791791

792792
.config(['$provide', function($provide) {
793-
794793
$provide.decorator('$animate', function($delegate) {
795794
var animate = {
796795
queue : [],
797796
enabled : $delegate.enabled,
798-
flushNext : function(name) {
799-
var tick = animate.queue.shift();
800-
801-
if (!tick) throw new Error('No animation to be flushed');
802-
if(tick.method !== name) {
803-
throw new Error('The next animation is not "' + name +
804-
'", but is "' + tick.method + '"');
805-
}
806-
tick.fn();
807-
return tick;
808-
}
809797
};
810798

811799
angular.forEach(['enter','leave','move','addClass','removeClass'], function(method) {
812800
animate[method] = function() {
813-
var params = arguments;
814801
animate.queue.push({
815-
method : method,
816-
params : params,
817-
element : angular.isElement(params[0]) && params[0],
818-
parent : angular.isElement(params[1]) && params[1],
819-
after : angular.isElement(params[2]) && params[2],
820-
fn : function() {
821-
$delegate[method].apply($delegate, params);
822-
}
802+
event : method,
803+
element : arguments[0],
804+
args : arguments
823805
});
806+
$delegate[method].apply($delegate, arguments);
824807
};
825808
});
826809

test/ng/compileSpec.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -4506,8 +4506,9 @@ describe('$compile', function() {
45064506
$rootScope.val2 = 'rice';
45074507
$rootScope.$digest();
45084508

4509-
data = $animate.flushNext('addClass');
4510-
expect(data.params[1]).toBe('ice rice');
4509+
data = $animate.queue.shift();
4510+
expect(data.event).toBe('addClass');
4511+
expect(data.args[1]).toBe('ice rice');
45114512

45124513
expect(element.hasClass('ice')).toBe(true);
45134514
expect(element.hasClass('rice')).toBe(true);
@@ -4516,10 +4517,12 @@ describe('$compile', function() {
45164517
$rootScope.val2 = 'dice';
45174518
$rootScope.$digest();
45184519

4519-
data = $animate.flushNext('removeClass');
4520-
expect(data.params[1]).toBe('rice');
4521-
data = $animate.flushNext('addClass');
4522-
expect(data.params[1]).toBe('dice');
4520+
data = $animate.queue.shift();
4521+
expect(data.event).toBe('removeClass');
4522+
expect(data.args[1]).toBe('rice');
4523+
data = $animate.queue.shift();
4524+
expect(data.event).toBe('addClass');
4525+
expect(data.args[1]).toBe('dice');
45234526

45244527
expect(element.hasClass('ice')).toBe(true);
45254528
expect(element.hasClass('dice')).toBe(true);
@@ -4529,8 +4532,9 @@ describe('$compile', function() {
45294532
$rootScope.val2 = '';
45304533
$rootScope.$digest();
45314534

4532-
data = $animate.flushNext('removeClass');
4533-
expect(data.params[1]).toBe('ice dice');
4535+
data = $animate.queue.shift();
4536+
expect(data.event).toBe('removeClass');
4537+
expect(data.args[1]).toBe('ice dice');
45344538

45354539
expect(element.hasClass('ice')).toBe(false);
45364540
expect(element.hasClass('dice')).toBe(false);

test/ng/directive/ngClassSpec.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -320,23 +320,23 @@ describe('ngClass animations', function() {
320320

321321
$rootScope.val = 'one';
322322
$rootScope.$digest();
323-
$animate.flushNext('addClass');
323+
expect($animate.queue.shift().event).toBe('addClass');
324324
expect($animate.queue.length).toBe(0);
325325

326326
$rootScope.val = '';
327327
$rootScope.$digest();
328-
$animate.flushNext('removeClass'); //only removeClass is called
328+
expect($animate.queue.shift().event).toBe('removeClass'); //only removeClass is called
329329
expect($animate.queue.length).toBe(0);
330330

331331
$rootScope.val = 'one';
332332
$rootScope.$digest();
333-
$animate.flushNext('addClass');
333+
expect($animate.queue.shift().event).toBe('addClass');
334334
expect($animate.queue.length).toBe(0);
335335

336336
$rootScope.val = 'two';
337337
$rootScope.$digest();
338-
$animate.flushNext('removeClass');
339-
$animate.flushNext('addClass');
338+
expect($animate.queue.shift().event).toBe('removeClass');
339+
expect($animate.queue.shift().event).toBe('addClass');
340340
expect($animate.queue.length).toBe(0);
341341
});
342342
});
@@ -430,28 +430,32 @@ describe('ngClass animations', function() {
430430
$rootScope.$digest();
431431

432432
//this fires twice due to the class observer firing
433-
className = $animate.flushNext('addClass').params[1];
434-
expect(className).toBe('one two three');
433+
var item = $animate.queue.shift();
434+
expect(item.event).toBe('addClass');
435+
expect(item.args[1]).toBe('one two three');
435436

436437
expect($animate.queue.length).toBe(0);
437438

438439
$rootScope.three = false;
439440
$rootScope.$digest();
440441

441-
className = $animate.flushNext('removeClass').params[1];
442-
expect(className).toBe('three');
442+
item = $animate.queue.shift();
443+
expect(item.event).toBe('removeClass');
444+
expect(item.args[1]).toBe('three');
443445

444446
expect($animate.queue.length).toBe(0);
445447

446448
$rootScope.two = false;
447449
$rootScope.three = true;
448450
$rootScope.$digest();
449451

450-
className = $animate.flushNext('removeClass').params[1];
451-
expect(className).toBe('two');
452+
item = $animate.queue.shift();
453+
expect(item.event).toBe('removeClass');
454+
expect(item.args[1]).toBe('two');
452455

453-
className = $animate.flushNext('addClass').params[1];
454-
expect(className).toBe('three');
456+
item = $animate.queue.shift();
457+
expect(item.event).toBe('addClass');
458+
expect(item.args[1]).toBe('three');
455459

456460
expect($animate.queue.length).toBe(0);
457461
});

test/ng/directive/ngIfSpec.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,9 @@ describe('ngIf animations', function () {
245245
$rootScope.$digest();
246246
$scope.$apply('value = true');
247247

248-
item = $animate.flushNext('enter').element;
249-
expect(item.text()).toBe('Hi');
248+
item = $animate.queue.shift();
249+
expect(item.event).toBe('enter');
250+
expect(item.element.text()).toBe('Hi');
250251

251252
expect(element.children().length).toBe(1);
252253
}));
@@ -262,14 +263,16 @@ describe('ngIf animations', function () {
262263
))($scope);
263264
$scope.$apply('value = true');
264265

265-
item = $animate.flushNext('enter').element;
266-
expect(item.text()).toBe('Hi');
266+
item = $animate.queue.shift();
267+
expect(item.event).toBe('enter');
268+
expect(item.element.text()).toBe('Hi');
267269

268-
$scope.$apply('value = false');
269270
expect(element.children().length).toBe(1);
271+
$scope.$apply('value = false');
270272

271-
item = $animate.flushNext('leave').element;
272-
expect(item.text()).toBe('Hi');
273+
item = $animate.queue.shift();
274+
expect(item.event).toBe('leave');
275+
expect(item.element.text()).toBe('Hi');
273276

274277
expect(element.children().length).toBe(0);
275278
}));

test/ng/directive/ngIncludeSpec.js

+22-19
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ describe('ngInclude', function() {
367367
});
368368

369369
expect(autoScrollSpy).not.toHaveBeenCalled();
370-
$animate.flushNext('enter');
370+
expect($animate.queue.shift().event).toBe('enter');
371371
$timeout.flush();
372372

373373
expect(autoScrollSpy).toHaveBeenCalledOnce();
@@ -384,25 +384,25 @@ describe('ngInclude', function() {
384384
$rootScope.value = true;
385385
});
386386

387-
$animate.flushNext('enter');
387+
expect($animate.queue.shift().event).toBe('enter');
388388
$timeout.flush();
389389

390390
$rootScope.$apply(function () {
391391
$rootScope.tpl = 'another.html';
392392
$rootScope.value = 'some-string';
393393
});
394394

395-
$animate.flushNext('leave');
396-
$animate.flushNext('enter');
395+
expect($animate.queue.shift().event).toBe('leave');
396+
expect($animate.queue.shift().event).toBe('enter');
397397
$timeout.flush();
398398

399399
$rootScope.$apply(function() {
400400
$rootScope.tpl = 'template.html';
401401
$rootScope.value = 100;
402402
});
403403

404-
$animate.flushNext('leave');
405-
$animate.flushNext('enter');
404+
expect($animate.queue.shift().event).toBe('leave');
405+
expect($animate.queue.shift().event).toBe('enter');
406406
$timeout.flush();
407407

408408
expect(autoScrollSpy).toHaveBeenCalled();
@@ -418,7 +418,7 @@ describe('ngInclude', function() {
418418
$rootScope.tpl = 'template.html';
419419
});
420420

421-
$animate.flushNext('enter');
421+
expect($animate.queue.shift().event).toBe('enter');
422422
$timeout.flush();
423423
expect(autoScrollSpy).not.toHaveBeenCalled();
424424
}));
@@ -434,7 +434,7 @@ describe('ngInclude', function() {
434434
$rootScope.value = false;
435435
});
436436

437-
$animate.flushNext('enter');
437+
expect($animate.queue.shift().event).toBe('enter');
438438
$timeout.flush();
439439

440440
$rootScope.$apply(function () {
@@ -456,7 +456,7 @@ describe('ngInclude', function() {
456456
expect(autoScrollSpy).not.toHaveBeenCalled();
457457

458458
$rootScope.$apply("tpl = 'template.html'");
459-
$animate.flushNext('enter');
459+
expect($animate.queue.shift().event).toBe('enter');
460460
$timeout.flush();
461461

462462
expect(autoScrollSpy).toHaveBeenCalledOnce();
@@ -608,8 +608,9 @@ describe('ngInclude animations', function() {
608608
))($rootScope);
609609
$rootScope.$digest();
610610

611-
item = $animate.flushNext('enter').element;
612-
expect(item.text()).toBe('data');
611+
var animation = $animate.queue.pop();
612+
expect(animation.event).toBe('enter');
613+
expect(animation.element.text()).toBe('data');
613614
}));
614615

615616
it('should fire off the leave animation',
@@ -624,14 +625,16 @@ describe('ngInclude animations', function() {
624625
))($rootScope);
625626
$rootScope.$digest();
626627

627-
item = $animate.flushNext('enter').element;
628-
expect(item.text()).toBe('data');
628+
var animation = $animate.queue.shift();
629+
expect(animation.event).toBe('enter');
630+
expect(animation.element.text()).toBe('data');
629631

630632
$rootScope.tpl = '';
631633
$rootScope.$digest();
632634

633-
item = $animate.flushNext('leave').element;
634-
expect(item.text()).toBe('data');
635+
animation = $animate.queue.shift();
636+
expect(animation.event).toBe('leave');
637+
expect(animation.element.text()).toBe('data');
635638
}));
636639

637640
it('should animate two separate ngInclude elements',
@@ -647,14 +650,14 @@ describe('ngInclude animations', function() {
647650
))($rootScope);
648651
$rootScope.$digest();
649652

650-
item = $animate.flushNext('enter').element;
651-
expect(item.text()).toBe('one');
653+
var item1 = $animate.queue.shift().element;
654+
expect(item1.text()).toBe('one');
652655

653656
$rootScope.tpl = 'two';
654657
$rootScope.$digest();
655658

656-
var itemA = $animate.flushNext('leave').element;
657-
var itemB = $animate.flushNext('enter').element;
659+
var itemA = $animate.queue.shift().element;
660+
var itemB = $animate.queue.shift().element;
658661
expect(itemA.attr('ng-include')).toBe('tpl');
659662
expect(itemB.attr('ng-include')).toBe('tpl');
660663
expect(itemA).not.toEqual(itemB);

0 commit comments

Comments
 (0)