@@ -2,8 +2,12 @@ import {describe, xit, it, expect, beforeEach} from 'test_lib/test_lib';
22import { ProtoView , ElementPropertyMemento , DirectivePropertyMemento } from 'core/compiler/view' ;
33import { Record } from 'change_detection/record' ;
44import { ProtoElementInjector , ElementInjector } from 'core/compiler/element_injector' ;
5+ import { ProtoWatchGroup } from 'change_detection/watch_group' ;
6+ import { ChangeDetector } from 'change_detection/change_detector' ;
57import { DOM , Element } from 'facade/dom' ;
68import { FIELD } from 'facade/lang' ;
9+ import { ImplicitReceiver , FieldRead } from 'change_detection/parser/ast' ;
10+ import { ClosureMap } from 'change_detection/parser/closure_map' ;
711
812class Directive {
913 @FIELD ( 'prop' )
@@ -13,32 +17,36 @@ class Directive {
1317}
1418
1519export function main ( ) {
20+ var oneFieldAst = ( fieldName ) =>
21+ new FieldRead ( new ImplicitReceiver ( ) , fieldName ,
22+ ( new ClosureMap ( ) ) . getter ( fieldName ) ) ;
23+
1624 describe ( 'view' , function ( ) {
1725 var tempalteWithThreeTypesOfBindings =
1826 '<section class="ng-binding">' +
1927 'Hello {}!' +
2028 '<div directive class="ng-binding">' +
21- '<span class="ng-binding" [hidden ]="exp">don\'t show me</span>' +
29+ '<span class="ng-binding" [id ]="exp">don\'t show me</span>' +
2230 '</div>' +
2331 '</section>' ;
2432
33+ function templateElInj ( ) {
34+ var sectionPI = new ProtoElementInjector ( null , [ ] , [ 0 ] , false ) ;
35+ var divPI = new ProtoElementInjector ( sectionPI , [ Directive ] , [ ] , false ) ;
36+ var spanPI = new ProtoElementInjector ( divPI , [ ] , [ ] , true ) ;
37+ return [ sectionPI , divPI , spanPI ] ;
38+ }
39+
2540 describe ( 'ProtoView' , function ( ) {
2641 it ( 'should create view instance and locate basic parts' , function ( ) {
2742 var template = DOM . createTemplate ( tempalteWithThreeTypesOfBindings ) ;
2843
2944 var diBindings = [ ] ;
30-
31- var sectionPI = new ProtoElementInjector ( null , [ ] , [ 0 ] , false ) ;
32- var divPI = new ProtoElementInjector ( sectionPI , [ Directive ] , [ ] , false ) ;
33- var spanPI = new ProtoElementInjector ( divPI , [ ] , [ ] , true ) ;
34- var protoElementInjectors = [ sectionPI , divPI , spanPI ] ;
35-
36- var protoWatchGroup = null ;
3745 var hasSingleRoot = false ;
38- var pv = new ProtoView ( template , diBindings , protoElementInjectors ,
39- protoWatchGroup , hasSingleRoot ) ;
46+ var pv = new ProtoView ( template , diBindings , templateElInj ( ) ,
47+ new ProtoWatchGroup ( ) , hasSingleRoot ) ;
4048
41- var view = pv . instantiate ( ) ;
49+ var view = pv . instantiate ( null , null ) ;
4250
4351 var section = DOM . firstChild ( template . content ) ;
4452
@@ -63,8 +71,9 @@ export function main() {
6371 var sectionPI = new ProtoElementInjector ( null , [ Directive ] , [ ] , false ) ;
6472 var divPI = new ProtoElementInjector ( sectionPI , [ Directive ] , [ ] , false ) ;
6573
66- var pv = new ProtoView ( template , [ ] , [ sectionPI , divPI ] , null , false ) ;
67- var view = pv . instantiate ( ) ;
74+ var pv = new ProtoView ( template , [ ] , [ sectionPI , divPI ] ,
75+ new ProtoWatchGroup ( ) , false ) ;
76+ var view = pv . instantiate ( null , null ) ;
6877
6978 expect ( view . rootElementInjectors . length ) . toEqual ( 1 ) ;
7079 } ) ;
@@ -73,20 +82,9 @@ export function main() {
7382 var view ;
7483 beforeEach ( ( ) => {
7584 var template = DOM . createTemplate ( tempalteWithThreeTypesOfBindings ) ;
76-
77- var diBindings = [ ] ;
78-
79- var sectionPI = new ProtoElementInjector ( null , [ ] , [ 0 ] , false ) ;
80- var divPI = new ProtoElementInjector ( sectionPI , [ Directive ] , [ ] , false ) ;
81- var spanPI = new ProtoElementInjector ( divPI , [ ] , [ ] , true ) ;
82- var protoElementInjectors = [ sectionPI , divPI , spanPI ] ;
83-
84- var protoWatchGroup = null ;
85- var hasSingleRoot = false ;
86- var pv = new ProtoView ( template , diBindings , protoElementInjectors ,
87- protoWatchGroup , hasSingleRoot ) ;
88-
89- view = pv . instantiate ( ) ;
85+ var pv = new ProtoView ( template , [ ] , templateElInj ( ) ,
86+ new ProtoWatchGroup ( ) , false ) ;
87+ view = pv . instantiate ( null , null ) ;
9088 } ) ;
9189
9290 it ( 'should consume text node changes' , ( ) => {
@@ -98,20 +96,17 @@ export function main() {
9896
9997 it ( 'should consume element binding changes' , ( ) => {
10098 var elementWithBinding = view . bindElements [ 0 ] ;
101- expect ( elementWithBinding . hidden ) . toEqual ( false ) ;
99+ expect ( elementWithBinding . id ) . toEqual ( '' ) ;
102100 var record = new Record ( null , null ) ;
103- var memento = new ElementPropertyMemento ( 0 , 'hidden ' ) ;
104- record . currentValue = true ;
101+ var memento = new ElementPropertyMemento ( 0 , 'id ' ) ;
102+ record . currentValue = 'foo' ;
105103 view . onRecordChange ( record , memento ) ;
106- expect ( elementWithBinding . hidden ) . toEqual ( true ) ;
104+ expect ( elementWithBinding . id ) . toEqual ( 'foo' ) ;
107105 } ) ;
108106
109107 it ( 'should consume directive watch expression change.' , ( ) => {
110108 var elInj = view . elementInjectors [ 1 ] ;
111109
112- // TODO(rado): hook-up instantiateDirectives in implementation and
113- // remove from here.
114- elInj . instantiateDirectives ( null ) ;
115110 expect ( elInj . get ( Directive ) . prop ) . toEqual ( 'foo' ) ;
116111 var record = new Record ( null , null ) ;
117112 var memento = new DirectivePropertyMemento ( 1 , 0 , 'prop' ,
@@ -121,6 +116,64 @@ export function main() {
121116 expect ( elInj . get ( Directive ) . prop ) . toEqual ( 'bar' ) ;
122117 } ) ;
123118 } ) ;
119+
120+ describe ( 'integration view update with change detector' , ( ) => {
121+ var view , cd , ctx ;
122+ function setUp ( memento ) {
123+ var template = DOM . createTemplate ( tempalteWithThreeTypesOfBindings ) ;
124+
125+ var protoWatchGroup = new ProtoWatchGroup ( ) ;
126+ protoWatchGroup . watch ( oneFieldAst ( 'foo' ) , memento ) ;
127+
128+ var pv = new ProtoView ( template , [ ] , templateElInj ( ) ,
129+ protoWatchGroup , false ) ;
130+
131+ ctx = new MyEvaluationContext ( ) ;
132+ view = pv . instantiate ( ctx , null ) ;
133+
134+ cd = new ChangeDetector ( view . watchGroup ) ;
135+ }
136+
137+ it ( 'should consume text node changes' , ( ) => {
138+ setUp ( 0 ) ;
139+
140+ ctx . foo = 'buz' ;
141+ cd . detectChanges ( ) ;
142+ expect ( view . textNodes [ 0 ] . nodeValue ) . toEqual ( 'buz' ) ;
143+ } ) ;
144+
145+ it ( 'should consume element binding changes' , ( ) => {
146+ setUp ( new ElementPropertyMemento ( 0 , 'id' ) ) ;
147+
148+ var elementWithBinding = view . bindElements [ 0 ] ;
149+ expect ( elementWithBinding . id ) . toEqual ( '' ) ;
150+
151+ ctx . foo = 'buz' ;
152+ cd . detectChanges ( ) ;
153+ expect ( elementWithBinding . id ) . toEqual ( 'buz' ) ;
154+ } ) ;
155+
156+ it ( 'should consume directive watch expression change.' , ( ) => {
157+ var memento = new DirectivePropertyMemento ( 1 , 0 , 'prop' ,
158+ ( o , v ) => o . prop = v ) ;
159+ setUp ( memento ) ;
160+
161+ var elInj = view . elementInjectors [ 1 ] ;
162+ expect ( elInj . get ( Directive ) . prop ) . toEqual ( 'foo' ) ;
163+
164+ ctx . foo = 'buz' ;
165+ cd . detectChanges ( ) ;
166+ expect ( elInj . get ( Directive ) . prop ) . toEqual ( 'buz' ) ;
167+ } ) ;
168+
169+ } ) ;
124170 } ) ;
125171 } ) ;
126172}
173+
174+ class MyEvaluationContext {
175+ @FIELD ( 'foo' )
176+ constructor ( ) {
177+ this . foo = 'bar' ;
178+ } ;
179+ }
0 commit comments