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

Commit a78f687

Browse files
committed
squash: flush rAF for tests that have more than one match for the current switch
Without flushing the $$rAF, the ngSwitch leave animations are not completed on their own, but are instead completed when the next switch change finds them unfinished and calls cancel on them. This causes the done callbacks to be called synchronously instead of asynchronously.
1 parent ad104a6 commit a78f687

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

test/ng/directive/ngSwitchSpec.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('ngSwitch', function() {
3636
}));
3737

3838

39-
it('should show all switch-whens that match the current value', inject(function($rootScope, $compile) {
39+
it('should show all switch-whens that match the current value', inject(function($rootScope, $compile, $$rAF) {
4040
element = $compile(
4141
'<ul ng-switch="select">' +
4242
'<li ng-switch-when="1">first:{{name}}</li>' +
@@ -54,14 +54,18 @@ describe('ngSwitch', function() {
5454
$rootScope.name = 'shyam';
5555
$rootScope.$apply();
5656
expect(element.text()).toEqual('first:shyam, first too:shyam');
57+
5758
$rootScope.select = 2;
5859
$rootScope.$apply();
60+
$$rAF.flush();
5961
expect(element.text()).toEqual('second:shyam');
6062
$rootScope.name = 'misko';
6163
$rootScope.$apply();
6264
expect(element.text()).toEqual('second:misko');
65+
6366
$rootScope.select = true;
6467
$rootScope.$apply();
68+
$$rAF.flush();
6569
expect(element.text()).toEqual('true:misko');
6670
}));
6771

@@ -278,7 +282,7 @@ describe('ngSwitch', function() {
278282
}));
279283

280284

281-
it('should properly support case labels with different numbers of transclude fns', inject(function($rootScope, $compile) {
285+
it('should properly support case labels with different numbers of transclude fns', inject(function($rootScope, $compile, $$rAF) {
282286
element = $compile(
283287
'<div ng-switch="mode">' +
284288
'<p ng-switch-when="a">Block1</p>' +
@@ -292,16 +296,18 @@ describe('ngSwitch', function() {
292296

293297
$rootScope.$apply('mode = "b"');
294298
expect(element.children().length).toBe(1);
299+
$$rAF.flush();
295300

296301
$rootScope.$apply('mode = "a"');
297302
expect(element.children().length).toBe(2);
303+
$$rAF.flush();
298304

299305
$rootScope.$apply('mode = "b"');
300306
expect(element.children().length).toBe(1);
301307
}));
302308

303309

304-
it('should not trigger a digest when an element is removed', inject(function($$rAF, $compile, $rootScope, $timeout) {
310+
it('should not trigger a digest after an element is removed', inject(function($$rAF, $compile, $rootScope, $timeout) {
305311
var spy = spyOn($rootScope, '$digest').and.callThrough();
306312

307313
$rootScope.select = 1;
@@ -328,6 +334,12 @@ describe('ngSwitch', function() {
328334

329335

330336
describe('ngSwitchWhen separator', function() {
337+
var $$rAF;
338+
339+
beforeEach(inject(function(_$$rAF_) {
340+
$$rAF = _$$rAF_;
341+
}));
342+
331343
it('should be possible to define a separator', inject(function($rootScope, $compile) {
332344
element = $compile(
333345
'<div ng-switch="mode">' +
@@ -341,9 +353,13 @@ describe('ngSwitch', function() {
341353
expect(element.children().length).toBe(2);
342354
expect(element.text()).toBe('Block1|Block2|');
343355
$rootScope.$apply('mode = "b"');
356+
$$rAF.flush();
357+
344358
expect(element.children().length).toBe(1);
345359
expect(element.text()).toBe('Block1|');
346360
$rootScope.$apply('mode = "c"');
361+
$$rAF.flush();
362+
347363
expect(element.children().length).toBe(1);
348364
expect(element.text()).toBe('Block3|');
349365
}));
@@ -362,9 +378,13 @@ describe('ngSwitch', function() {
362378
expect(element.children().length).toBe(2);
363379
expect(element.text()).toBe('Block1|Block2|');
364380
$rootScope.$apply('mode = ""');
381+
$$rAF.flush();
382+
365383
expect(element.children().length).toBe(1);
366384
expect(element.text()).toBe('Block1|');
367385
$rootScope.$apply('mode = "c"');
386+
$$rAF.flush();
387+
368388
expect(element.children().length).toBe(1);
369389
expect(element.text()).toBe('Block3|');
370390
}));
@@ -383,9 +403,13 @@ describe('ngSwitch', function() {
383403
expect(element.children().length).toBe(2);
384404
expect(element.text()).toBe('Block1|Block2|');
385405
$rootScope.$apply('mode = "b"');
406+
$$rAF.flush();
407+
386408
expect(element.children().length).toBe(1);
387409
expect(element.text()).toBe('Block1|');
388410
$rootScope.$apply('mode = "c"');
411+
$$rAF.flush();
412+
389413
expect(element.children().length).toBe(1);
390414
expect(element.text()).toBe('Block3|');
391415
}));
@@ -404,9 +428,13 @@ describe('ngSwitch', function() {
404428
expect(element.children().length).toBe(2);
405429
expect(element.text()).toBe('Block1|Block2|');
406430
$rootScope.$apply('mode = "b|a"');
431+
$$rAF.flush();
432+
407433
expect(element.children().length).toBe(1);
408434
expect(element.text()).toBe('Block1|');
409435
$rootScope.$apply('mode = "c"');
436+
$$rAF.flush();
437+
410438
expect(element.children().length).toBe(1);
411439
expect(element.text()).toBe('Block3|');
412440
}));
@@ -425,9 +453,13 @@ describe('ngSwitch', function() {
425453
expect(element.children().length).toBe(2);
426454
expect(element.text()).toBe('Block1|Block2|');
427455
$rootScope.$apply('mode = "b"');
456+
$$rAF.flush();
457+
428458
expect(element.children().length).toBe(1);
429459
expect(element.text()).toBe('Block1|');
430460
$rootScope.$apply('mode = "c"');
461+
$$rAF.flush();
462+
431463
expect(element.children().length).toBe(1);
432464
expect(element.text()).toBe('Block3|');
433465
}));

0 commit comments

Comments
 (0)