9
9
import { Inject , InjectionToken , QueryList } from '../../src/core' ;
10
10
import { ComponentDef , DirectiveDef , InheritDefinitionFeature , NgOnChangesFeature , ProvidersFeature , RenderFlags , allocHostVars , bind , defineBase , defineComponent , defineDirective , directiveInject , element , elementProperty , loadViewQuery , queryRefresh , viewQuery } from '../../src/render3/index' ;
11
11
12
- import { ComponentFixture , createComponent } from './render_util' ;
12
+ import { ComponentFixture , createComponent , getDirectiveOnNode } from './render_util' ;
13
13
14
14
describe ( 'InheritDefinitionFeature' , ( ) => {
15
15
it ( 'should inherit lifecycle hooks' , ( ) => {
@@ -363,48 +363,10 @@ describe('InheritDefinitionFeature', () => {
363
363
expect ( divEl . title ) . toEqual ( 'new-title' ) ;
364
364
} ) ;
365
365
366
- it ( 'should compose viewQuery (basic mechanics check)' , ( ) => {
367
- const log : Array < [ string , RenderFlags , any ] > = [ ] ;
366
+ describe ( 'view query' , ( ) => {
368
367
369
- class SuperComponent {
370
- static ngComponentDef = defineComponent ( {
371
- type : SuperComponent ,
372
- template : ( ) => { } ,
373
- consts : 0 ,
374
- vars : 0 ,
375
- selectors : [ [ '' , 'superDir' , '' ] ] ,
376
- viewQuery : < T > ( rf : RenderFlags , ctx : T ) => {
377
- log . push ( [ 'super' , rf , ctx ] ) ;
378
- } ,
379
- factory : ( ) => new SuperComponent ( ) ,
380
- } ) ;
381
- }
382
-
383
- class SubComponent extends SuperComponent {
384
- static ngComponentDef = defineComponent ( {
385
- type : SubComponent ,
386
- template : ( ) => { } ,
387
- consts : 0 ,
388
- vars : 0 ,
389
- selectors : [ [ '' , 'subDir' , '' ] ] ,
390
- viewQuery : ( directiveIndex : number , elementIndex : number ) => {
391
- log . push ( [ 'sub' , directiveIndex , elementIndex ] ) ;
392
- } ,
393
- factory : ( ) => new SubComponent ( ) ,
394
- features : [ InheritDefinitionFeature ]
395
- } ) ;
396
- }
397
-
398
- const subDef = SubComponent . ngComponentDef as ComponentDef < any > ;
399
-
400
- const context = { foo : 'bar' } ;
401
-
402
- subDef . viewQuery ! ( 1 , context ) ;
403
-
404
- expect ( log ) . toEqual ( [ [ 'super' , 1 , context ] , [ 'sub' , 1 , context ] ] ) ;
405
- } ) ;
368
+ const SomeComp = createComponent ( 'some-comp' , ( rf : RenderFlags , ctx : any ) => { } ) ;
406
369
407
- it ( 'should compose viewQuery (query logic check)' , ( ) => {
408
370
/*
409
371
* class SuperComponent {
410
372
* @ViewChildren ('super') superQuery;
@@ -417,7 +379,7 @@ describe('InheritDefinitionFeature', () => {
417
379
template : ( ) => { } ,
418
380
consts : 0 ,
419
381
vars : 0 ,
420
- selectors : [ [ '' , 'superDir' , ' '] ] ,
382
+ selectors : [ [ 'super-comp ' ] ] ,
421
383
viewQuery : < T > ( rf : RenderFlags , ctx : any ) => {
422
384
if ( rf & RenderFlags . Create ) {
423
385
viewQuery ( [ 'super' ] , false ) ;
@@ -435,6 +397,7 @@ describe('InheritDefinitionFeature', () => {
435
397
/**
436
398
* <div id="sub" #sub></div>
437
399
* <div id="super" #super></div>
400
+ * <some-comp></some-comp>
438
401
* class SubComponent extends SuperComponent {
439
402
* @ViewChildren ('sub') subQuery;
440
403
* }
@@ -447,11 +410,12 @@ describe('InheritDefinitionFeature', () => {
447
410
if ( rf & RenderFlags . Create ) {
448
411
element ( 0 , 'div' , [ 'id' , 'sub' ] , [ 'sub' , '' ] ) ;
449
412
element ( 2 , 'div' , [ 'id' , 'super' ] , [ 'super' , '' ] ) ;
413
+ element ( 4 , 'some-comp' ) ;
450
414
}
451
415
} ,
452
- consts : 4 ,
416
+ consts : 5 ,
453
417
vars : 0 ,
454
- selectors : [ [ '' , 'subDir' , ' '] ] ,
418
+ selectors : [ [ 'sub-comp ' ] ] ,
455
419
viewQuery : ( rf : RenderFlags , ctx : any ) => {
456
420
if ( rf & RenderFlags . Create ) {
457
421
viewQuery ( [ 'sub' ] , false ) ;
@@ -463,23 +427,98 @@ describe('InheritDefinitionFeature', () => {
463
427
}
464
428
} ,
465
429
factory : ( ) => new SubComponent ( ) ,
466
- features : [ InheritDefinitionFeature ]
430
+ features : [ InheritDefinitionFeature ] ,
431
+ directives : [ SomeComp ]
467
432
} ) ;
468
433
}
469
434
470
- const fixture = new ComponentFixture ( SubComponent ) ;
471
435
472
- const check = ( key : string ) : void => {
473
- const qList = ( fixture . component as any ) [ `${ key } Query` ] as QueryList < any > ;
474
- expect ( qList . length ) . toBe ( 1 ) ;
475
- expect ( qList . first . nativeElement ) . toEqual ( fixture . hostElement . querySelector ( `#${ key } ` ) ) ;
476
- expect ( qList . first . nativeElement . id ) . toEqual ( key ) ;
477
- } ;
436
+ it ( 'should compose viewQuery (basic mechanics check)' , ( ) => {
437
+ const log : Array < [ string , RenderFlags , any ] > = [ ] ;
438
+
439
+ class SuperComponent {
440
+ static ngComponentDef = defineComponent ( {
441
+ type : SuperComponent ,
442
+ template : ( ) => { } ,
443
+ consts : 0 ,
444
+ vars : 0 ,
445
+ selectors : [ [ '' , 'superDir' , '' ] ] ,
446
+ viewQuery : < T > ( rf : RenderFlags , ctx : T ) => {
447
+ log . push ( [ 'super' , rf , ctx ] ) ;
448
+ } ,
449
+ factory : ( ) => new SuperComponent ( ) ,
450
+ } ) ;
451
+ }
452
+
453
+ class SubComponent extends SuperComponent {
454
+ static ngComponentDef = defineComponent ( {
455
+ type : SubComponent ,
456
+ template : ( ) => { } ,
457
+ consts : 0 ,
458
+ vars : 0 ,
459
+ selectors : [ [ '' , 'subDir' , '' ] ] ,
460
+ viewQuery : ( directiveIndex : number , elementIndex : number ) => {
461
+ log . push ( [ 'sub' , directiveIndex , elementIndex ] ) ;
462
+ } ,
463
+ factory : ( ) => new SubComponent ( ) ,
464
+ features : [ InheritDefinitionFeature ]
465
+ } ) ;
466
+ }
467
+
468
+ const subDef = SubComponent . ngComponentDef as ComponentDef < any > ;
469
+
470
+ const context = { foo : 'bar' } ;
471
+
472
+ subDef . viewQuery ! ( 1 , context ) ;
473
+
474
+ expect ( log ) . toEqual ( [ [ 'super' , 1 , context ] , [ 'sub' , 1 , context ] ] ) ;
475
+ } ) ;
476
+
477
+
478
+
479
+ it ( 'should compose viewQuery (query logic check)' , ( ) => {
480
+ const fixture = new ComponentFixture ( SubComponent ) ;
481
+
482
+ const check = ( key : string ) : void => {
483
+ const qList = ( fixture . component as any ) [ `${ key } Query` ] as QueryList < any > ;
484
+ expect ( qList . length ) . toBe ( 1 ) ;
485
+ expect ( qList . first . nativeElement ) . toEqual ( fixture . hostElement . querySelector ( `#${ key } ` ) ) ;
486
+ expect ( qList . first . nativeElement . id ) . toEqual ( key ) ;
487
+ } ;
488
+
489
+ check ( 'sub' ) ;
490
+ check ( 'super' ) ;
491
+ } ) ;
492
+
493
+ it ( 'should work with multiple viewQuery comps' , ( ) => {
494
+ let subCompOne ! : SubComponent ;
495
+ let subCompTwo ! : SubComponent ;
496
+
497
+ const App = createComponent ( 'app' , ( rf : RenderFlags , ctx : any ) => {
498
+ if ( rf & RenderFlags . Create ) {
499
+ element ( 0 , 'sub-comp' ) ;
500
+ element ( 1 , 'sub-comp' ) ;
501
+ }
502
+ subCompOne = getDirectiveOnNode ( 0 ) ;
503
+ subCompTwo = getDirectiveOnNode ( 1 ) ;
504
+ } , 2 , 0 , [ SubComponent , SuperComponent ] ) ;
505
+
506
+ const fixture = new ComponentFixture ( App ) ;
507
+
508
+ const check = ( comp : SubComponent ) : void => {
509
+ const qList = comp . subQuery as QueryList < any > ;
510
+ expect ( qList . length ) . toBe ( 1 ) ;
511
+ expect ( qList . first . nativeElement ) . toEqual ( fixture . hostElement . querySelector ( '#sub' ) ) ;
512
+ expect ( qList . first . nativeElement . id ) . toEqual ( 'sub' ) ;
513
+ } ;
514
+
515
+ check ( subCompOne ) ;
516
+ check ( subCompTwo ) ;
517
+ } ) ;
478
518
479
- check ( 'sub' ) ;
480
- check ( 'super' ) ;
481
519
} ) ;
482
520
521
+
483
522
it ( 'should compose contentQueries' , ( ) => {
484
523
const log : string [ ] = [ ] ;
485
524
0 commit comments