|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import {ɵPLATFORM_BROWSER_ID as PLATFORM_BROWSER_ID} from '@angular/common'; |
10 | | -import {ApplicationRef, Attribute, ChangeDetectionStrategy, ChangeDetectorRef, Component, ComponentRef, createComponent, DebugElement, Directive, EnvironmentInjector, ErrorHandler, getDebugNode, inject, Input, NgZone, Pipe, PipeTransform, PLATFORM_ID, QueryList, Type, ViewChildren, ɵDEFER_BLOCK_DEPENDENCY_INTERCEPTOR} from '@angular/core'; |
| 10 | +import {ApplicationRef, Attribute, ChangeDetectionStrategy, ChangeDetectorRef, Component, ComponentRef, createComponent, DebugElement, Directive, EnvironmentInjector, ErrorHandler, getDebugNode, inject, Injectable, InjectionToken, Input, NgModule, NgZone, Pipe, PipeTransform, PLATFORM_ID, QueryList, Type, ViewChildren, ɵDEFER_BLOCK_DEPENDENCY_INTERCEPTOR} from '@angular/core'; |
11 | 11 | import {getComponentDef} from '@angular/core/src/render3/definition'; |
12 | 12 | import {ComponentFixture, DeferBlockBehavior, fakeAsync, flush, TestBed, tick} from '@angular/core/testing'; |
13 | 13 |
|
@@ -3979,4 +3979,176 @@ describe('@defer', () => { |
3979 | 3979 | .toHaveBeenCalledWith('focusin', jasmine.any(Function), jasmine.any(Object)); |
3980 | 3980 | }); |
3981 | 3981 | }); |
| 3982 | + |
| 3983 | + describe('DI', () => { |
| 3984 | + it('should provide access to tokens from a parent component', async () => { |
| 3985 | + const TokenA = new InjectionToken('A'); |
| 3986 | + const TokenB = new InjectionToken('B'); |
| 3987 | + |
| 3988 | + @Component({ |
| 3989 | + standalone: true, |
| 3990 | + selector: 'parent-cmp', |
| 3991 | + template: '<ng-content />', |
| 3992 | + providers: [{provide: TokenA, useValue: 'TokenA.ParentCmp'}], |
| 3993 | + }) |
| 3994 | + class ParentCmp { |
| 3995 | + } |
| 3996 | + |
| 3997 | + @Component({ |
| 3998 | + standalone: true, |
| 3999 | + selector: 'child-cmp', |
| 4000 | + template: 'Token A: {{ parentTokenA }} | Token B: {{ parentTokenB }}', |
| 4001 | + }) |
| 4002 | + class ChildCmp { |
| 4003 | + parentTokenA = inject(TokenA); |
| 4004 | + parentTokenB = inject(TokenB); |
| 4005 | + } |
| 4006 | + |
| 4007 | + @Component({ |
| 4008 | + standalone: true, |
| 4009 | + selector: 'app-root', |
| 4010 | + template: ` |
| 4011 | + <parent-cmp> |
| 4012 | + @defer (when isVisible) { |
| 4013 | + <child-cmp /> |
| 4014 | + } |
| 4015 | + </parent-cmp> |
| 4016 | + `, |
| 4017 | + imports: [ChildCmp, ParentCmp], |
| 4018 | + providers: [{provide: TokenB, useValue: 'TokenB.RootCmp'}] |
| 4019 | + }) |
| 4020 | + class RootCmp { |
| 4021 | + isVisible = true; |
| 4022 | + } |
| 4023 | + |
| 4024 | + const deferDepsInterceptor = { |
| 4025 | + intercept() { |
| 4026 | + return () => { |
| 4027 | + return [dynamicImportOf(ChildCmp)]; |
| 4028 | + }; |
| 4029 | + } |
| 4030 | + }; |
| 4031 | + |
| 4032 | + TestBed.configureTestingModule({ |
| 4033 | + providers: [ |
| 4034 | + {provide: ɵDEFER_BLOCK_DEPENDENCY_INTERCEPTOR, useValue: deferDepsInterceptor}, |
| 4035 | + ], |
| 4036 | + deferBlockBehavior: DeferBlockBehavior.Playthrough, |
| 4037 | + }); |
| 4038 | + |
| 4039 | + const fixture = TestBed.createComponent(RootCmp); |
| 4040 | + fixture.detectChanges(); |
| 4041 | + |
| 4042 | + await allPendingDynamicImports(); |
| 4043 | + fixture.detectChanges(); |
| 4044 | + |
| 4045 | + // Verify that tokens from parent components are available for injection |
| 4046 | + // inside a component within a `@defer` block. |
| 4047 | + const tokenA = 'TokenA.ParentCmp'; |
| 4048 | + const tokenB = 'TokenB.RootCmp'; |
| 4049 | + |
| 4050 | + expect(fixture.nativeElement.innerHTML) |
| 4051 | + .toContain(`<child-cmp>Token A: ${tokenA} | Token B: ${tokenB}</child-cmp>`); |
| 4052 | + }); |
| 4053 | + }); |
| 4054 | + |
| 4055 | + describe('NgModules', () => { |
| 4056 | + it('should provide access to tokens from imported NgModules', async () => { |
| 4057 | + let serviceInitCount = 0; |
| 4058 | + |
| 4059 | + const TokenA = new InjectionToken(''); |
| 4060 | + |
| 4061 | + @Injectable() |
| 4062 | + class Service { |
| 4063 | + id = 'ChartsModule.Service'; |
| 4064 | + constructor() { |
| 4065 | + serviceInitCount++; |
| 4066 | + } |
| 4067 | + } |
| 4068 | + |
| 4069 | + @Component({ |
| 4070 | + selector: 'chart', |
| 4071 | + template: 'Service:{{ svc.id }}|TokenA:{{ tokenA }}', |
| 4072 | + }) |
| 4073 | + class Chart { |
| 4074 | + svc = inject(Service); |
| 4075 | + tokenA = inject(TokenA); |
| 4076 | + } |
| 4077 | + |
| 4078 | + @NgModule({ |
| 4079 | + providers: [Service], |
| 4080 | + declarations: [Chart], |
| 4081 | + exports: [Chart], |
| 4082 | + }) |
| 4083 | + class ChartsModule { |
| 4084 | + } |
| 4085 | + |
| 4086 | + @Component({ |
| 4087 | + selector: 'chart-collection', |
| 4088 | + template: '<chart />', |
| 4089 | + standalone: true, |
| 4090 | + imports: [ChartsModule], |
| 4091 | + }) |
| 4092 | + class ChartCollectionComponent { |
| 4093 | + } |
| 4094 | + |
| 4095 | + @Component({ |
| 4096 | + selector: 'app-root', |
| 4097 | + standalone: true, |
| 4098 | + template: ` |
| 4099 | + @for(item of items; track $index) { |
| 4100 | + @defer (when isVisible) { |
| 4101 | + <chart-collection /> |
| 4102 | + } |
| 4103 | + } |
| 4104 | + `, |
| 4105 | + imports: [ChartCollectionComponent], |
| 4106 | + providers: [{provide: TokenA, useValue: 'MyCmp.A'}] |
| 4107 | + }) |
| 4108 | + class MyCmp { |
| 4109 | + items = [1, 2, 3]; |
| 4110 | + isVisible = true; |
| 4111 | + } |
| 4112 | + |
| 4113 | + const deferDepsInterceptor = { |
| 4114 | + intercept() { |
| 4115 | + return () => { |
| 4116 | + return [dynamicImportOf(ChartCollectionComponent)]; |
| 4117 | + }; |
| 4118 | + } |
| 4119 | + }; |
| 4120 | + |
| 4121 | + TestBed.configureTestingModule({ |
| 4122 | + providers: [ |
| 4123 | + {provide: ɵDEFER_BLOCK_DEPENDENCY_INTERCEPTOR, useValue: deferDepsInterceptor}, |
| 4124 | + ], |
| 4125 | + deferBlockBehavior: DeferBlockBehavior.Playthrough, |
| 4126 | + }); |
| 4127 | + |
| 4128 | + clearDirectiveDefs(MyCmp); |
| 4129 | + |
| 4130 | + const fixture = TestBed.createComponent(MyCmp); |
| 4131 | + fixture.detectChanges(); |
| 4132 | + |
| 4133 | + await allPendingDynamicImports(); |
| 4134 | + fixture.detectChanges(); |
| 4135 | + |
| 4136 | + // Verify that the `Service` injectable was initialized only once, |
| 4137 | + // even though it was injected in 3 instances of the `<chart>` component, |
| 4138 | + // used within defer blocks. |
| 4139 | + expect(serviceInitCount).toBe(1); |
| 4140 | + expect(fixture.nativeElement.querySelectorAll('chart').length).toBe(3); |
| 4141 | + |
| 4142 | + // Verify that a service defined within an NgModule can inject services |
| 4143 | + // provided within the same NgModule. |
| 4144 | + const serviceFromNgModule = 'Service:ChartsModule.Service'; |
| 4145 | + |
| 4146 | + // Make sure sure that a nested `<chart>` component from the defer block |
| 4147 | + // can inject tokens provided in parent component (that contains `@defer` |
| 4148 | + // in its template). |
| 4149 | + const tokenFromRootComponent = 'TokenA:MyCmp.A'; |
| 4150 | + expect(fixture.nativeElement.innerHTML) |
| 4151 | + .toContain(`<chart>${serviceFromNgModule}|${tokenFromRootComponent}</chart>`); |
| 4152 | + }); |
| 4153 | + }); |
3982 | 4154 | }); |
0 commit comments