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

Commit 3b17d5d

Browse files
committed
test(ngRepeat): Add 'toContainQueueItem' Jasmine matcher
The 'ngRepeat animations' test group should not assume the order of items in the animation queue. This matcher searches the animation queue for an item with an expected element text and event.
1 parent 4d6feb9 commit 3b17d5d

File tree

1 file changed

+73
-77
lines changed

1 file changed

+73
-77
lines changed

test/ng/directive/ngRepeatSpec.js

Lines changed: 73 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,15 +1424,58 @@ describe('ngRepeat animations', function() {
14241424
};
14251425
}));
14261426

1427+
beforeEach(function() {
1428+
jasmine.addMatchers({
1429+
toContainQueueItem: function() {
1430+
return {
1431+
compare: generateCompare(false),
1432+
negativeCompare: generateCompare(true)
1433+
};
1434+
function generateCompare(isNot) {
1435+
/**
1436+
* Matcher that checks that the expected element text is in the actual Array of
1437+
* animation queue items and that the event matches.
1438+
* @param {array} actual
1439+
* @param {string} expectedElementText
1440+
* @param {string} expectedEvent optional if isNot = true
1441+
* @returns {{pass: boolean, message: message}}
1442+
*/
1443+
return function(actual, expectedElementText, expectedEvent) {
1444+
if (expectedEvent === undefined) {
1445+
expectedEvent = '';
1446+
}
1447+
var actualLength = actual.length;
1448+
var i;
1449+
var item;
1450+
var message = valueFn(
1451+
'Expected animation queue to ' + (isNot ? 'not ' : '') + 'contain an item '
1452+
+ 'where the element\'s text is "' + expectedElementText
1453+
+ '"' + (isNot ? '' : ' and the event is "' + expectedEvent + '"')
1454+
);
1455+
var pass = isNot;
1456+
1457+
for (i = 0; i < actualLength; i++) {
1458+
item = actual[i];
1459+
if (item.element.text() === expectedElementText) {
1460+
pass = item.event === expectedEvent;
1461+
break;
1462+
}
1463+
}
1464+
1465+
return {'pass': pass, 'message': message};
1466+
};
1467+
}
1468+
}
1469+
});
1470+
});
1471+
14271472
afterEach(function() {
14281473
body.empty();
14291474
});
14301475

