aboutsummaryrefslogtreecommitdiffstats
path: root/test/directivesSpec.js
diff options
context:
space:
mode:
authorIgor Minar2010-11-10 16:14:47 -0800
committerIgor Minar2010-11-10 21:04:44 -0800
commitc5b2bf083c7044aaf9269ad2cd6469fd14653917 (patch)
tree2db5c806e834d33760b8a9d2ae2e154bdc4467f0 /test/directivesSpec.js
parent0499c4727036446f5c8a5722bbd9c4018dae146f (diff)
downloadangular.js-c5b2bf083c7044aaf9269ad2cd6469fd14653917.tar.bz2
Make ng:repeat expose $position.
- $position is a textual representation of the position of repeated item ('first', 'middle', 'last') - added specs for $index
Diffstat (limited to 'test/directivesSpec.js')
-rw-r--r--test/directivesSpec.js89
1 files changed, 63 insertions, 26 deletions
diff --git a/test/directivesSpec.js b/test/directivesSpec.js
index a3aa2481..d575c062 100644
--- a/test/directivesSpec.js
+++ b/test/directivesSpec.js
@@ -135,39 +135,76 @@ describe("directives", function(){
expect(element.text()).toEqual('');
});
- it('should ng:repeat over array', function(){
- var scope = compile('<ul><li ng:repeat="item in items" ng:init="suffix = \';\'" ng:bind="item + suffix"></li></ul>');
- Array.prototype.extraProperty = "should be ignored";
- scope.items = ['misko', 'shyam'];
- scope.$eval();
- expect(element.text()).toEqual('misko;shyam;');
- delete Array.prototype.extraProperty;
+ describe('ng:repeat', function() {
- scope.items = ['adam', 'kai', 'brad'];
- scope.$eval();
- expect(element.text()).toEqual('adam;kai;brad;');
+ it('should ng:repeat over array', function(){
+ var scope = compile('<ul><li ng:repeat="item in items" ng:init="suffix = \';\'" ng:bind="item + suffix"></li></ul>');
- scope.items = ['brad'];
- scope.$eval();
- expect(element.text()).toEqual('brad;');
- });
+ Array.prototype.extraProperty = "should be ignored";
+ scope.items = ['misko', 'shyam'];
+ scope.$eval();
+ expect(element.text()).toEqual('misko;shyam;');
+ delete Array.prototype.extraProperty;
- it('should ng:repeat over object', function(){
- var scope = compile('<ul><li ng:repeat="(key, value) in items" ng:bind="key + \':\' + value + \';\' "></li></ul>');
- scope.$set('items', {misko:'swe', shyam:'set'});
- scope.$eval();
- expect(element.text()).toEqual('misko:swe;shyam:set;');
- });
+ scope.items = ['adam', 'kai', 'brad'];
+ scope.$eval();
+ expect(element.text()).toEqual('adam;kai;brad;');
+
+ scope.items = ['brad'];
+ scope.$eval();
+ expect(element.text()).toEqual('brad;');
+ });
+
+ it('should ng:repeat over object', function(){
+ var scope = compile('<ul><li ng:repeat="(key, value) in items" ng:bind="key + \':\' + value + \';\' "></li></ul>');
+ scope.$set('items', {misko:'swe', shyam:'set'});
+ scope.$eval();
+ expect(element.text()).toEqual('misko:swe;shyam:set;');
+ });
- it('should error on wrong parsing of ng:repeat', function(){
- var scope = compile('<ul><li ng:repeat="i dont parse"></li></ul>');
- var log = "";
- log += element.attr('ng-exception') + ';';
- log += element.hasClass('ng-exception') + ';';
- expect(log).toEqual("\"Expected ng:repeat in form of 'item in collection' but got 'i dont parse'.\";true;");
+ it('should error on wrong parsing of ng:repeat', function(){
+ var scope = compile('<ul><li ng:repeat="i dont parse"></li></ul>');
+ var log = "";
+ log += element.attr('ng-exception') + ';';
+ log += element.hasClass('ng-exception') + ';';
+ expect(log).toEqual("\"Expected ng:repeat in form of 'item in collection' but got 'i dont parse'.\";true;");
+ });
+
+ it('should expose iterator offset as $index when iterating over arrays', function() {
+ var scope = compile('<ul><li ng:repeat="item in items" ' +
+ 'ng:bind="item + $index + \'|\'"></li></ul>');
+ scope.items = ['misko', 'shyam', 'frodo'];
+ scope.$eval();
+ expect(element.text()).toEqual('misko0|shyam1|frodo2|');
+ });
+
+ it('should expose iterator offset as $index when iterating over objects', function() {
+ var scope = compile('<ul><li ng:repeat="(key, val) in items" ' +
+ 'ng:bind="key + \':\' + val + $index + \'|\'"></li></ul>');
+ scope.items = {'misko':'m', 'shyam':'s', 'frodo':'f'};
+ scope.$eval();
+ expect(element.text()).toEqual('misko:m0|shyam:s1|frodo:f2|');
+ });
+
+ it('should expose iterator position as $position when iterating over arrays', function() {
+ var scope = compile('<ul><li ng:repeat="item in items" ' +
+ 'ng:bind="item + \':\' + $position + \'|\'"></li></ul>');
+ scope.items = ['misko', 'shyam', 'doug', 'frodo'];
+ scope.$eval();
+ expect(element.text()).toEqual('misko:first|shyam:middle|doug:middle|frodo:last|');
+ });
+
+ it('should expose iterator position as $position when iterating over objects', function() {
+ var scope = compile('<ul><li ng:repeat="(key, val) in items" ' +
+ 'ng:bind="key + \':\' + val + \':\' + $position + \'|\'"></li></ul>');
+ scope.items = {'misko':'m', 'shyam':'s', 'doug':'d', 'frodo':'f'};
+ scope.$eval();
+ expect(element.text()).toEqual('misko:m:first|shyam:s:middle|doug:d:middle|frodo:f:last|');
+ });
});
+
it('should ng:watch', function(){
var scope = compile('<div ng:watch="i: count = count + 1" ng:init="count = 0">');
scope.$eval();