From 33bff17f33e27387bb62d3019fa2c7d3c455f5a2 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Fri, 13 Mar 2015 17:52:27 -0700 Subject: [PATCH] refactor(tests): refactor tests to clarify the behavior of onChange --- modules/angular2/change_detection.js | 2 +- modules/angular2/src/core/compiler/view.js | 8 ++++++-- modules/angular2/test/core/compiler/view_spec.js | 16 ++++++++++------ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/modules/angular2/change_detection.js b/modules/angular2/change_detection.js index 3da163a679..b330cd5f5d 100644 --- a/modules/angular2/change_detection.js +++ b/modules/angular2/change_detection.js @@ -12,9 +12,9 @@ export {ProtoChangeDetector, DynamicProtoChangeDetector, JitProtoChangeDetector, export {DynamicChangeDetector} from './src/change_detection/dynamic_change_detector'; export * from './src/change_detection/pipes/pipe_registry'; +export {uninitialized} from './src/change_detection/change_detection_util'; export * from './src/change_detection/pipes/pipe'; - import {ProtoChangeDetector, DynamicProtoChangeDetector, JitProtoChangeDetector} from './src/change_detection/proto_change_detector'; import {PipeRegistry} from './src/change_detection/pipes/pipe_registry'; diff --git a/modules/angular2/src/core/compiler/view.js b/modules/angular2/src/core/compiler/view.js index b58ce9d84c..1092f7fe2e 100644 --- a/modules/angular2/src/core/compiler/view.js +++ b/modules/angular2/src/core/compiler/view.js @@ -2,7 +2,7 @@ import {DOM} from 'angular2/src/dom/dom_adapter'; import {Promise} from 'angular2/src/facade/async'; import {ListWrapper, MapWrapper, Map, StringMapWrapper, List} from 'angular2/src/facade/collection'; import {AST, ContextWithVariableBindings, ChangeDispatcher, ProtoChangeDetector, ChangeDetector, - ChangeRecord, BindingRecord} from 'angular2/change_detection'; + ChangeRecord, BindingRecord, uninitialized} from 'angular2/change_detection'; import {ProtoElementInjector, ElementInjector, PreBuiltObjects} from './element_injector'; import {BindingPropagationConfig} from './binding_propagation_config'; @@ -682,7 +682,7 @@ class DirectiveMemento { } } -class PropertyUpdate { +export class PropertyUpdate { currentValue; previousValue; @@ -690,4 +690,8 @@ class PropertyUpdate { this.currentValue = currentValue; this.previousValue = previousValue; } + + static createWithoutPrevious(currentValue) { + return new PropertyUpdate(currentValue, uninitialized); + } } diff --git a/modules/angular2/test/core/compiler/view_spec.js b/modules/angular2/test/core/compiler/view_spec.js index 3f5a4c001c..d84266ccbe 100644 --- a/modules/angular2/test/core/compiler/view_spec.js +++ b/modules/angular2/test/core/compiler/view_spec.js @@ -11,7 +11,7 @@ import {List, MapWrapper} from 'angular2/src/facade/collection'; import {DOM} from 'angular2/src/dom/dom_adapter'; import {int, IMPLEMENTS} from 'angular2/src/facade/lang'; import {Injector} from 'angular2/di'; -import {View} from 'angular2/src/core/compiler/view'; +import {View, PropertyUpdate} from 'angular2/src/core/compiler/view'; import {ViewContainer} from 'angular2/src/core/compiler/view_container'; import {VmTurnZone} from 'angular2/src/core/zone/vm_turn_zone'; import {EventManager, DomEventsPlugin} from 'angular2/src/core/events/event_manager'; @@ -601,7 +601,7 @@ export function main() { expect(directive.c).toEqual(300); }); - it('should provide a map of updated properties', () => { + it('should provide a map of updated properties using onChange callback', () => { var pv = new ProtoView(el('
'), new DynamicProtoChangeDetector(null), null); @@ -612,16 +612,20 @@ export function main() { pv.bindDirectiveProperty( 0, parser.parseBinding('b', null), 'b', reflector.setter('b')); createViewAndChangeDetector(pv); + var directive = view.elementInjectors[0].get(DirectiveImplementingOnChange); + ctx.a = 0; ctx.b = 0; cd.detectChanges(); + expect(directive.changes).toEqual({ + "a" : PropertyUpdate.createWithoutPrevious(0), + "b" : PropertyUpdate.createWithoutPrevious(0) + }); + ctx.a = 100; cd.detectChanges(); - - var directive = view.elementInjectors[0].get(DirectiveImplementingOnChange); - expect(directive.changes["a"].currentValue).toEqual(100); - expect(directive.changes["b"]).not.toBeDefined(); + expect(directive.changes).toEqual({"a" : new PropertyUpdate(100, 0)}); }); }); });