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

Commit da8c320

Browse files
lgalfasopetebacondarwin
authored andcommitted
test(parse): Test for the parsing not invoking twice to get self
New tests to not call twice a function to get self
1 parent 06f2b2a commit da8c320

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

test/ng/parseSpec.js

+106
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,112 @@ describe('parser', function() {
483483
});
484484

485485

486+
it('should call the function from the received instance and not from a new one', function() {
487+
var n = 0;
488+
scope.fn = function() {
489+
var c = n++;
490+
return { c: c, anotherFn: function() { return this.c == c; } };
491+
};
492+
expect(scope.$eval('fn().anotherFn()')).toBe(true);
493+
});
494+
495+
496+
it('should call the function once when it is part of the context', function() {
497+
var count = 0;
498+
scope.fn = function() {
499+
count++;
500+
return { anotherFn: function() { return "lucas"; } };
501+
};
502+
expect(scope.$eval('fn().anotherFn()')).toBe('lucas');
503+
expect(count).toBe(1);
504+
});
505+
506+
507+
it('should call the function once when it is not part of the context', function() {
508+
var count = 0;
509+
scope.fn = function() {
510+
count++;
511+
return function() { return 'lucas'; };
512+
};
513+
expect(scope.$eval('fn()()')).toBe('lucas');
514+
expect(count).toBe(1);
515+
});
516+
517+
518+
it('should call the function once when it is not part of the context', function() {
519+
var count = 0;
520+
scope.fn = function() {
521+
count++;
522+
return function() { return 'lucas'; };
523+
};
524+
expect(scope.$eval('fn()()')).toBe('lucas');
525+
expect(count).toBe(1);
526+
});
527+
528+
529+
it('should call the function once when it is part of the context on assignments', function() {
530+
var count = 0;
531+
var element = {};
532+
scope.fn = function() {
533+
count++;
534+
return element;
535+
};
536+
expect(scope.$eval('fn().name = "lucas"')).toBe('lucas');
537+
expect(element.name).toBe('lucas');
538+
expect(count).toBe(1);
539+
});
540+
541+
542+
it('should call the function once when it is part of the context on array lookups', function() {
543+
var count = 0;
544+
var element = [];
545+
scope.fn = function() {
546+
count++;
547+
return element;
548+
};
549+
expect(scope.$eval('fn()[0] = "lucas"')).toBe('lucas');
550+
expect(element[0]).toBe('lucas');
551+
expect(count).toBe(1);
552+
});
553+
554+
555+
it('should call the function once when it is part of the context on array lookup function', function() {
556+
var count = 0;
557+
var element = [{anotherFn: function() { return 'lucas';} }];
558+
scope.fn = function() {
559+
count++;
560+
return element;
561+
};
562+
expect(scope.$eval('fn()[0].anotherFn()')).toBe('lucas');
563+
expect(count).toBe(1);
564+
});
565+
566+
567+
it('should call the function once when it is part of the context on array lookup function', function() {
568+
var count = 0;
569+
var element = {name: {anotherFn: function() { return 'lucas';} } };
570+
scope.fn = function() {
571+
count++;
572+
return element;
573+
};
574+
expect(scope.$eval('fn().name.anotherFn()')).toBe('lucas');
575+
expect(count).toBe(1);
576+
});
577+
578+
579+
it('should call the function once when it is part of a sub-expression', function() {
580+
var count = 0;
581+
scope.element = [{}];
582+
scope.fn = function() {
583+
count++;
584+
return 0;
585+
};
586+
expect(scope.$eval('element[fn()].name = "lucas"')).toBe('lucas');
587+
expect(scope.element[0].name).toBe('lucas');
588+
expect(count).toBe(1);
589+
});
590+
591+
486592
describe('promises', function() {
487593
var deferred, promise, q;
488594

0 commit comments

Comments
 (0)