From 91fd5a69bf43ab1a9188305f41136cea7b70eb9f Mon Sep 17 00:00:00 2001 From: vsavkin Date: Wed, 11 Feb 2015 16:28:10 -0800 Subject: [PATCH] test(change_detection): add an integration test verifying that binding propagation config works --- modules/angular2/core.js | 3 +- .../abstract_change_detector.js | 8 +++ .../change_detection/change_detection_util.js | 8 --- .../change_detection/change_detection_spec.js | 2 +- .../test/core/compiler/integration_spec.js | 49 ++++++++++++++++++- 5 files changed, 59 insertions(+), 11 deletions(-) diff --git a/modules/angular2/core.js b/modules/angular2/core.js index 5dbc4d61fb..f761b87ea1 100644 --- a/modules/angular2/core.js +++ b/modules/angular2/core.js @@ -2,12 +2,13 @@ export * from './src/core/annotations/annotations'; export * from './src/core/annotations/visibility'; export * from './src/core/compiler/interfaces'; export * from './src/core/annotations/template_config'; - export * from './src/core/application'; export * from './src/core/compiler/compiler'; + export * from './src/core/compiler/template_loader'; export * from './src/core/compiler/view'; export * from './src/core/compiler/viewport'; +export * from './src/core/compiler/binding_propagation_config'; export * from './src/core/dom/element'; diff --git a/modules/angular2/src/change_detection/abstract_change_detector.js b/modules/angular2/src/change_detection/abstract_change_detector.js index f97b3dbb8e..a5274e1680 100644 --- a/modules/angular2/src/change_detection/abstract_change_detector.js +++ b/modules/angular2/src/change_detection/abstract_change_detector.js @@ -51,4 +51,12 @@ export class AbstractChangeDetector extends ChangeDetector { children[i]._detectChanges(throwOnChange); } } + + markPathToRootAsCheckOnce() { + var c = this; + while(isPresent(c) && c.mode != DETACHED) { + if (c.mode === CHECKED) c.mode = CHECK_ONCE; + c = c.parent; + } + } } diff --git a/modules/angular2/src/change_detection/change_detection_util.js b/modules/angular2/src/change_detection/change_detection_util.js index 7cc880c78c..bbe2edaacb 100644 --- a/modules/angular2/src/change_detection/change_detection_util.js +++ b/modules/angular2/src/change_detection/change_detection_util.js @@ -214,12 +214,4 @@ export class ChangeDetectionUtil { } return updatedRecords; } - - static markPathToRootAsCheckOnce(cd:ChangeDetector) { - var c = cd; - while(isPresent(c) && c.mode != DETACHED) { - if (c.mode === CHECKED) c.mode = CHECK_ONCE; - c = c.parent; - } - } } \ No newline at end of file diff --git a/modules/angular2/test/change_detection/change_detection_spec.js b/modules/angular2/test/change_detection/change_detection_spec.js index 22214ca0bf..7b9f39da33 100644 --- a/modules/angular2/test/change_detection/change_detection_spec.js +++ b/modules/angular2/test/change_detection/change_detection_spec.js @@ -535,7 +535,7 @@ export function main() { var checkOnceChild = changeDetector(CHECK_ONCE, checkAlwaysChild); var checkedChild = changeDetector(CHECKED, checkOnceChild); - ChangeDetectionUtil.markPathToRootAsCheckOnce(checkedChild); + checkedChild.markPathToRootAsCheckOnce(); expect(root.mode).toEqual(CHECK_ALWAYS); expect(disabled.mode).toEqual(DETACHED); diff --git a/modules/angular2/test/core/compiler/integration_spec.js b/modules/angular2/test/core/compiler/integration_spec.js index 9e687e8661..451b9ae98e 100644 --- a/modules/angular2/test/core/compiler/integration_spec.js +++ b/modules/angular2/test/core/compiler/integration_spec.js @@ -9,6 +9,7 @@ import {Compiler, CompilerCache} from 'angular2/src/core/compiler/compiler'; import {DirectiveMetadataReader} from 'angular2/src/core/compiler/directive_metadata_reader'; import {NativeShadowDomStrategy} from 'angular2/src/core/compiler/shadow_dom_strategy'; import {TemplateLoader} from 'angular2/src/core/compiler/template_loader'; +import {BindingPropagationConfig} from 'angular2/src/core/compiler/binding_propagation_config'; import {Decorator, Component, Template} from 'angular2/src/core/annotations/annotations'; import {TemplateConfig} from 'angular2/src/core/annotations/template_config'; @@ -198,6 +199,26 @@ export function main() { done(); }) }); + + it('should provide binding configuration config to the component', (done) => { + compiler.compile(MyComp, el('')).then((pv) => { + createView(pv); + + var cmp = view.contextWithLocals.get('cmp'); + + cd.detectChanges(); + expect(cmp.numberOfChecks).toEqual(1); + + cd.detectChanges(); + expect(cmp.numberOfChecks).toEqual(1); + + cmp.propagate(); + + cd.detectChanges(); + expect(cmp.numberOfChecks).toEqual(2); + done(); + }) + }); }); }); } @@ -214,8 +235,34 @@ class MyDir { } @Component({ + selector: 'push-cmp', template: new TemplateConfig({ - directives: [MyDir, ChildComp, SomeTemplate] + inline: '{{field}}' + }) +}) +class PushBasedComp { + numberOfChecks:number; + bpc:BindingPropagationConfig; + + constructor(bpc:BindingPropagationConfig) { + this.numberOfChecks = 0; + this.bpc = bpc; + bpc.shouldBePropagated(); + } + + get field(){ + this.numberOfChecks++; + return "fixed"; + } + + propagate() { + this.bpc.shouldBePropagatedFromRoot(); + } +} + +@Component({ + template: new TemplateConfig({ + directives: [MyDir, ChildComp, SomeTemplate, PushBasedComp] }) }) class MyComp {