|
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {Component as _Component, ComponentFactoryResolver, ElementRef, Injectable as _Injectable, InjectFlags, InjectionToken, InjectorType, Provider, RendererFactory2, ViewContainerRef, ɵNgModuleDef as NgModuleDef, ɵɵdefineInjectable, ɵɵdefineInjector, ɵɵinject} from '../../src/core'; |
| 9 | +import {Component as _Component, ComponentFactoryResolver, ElementRef, Injectable as _Injectable, InjectFlags, InjectionToken, InjectorType, Provider, RendererFactory2, Type, ViewContainerRef, ɵNgModuleDef as NgModuleDef, ɵɵdefineInjectable, ɵɵdefineInjector, ɵɵinject} from '../../src/core'; |
10 | 10 | import {forwardRef} from '../../src/di/forward_ref'; |
11 | 11 | import {createInjector} from '../../src/di/r3_injector'; |
12 | | -import {injectComponentFactoryResolver, ɵɵdefineComponent, ɵɵdefineDirective, ɵɵdirectiveInject, ɵɵelement, ɵɵelementEnd, ɵɵelementStart, ɵɵProvidersFeature, ɵɵtext, ɵɵtextInterpolate1} from '../../src/render3/index'; |
| 12 | +import {injectComponentFactoryResolver, ɵɵdefineComponent, ɵɵdefineDirective, ɵɵdirectiveInject, ɵɵelement, ɵɵelementEnd, ɵɵelementStart, ɵɵgetInheritedFactory, ɵɵProvidersFeature, ɵɵtext, ɵɵtextInterpolate1} from '../../src/render3/index'; |
13 | 13 | import {RenderFlags} from '../../src/render3/interfaces/definition'; |
14 | 14 | import {NgModuleFactory} from '../../src/render3/ng_module_ref'; |
15 | 15 | import {getInjector} from '../../src/render3/util/discovery_utils'; |
@@ -1282,7 +1282,126 @@ describe('providers', () => { |
1282 | 1282 | expect(injector.get(Some).location).toEqual('From app component'); |
1283 | 1283 | }); |
1284 | 1284 | }); |
| 1285 | + |
| 1286 | + // Note: these tests check the behavior of `getInheritedFactory` specifically. |
| 1287 | + // Since `getInheritedFactory` is only generated in AOT, the tests can't be |
| 1288 | + // ported directly to TestBed while running in JIT mode. |
| 1289 | + describe('getInheritedFactory on class with custom decorator', () => { |
| 1290 | + function addFoo() { |
| 1291 | + return (constructor: Type<any>): any => { |
| 1292 | + const decoratedClass = class Extender extends constructor { foo = 'bar'; }; |
| 1293 | + |
| 1294 | + // On IE10 child classes don't inherit static properties by default. If we detect |
| 1295 | + // such a case, try to account for it so the tests are consistent between browsers. |
| 1296 | + if (Object.getPrototypeOf(decoratedClass) !== constructor) { |
| 1297 | + decoratedClass.prototype = constructor.prototype; |
| 1298 | + } |
| 1299 | + |
| 1300 | + return decoratedClass; |
| 1301 | + }; |
| 1302 | + } |
| 1303 | + |
| 1304 | + it('should find the correct factories if a parent class has a custom decorator', () => { |
| 1305 | + class GrandParent { |
| 1306 | + static ɵfac = function GrandParent_Factory() {}; |
| 1307 | + } |
| 1308 | + |
| 1309 | + @addFoo() |
| 1310 | + class Parent extends GrandParent { |
| 1311 | + static ɵfac = function Parent_Factory() {}; |
| 1312 | + } |
| 1313 | + |
| 1314 | + class Child extends Parent { |
| 1315 | + static ɵfac = function Child_Factory() {}; |
| 1316 | + } |
| 1317 | + |
| 1318 | + expect(ɵɵgetInheritedFactory(Child).name).toBe('Parent_Factory'); |
| 1319 | + expect(ɵɵgetInheritedFactory(Parent).name).toBe('GrandParent_Factory'); |
| 1320 | + expect(ɵɵgetInheritedFactory(GrandParent).name).toBeFalsy(); |
| 1321 | + }); |
| 1322 | + |
| 1323 | + it('should find the correct factories if a child class has a custom decorator', () => { |
| 1324 | + class GrandParent { |
| 1325 | + static ɵfac = function GrandParent_Factory() {}; |
| 1326 | + } |
| 1327 | + |
| 1328 | + class Parent extends GrandParent { |
| 1329 | + static ɵfac = function Parent_Factory() {}; |
| 1330 | + } |
| 1331 | + |
| 1332 | + @addFoo() |
| 1333 | + class Child extends Parent { |
| 1334 | + static ɵfac = function Child_Factory() {}; |
| 1335 | + } |
| 1336 | + |
| 1337 | + expect(ɵɵgetInheritedFactory(Child).name).toBe('Parent_Factory'); |
| 1338 | + expect(ɵɵgetInheritedFactory(Parent).name).toBe('GrandParent_Factory'); |
| 1339 | + expect(ɵɵgetInheritedFactory(GrandParent).name).toBeFalsy(); |
| 1340 | + }); |
| 1341 | + |
| 1342 | + it('should find the correct factories if a grandparent class has a custom decorator', () => { |
| 1343 | + @addFoo() |
| 1344 | + class GrandParent { |
| 1345 | + static ɵfac = function GrandParent_Factory() {}; |
| 1346 | + } |
| 1347 | + |
| 1348 | + class Parent extends GrandParent { |
| 1349 | + static ɵfac = function Parent_Factory() {}; |
| 1350 | + } |
| 1351 | + |
| 1352 | + class Child extends Parent { |
| 1353 | + static ɵfac = function Child_Factory() {}; |
| 1354 | + } |
| 1355 | + |
| 1356 | + expect(ɵɵgetInheritedFactory(Child).name).toBe('Parent_Factory'); |
| 1357 | + expect(ɵɵgetInheritedFactory(Parent).name).toBe('GrandParent_Factory'); |
| 1358 | + expect(ɵɵgetInheritedFactory(GrandParent).name).toBeFalsy(); |
| 1359 | + }); |
| 1360 | + |
| 1361 | + it('should find the correct factories if all classes have a custom decorator', () => { |
| 1362 | + @addFoo() |
| 1363 | + class GrandParent { |
| 1364 | + static ɵfac = function GrandParent_Factory() {}; |
| 1365 | + } |
| 1366 | + |
| 1367 | + @addFoo() |
| 1368 | + class Parent extends GrandParent { |
| 1369 | + static ɵfac = function Parent_Factory() {}; |
| 1370 | + } |
| 1371 | + |
| 1372 | + @addFoo() |
| 1373 | + class Child extends Parent { |
| 1374 | + static ɵfac = function Child_Factory() {}; |
| 1375 | + } |
| 1376 | + |
| 1377 | + expect(ɵɵgetInheritedFactory(Child).name).toBe('Parent_Factory'); |
| 1378 | + expect(ɵɵgetInheritedFactory(Parent).name).toBe('GrandParent_Factory'); |
| 1379 | + expect(ɵɵgetInheritedFactory(GrandParent).name).toBeFalsy(); |
| 1380 | + }); |
| 1381 | + |
| 1382 | + it('should find the correct factories if parent and grandparent classes have a custom decorator', |
| 1383 | + () => { |
| 1384 | + @addFoo() |
| 1385 | + class GrandParent { |
| 1386 | + static ɵfac = function GrandParent_Factory() {}; |
| 1387 | + } |
| 1388 | + |
| 1389 | + @addFoo() |
| 1390 | + class Parent extends GrandParent { |
| 1391 | + static ɵfac = function Parent_Factory() {}; |
| 1392 | + } |
| 1393 | + |
| 1394 | + class Child extends Parent { |
| 1395 | + static ɵfac = function Child_Factory() {}; |
| 1396 | + } |
| 1397 | + |
| 1398 | + expect(ɵɵgetInheritedFactory(Child).name).toBe('Parent_Factory'); |
| 1399 | + expect(ɵɵgetInheritedFactory(Parent).name).toBe('GrandParent_Factory'); |
| 1400 | + expect(ɵɵgetInheritedFactory(GrandParent).name).toBeFalsy(); |
| 1401 | + }); |
| 1402 | + }); |
1285 | 1403 | }); |
| 1404 | + |
1286 | 1405 | interface ComponentTest { |
1287 | 1406 | providers?: Provider[]; |
1288 | 1407 | viewProviders?: Provider[]; |
|
0 commit comments