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

Fix the ngMock issue with ngAnimate #5941

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 15 additions & 46 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,70 +757,39 @@ angular.mock.TzDate = function (offset, timestamp) {
angular.mock.TzDate.prototype = Date.prototype;
/* jshint +W101 */

// TODO(matias): remove this IMMEDIATELY once we can properly detect the
// presence of a registered module
var animateLoaded;
try {
angular.module('ngAnimate');
animateLoaded = true;
} catch(e) {}

if(animateLoaded) {
angular.module('ngAnimate').config(['$provide', function($provide) {
angular.mock.animate = angular.module('ngAnimateMock', ['ng'])

.config(['$provide', function($provide) {
var reflowQueue = [];

$provide.value('$$animateReflow', function(fn) {
reflowQueue.push(fn);
return angular.noop;
});
$provide.decorator('$animate', function($delegate) {
$delegate.triggerReflow = function() {
if(reflowQueue.length === 0) {
throw new Error('No animation reflows present');
}
angular.forEach(reflowQueue, function(fn) {
fn();
});
reflowQueue = [];
};
return $delegate;
});
}]);
}

angular.mock.animate = angular.module('mock.animate', ['ng'])

.config(['$provide', function($provide) {

$provide.decorator('$animate', function($delegate) {
var animate = {
queue : [],
enabled : $delegate.enabled,
flushNext : function(name) {
var tick = animate.queue.shift();

if (!tick) throw new Error('No animation to be flushed');
if(tick.method !== name) {
throw new Error('The next animation is not "' + name +
'", but is "' + tick.method + '"');
triggerReflow : function() {
if(reflowQueue.length === 0) {
throw new Error('No animation reflows present');
}
tick.fn();
return tick;
angular.forEach(reflowQueue, function(fn) {
fn();
});
reflowQueue = [];
}
};

angular.forEach(['enter','leave','move','addClass','removeClass'], function(method) {
animate[method] = function() {
var params = arguments;
animate.queue.push({
method : method,
params : params,
element : angular.isElement(params[0]) && params[0],
parent : angular.isElement(params[1]) && params[1],
after : angular.isElement(params[2]) && params[2],
fn : function() {
$delegate[method].apply($delegate, params);
}
event : method,
element : arguments[0],
args : arguments
});
$delegate[method].apply($delegate, arguments);
};
});

Expand Down
22 changes: 13 additions & 9 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4490,7 +4490,7 @@ describe('$compile', function() {

describe('$animate animation hooks', function() {

beforeEach(module('mock.animate'));
beforeEach(module('ngAnimateMock'));

it('should automatically fire the addClass and removeClass animation hooks',
inject(function($compile, $animate, $rootScope) {
Expand All @@ -4506,8 +4506,9 @@ describe('$compile', function() {
$rootScope.val2 = 'rice';
$rootScope.$digest();

data = $animate.flushNext('addClass');
expect(data.params[1]).toBe('ice rice');
data = $animate.queue.shift();
expect(data.event).toBe('addClass');
expect(data.args[1]).toBe('ice rice');

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

data = $animate.flushNext('removeClass');
expect(data.params[1]).toBe('rice');
data = $animate.flushNext('addClass');
expect(data.params[1]).toBe('dice');
data = $animate.queue.shift();
expect(data.event).toBe('removeClass');
expect(data.args[1]).toBe('rice');
data = $animate.queue.shift();
expect(data.event).toBe('addClass');
expect(data.args[1]).toBe('dice');

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

data = $animate.flushNext('removeClass');
expect(data.params[1]).toBe('ice dice');
data = $animate.queue.shift();
expect(data.event).toBe('removeClass');
expect(data.args[1]).toBe('ice dice');

expect(element.hasClass('ice')).toBe(false);
expect(element.hasClass('dice')).toBe(false);
Expand Down
34 changes: 19 additions & 15 deletions test/ng/directive/ngClassSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ describe('ngClass animations', function() {
var body, element, $rootElement;

it("should avoid calling addClass accidentally when removeClass is going on", function() {
module('mock.animate');
module('ngAnimateMock');
inject(function($compile, $rootScope, $animate, $timeout) {
var element = angular.element('<div ng-class="val"></div>');
var body = jqLite(document.body);
Expand All @@ -320,23 +320,23 @@ describe('ngClass animations', function() {

$rootScope.val = 'one';
$rootScope.$digest();
$animate.flushNext('addClass');
expect($animate.queue.shift().event).toBe('addClass');
expect($animate.queue.length).toBe(0);

$rootScope.val = '';
$rootScope.$digest();
$animate.flushNext('removeClass'); //only removeClass is called
expect($animate.queue.shift().event).toBe('removeClass'); //only removeClass is called
expect($animate.queue.length).toBe(0);

$rootScope.val = 'one';
$rootScope.$digest();
$animate.flushNext('addClass');
expect($animate.queue.shift().event).toBe('addClass');
expect($animate.queue.length).toBe(0);

$rootScope.val = 'two';
$rootScope.$digest();
$animate.flushNext('removeClass');
$animate.flushNext('addClass');
expect($animate.queue.shift().event).toBe('removeClass');
expect($animate.queue.shift().event).toBe('addClass');
expect($animate.queue.length).toBe(0);
});
});
Expand Down Expand Up @@ -416,7 +416,7 @@ describe('ngClass animations', function() {
});

it("should not remove classes if they're going to be added back right after", function() {
module('mock.animate');
module('ngAnimateMock');

inject(function($rootScope, $compile, $animate) {
var className;
Expand All @@ -430,28 +430,32 @@ describe('ngClass animations', function() {
$rootScope.$digest();

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

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

$rootScope.three = false;
$rootScope.$digest();

className = $animate.flushNext('removeClass').params[1];
expect(className).toBe('three');
item = $animate.queue.shift();
expect(item.event).toBe('removeClass');
expect(item.args[1]).toBe('three');

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

$rootScope.two = false;
$rootScope.three = true;
$rootScope.$digest();

className = $animate.flushNext('removeClass').params[1];
expect(className).toBe('two');
item = $animate.queue.shift();
expect(item.event).toBe('removeClass');
expect(item.args[1]).toBe('two');

className = $animate.flushNext('addClass').params[1];
expect(className).toBe('three');
item = $animate.queue.shift();
expect(item.event).toBe('addClass');
expect(item.args[1]).toBe('three');

expect($animate.queue.length).toBe(0);
});
Expand Down
19 changes: 11 additions & 8 deletions test/ng/directive/ngIfSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ describe('ngIf animations', function () {
return element;
}

beforeEach(module('mock.animate'));
beforeEach(module('ngAnimateMock'));

beforeEach(module(function() {
// we need to run animation on attached elements;
Expand Down Expand Up @@ -245,8 +245,9 @@ describe('ngIf animations', function () {
$rootScope.$digest();
$scope.$apply('value = true');

item = $animate.flushNext('enter').element;
expect(item.text()).toBe('Hi');
item = $animate.queue.shift();
expect(item.event).toBe('enter');
expect(item.element.text()).toBe('Hi');

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

item = $animate.flushNext('enter').element;
expect(item.text()).toBe('Hi');
item = $animate.queue.shift();
expect(item.event).toBe('enter');
expect(item.element.text()).toBe('Hi');

$scope.$apply('value = false');
expect(element.children().length).toBe(1);
$scope.$apply('value = false');

item = $animate.flushNext('leave').element;
expect(item.text()).toBe('Hi');
item = $animate.queue.shift();
expect(item.event).toBe('leave');
expect(item.element.text()).toBe('Hi');

expect(element.children().length).toBe(0);
}));
Expand Down
Loading