@@ -491,6 +491,42 @@ export function main() {
491491
492492 expect ( renderLog . log ) . toEqual ( [ 'someProp=Megatron' ] ) ;
493493 } ) ) ;
494+
495+ it ( 'should call pure pipes only if the arguments change' , fakeAsync ( ( ) => {
496+ var ctx = _bindSimpleValue ( 'name | countingPipe' , Person ) ;
497+ // change from undefined -> null
498+ ctx . componentInstance . name = null ;
499+ ctx . detectChanges ( false ) ;
500+ expect ( renderLog . loggedValues ) . toEqual ( [ 'null state:0' ] ) ;
501+ ctx . detectChanges ( false ) ;
502+ expect ( renderLog . loggedValues ) . toEqual ( [ 'null state:0' ] ) ;
503+
504+ // change from null -> some value
505+ ctx . componentInstance . name = 'bob' ;
506+ ctx . detectChanges ( false ) ;
507+ expect ( renderLog . loggedValues ) . toEqual ( [ 'null state:0' , 'bob state:1' ] ) ;
508+ ctx . detectChanges ( false ) ;
509+ expect ( renderLog . loggedValues ) . toEqual ( [ 'null state:0' , 'bob state:1' ] ) ;
510+
511+ // change from some value -> some other value
512+ ctx . componentInstance . name = 'bart' ;
513+ ctx . detectChanges ( false ) ;
514+ expect ( renderLog . loggedValues )
515+ . toEqual ( [ 'null state:0' , 'bob state:1' , 'bart state:2' ] ) ;
516+ ctx . detectChanges ( false ) ;
517+ expect ( renderLog . loggedValues )
518+ . toEqual ( [ 'null state:0' , 'bob state:1' , 'bart state:2' ] ) ;
519+
520+ } ) ) ;
521+
522+ it ( 'should call impure pipes on each change detection run' , fakeAsync ( ( ) => {
523+ var ctx = _bindSimpleValue ( 'name | countingImpurePipe' , Person ) ;
524+ ctx . componentInstance . name = 'bob' ;
525+ ctx . detectChanges ( false ) ;
526+ expect ( renderLog . loggedValues ) . toEqual ( [ 'bob state:0' ] ) ;
527+ ctx . detectChanges ( false ) ;
528+ expect ( renderLog . loggedValues ) . toEqual ( [ 'bob state:0' , 'bob state:1' ] ) ;
529+ } ) ) ;
494530 } ) ;
495531
496532 describe ( 'event expressions' , ( ) => {
@@ -1014,6 +1050,7 @@ const ALL_DIRECTIVES = CONST_EXPR([
10141050
10151051const ALL_PIPES = CONST_EXPR ( [
10161052 forwardRef ( ( ) => CountingPipe ) ,
1053+ forwardRef ( ( ) => CountingImpurePipe ) ,
10171054 forwardRef ( ( ) => MultiArgPipe ) ,
10181055 forwardRef ( ( ) => PipeWithOnDestroy ) ,
10191056 forwardRef ( ( ) => IdentityPipe ) ,
@@ -1089,6 +1126,12 @@ class CountingPipe implements PipeTransform {
10891126 transform ( value , args = null ) { return `${ value } state:${ this . state ++ } ` ; }
10901127}
10911128
1129+ @Pipe ( { name : 'countingImpurePipe' , pure : false } )
1130+ class CountingImpurePipe implements PipeTransform {
1131+ state : number = 0 ;
1132+ transform ( value , args = null ) { return `${ value } state:${ this . state ++ } ` ; }
1133+ }
1134+
10921135@Pipe ( { name : 'pipeWithOnDestroy' } )
10931136class PipeWithOnDestroy implements PipeTransform , OnDestroy {
10941137 constructor ( private directiveLog : DirectiveLog ) { }
0 commit comments