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

Commit 6f1bcfc

Browse files
drpicoxgkalpak
authored andcommitted
test(ngClass): add some tests about one-time bindings and objects inside arrays
1 parent 996914c commit 6f1bcfc

File tree

1 file changed

+94
-1
lines changed

1 file changed

+94
-1
lines changed

test/ng/directive/ngClassSpec.js

+94-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ describe('ngClass', function() {
310310
expect(e2.hasClass('D')).toBeFalsy();
311311
}));
312312

313-
314313
it('should reapply ngClass when interpolated class attribute changes',
315314
inject(function($compile, $rootScope) {
316315
element = $compile(
@@ -477,6 +476,100 @@ describe('ngClass', function() {
477476
})
478477
);
479478

479+
it('should do value stabilization as expected when one-time binding',
480+
inject(function($rootScope, $compile) {
481+
element = $compile('<div ng-class="::className"></div>')($rootScope);
482+
483+
$rootScope.$apply('className = "foo"');
484+
expect(element).toHaveClass('foo');
485+
486+
$rootScope.$apply('className = "bar"');
487+
expect(element).toHaveClass('foo');
488+
})
489+
);
490+
491+
it('should remove the watcher when static array one-time binding',
492+
inject(function($rootScope, $compile) {
493+
element = $compile('<div ng-class="::[className]"></div>')($rootScope);
494+
495+
$rootScope.$apply('className = "foo"');
496+
expect(element).toHaveClass('foo');
497+
498+
$rootScope.$apply('className = "bar"');
499+
expect(element).toHaveClass('foo');
500+
expect(element).not.toHaveClass('bar');
501+
})
502+
);
503+
504+
it('should remove the watcher when static map one-time binding',
505+
inject(function($rootScope, $compile) {
506+
element = $compile('<div ng-class="::{foo: fooPresent}"></div>')($rootScope);
507+
508+
$rootScope.$apply('fooPresent = true');
509+
expect(element).toHaveClass('foo');
510+
511+
$rootScope.$apply('fooPresent = false');
512+
expect(element).toHaveClass('foo');
513+
})
514+
);
515+
516+
it('should track changes of mutating object inside an array',
517+
inject(function($rootScope, $compile) {
518+
$rootScope.classVar = [{orange: true}];
519+
element = $compile('<div ng-class="classVar"></div>')($rootScope);
520+
521+
$rootScope.$digest();
522+
expect(element).toHaveClass('orange');
523+
524+
$rootScope.$apply('classVar[0].orange = false');
525+
expect(element).not.toHaveClass('orange');
526+
})
527+
);
528+
529+
describe('large objects', function() {
530+
531+
var verylargeobject, getProp;
532+
beforeEach(function() {
533+
getProp = jasmine.createSpy('getProp');
534+
verylargeobject = {};
535+
Object.defineProperty(verylargeobject, 'prop', {
536+
get: getProp,
537+
enumerable: true
538+
});
539+
});
540+
541+
it('should not be copied if static', inject(function($rootScope, $compile) {
542+
element = $compile('<div ng-class="{foo: verylargeobject}"></div>')($rootScope);
543+
$rootScope.verylargeobject = verylargeobject;
544+
$rootScope.$digest();
545+
546+
expect(getProp).not.toHaveBeenCalled();
547+
}));
548+
549+
it('should not be copied if dynamic', inject(function($rootScope, $compile) {
550+
$rootScope.fooClass = {foo: verylargeobject};
551+
element = $compile('<div ng-class="fooClass"></div>')($rootScope);
552+
$rootScope.$digest();
553+
554+
expect(getProp).not.toHaveBeenCalled();
555+
}));
556+
557+
it('should not be copied if inside an array', inject(function($rootScope, $compile) {
558+
element = $compile('<div ng-class="[{foo: verylargeobject}]"></div>')($rootScope);
559+
$rootScope.verylargeobject = verylargeobject;
560+
$rootScope.$digest();
561+
562+
expect(getProp).not.toHaveBeenCalled();
563+
}));
564+
565+
it('should not be copied when one-time binding', inject(function($rootScope, $compile) {
566+
element = $compile('<div ng-class="::{foo: verylargeobject}"></div>')($rootScope);
567+
$rootScope.verylargeobject = verylargeobject;
568+
$rootScope.$digest();
569+
570+
expect(getProp).not.toHaveBeenCalled();
571+
}));
572+
});
480573
});
481574

482575
describe('ngClass animations', function() {

0 commit comments

Comments
 (0)