@@ -13,7 +13,7 @@ import {EventEmitter} from '../..';
13
13
14
14
import { AttributeMarker , QueryList , defineComponent , defineDirective , detectChanges } from '../../src/render3/index' ;
15
15
import { getNativeByIndex } from '../../src/render3/util' ;
16
- import { bind , container , containerRefreshEnd , containerRefreshStart , directiveInject , element , elementContainerEnd , elementContainerStart , elementEnd , elementProperty , elementStart , embeddedViewEnd , embeddedViewStart , load , loadQueryList , reference , registerContentQuery , template } from '../../src/render3/instructions' ;
16
+ import { bind , container , containerRefreshEnd , containerRefreshStart , directiveInject , element , elementContainerEnd , elementContainerStart , elementEnd , elementProperty , elementStart , embeddedViewEnd , embeddedViewStart , load , loadQueryList , reference , registerContentQuery , template , text } from '../../src/render3/instructions' ;
17
17
import { RenderFlags } from '../../src/render3/interfaces/definition' ;
18
18
import { query , queryRefresh } from '../../src/render3/query' ;
19
19
import { getViewData } from '../../src/render3/state' ;
@@ -949,7 +949,7 @@ describe('query', () => {
949
949
expect ( qList . last ) . toBe ( childInstance ) ;
950
950
} ) ;
951
951
952
- it ( 'should not add results to query if a requested token cant be read' , ( ) => {
952
+ it ( 'should not add results to selector-based query if a requested token cant be read' , ( ) => {
953
953
const Child = createDirective ( 'child' ) ;
954
954
955
955
/**
@@ -976,11 +976,226 @@ describe('query', () => {
976
976
}
977
977
} ) ;
978
978
979
- const cmptInstance = renderComponent ( Cmpt ) ;
980
- const qList = ( cmptInstance . query as QueryList < any > ) ;
979
+ const { component} = new ComponentFixture ( Cmpt ) ;
980
+ const qList = component . query ;
981
+ expect ( qList . length ) . toBe ( 0 ) ;
982
+ } ) ;
983
+
984
+ it ( 'should not add results to directive-based query if requested token cant be read' , ( ) => {
985
+ const Child = createDirective ( 'child' ) ;
986
+ const OtherChild = createDirective ( 'otherchild' ) ;
987
+
988
+ /**
989
+ * <div child></div>
990
+ * class Cmpt {
991
+ * @ViewChildren (Child, {read: OtherChild}) query;
992
+ * }
993
+ */
994
+ const Cmpt = createComponent (
995
+ 'cmpt' ,
996
+ function ( rf : RenderFlags , ctx : any ) {
997
+ if ( rf & RenderFlags . Create ) {
998
+ element ( 1 , 'div' , [ 'child' , '' ] ) ;
999
+ }
1000
+ } ,
1001
+ 2 , 0 , [ Child , OtherChild ] , [ ] ,
1002
+ function ( rf : RenderFlags , ctx : any ) {
1003
+ if ( rf & RenderFlags . Create ) {
1004
+ query ( 0 , Child , false , OtherChild ) ;
1005
+ }
1006
+ if ( rf & RenderFlags . Update ) {
1007
+ let tmp : any ;
1008
+ queryRefresh ( tmp = load < QueryList < any > > ( 0 ) ) && ( ctx . query = tmp as QueryList < any > ) ;
1009
+ }
1010
+ } ) ;
1011
+
1012
+ const { component} = new ComponentFixture ( Cmpt ) ;
1013
+ const qList = component . query ;
1014
+ expect ( qList . length ) . toBe ( 0 ) ;
1015
+ } ) ;
1016
+
1017
+ it ( 'should not add results to directive-based query if only read token matches' , ( ) => {
1018
+ const Child = createDirective ( 'child' ) ;
1019
+ const OtherChild = createDirective ( 'otherchild' ) ;
1020
+
1021
+ /**
1022
+ * <div child></div>
1023
+ * class Cmpt {
1024
+ * @ViewChildren (OtherChild, {read: Child}) query;
1025
+ * }
1026
+ */
1027
+ const Cmpt = createComponent (
1028
+ 'cmpt' ,
1029
+ function ( rf : RenderFlags , ctx : any ) {
1030
+ if ( rf & RenderFlags . Create ) {
1031
+ element ( 1 , 'div' , [ 'child' , '' ] ) ;
1032
+ }
1033
+ } ,
1034
+ 2 , 0 , [ Child , OtherChild ] , [ ] ,
1035
+ function ( rf : RenderFlags , ctx : any ) {
1036
+ if ( rf & RenderFlags . Create ) {
1037
+ query ( 0 , OtherChild , false , Child ) ;
1038
+ }
1039
+ if ( rf & RenderFlags . Update ) {
1040
+ let tmp : any ;
1041
+ queryRefresh ( tmp = load < QueryList < any > > ( 0 ) ) && ( ctx . query = tmp as QueryList < any > ) ;
1042
+ }
1043
+ } ) ;
1044
+
1045
+ const { component} = new ComponentFixture ( Cmpt ) ;
1046
+ const qList = component . query ;
1047
+ expect ( qList . length ) . toBe ( 0 ) ;
1048
+ } ) ;
1049
+
1050
+ it ( 'should not add results to TemplateRef-based query if only read token matches' , ( ) => {
1051
+ /**
1052
+ * <div></div>
1053
+ * class Cmpt {
1054
+ * @ViewChildren (TemplateRef, {read: ElementRef}) query;
1055
+ * }
1056
+ */
1057
+ const Cmpt = createComponent (
1058
+ 'cmpt' ,
1059
+ function ( rf : RenderFlags , ctx : any ) {
1060
+ if ( rf & RenderFlags . Create ) {
1061
+ element ( 1 , 'div' ) ;
1062
+ }
1063
+ } ,
1064
+ 2 , 0 , [ ] , [ ] ,
1065
+ function ( rf : RenderFlags , ctx : any ) {
1066
+ if ( rf & RenderFlags . Create ) {
1067
+ query ( 0 , TemplateRef as any , false , ElementRef ) ;
1068
+ }
1069
+ if ( rf & RenderFlags . Update ) {
1070
+ let tmp : any ;
1071
+ queryRefresh ( tmp = load < QueryList < any > > ( 0 ) ) && ( ctx . query = tmp as QueryList < any > ) ;
1072
+ }
1073
+ } ) ;
1074
+
1075
+ const { component} = new ComponentFixture ( Cmpt ) ;
1076
+ const qList = component . query ;
981
1077
expect ( qList . length ) . toBe ( 0 ) ;
982
1078
} ) ;
983
1079
1080
+ it ( 'should match using string selector and directive as a read argument' , ( ) => {
1081
+ const Child = createDirective ( 'child' ) ;
1082
+
1083
+ /**
1084
+ * <div child #foo></div>
1085
+ * class Cmpt {
1086
+ * @ViewChildren ('foo', {read: Child}) query;
1087
+ * }
1088
+ */
1089
+ const Cmpt = createComponent (
1090
+ 'cmpt' ,
1091
+ function ( rf : RenderFlags , ctx : any ) {
1092
+ if ( rf & RenderFlags . Create ) {
1093
+ element ( 1 , 'div' , [ 'child' , '' ] , [ 'foo' , '' ] ) ;
1094
+ }
1095
+ } ,
1096
+ 3 , 0 , [ Child ] , [ ] ,
1097
+ function ( rf : RenderFlags , ctx : any ) {
1098
+ if ( rf & RenderFlags . Create ) {
1099
+ query ( 0 , [ 'foo' ] , false , Child ) ;
1100
+ }
1101
+ if ( rf & RenderFlags . Update ) {
1102
+ let tmp : any ;
1103
+ queryRefresh ( tmp = load < QueryList < any > > ( 0 ) ) && ( ctx . query = tmp as QueryList < any > ) ;
1104
+ }
1105
+ } ) ;
1106
+
1107
+ const { component} = new ComponentFixture ( Cmpt ) ;
1108
+ const qList = component . query ;
1109
+ expect ( qList . length ) . toBe ( 1 ) ;
1110
+ expect ( qList . first instanceof Child ) . toBeTruthy ( ) ;
1111
+ } ) ;
1112
+
1113
+ it ( 'should not add results to the query in case no match found (via TemplateRef)' , ( ) => {
1114
+ const Child = createDirective ( 'child' ) ;
1115
+
1116
+ /**
1117
+ * <div child></div>
1118
+ * class Cmpt {
1119
+ * @ViewChildren (TemplateRef) query;
1120
+ * }
1121
+ */
1122
+ const Cmpt = createComponent (
1123
+ 'cmpt' ,
1124
+ function ( rf : RenderFlags , ctx : any ) {
1125
+ if ( rf & RenderFlags . Create ) {
1126
+ element ( 1 , 'div' , [ 'child' , '' ] ) ;
1127
+ }
1128
+ } ,
1129
+ 2 , 0 , [ Child ] , [ ] ,
1130
+ function ( rf : RenderFlags , ctx : any ) {
1131
+ if ( rf & RenderFlags . Create ) {
1132
+ query ( 0 , TemplateRef as any , false ) ;
1133
+ }
1134
+ if ( rf & RenderFlags . Update ) {
1135
+ let tmp : any ;
1136
+ queryRefresh ( tmp = load < QueryList < any > > ( 0 ) ) && ( ctx . query = tmp as QueryList < any > ) ;
1137
+ }
1138
+ } ) ;
1139
+
1140
+ const { component} = new ComponentFixture ( Cmpt ) ;
1141
+ const qList = component . query ;
1142
+ expect ( qList . length ) . toBe ( 0 ) ;
1143
+ } ) ;
1144
+
1145
+ it ( 'should query templates if the type is TemplateRef (and respect "read" option)' , ( ) => {
1146
+ function Cmpt_Template_1 ( rf : RenderFlags , ctx1 : any ) {
1147
+ if ( rf & RenderFlags . Create ) {
1148
+ elementStart ( 0 , 'div' ) ;
1149
+ text ( 1 , 'Test' ) ;
1150
+ elementEnd ( ) ;
1151
+ }
1152
+ }
1153
+ /**
1154
+ * <ng-template #foo><div>Test</div></ng-template>
1155
+ * <ng-template #bar><div>Test</div></ng-template>
1156
+ * <ng-template #baz><div>Test</div></ng-template>
1157
+ * class Cmpt {
1158
+ * @ViewChildren (TemplateRef) tmplQuery;
1159
+ * @ViewChildren (TemplateRef, {read: ElementRef}) elemQuery;
1160
+ * }
1161
+ */
1162
+ const Cmpt = createComponent (
1163
+ 'cmpt' ,
1164
+ function ( rf : RenderFlags , ctx : any ) {
1165
+ if ( rf & RenderFlags . Create ) {
1166
+ template ( 2 , Cmpt_Template_1 , 2 , 0 , null , null , [ 'foo' , '' ] , templateRefExtractor ) ;
1167
+ template ( 3 , Cmpt_Template_1 , 2 , 0 , null , null , [ 'bar' , '' ] , templateRefExtractor ) ;
1168
+ template ( 4 , Cmpt_Template_1 , 2 , 0 , null , null , [ 'baz' , '' ] , templateRefExtractor ) ;
1169
+ }
1170
+ } ,
1171
+ 5 , 0 , [ ] , [ ] ,
1172
+ function ( rf : RenderFlags , ctx : any ) {
1173
+ if ( rf & RenderFlags . Create ) {
1174
+ query ( 0 , TemplateRef as any , false ) ;
1175
+ query ( 1 , TemplateRef as any , false , ElementRef ) ;
1176
+ }
1177
+ if ( rf & RenderFlags . Update ) {
1178
+ let tmp : any ;
1179
+ queryRefresh ( tmp = load < QueryList < any > > ( 0 ) ) &&
1180
+ ( ctx . tmplQuery = tmp as QueryList < any > ) ;
1181
+ queryRefresh ( tmp = load < QueryList < any > > ( 1 ) ) &&
1182
+ ( ctx . elemQuery = tmp as QueryList < any > ) ;
1183
+ }
1184
+ } ) ;
1185
+
1186
+ const { component} = new ComponentFixture ( Cmpt ) ;
1187
+
1188
+ // check template-based query set
1189
+ const tmplQList = component . tmplQuery ;
1190
+ expect ( tmplQList . length ) . toBe ( 3 ) ;
1191
+ expect ( isTemplateRef ( tmplQList . first ) ) . toBeTruthy ( ) ;
1192
+
1193
+ // check element-based query set
1194
+ const elemQList = component . elemQuery ;
1195
+ expect ( elemQList . length ) . toBe ( 3 ) ;
1196
+ expect ( isElementRef ( elemQList . first ) ) . toBeTruthy ( ) ;
1197
+ } ) ;
1198
+
984
1199
} ) ;
985
1200
} ) ;
986
1201
0 commit comments