14311476
it('should fire off the enter animation',
14321477
inject(function($compile, $rootScope, $animate) {
14331478

1434-
var item;
1435-
14361479
element = $compile(html(
14371480
'<div><div ' +
14381481
'ng-repeat="item in items">' +
@@ -1445,24 +1488,15 @@ describe('ngRepeat animations', function() {
14451488
$rootScope.items = ['1','2','3'];
14461489
$rootScope.$digest();
14471490

1448-
item = $animate.queue.shift();
1449-
expect(item.event).toBe('enter');
1450-
expect(item.element.text()).toBe('1');
1451-
1452-
item = $animate.queue.shift();
1453-
expect(item.event).toBe('enter');
1454-
expect(item.element.text()).toBe('2');
1455-
1456-
item = $animate.queue.shift();
1457-
expect(item.event).toBe('enter');
1458-
expect(item.element.text()).toBe('3');
1491+
expect($animate.queue).toContainQueueItem('1', 'enter');
1492+
expect($animate.queue).toContainQueueItem('2', 'enter');
1493+
expect($animate.queue).toContainQueueItem('3', 'enter');
1494+
$animate.queue = [];
14591495
}));
14601496

14611497
it('should fire off the leave animation',
14621498
inject(function($compile, $rootScope, $animate) {
14631499

1464-
var item;
1465-
14661500
element = $compile(html(
14671501
'<div><div ' +
14681502
'ng-repeat="item in items">' +
@@ -1473,24 +1507,18 @@ describe('ngRepeat animations', function() {
14731507
$rootScope.items = ['1','2','3'];
14741508
$rootScope.$digest();
14751509

1476-
item = $animate.queue.shift();
1477-
expect(item.event).toBe('enter');
1478-
expect(item.element.text()).toBe('1');
1479-
1480-
item = $animate.queue.shift();
1481-
expect(item.event).toBe('enter');
1482-
expect(item.element.text()).toBe('2');
1483-
1484-
item = $animate.queue.shift();
1485-
expect(item.event).toBe('enter');
1486-
expect(item.element.text()).toBe('3');
1510+
expect($animate.queue).toContainQueueItem('1', 'enter');
1511+
expect($animate.queue).toContainQueueItem('2', 'enter');
1512+
expect($animate.queue).toContainQueueItem('3', 'enter');
1513+
$animate.queue = [];
14871514

14881515
$rootScope.items = ['1','3'];
14891516
$rootScope.$digest();
14901517

1491-
item = $animate.queue.shift();
1492-
expect(item.event).toBe('leave');
1493-
expect(item.element.text()).toBe('2');
1518+
expect($animate.queue).not.toContainQueueItem('1');
1519+
expect($animate.queue).toContainQueueItem('2', 'leave');
1520+
expect($animate.queue).toContainQueueItem('3', 'move');
1521+
$animate.queue = [];
14941522
}));
14951523

14961524
it('should not change the position of the block that is being animated away via a leave animation',
@@ -1530,8 +1558,6 @@ describe('ngRepeat animations', function() {
15301558
it('should fire off the move animation',
15311559
inject(function($compile, $rootScope, $animate) {
15321560

1533-
var item;
1534-
15351561
element = $compile(html(
15361562
'<div>' +
15371563
'<div ng-repeat="item in items">' +
@@ -1543,40 +1569,24 @@ describe('ngRepeat animations', function() {
15431569
$rootScope.items = ['1','2','3'];
15441570
$rootScope.$digest();
15451571

1546-
item = $animate.queue.shift();
1547-
expect(item.event).toBe('enter');
1548-
expect(item.element.text()).toBe('1');
1549-
1550-
item = $animate.queue.shift();
1551-
expect(item.event).toBe('enter');
1552-
expect(item.element.text()).toBe('2');
1553-
1554-
item = $animate.queue.shift();
1555-
expect(item.event).toBe('enter');
1556-
expect(item.element.text()).toBe('3');
1572+
expect($animate.queue).toContainQueueItem('1', 'enter');
1573+
expect($animate.queue).toContainQueueItem('2', 'enter');
1574+
expect($animate.queue).toContainQueueItem('3', 'enter');
1575+
$animate.queue = [];
15571576

15581577
$rootScope.items = ['2','3','1'];
15591578
$rootScope.$digest();
15601579

1561-
item = $animate.queue.shift();
1562-
expect(item.event).toBe('move');
1563-
expect(item.element.text()).toBe('2');
1564-
1565-
item = $animate.queue.shift();
1566-
expect(item.event).toBe('move');
1567-
expect(item.element.text()).toBe('3');
1568-
1569-
item = $animate.queue.shift();
1570-
expect(item.event).toBe('move');
1571-
expect(item.element.text()).toBe('1');
1580+
expect($animate.queue).toContainQueueItem('1', 'move');
1581+
expect($animate.queue).toContainQueueItem('2', 'move');
1582+
expect($animate.queue).toContainQueueItem('3', 'move');
1583+
$animate.queue = [];
15721584
})
15731585
);
15741586

15751587
it('should fire off the move animation for items that change position when other items are filtered out',
15761588
inject(function($compile, $rootScope, $animate) {
15771589

1578-
var item;
1579-
15801590
element = $compile(html(
15811591
'<div>' +
15821592
'<div ng-repeat="item in items | filter:search">' +
@@ -1589,32 +1599,18 @@ describe('ngRepeat animations', function() {
15891599
$rootScope.search = '';
15901600
$rootScope.$digest();
15911601

1592-
item = $animate.queue.shift();
1593-
expect(item.event).toBe('enter');
1594-
expect(item.element.text()).toBe('1');
1595-
1596-
item = $animate.queue.shift();
1597-
expect(item.event).toBe('enter');
1598-
expect(item.element.text()).toBe('2');
1599-
1600-
item = $animate.queue.shift();
1601-
expect(item.event).toBe('enter');
1602-
expect(item.element.text()).toBe('3');
1602+
expect($animate.queue).toContainQueueItem('1', 'enter');
1603+
expect($animate.queue).toContainQueueItem('2', 'enter');
1604+
expect($animate.queue).toContainQueueItem('3', 'enter');
1605+
$animate.queue = [];
16031606

16041607
$rootScope.search = '3';
16051608
$rootScope.$digest();
16061609

1607-
item = $animate.queue.shift();
1608-
expect(item.event).toBe('move');
1609-
expect(item.element.text()).toBe('3');
1610-
1611-
item = $animate.queue.shift();
1612-
expect(item.event).toBe('leave');
1613-
expect(item.element.text()).toBe('1');
1614-
1615-
item = $animate.queue.shift();
1616-
expect(item.event).toBe('leave');
1617-
expect(item.element.text()).toBe('2');
1610+
expect($animate.queue).toContainQueueItem('1', 'leave');
1611+
expect($animate.queue).toContainQueueItem('2', 'leave');
1612+
expect($animate.queue).toContainQueueItem('3', 'move');
1613+
$animate.queue = [];
16181614
})
16191615
);
16201616

0 commit comments

Comments
 (0)