|
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {Component, ComponentFactoryResolver, createEnvironmentInjector, ENVIRONMENT_INITIALIZER, EnvironmentInjector, InjectionToken, INJECTOR, Injector, NgModuleRef} from '@angular/core'; |
| 9 | +import {Component, ComponentFactoryResolver, createEnvironmentInjector, ENVIRONMENT_INITIALIZER, EnvironmentInjector, inject, InjectFlags, InjectionToken, INJECTOR, Injector, NgModuleRef, ViewContainerRef} from '@angular/core'; |
10 | 10 | import {R3Injector} from '@angular/core/src/di/r3_injector'; |
11 | 11 | import {TestBed} from '@angular/core/testing'; |
12 | 12 |
|
@@ -119,4 +119,94 @@ describe('environment injector', () => { |
119 | 119 | }); |
120 | 120 | expect(injector.get(EnvScopedToken, false)).toBeTrue(); |
121 | 121 | }); |
| 122 | + |
| 123 | + describe('runInContext()', () => { |
| 124 | + it('should return the function\'s return value', () => { |
| 125 | + const injector = TestBed.inject(EnvironmentInjector); |
| 126 | + const returnValue = injector.runInContext(() => 3); |
| 127 | + expect(returnValue).toBe(3); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should make inject() available', () => { |
| 131 | + const TOKEN = new InjectionToken<string>('TOKEN'); |
| 132 | + const injector = createEnvironmentInjector( |
| 133 | + [{provide: TOKEN, useValue: 'from injector'}], TestBed.inject(EnvironmentInjector)); |
| 134 | + |
| 135 | + const result = injector.runInContext(() => inject(TOKEN)); |
| 136 | + expect(result).toEqual('from injector'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should properly clean up after the function returns', () => { |
| 140 | + const TOKEN = new InjectionToken<string>('TOKEN'); |
| 141 | + const injector = TestBed.inject(EnvironmentInjector); |
| 142 | + injector.runInContext(() => {}); |
| 143 | + expect(() => inject(TOKEN, InjectFlags.Optional)).toThrow(); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should properly clean up after the function throws', () => { |
| 147 | + const TOKEN = new InjectionToken<string>('TOKEN'); |
| 148 | + const injector = TestBed.inject(EnvironmentInjector); |
| 149 | + expect(() => injector.runInContext(() => { |
| 150 | + throw new Error('crashes!'); |
| 151 | + })).toThrow(); |
| 152 | + expect(() => inject(TOKEN, InjectFlags.Optional)).toThrow(); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should set the correct inject implementation', () => { |
| 156 | + const TOKEN = new InjectionToken<string>('TOKEN', { |
| 157 | + providedIn: 'root', |
| 158 | + factory: () => 'from root', |
| 159 | + }); |
| 160 | + |
| 161 | + @Component({ |
| 162 | + standalone: true, |
| 163 | + template: '', |
| 164 | + providers: [{provide: TOKEN, useValue: 'from component'}], |
| 165 | + }) |
| 166 | + class TestCmp { |
| 167 | + envInjector = inject(EnvironmentInjector); |
| 168 | + |
| 169 | + tokenFromComponent = inject(TOKEN); |
| 170 | + tokenFromEnvContext = this.envInjector.runInContext(() => inject(TOKEN)); |
| 171 | + |
| 172 | + // Attempt to inject ViewContainerRef within the environment injector's context. This should |
| 173 | + // not be available, so the result should be `null`. |
| 174 | + vcrFromEnvContext = |
| 175 | + this.envInjector.runInContext(() => inject(ViewContainerRef, InjectFlags.Optional)); |
| 176 | + } |
| 177 | + |
| 178 | + const instance = TestBed.createComponent(TestCmp).componentInstance; |
| 179 | + expect(instance.tokenFromComponent).toEqual('from component'); |
| 180 | + expect(instance.tokenFromEnvContext).toEqual('from root'); |
| 181 | + expect(instance.vcrFromEnvContext).toBeNull(); |
| 182 | + }); |
| 183 | + |
| 184 | + it('should be reentrant', () => { |
| 185 | + const TOKEN = new InjectionToken<string>('TOKEN', { |
| 186 | + providedIn: 'root', |
| 187 | + factory: () => 'from root', |
| 188 | + }); |
| 189 | + |
| 190 | + const parentInjector = TestBed.inject(EnvironmentInjector); |
| 191 | + const childInjector = |
| 192 | + createEnvironmentInjector([{provide: TOKEN, useValue: 'from child'}], parentInjector); |
| 193 | + |
| 194 | + const results = parentInjector.runInContext(() => { |
| 195 | + const fromParentBefore = inject(TOKEN); |
| 196 | + const fromChild = childInjector.runInContext(() => inject(TOKEN)); |
| 197 | + const fromParentAfter = inject(TOKEN); |
| 198 | + return {fromParentBefore, fromChild, fromParentAfter}; |
| 199 | + }); |
| 200 | + |
| 201 | + expect(results.fromParentBefore).toEqual('from root'); |
| 202 | + expect(results.fromChild).toEqual('from child'); |
| 203 | + expect(results.fromParentAfter).toEqual('from root'); |
| 204 | + }); |
| 205 | + |
| 206 | + it('should not function on a destroyed injector', () => { |
| 207 | + const injector = createEnvironmentInjector([], TestBed.inject(EnvironmentInjector)); |
| 208 | + injector.destroy(); |
| 209 | + expect(() => injector.runInContext(() => {})).toThrow(); |
| 210 | + }); |
| 211 | + }); |
122 | 212 | }); |
0 commit comments