@@ -401,7 +401,95 @@ export function main() {
401401 } ) ;
402402 } ) ;
403403
404- // TODO(kegluneq): Insert describe('mode', ...) testcases.
404+ describe ( 'mode' , ( ) => {
405+ var _createWithoutHydrate = function ( expression : string ) {
406+ var registry = null ;
407+ return _getProtoChangeDetector ( getDefinition ( expression ) . cdDef , registry ) . instantiate ( new TestDispatcher ( ) ) ;
408+ } ;
409+
410+ it ( 'should set the mode to CHECK_ALWAYS when the default change detection is used' ,
411+ ( ) => {
412+ var cd = _createWithoutHydrate ( 'emptyUsingDefaultStrategy' ) ;
413+ expect ( cd . mode ) . toEqual ( null ) ;
414+
415+ cd . hydrate ( _DEFAULT_CONTEXT , null , null ) ;
416+ expect ( cd . mode ) . toEqual ( CHECK_ALWAYS ) ;
417+ } ) ;
418+
419+ it ( 'should set the mode to CHECK_ONCE when the push change detection is used' , ( ) => {
420+ var cd = _createWithoutHydrate ( 'emptyUsingOnPushStrategy' ) ;
421+ cd . hydrate ( _DEFAULT_CONTEXT , null , null ) ;
422+
423+ expect ( cd . mode ) . toEqual ( CHECK_ONCE ) ;
424+ } ) ;
425+
426+ it ( 'should not check a detached change detector' , ( ) => {
427+ var val = _createChangeDetector ( 'a' , new TestData ( 'value' ) ) ;
428+
429+ val . changeDetector . hydrate ( _DEFAULT_CONTEXT , null , null ) ;
430+ val . changeDetector . mode = DETACHED ;
431+ val . changeDetector . detectChanges ( ) ;
432+
433+ expect ( val . dispatcher . log ) . toEqual ( [ ] ) ;
434+ } ) ;
435+
436+ it ( 'should not check a checked change detector' , ( ) => {
437+ var val = _createChangeDetector ( 'a' , new TestData ( 'value' ) ) ;
438+
439+ val . changeDetector . hydrate ( _DEFAULT_CONTEXT , null , null ) ;
440+ val . changeDetector . mode = CHECKED ;
441+ val . changeDetector . detectChanges ( ) ;
442+
443+ expect ( val . dispatcher . log ) . toEqual ( [ ] ) ;
444+ } ) ;
445+
446+ it ( 'should change CHECK_ONCE to CHECKED' , ( ) => {
447+ var cd = _createChangeDetector ( '10' ) . changeDetector ;
448+ cd . hydrate ( _DEFAULT_CONTEXT , null , null ) ;
449+ cd . mode = CHECK_ONCE ;
450+
451+ cd . detectChanges ( ) ;
452+
453+ expect ( cd . mode ) . toEqual ( CHECKED ) ;
454+ } ) ;
455+
456+ it ( 'should not change the CHECK_ALWAYS' , ( ) => {
457+ var cd = _createChangeDetector ( '10' ) . changeDetector ;
458+ cd . hydrate ( _DEFAULT_CONTEXT , null , null ) ;
459+ cd . mode = CHECK_ALWAYS ;
460+
461+ cd . detectChanges ( ) ;
462+
463+ expect ( cd . mode ) . toEqual ( CHECK_ALWAYS ) ;
464+ } ) ;
465+
466+ describe ( 'marking ON_PUSH detectors as CHECK_ONCE after an update' , ( ) => {
467+ var checkedDetector ;
468+ var directives ;
469+
470+ beforeEach ( ( ) => {
471+ checkedDetector = _createWithoutHydrate ( 'emptyUsingOnPushStrategy' ) ;
472+ checkedDetector . hydrate ( _DEFAULT_CONTEXT , null , null ) ;
473+ checkedDetector . mode = CHECKED ;
474+
475+ var targetDirective = new TestData ( null ) ;
476+ directives = new FakeDirectives ( [ targetDirective ] , [ checkedDetector ] ) ;
477+ } ) ;
478+
479+ it ( 'should set the mode to CHECK_ONCE when a binding is updated' , ( ) => {
480+ var cd = _createWithoutHydrate ( 'onPushRecordsUsingDefaultStrategy' ) ;
481+ cd . hydrate ( _DEFAULT_CONTEXT , null , directives ) ;
482+
483+ expect ( checkedDetector . mode ) . toEqual ( CHECKED ) ;
484+
485+ // evaluate the record, update the targetDirective, and mark its detector as
486+ // CHECK_ONCE
487+ cd . detectChanges ( ) ;
488+
489+ expect ( checkedDetector . mode ) . toEqual ( CHECK_ONCE ) ;
490+ } ) ;
491+ } ) ;
492+ } ) ;
405493
406494 describe ( 'markPathToRootAsCheckOnce' , ( ) => {
407495 function changeDetector ( mode , parent ) {
@@ -595,21 +683,6 @@ export function main() {
595683
596684 function dirs ( directives : List < any > ) { return new FakeDirectives ( directives , [ ] ) ; }
597685
598- function createChangeDetector ( propName : string , exp : string , context = _DEFAULT_CONTEXT ,
599- registry = null ) {
600- var dispatcher = new TestDispatcher ( ) ;
601-
602- var locals = null ;
603- var variableBindings = [ ] ;
604-
605- var records = [ BindingRecord . createForElement ( ast ( exp ) , 0 , propName ) ] ;
606- var pcd = createProtoChangeDetector ( records , variableBindings , [ ] , registry ) ;
607- var cd = pcd . instantiate ( dispatcher ) ;
608- cd . hydrate ( context , locals , null ) ;
609-
610- return { "changeDetector" : cd , "dispatcher" : dispatcher } ;
611- }
612-
613686 describe ( `${ name } change detection` , ( ) => {
614687 var dispatcher ;
615688
@@ -879,113 +952,6 @@ export function main() {
879952 } ) ;
880953 } ) ;
881954 } ) ;
882-
883- describe ( "mode" , ( ) => {
884- it ( "should set the mode to CHECK_ALWAYS when the default change detection is used" ,
885- ( ) => {
886- var proto = createProtoChangeDetector ( [ ] , [ ] , [ ] , null , DEFAULT ) ;
887- var cd = proto . instantiate ( null ) ;
888-
889- expect ( cd . mode ) . toEqual ( null ) ;
890-
891- cd . hydrate ( _DEFAULT_CONTEXT , null , null ) ;
892-
893- expect ( cd . mode ) . toEqual ( CHECK_ALWAYS ) ;
894- } ) ;
895-
896- it ( "should set the mode to CHECK_ONCE when the push change detection is used" , ( ) => {
897- var proto = createProtoChangeDetector ( [ ] , [ ] , [ ] , null , ON_PUSH ) ;
898- var cd = proto . instantiate ( null ) ;
899- cd . hydrate ( _DEFAULT_CONTEXT , null , null ) ;
900-
901- expect ( cd . mode ) . toEqual ( CHECK_ONCE ) ;
902- } ) ;
903-
904- it ( "should not check a detached change detector" , ( ) => {
905- var c = createChangeDetector ( 'name' , 'a' , new TestData ( "value" ) ) ;
906- var cd = c [ "changeDetector" ] ;
907- var dispatcher = c [ "dispatcher" ] ;
908-
909- cd . hydrate ( _DEFAULT_CONTEXT , null , null ) ;
910- cd . mode = DETACHED ;
911- cd . detectChanges ( ) ;
912-
913- expect ( dispatcher . log ) . toEqual ( [ ] ) ;
914- } ) ;
915-
916- it ( "should not check a checked change detector" , ( ) => {
917- var c = createChangeDetector ( 'name' , 'a' , new TestData ( "value" ) ) ;
918- var cd = c [ "changeDetector" ] ;
919- var dispatcher = c [ "dispatcher" ] ;
920-
921- cd . hydrate ( _DEFAULT_CONTEXT , null , null ) ;
922- cd . mode = CHECKED ;
923- cd . detectChanges ( ) ;
924-
925- expect ( dispatcher . log ) . toEqual ( [ ] ) ;
926- } ) ;
927-
928- it ( "should change CHECK_ONCE to CHECKED" , ( ) => {
929- var cd = createProtoChangeDetector ( [ ] ) . instantiate ( null ) ;
930- cd . hydrate ( _DEFAULT_CONTEXT , null , null ) ;
931- cd . mode = CHECK_ONCE ;
932-
933- cd . detectChanges ( ) ;
934-
935- expect ( cd . mode ) . toEqual ( CHECKED ) ;
936- } ) ;
937-
938- it ( "should not change the CHECK_ALWAYS" , ( ) => {
939- var cd = createProtoChangeDetector ( [ ] ) . instantiate ( null ) ;
940- cd . hydrate ( _DEFAULT_CONTEXT , null , null ) ;
941- cd . mode = CHECK_ALWAYS ;
942-
943- cd . detectChanges ( ) ;
944-
945- expect ( cd . mode ) . toEqual ( CHECK_ALWAYS ) ;
946- } ) ;
947-
948- describe ( "marking ON_PUSH detectors as CHECK_ONCE after an update" , ( ) => {
949- var checkedDetector ;
950- var dirRecordWithOnPush ;
951- var updateDirWithOnPushRecord ;
952- var directives ;
953-
954- beforeEach ( ( ) => {
955- var proto = createProtoChangeDetector ( [ ] , [ ] , [ ] , null , ON_PUSH ) ;
956- checkedDetector = proto . instantiate ( null ) ;
957- checkedDetector . hydrate ( _DEFAULT_CONTEXT , null , null ) ;
958- checkedDetector . mode = CHECKED ;
959-
960- // this directive is a component with ON_PUSH change detection
961- dirRecordWithOnPush = new DirectiveRecord (
962- { directiveIndex : new DirectiveIndex ( 0 , 0 ) , changeDetection : ON_PUSH } ) ;
963-
964- // a record updating a component
965- updateDirWithOnPushRecord = BindingRecord . createForDirective (
966- ast ( "42" ) , "a" , ( o , v ) => o . a = v , dirRecordWithOnPush ) ;
967-
968- var targetDirective = new TestData ( null ) ;
969- directives = new FakeDirectives ( [ targetDirective ] , [ checkedDetector ] ) ;
970- } ) ;
971-
972- it ( "should set the mode to CHECK_ONCE when a binding is updated" , ( ) => {
973- var proto = createProtoChangeDetector ( [ updateDirWithOnPushRecord ] , [ ] ,
974- [ dirRecordWithOnPush ] ) ;
975-
976- var cd = proto . instantiate ( null ) ;
977- cd . hydrate ( _DEFAULT_CONTEXT , null , directives ) ;
978-
979- expect ( checkedDetector . mode ) . toEqual ( CHECKED ) ;
980-
981- // evaluate the record, update the targetDirective, and mark its detector as
982- // CHECK_ONCE
983- cd . detectChanges ( ) ;
984-
985- expect ( checkedDetector . mode ) . toEqual ( CHECK_ONCE ) ;
986- } ) ;
987- } ) ;
988- } ) ;
989955 } ) ;
990956 } ) ;
991957}
0 commit comments