This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
ngClass[Odd/Even] overhaul #15228
Closed
Closed
ngClass[Odd/Even] overhaul #15228
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8822006
refactor(ngClass): remove unnecessary dependency on `$animate`
gkalpak 0409589
refactor(ngClass): remove redundant `$observe`r
gkalpak 2705085
test(ngClass): add some tests about one-time bindings and objects ins…
gkalpak 0ba4be6
fix(ngClassOdd/Even): keep track of classes even if odd/even do not m…
gkalpak 89268af
perf(ngClass): only access the element's `data` once
gkalpak 2871a11
refactor(ngClass): simplify conditions
gkalpak a7e69f7
refactor(ngClass): move helper functions outside the closure
gkalpak 37cec7a
refactor(ngClass): exit `arrayDifference()` early if an input is empty
gkalpak 5708424
perf(ngClass): avoid deep-watching (if possible) and unnecessary copies
gkalpak 739f723
fixup! refactor(ngClass): remove unnecessary dependency on `$animate`
gkalpak 181c51b
fixup! refactor(ngClass): remove redundant `$observe`r
gkalpak 16e666b
fixup! test(ngClass): add some tests about one-time bindings and obje…
gkalpak dfe4307
squash! fix(ngClassOdd/Even): keep track of classes even if odd/even …
gkalpak f766698
fixup! perf(ngClass): avoid deep-watching (if possible) and unnecessa…
gkalpak a9742f9
fixup! refactor(ngClass): simplify conditions
gkalpak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
'use strict'; | ||
|
||
describe('ngClass', function() { | ||
fdescribe('ngClass', function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naught naughty There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I already have a fixup commit at the end 😛 |
||
var element; | ||
|
||
beforeEach(module(function($compileProvider) { | ||
|
@@ -244,31 +244,42 @@ describe('ngClass', function() { | |
})); | ||
|
||
|
||
it('should allow ngClassOdd/Even on the same element with overlapping classes', inject(function($rootScope, $compile, $animate) { | ||
var className; | ||
|
||
element = $compile('<ul><li ng-repeat="i in [0,1,2]" ng-class-odd="\'same odd\'" ng-class-even="\'same even\'"></li><ul>')($rootScope); | ||
it('should allow ngClassOdd/Even on the same element with overlapping classes', | ||
inject(function($compile, $rootScope) { | ||
element = $compile( | ||
'<ul>' + | ||
'<li ng-repeat="i in [0,1,2]" ' + | ||
'ng-class-odd="\'same odd\'" ' + | ||
'ng-class-even="\'same even\'">' + | ||
'</li>' + | ||
'<ul>')($rootScope); | ||
$rootScope.$digest(); | ||
var e1 = jqLite(element[0].childNodes[1]); | ||
var e2 = jqLite(element[0].childNodes[5]); | ||
expect(e1.hasClass('same')).toBeTruthy(); | ||
expect(e1.hasClass('odd')).toBeTruthy(); | ||
expect(e2.hasClass('same')).toBeTruthy(); | ||
expect(e2.hasClass('odd')).toBeTruthy(); | ||
|
||
var e1 = element.children().eq(0); | ||
var e2 = element.children().eq(1); | ||
var e3 = element.children().eq(2); | ||
|
||
expect(e1).toHaveClass('same'); | ||
expect(e1).toHaveClass('odd'); | ||
expect(e1).not.toHaveClass('even'); | ||
expect(e2).toHaveClass('same'); | ||
expect(e2).not.toHaveClass('odd'); | ||
expect(e2).toHaveClass('even'); | ||
expect(e3).toHaveClass('same'); | ||
expect(e3).toHaveClass('odd'); | ||
expect(e3).not.toHaveClass('even'); | ||
}) | ||
); | ||
|
||
it('should allow ngClass with overlapping classes', inject(function($rootScope, $compile, $animate) { | ||
it('should allow ngClass with overlapping classes', inject(function($rootScope, $compile) { | ||
element = $compile('<div ng-class="{\'same yes\': test, \'same no\': !test}"></div>')($rootScope); | ||
$rootScope.$digest(); | ||
|
||
expect(element).toHaveClass('same'); | ||
expect(element).not.toHaveClass('yes'); | ||
expect(element).toHaveClass('no'); | ||
|
||
$rootScope.$apply(function() { | ||
$rootScope.test = true; | ||
}); | ||
$rootScope.$apply('test = true'); | ||
|
||
expect(element).toHaveClass('same'); | ||
expect(element).toHaveClass('yes'); | ||
|
@@ -300,37 +311,79 @@ describe('ngClass', function() { | |
})); | ||
|
||
|
||
it('should reapply ngClass when interpolated class attribute changes', inject(function($rootScope, $compile) { | ||
element = $compile('<div class="one {{cls}} three" ng-class="{four: four}"></div>')($rootScope); | ||
|
||
$rootScope.$apply(function() { | ||
$rootScope.cls = 'two'; | ||
$rootScope.four = true; | ||
}); | ||
expect(element).toHaveClass('one'); | ||
expect(element).toHaveClass('two'); // interpolated | ||
expect(element).toHaveClass('three'); | ||
expect(element).toHaveClass('four'); | ||
|
||
$rootScope.$apply(function() { | ||
$rootScope.cls = 'too'; | ||
}); | ||
expect(element).toHaveClass('one'); | ||
expect(element).toHaveClass('too'); // interpolated | ||
expect(element).toHaveClass('three'); | ||
expect(element).toHaveClass('four'); // should still be there | ||
expect(element.hasClass('two')).toBeFalsy(); | ||
|
||
$rootScope.$apply(function() { | ||
$rootScope.cls = 'to'; | ||
}); | ||
expect(element).toHaveClass('one'); | ||
expect(element).toHaveClass('to'); // interpolated | ||
expect(element).toHaveClass('three'); | ||
expect(element).toHaveClass('four'); // should still be there | ||
expect(element.hasClass('two')).toBeFalsy(); | ||
expect(element.hasClass('too')).toBeFalsy(); | ||
})); | ||
it('should reapply ngClass when interpolated class attribute changes', | ||
inject(function($compile, $rootScope) { | ||
element = $compile( | ||
'<div>' + | ||
'<div class="one {{two}} three" ng-class="{five: five}"></div>' + | ||
'<div class="one {{two}} three {{four}}" ng-class="{five: five}"></div>' + | ||
'</div>')($rootScope); | ||
var e1 = element.children().eq(0); | ||
var e2 = element.children().eq(1); | ||
|
||
$rootScope.$apply('two = "two"; five = true'); | ||
|
||
expect(e1).toHaveClass('one'); | ||
expect(e1).toHaveClass('two'); | ||
expect(e1).toHaveClass('three'); | ||
expect(e1).not.toHaveClass('four'); | ||
expect(e1).toHaveClass('five'); | ||
expect(e2).toHaveClass('one'); | ||
expect(e2).toHaveClass('two'); | ||
expect(e2).toHaveClass('three'); | ||
expect(e2).not.toHaveClass('four'); | ||
expect(e2).toHaveClass('five'); | ||
|
||
$rootScope.$apply('two = "too"'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. although this is very clever "two" and "too" are very similar and actually make the test harder to follow. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The cleverness credits go to the original test 😃 |
||
|
||
expect(e1).toHaveClass('one'); | ||
expect(e1).not.toHaveClass('two'); | ||
expect(e1).toHaveClass('too'); | ||
expect(e1).toHaveClass('three'); | ||
expect(e1).not.toHaveClass('four'); | ||
expect(e1).toHaveClass('five'); | ||
expect(e2).toHaveClass('one'); | ||
expect(e2).not.toHaveClass('two'); | ||
expect(e2).toHaveClass('too'); | ||
expect(e2).toHaveClass('three'); | ||
expect(e2).not.toHaveClass('four'); | ||
expect(e2).toHaveClass('five'); | ||
|
||
$rootScope.$apply('two = "to"; four = "four"'); | ||
|
||
expect(e1).toHaveClass('one'); | ||
expect(e1).not.toHaveClass('two'); | ||
expect(e1).not.toHaveClass('too'); | ||
expect(e1).toHaveClass('to'); | ||
expect(e1).toHaveClass('three'); | ||
expect(e1).not.toHaveClass('four'); | ||
expect(e1).toHaveClass('five'); | ||
expect(e2).toHaveClass('one'); | ||
expect(e2).not.toHaveClass('two'); | ||
expect(e2).not.toHaveClass('too'); | ||
expect(e2).toHaveClass('to'); | ||
expect(e2).toHaveClass('three'); | ||
expect(e2).toHaveClass('four'); | ||
expect(e2).toHaveClass('five'); | ||
|
||
$rootScope.$apply('five = false'); | ||
|
||
expect(e1).toHaveClass('one'); | ||
expect(e1).not.toHaveClass('two'); | ||
expect(e1).not.toHaveClass('too'); | ||
expect(e1).toHaveClass('to'); | ||
expect(e1).toHaveClass('three'); | ||
expect(e1).not.toHaveClass('four'); | ||
expect(e1).not.toHaveClass('five'); | ||
expect(e2).toHaveClass('one'); | ||
expect(e2).not.toHaveClass('two'); | ||
expect(e2).not.toHaveClass('too'); | ||
expect(e2).toHaveClass('to'); | ||
expect(e2).toHaveClass('three'); | ||
expect(e2).toHaveClass('four'); | ||
expect(e2).not.toHaveClass('five'); | ||
}) | ||
); | ||
|
||
|
||
it('should not mess up class value due to observing an interpolated class attribute', inject(function($rootScope, $compile) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I never knew about these helpers!!