forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(common): ensure diffing in ngStyle/ngClass correctly emits value …
…changes (angular#34307) Prior to this change, in Ivy mode ngStyle/ngClass would accidentally emit value changes for static (string-based) values even if the value itself had not changed. This patch ensures that the style/class diffing code is more strict and when it signals ngClass/ngStyle that there has been a value change. Fixes angular#34336, angular#34444 PR Close angular#34307
- Loading branch information
Showing
4 changed files
with
222 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
import {StylingDiffer, StylingDifferOptions} from '@angular/common/src/directives/styling_differ'; | ||
|
||
describe('StylingDiffer', () => { | ||
it('should create a key/value object of values from a string', () => { | ||
const d = new StylingDiffer( | ||
'ngClass', StylingDifferOptions.ForceAsMap | StylingDifferOptions.AllowStringValue); | ||
expect(d.value).toEqual(null); | ||
|
||
d.setValue('one two'); | ||
expect(d.value).toEqual({one: true, two: true}); | ||
|
||
d.setValue('three'); | ||
expect(d.value).toEqual({three: true}); | ||
}); | ||
|
||
it('should not emit that a value has changed if a new non-collection value was not set', () => { | ||
const d = new StylingDiffer( | ||
'ngClass', StylingDifferOptions.ForceAsMap | StylingDifferOptions.AllowStringValue); | ||
expect(d.value).toEqual(null); | ||
|
||
d.setValue('one two'); | ||
expect(d.hasValueChanged()).toBeTruthy(); | ||
expect(d.value).toEqual({one: true, two: true}); | ||
expect(d.hasValueChanged()).toBeFalsy(); | ||
expect(d.value).toEqual({one: true, two: true}); | ||
|
||
d.setValue('three'); | ||
expect(d.hasValueChanged()).toBeTruthy(); | ||
expect(d.value).toEqual({three: true}); | ||
expect(d.hasValueChanged()).toBeFalsy(); | ||
expect(d.value).toEqual({three: true}); | ||
|
||
d.setValue(null); | ||
expect(d.hasValueChanged()).toBeTruthy(); | ||
expect(d.value).toEqual(null); | ||
expect(d.hasValueChanged()).toBeFalsy(); | ||
expect(d.value).toEqual(null); | ||
}); | ||
|
||
it('should watch the contents of a StringMap value and emit new values if they change', () => { | ||
const d = new StylingDiffer('ngClass', StylingDifferOptions.ForceAsMap); | ||
|
||
const myMap: {[key: string]: any} = {}; | ||
myMap['abc'] = true; | ||
|
||
d.setValue(myMap); | ||
expect(d.hasValueChanged()).toBeTruthy(); | ||
expect(d.value).toEqual({abc: true}); | ||
expect(d.hasValueChanged()).toBeFalsy(); | ||
|
||
myMap['def'] = true; | ||
expect(d.hasValueChanged()).toBeTruthy(); | ||
expect(d.value).toEqual({abc: true, def: true}); | ||
expect(d.hasValueChanged()).toBeFalsy(); | ||
|
||
delete myMap['abc']; | ||
delete myMap['def']; | ||
expect(d.hasValueChanged()).toBeTruthy(); | ||
expect(d.value).toEqual({}); | ||
expect(d.hasValueChanged()).toBeFalsy(); | ||
}); | ||
|
||
it('should watch the contents of an Array value and emit new values if they change', () => { | ||
const d = new StylingDiffer('ngClass', StylingDifferOptions.ForceAsMap); | ||
|
||
const myArray: string[] = []; | ||
myArray.push('abc'); | ||
|
||
d.setValue(myArray); | ||
expect(d.hasValueChanged()).toBeTruthy(); | ||
expect(d.value).toEqual({abc: true}); | ||
expect(d.hasValueChanged()).toBeFalsy(); | ||
|
||
myArray.push('def'); | ||
expect(d.hasValueChanged()).toBeTruthy(); | ||
expect(d.value).toEqual({abc: true, def: true}); | ||
expect(d.hasValueChanged()).toBeFalsy(); | ||
|
||
myArray.length = 0; | ||
expect(d.hasValueChanged()).toBeTruthy(); | ||
expect(d.value).toEqual({}); | ||
expect(d.hasValueChanged()).toBeFalsy(); | ||
}); | ||
|
||
it('should watch the contents of a Set value and emit new values if they change', () => { | ||
const d = new StylingDiffer('ngClass', StylingDifferOptions.ForceAsMap); | ||
|
||
const mySet = new Set<string>(); | ||
mySet.add('abc'); | ||
|
||
d.setValue(mySet); | ||
expect(d.hasValueChanged()).toBeTruthy(); | ||
expect(d.value).toEqual({abc: true}); | ||
expect(d.hasValueChanged()).toBeFalsy(); | ||
|
||
mySet.add('def'); | ||
expect(d.hasValueChanged()).toBeTruthy(); | ||
expect(d.value).toEqual({abc: true, def: true}); | ||
expect(d.hasValueChanged()).toBeFalsy(); | ||
|
||
mySet.clear(); | ||
expect(d.hasValueChanged()).toBeTruthy(); | ||
expect(d.value).toEqual({}); | ||
expect(d.hasValueChanged()).toBeFalsy(); | ||
}); | ||
}); |