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
fix($compile): don't run unnecessary update to one-way bindings #14580
Merged
Merged
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -3823,6 +3823,7 @@ describe('$compile', function() { | |
|
||
$rootScope.$apply('a = 7'); | ||
element = $compile('<c1 prop="a" attr="{{a}}"></c1>')($rootScope); | ||
|
||
expect(log).toEqual([ | ||
{ | ||
prop: jasmine.objectContaining({currentValue: 7}), | ||
|
@@ -3862,6 +3863,7 @@ describe('$compile', function() { | |
inject(function($compile, $rootScope) { | ||
$rootScope.$apply('a = 7'); | ||
element = $compile('<c1 prop="a" attr="{{a}}"></c1>')($rootScope); | ||
|
||
expect(log).toEqual([ | ||
{ | ||
prop: jasmine.objectContaining({currentValue: 7}), | ||
|
@@ -4744,6 +4746,7 @@ describe('$compile', function() { | |
describe('one-way binding', function() { | ||
it('should update isolate when the identity of origin changes', inject(function() { | ||
compile('<div><span my-component ow-ref="obj">'); | ||
|
||
expect(componentScope.owRef).toBeUndefined(); | ||
expect(componentScope.owRefAlias).toBe(componentScope.owRef); | ||
|
||
|
@@ -4780,6 +4783,7 @@ describe('$compile', function() { | |
|
||
it('should update isolate when both change', inject(function() { | ||
compile('<div><span my-component ow-ref="name">'); | ||
|
||
$rootScope.name = {mark:123}; | ||
componentScope.owRef = 'misko'; | ||
|
||
|
@@ -4796,6 +4800,101 @@ describe('$compile', function() { | |
expect(componentScope.owRefAlias).toBe($rootScope.name); | ||
})); | ||
|
||
describe('initialization', function() { | ||
var component, log; | ||
|
||
beforeEach(function() { | ||
log = []; | ||
angular.module('owComponentTest', []) | ||
.component('owComponent', { | ||
bindings: { input: '<' }, | ||
controller: function() { | ||
component = this; | ||
this.input = 'constructor'; | ||
log.push('constructor'); | ||
|
||
this.$onInit = function() { | ||
this.input = '$onInit'; | ||
log.push('$onInit'); | ||
}; | ||
|
||
this.$onChanges = function(changes) { | ||
if (changes.input) { | ||
log.push(['$onChanges', changes.input]); | ||
} | ||
}; | ||
} | ||
}); | ||
}); | ||
|
||
it('should not update isolate again after $onInit if outer has not changed', function() { | ||
module('owComponentTest'); | ||
inject(function() { | ||
$rootScope.name = 'outer'; | ||
compile('<ow-component input="name"></ow-component>'); | ||
|
||
expect($rootScope.name).toEqual('outer'); | ||
expect(component.input).toEqual('$onInit'); | ||
|
||
$rootScope.$apply(); | ||
|
||
expect($rootScope.name).toEqual('outer'); | ||
expect(component.input).toEqual('$onInit'); | ||
|
||
expect(log).toEqual([ | ||
'constructor', | ||
['$onChanges', jasmine.objectContaining({ currentValue: 'outer' })], | ||
'$onInit' | ||
]); | ||
}); | ||
}); | ||
|
||
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. Inconsistent number of newlines between tests 😁 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. Shock horror! Isn't there a JSCS rule for that?? |
||
it('should update isolate again after $onInit if outer has changed (before initial watchAction call)', function() { | ||
module('owComponentTest'); | ||
inject(function() { | ||
$rootScope.name = 'outer1'; | ||
compile('<ow-component input="name"></ow-component>'); | ||
|
||
expect(component.input).toEqual('$onInit'); | ||
$rootScope.$apply('name = "outer2"'); | ||
|
||
expect($rootScope.name).toEqual('outer2'); | ||
expect(component.input).toEqual('outer2'); | ||
expect(log).toEqual([ | ||
'constructor', | ||
['$onChanges', jasmine.objectContaining({ currentValue: 'outer1' })], | ||
'$onInit', | ||
['$onChanges', jasmine.objectContaining({ currentValue: 'outer2', previousValue: 'outer1' })] | ||
]); | ||
}); | ||
}); | ||
|
||
it('should update isolate again after $onInit if outer has changed (before initial watchAction call)', function() { | ||
angular.module('owComponentTest') | ||
.directive('changeInput', function() { | ||
return function(scope, elem, attrs) { | ||
scope.name = 'outer2'; | ||
}; | ||
}); | ||
module('owComponentTest'); | ||
inject(function() { | ||
$rootScope.name = 'outer1'; | ||
compile('<ow-component input="name" change-input></ow-component>'); | ||
|
||
expect(component.input).toEqual('$onInit'); | ||
$rootScope.$digest(); | ||
|
||
expect($rootScope.name).toEqual('outer2'); | ||
expect(component.input).toEqual('outer2'); | ||
expect(log).toEqual([ | ||
'constructor', | ||
['$onChanges', jasmine.objectContaining({ currentValue: 'outer1' })], | ||
'$onInit', | ||
['$onChanges', jasmine.objectContaining({ currentValue: 'outer2', previousValue: 'outer1' })] | ||
]); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should not break when isolate and origin both change to the same value', inject(function() { | ||
$rootScope.name = 'aaa'; | ||
|
@@ -4819,7 +4918,6 @@ describe('$compile', function() { | |
$rootScope.name = {mark:123}; | ||
compile('<div><span my-component ow-ref="name">'); | ||
|
||
$rootScope.$apply(); | ||
expect($rootScope.name).toEqual({mark:123}); | ||
expect(componentScope.owRef).toBe($rootScope.name); | ||
expect(componentScope.owRefAlias).toBe($rootScope.name); | ||
|
@@ -4836,7 +4934,6 @@ describe('$compile', function() { | |
$rootScope.obj = {mark:123}; | ||
compile('<div><span my-component ow-ref="obj">'); | ||
|
||
$rootScope.$apply(); | ||
expect($rootScope.obj).toEqual({mark:123}); | ||
expect(componentScope.owRef).toBe($rootScope.obj); | ||
|
||
|
@@ -4849,6 +4946,7 @@ describe('$compile', function() { | |
|
||
it('should not throw on non assignable expressions in the parent', inject(function() { | ||
compile('<div><span my-component ow-ref="\'hello \' + name">'); | ||
|
||
$rootScope.name = 'world'; | ||
$rootScope.$apply(); | ||
expect(componentScope.owRef).toBe('hello world'); | ||
|
@@ -4865,7 +4963,7 @@ describe('$compile', function() { | |
|
||
it('should not throw when assigning to undefined', inject(function() { | ||
compile('<div><span my-component>'); | ||
$rootScope.$apply(); | ||
|
||
expect(componentScope.owRef).toBeUndefined(); | ||
|
||
componentScope.owRef = 'ignore me'; | ||
|
@@ -4879,6 +4977,7 @@ describe('$compile', function() { | |
it('should update isolate scope when "<"-bound NaN changes', inject(function() { | ||
$rootScope.num = NaN; | ||
compile('<div my-component ow-ref="num"></div>'); | ||
|
||
var isolateScope = element.isolateScope(); | ||
expect(isolateScope.owRef).toBeNaN(); | ||
|
||
|
@@ -4917,7 +5016,7 @@ describe('$compile', function() { | |
$rootScope.name = 'georgios'; | ||
$rootScope.obj = {name: 'pete'}; | ||
compile('<div><span my-component ow-ref="[{name: name}, obj]">'); | ||
$rootScope.$apply(); | ||
|
||
expect(componentScope.owRef).toEqual([{name: 'georgios'}, {name: 'pete'}]); | ||
|
||
$rootScope.name = 'lucas'; | ||
|
@@ -4931,7 +5030,7 @@ describe('$compile', function() { | |
$rootScope.name = 'georgios'; | ||
$rootScope.obj = {name: 'pete'}; | ||
compile('<div><span my-component ow-ref="{name: name, item: obj}">'); | ||
$rootScope.$apply(); | ||
|
||
expect(componentScope.owRef).toEqual({name: 'georgios', item: {name: 'pete'}}); | ||
|
||
$rootScope.name = 'lucas'; | ||
|
@@ -4967,7 +5066,6 @@ describe('$compile', function() { | |
function test(literalString, literalValue) { | ||
compile('<div><span my-component ow-ref="' + literalString + '">'); | ||
|
||
$rootScope.$apply(); | ||
expect(componentScope.owRef).toBe(literalValue); | ||
dealoc(element); | ||
} | ||
|
@@ -4976,6 +5074,7 @@ describe('$compile', function() { | |
describe('optional one-way binding', function() { | ||
it('should update local when origin changes', inject(function() { | ||
compile('<div><span my-component ow-optref="name">'); | ||
|
||
expect(componentScope.owOptref).toBeUndefined(); | ||
expect(componentScope.owOptrefAlias).toBe(componentScope.owOptref); | ||
|
||
|
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.
Wouldn't this test pass even without the fix ? (I haven't tested it, but I think so.)
I think it's a good idea to add a
$rootScope.$digest()
and another round of expectation to make sure that nothing is overwritten when the watch callback is executed.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.
OK. It did fail before the fix. But then I might have changed it again since then.
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.
It totally fails as it is without the fix. But I will add the extra digest and check