2018-11-12 21:40:08 -05:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2018-11-12 21:40:08 -05:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2020-10-30 15:31:47 -04:00
|
|
|
import {Component as _Component, ComponentFactoryResolver, ElementRef, Injectable as _Injectable, InjectFlags, InjectionToken, InjectorType, Provider, RendererFactory2, Type, ViewContainerRef, ɵɵdefineInjectable, ɵɵdefineInjector, ɵɵdefineNgModule, ɵɵinject} from '../../src/core';
|
2018-11-12 21:40:08 -05:00
|
|
|
import {forwardRef} from '../../src/di/forward_ref';
|
2019-02-03 23:43:20 -05:00
|
|
|
import {createInjector} from '../../src/di/r3_injector';
|
2020-05-09 08:33:54 -04:00
|
|
|
import {injectComponentFactoryResolver, ɵɵdefineComponent, ɵɵdefineDirective, ɵɵdirectiveInject, ɵɵelement, ɵɵelementEnd, ɵɵelementStart, ɵɵgetInheritedFactory, ɵɵProvidersFeature, ɵɵtext, ɵɵtextInterpolate1} from '../../src/render3/index';
|
2018-11-12 21:40:08 -05:00
|
|
|
import {RenderFlags} from '../../src/render3/interfaces/definition';
|
2018-11-14 11:04:22 -05:00
|
|
|
import {NgModuleFactory} from '../../src/render3/ng_module_ref';
|
2019-02-20 17:21:20 -05:00
|
|
|
import {getInjector} from '../../src/render3/util/discovery_utils';
|
2019-09-06 17:43:16 -04:00
|
|
|
|
2018-11-14 11:04:22 -05:00
|
|
|
import {getRendererFactory2} from './imported_renderer2';
|
2018-11-12 21:40:08 -05:00
|
|
|
import {ComponentFixture} from './render_util';
|
|
|
|
|
|
|
|
const Component: typeof _Component = function(...args: any[]): any {
|
|
|
|
// In test we use @Component for documentation only so it's safe to mock out the implementation.
|
|
|
|
return () => undefined;
|
|
|
|
} as any;
|
|
|
|
const Injectable: typeof _Injectable = function(...args: any[]): any {
|
|
|
|
// In test we use @Injectable for documentation only so it's safe to mock out the implementation.
|
|
|
|
return () => undefined;
|
|
|
|
} as any;
|
|
|
|
|
|
|
|
|
|
|
|
describe('providers', () => {
|
|
|
|
describe('should support all types of Provider:', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
abstract class Greeter {
|
|
|
|
abstract greet: string;
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
|
|
|
|
const GREETER = new InjectionToken<Greeter>('greeter');
|
|
|
|
|
|
|
|
class GreeterClass implements Greeter {
|
|
|
|
greet = 'Class';
|
|
|
|
}
|
|
|
|
|
|
|
|
class GreeterDeps implements Greeter {
|
|
|
|
constructor(public greet: string) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class GreeterBuiltInDeps implements Greeter {
|
|
|
|
public greet: string;
|
|
|
|
constructor(private message: string, private elementRef: ElementRef) {
|
|
|
|
this.greet = this.message + ' from ' + this.elementRef.nativeElement.tagName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class GreeterProvider {
|
2020-04-13 19:40:21 -04:00
|
|
|
provide() {
|
|
|
|
return 'Provided';
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
class GreeterInj implements Greeter {
|
|
|
|
public greet: string;
|
2020-04-13 19:40:21 -04:00
|
|
|
constructor(private provider: GreeterProvider) {
|
|
|
|
this.greet = this.provider.provide();
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
|
2019-10-15 15:41:30 -04:00
|
|
|
static ɵprov = ɵɵdefineInjectable({
|
2019-06-07 13:12:07 -04:00
|
|
|
token: GreeterInj,
|
|
|
|
factory: () => new GreeterInj(ɵɵinject(GreeterProvider as any)),
|
|
|
|
});
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
it('TypeProvider', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [GreeterClass],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(GreeterClass).greet).toEqual('Class');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('ValueProvider', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: GREETER, useValue: {greet: 'Value'}}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(GREETER).greet).toEqual('Value');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('ClassProvider', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: GREETER, useClass: GreeterClass}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(GREETER).greet).toEqual('Class');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('ExistingProvider', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [GreeterClass, {provide: GREETER, useExisting: GreeterClass}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(GREETER).greet).toEqual('Class');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('FactoryProvider', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [GreeterClass, {provide: GREETER, useFactory: () => new GreeterClass()}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(GREETER).greet).toEqual('Class');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const MESSAGE = new InjectionToken<string>('message');
|
|
|
|
|
|
|
|
it('ClassProvider with deps', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [
|
|
|
|
{provide: MESSAGE, useValue: 'Message'},
|
|
|
|
{provide: GREETER, useClass: GreeterDeps, deps: [MESSAGE]}
|
|
|
|
],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(GREETER).greet).toEqual('Message');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('ClassProvider with built-in deps', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [
|
|
|
|
{provide: MESSAGE, useValue: 'Message'},
|
|
|
|
{provide: GREETER, useClass: GreeterBuiltInDeps, deps: [MESSAGE, ElementRef]}
|
|
|
|
],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(GREETER).greet).toEqual('Message from PARENT');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('FactoryProvider with deps', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [
|
|
|
|
{provide: MESSAGE, useValue: 'Message'},
|
|
|
|
{provide: GREETER, useFactory: (msg: string) => new GreeterDeps(msg), deps: [MESSAGE]}
|
|
|
|
],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(GREETER).greet).toEqual('Message');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('FactoryProvider with built-in deps', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [
|
|
|
|
{provide: MESSAGE, useValue: 'Message'}, {
|
|
|
|
provide: GREETER,
|
|
|
|
useFactory: (msg: string, elementRef: ElementRef) =>
|
2020-04-13 19:40:21 -04:00
|
|
|
new GreeterBuiltInDeps(msg, elementRef),
|
2018-11-12 21:40:08 -05:00
|
|
|
deps: [MESSAGE, ElementRef]
|
|
|
|
}
|
|
|
|
],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(GREETER).greet).toEqual('Message from PARENT');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('ClassProvider with injectable', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [GreeterProvider, {provide: GREETER, useClass: GreeterInj}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(GREETER).greet).toEqual('Provided');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('forwardRef', () => {
|
|
|
|
it('forwardRef resolves later', (done) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [forwardRef(() => ForLater)],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(ForLater) instanceof ForLater).toBeTruthy();
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
class ForLater {}
|
|
|
|
|
|
|
|
// The following test that forwardRefs are called, so we don't search for an anon fn
|
|
|
|
it('ValueProvider wrapped in forwardRef', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
2020-04-13 19:40:21 -04:00
|
|
|
providers: [{
|
|
|
|
provide: GREETER,
|
|
|
|
useValue: forwardRef(() => {
|
|
|
|
return {greet: 'Value'};
|
|
|
|
})
|
|
|
|
}],
|
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(GREETER).greet).toEqual('Value');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('ClassProvider wrapped in forwardRef', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: GREETER, useClass: forwardRef(() => GreeterClass)}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(GREETER).greet).toEqual('Class');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('ExistingProvider wrapped in forwardRef', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers:
|
|
|
|
[GreeterClass, {provide: GREETER, useExisting: forwardRef(() => GreeterClass)}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(GREETER).greet).toEqual('Class');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-11-12 21:52:51 -05:00
|
|
|
it('@Inject annotation wrapped in forwardRef', () => {
|
|
|
|
// @Inject(forwardRef(() => GREETER))
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: GREETER, useValue: {greet: 'Value'}}],
|
2019-04-04 14:41:52 -04:00
|
|
|
componentAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(forwardRef(() => GREETER)).greet).toEqual('Value');
|
2019-04-04 14:41:52 -04:00
|
|
|
}
|
2018-11-12 21:52:51 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-11-12 21:40:08 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All tests below assume this structure:
|
|
|
|
* ```
|
|
|
|
* <parent>
|
|
|
|
* <#VIEW#>
|
|
|
|
* <view-child>
|
|
|
|
* </view-child>
|
|
|
|
* </#VIEW#>
|
|
|
|
* <content-child>
|
|
|
|
* </content-child>
|
|
|
|
* </parent>
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
|
|
|
|
describe('override rules:', () => {
|
|
|
|
it('directiveProviders should override providers', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'Message 1'}],
|
|
|
|
directiveProviders: [{provide: String, useValue: 'Message 2'}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('Message 2');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('viewProviders should override providers', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'Message 1'}],
|
|
|
|
viewProviders: [{provide: String, useValue: 'Message 2'}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('Message 2');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('viewProviders should override directiveProviders', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
directiveProviders: [{provide: String, useValue: 'Message 1'}],
|
|
|
|
viewProviders: [{provide: String, useValue: 'Message 2'}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('Message 2');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('last declared directive should override other directives', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
directive2Providers: [{provide: String, useValue: 'Message 1'}],
|
|
|
|
directiveProviders: [{provide: String, useValue: 'Message 2'}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('Message 2');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('last provider should override previous one in component providers', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers:
|
|
|
|
[{provide: String, useValue: 'Message 1'}, {provide: String, useValue: 'Message 2'}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('Message 2');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('last provider should override previous one in component view providers', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
viewProviders:
|
|
|
|
[{provide: String, useValue: 'Message 1'}, {provide: String, useValue: 'Message 2'}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('Message 2');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('last provider should override previous one in directive providers', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
directiveProviders:
|
|
|
|
[{provide: String, useValue: 'Message 1'}, {provide: String, useValue: 'Message 2'}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('Message 2');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('single', () => {
|
|
|
|
class MyModule {
|
2019-10-14 18:28:01 -04:00
|
|
|
static ɵinj = ɵɵdefineInjector(
|
2018-11-12 21:40:08 -05:00
|
|
|
{factory: () => new MyModule(), providers: [{provide: String, useValue: 'From module'}]});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('without directives', () => {
|
|
|
|
it('should work without providers nor viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From module');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From module');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
viewChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From module');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From module');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
contentChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From module');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From module');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with only providers in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'From providers'}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From providers');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From providers');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
viewChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From providers');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From providers');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
contentChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From providers');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From providers');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with only viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
viewProviders: [{provide: String, useValue: 'From viewProviders'}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From viewProviders');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From module');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
viewChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From viewProviders');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From viewProviders');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
contentChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From module');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From module');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with both providers and viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'From providers'}],
|
|
|
|
viewProviders: [{provide: String, useValue: 'From viewProviders'}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From viewProviders');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From providers');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
viewChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From viewProviders');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From viewProviders');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
contentChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From providers');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From providers');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-10 17:57:15 -04:00
|
|
|
describe('with directives (order in ɵcmp.directives matters)', () => {
|
2018-11-12 21:40:08 -05:00
|
|
|
it('should work without providers nor viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
directiveProviders: [{provide: String, useValue: 'From directive'}],
|
|
|
|
directive2Providers: [{provide: String, useValue: 'Never'}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From directive');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From directive');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
viewChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From directive');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From directive');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
contentChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From directive');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From directive');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with only providers in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'From providers'}],
|
|
|
|
directiveProviders: [{provide: String, useValue: 'From directive'}],
|
|
|
|
directive2Providers: [{provide: String, useValue: 'Never'}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From directive');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From directive');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
viewChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From directive');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From directive');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
contentChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From directive');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From directive');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with only viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
viewProviders: [{provide: String, useValue: 'From viewProviders'}],
|
|
|
|
directiveProviders: [{provide: String, useValue: 'From directive'}],
|
|
|
|
directive2Providers: [{provide: String, useValue: 'Never'}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From viewProviders');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From directive');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
viewChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From viewProviders');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From viewProviders');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
contentChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From directive');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From directive');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with both providers and viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'From providers'}],
|
|
|
|
viewProviders: [{provide: String, useValue: 'From viewProviders'}],
|
|
|
|
directiveProviders: [{provide: String, useValue: 'From directive'}],
|
|
|
|
directive2Providers: [{provide: String, useValue: 'Never'}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From viewProviders');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From directive');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
viewChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From viewProviders');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From viewProviders');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
contentChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From directive');
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('From directive');
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('multi', () => {
|
|
|
|
class MyModule {
|
2019-10-14 18:28:01 -04:00
|
|
|
static ɵinj = ɵɵdefineInjector({
|
2018-11-12 21:40:08 -05:00
|
|
|
factory: () => new MyModule(),
|
|
|
|
providers: [{provide: String, useValue: 'From module', multi: true}]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('without directives', () => {
|
|
|
|
it('should work without providers nor viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From module']);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From module']);
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
viewChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From module']);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From module']);
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
contentChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From module']);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From module']);
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with only providers in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'From providers', multi: true}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From providers']);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From providers']);
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
viewChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From providers']);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From providers']);
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
contentChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From providers']);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From providers']);
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with only viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
viewProviders: [{provide: String, useValue: 'From viewProviders', multi: true}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From viewProviders']);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From module']);
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
viewChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From viewProviders']);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From viewProviders']);
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
contentChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From module']);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From module']);
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with both providers and viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'From providers', multi: true}],
|
|
|
|
viewProviders: [{provide: String, useValue: 'From viewProviders', multi: true}],
|
|
|
|
componentAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From providers', 'From viewProviders']);
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
2020-04-13 19:40:21 -04:00
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From providers']);
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From providers', 'From viewProviders']);
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From providers', 'From viewProviders']);
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
contentChild: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From providers']);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From providers']);
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-10 17:57:15 -04:00
|
|
|
describe('with directives (order in ɵcmp.directives matters)', () => {
|
2018-11-12 21:40:08 -05:00
|
|
|
it('should work without providers nor viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
directiveProviders: [{provide: String, useValue: 'From directive 1', multi: true}],
|
|
|
|
directive2Providers: [{provide: String, useValue: 'From directive 2', multi: true}],
|
|
|
|
componentAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From directive 2', 'From directive 1']);
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From directive 2', 'From directive 1']);
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From directive 2', 'From directive 1']);
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From directive 2', 'From directive 1']);
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From directive 2', 'From directive 1']);
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From directive 2', 'From directive 1']);
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with only providers in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'From providers', multi: true}],
|
|
|
|
directiveProviders: [{provide: String, useValue: 'From directive 1', multi: true}],
|
|
|
|
directive2Providers: [{provide: String, useValue: 'From directive 2', multi: true}],
|
|
|
|
componentAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual([
|
2018-11-12 21:40:08 -05:00
|
|
|
'From providers', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual([
|
2018-11-12 21:40:08 -05:00
|
|
|
'From providers', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual([
|
2018-11-12 21:40:08 -05:00
|
|
|
'From providers', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual([
|
2018-11-12 21:40:08 -05:00
|
|
|
'From providers', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual([
|
2018-11-12 21:40:08 -05:00
|
|
|
'From providers', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual([
|
2018-11-12 21:40:08 -05:00
|
|
|
'From providers', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with only viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
viewProviders: [{provide: String, useValue: 'From viewProviders', multi: true}],
|
|
|
|
directiveProviders: [{provide: String, useValue: 'From directive 1', multi: true}],
|
|
|
|
directive2Providers: [{provide: String, useValue: 'From directive 2', multi: true}],
|
|
|
|
componentAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual([
|
2018-11-12 21:40:08 -05:00
|
|
|
'From viewProviders', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From directive 2', 'From directive 1']);
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual([
|
2018-11-12 21:40:08 -05:00
|
|
|
'From viewProviders', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual([
|
2018-11-12 21:40:08 -05:00
|
|
|
'From viewProviders', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From directive 2', 'From directive 1']);
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual(['From directive 2', 'From directive 1']);
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with both providers and viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'From providers', multi: true}],
|
|
|
|
viewProviders: [{provide: String, useValue: 'From viewProviders', multi: true}],
|
|
|
|
directiveProviders: [{provide: String, useValue: 'From directive 1', multi: true}],
|
|
|
|
directive2Providers: [{provide: String, useValue: 'From directive 2', multi: true}],
|
|
|
|
componentAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual([
|
2018-11-12 21:40:08 -05:00
|
|
|
'From providers', 'From viewProviders', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual([
|
2018-11-12 21:40:08 -05:00
|
|
|
'From providers', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual([
|
2018-11-12 21:40:08 -05:00
|
|
|
'From providers', 'From viewProviders', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual([
|
2018-11-12 21:40:08 -05:00
|
|
|
'From providers', 'From viewProviders', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual([
|
2018-11-12 21:40:08 -05:00
|
|
|
'From providers', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual([
|
2018-11-12 21:40:08 -05:00
|
|
|
'From providers', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('tree-shakable injectables', () => {
|
|
|
|
it('should work with root', () => {
|
|
|
|
@Injectable({providedIn: 'root'})
|
|
|
|
class FooForRoot {
|
2019-10-15 15:41:30 -04:00
|
|
|
static ɵprov = ɵɵdefineInjectable({
|
2019-06-07 13:12:07 -04:00
|
|
|
token: FooForRoot,
|
|
|
|
factory: () => new FooForRoot(),
|
|
|
|
providedIn: 'root',
|
|
|
|
});
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(FooForRoot) instanceof FooForRoot).toBeTruthy();
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with a module', () => {
|
|
|
|
class MyModule {
|
2019-10-14 18:28:01 -04:00
|
|
|
static ɵinj = ɵɵdefineInjector({
|
2018-11-12 21:40:08 -05:00
|
|
|
factory: () => new MyModule(),
|
|
|
|
providers: [{provide: String, useValue: 'From module'}]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable({providedIn: MyModule})
|
|
|
|
class FooForModule {
|
2019-10-15 15:41:30 -04:00
|
|
|
static ɵprov = ɵɵdefineInjectable({
|
2019-06-07 13:12:07 -04:00
|
|
|
token: FooForModule,
|
|
|
|
factory: () => new FooForModule(),
|
|
|
|
providedIn: MyModule,
|
|
|
|
});
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
2019-05-17 21:49:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(FooForModule) instanceof FooForModule).toBeTruthy();
|
|
|
|
}
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-11-14 11:04:22 -05:00
|
|
|
describe('- dynamic components dependency resolution', () => {
|
|
|
|
let hostComponent: HostComponent|null = null;
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: `{{s}}`,
|
|
|
|
})
|
|
|
|
class EmbeddedComponent {
|
|
|
|
constructor(private s: String) {}
|
|
|
|
|
2019-10-11 17:18:45 -04:00
|
|
|
static ɵfac = () => new EmbeddedComponent(ɵɵdirectiveInject(String));
|
2019-10-10 17:57:15 -04:00
|
|
|
static ɵcmp = ɵɵdefineComponent({
|
2018-11-14 11:04:22 -05:00
|
|
|
type: EmbeddedComponent,
|
|
|
|
selectors: [['embedded-cmp']],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 1,
|
2018-11-14 11:04:22 -05:00
|
|
|
vars: 1,
|
2020-04-13 19:40:21 -04:00
|
|
|
template:
|
|
|
|
(rf: RenderFlags, cmp: EmbeddedComponent) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
|
|
|
ɵɵtext(0);
|
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
ɵɵtextInterpolate1('', cmp.s, '');
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 11:04:22 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({template: `foo`, providers: [{provide: String, useValue: 'From host component'}]})
|
|
|
|
class HostComponent {
|
|
|
|
constructor(public vcref: ViewContainerRef, public cfr: ComponentFactoryResolver) {}
|
|
|
|
|
2019-10-11 17:18:45 -04:00
|
|
|
static ɵfac = () => hostComponent = new HostComponent(
|
2019-08-12 02:26:20 -04:00
|
|
|
ɵɵdirectiveInject(ViewContainerRef as any), injectComponentFactoryResolver())
|
|
|
|
|
2019-10-10 17:57:15 -04:00
|
|
|
static ɵcmp = ɵɵdefineComponent({
|
2019-08-12 02:26:20 -04:00
|
|
|
type: HostComponent,
|
|
|
|
selectors: [['host-cmp']],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 1,
|
2019-08-12 02:26:20 -04:00
|
|
|
vars: 0,
|
2020-04-13 19:40:21 -04:00
|
|
|
template:
|
|
|
|
(rf: RenderFlags, cmp: HostComponent) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
|
|
|
ɵɵtext(0, 'foo');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
features:
|
|
|
|
[
|
|
|
|
ɵɵProvidersFeature([{provide: String, useValue: 'From host component'}]),
|
|
|
|
],
|
2019-08-12 02:26:20 -04:00
|
|
|
});
|
2018-11-14 11:04:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: `<host-cmp></host-cmp>`,
|
|
|
|
providers: [{provide: String, useValue: 'From app component'}]
|
|
|
|
})
|
|
|
|
class AppComponent {
|
|
|
|
constructor() {}
|
|
|
|
|
2019-10-11 17:18:45 -04:00
|
|
|
static ɵfac = () => new AppComponent();
|
2019-10-10 17:57:15 -04:00
|
|
|
static ɵcmp = ɵɵdefineComponent({
|
2018-11-14 11:04:22 -05:00
|
|
|
type: AppComponent,
|
|
|
|
selectors: [['app-cmp']],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 1,
|
2018-11-14 11:04:22 -05:00
|
|
|
vars: 0,
|
2020-04-13 19:40:21 -04:00
|
|
|
template:
|
|
|
|
(rf: RenderFlags, cmp: AppComponent) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
|
|
|
ɵɵelement(0, 'host-cmp');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
features:
|
|
|
|
[
|
|
|
|
ɵɵProvidersFeature([{provide: String, useValue: 'From app component'}]),
|
|
|
|
],
|
2018-11-14 11:04:22 -05:00
|
|
|
directives: [HostComponent]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should not cross the root view boundary, and use the root view injector', () => {
|
|
|
|
const fixture = new ComponentFixture(AppComponent);
|
|
|
|
expect(fixture.html).toEqual('<host-cmp>foo</host-cmp>');
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
hostComponent!.vcref.createComponent(
|
|
|
|
hostComponent!.cfr.resolveComponentFactory(EmbeddedComponent), undefined, {
|
2018-11-14 11:04:22 -05:00
|
|
|
get: (token: any, notFoundValue?: any) => {
|
|
|
|
return token === String ? 'From custom root view injector' : notFoundValue;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
fixture.update();
|
|
|
|
expect(fixture.html)
|
|
|
|
.toEqual(
|
|
|
|
'<host-cmp>foo</host-cmp><embedded-cmp>From custom root view injector</embedded-cmp>');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not cross the root view boundary, and use the module injector if no root view injector',
|
|
|
|
() => {
|
|
|
|
const fixture = new ComponentFixture(AppComponent);
|
|
|
|
expect(fixture.html).toEqual('<host-cmp>foo</host-cmp>');
|
|
|
|
|
|
|
|
class MyAppModule {
|
2019-10-14 18:28:01 -04:00
|
|
|
static ɵinj = ɵɵdefineInjector({
|
2018-11-14 11:04:22 -05:00
|
|
|
factory: () => new MyAppModule(),
|
|
|
|
imports: [],
|
2020-04-13 19:40:21 -04:00
|
|
|
providers:
|
|
|
|
[
|
|
|
|
{provide: RendererFactory2, useValue: getRendererFactory2(document)},
|
|
|
|
{provide: String, useValue: 'From module injector'}
|
|
|
|
]
|
2018-11-14 11:04:22 -05:00
|
|
|
});
|
2020-10-30 15:31:47 -04:00
|
|
|
static ɵmod = ɵɵdefineNgModule({type: MyAppModule});
|
2018-11-14 11:04:22 -05:00
|
|
|
}
|
|
|
|
const myAppModuleFactory = new NgModuleFactory(MyAppModule);
|
|
|
|
const ngModuleRef = myAppModuleFactory.create(null);
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
hostComponent!.vcref.createComponent(
|
|
|
|
hostComponent!.cfr.resolveComponentFactory(EmbeddedComponent), undefined,
|
2018-11-14 11:04:22 -05:00
|
|
|
{get: (token: any, notFoundValue?: any) => notFoundValue}, undefined, ngModuleRef);
|
|
|
|
fixture.update();
|
|
|
|
expect(fixture.html)
|
|
|
|
.toMatch(
|
2017-06-24 15:09:38 -04:00
|
|
|
/<host-cmp>foo<\/host-cmp><embedded-cmp _nghost-[a-z]+-c(\d+)="">From module injector<\/embedded-cmp>/);
|
2018-11-14 11:04:22 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should cross the root view boundary to the parent of the host, thanks to the default root view injector',
|
|
|
|
() => {
|
|
|
|
const fixture = new ComponentFixture(AppComponent);
|
|
|
|
expect(fixture.html).toEqual('<host-cmp>foo</host-cmp>');
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
hostComponent!.vcref.createComponent(
|
|
|
|
hostComponent!.cfr.resolveComponentFactory(EmbeddedComponent));
|
2018-11-14 11:04:22 -05:00
|
|
|
fixture.update();
|
|
|
|
expect(fixture.html)
|
|
|
|
.toEqual('<host-cmp>foo</host-cmp><embedded-cmp>From app component</embedded-cmp>');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-11-12 21:40:08 -05:00
|
|
|
describe('deps boundary:', () => {
|
|
|
|
it('the deps of a token declared in providers should not be resolved with tokens from viewProviders',
|
|
|
|
() => {
|
|
|
|
@Injectable()
|
|
|
|
class MyService {
|
|
|
|
constructor(public value: String) {}
|
|
|
|
|
2019-10-15 15:41:30 -04:00
|
|
|
static ɵprov = ɵɵdefineInjectable({
|
2019-06-07 13:12:07 -04:00
|
|
|
token: MyService,
|
|
|
|
factory: () => new MyService(ɵɵinject(String)),
|
|
|
|
});
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [MyService, {provide: String, useValue: 'providers'}],
|
|
|
|
viewProviders: [{provide: String, useValue: 'viewProviders'}],
|
|
|
|
componentAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('viewProviders');
|
|
|
|
expect(ɵɵdirectiveInject(MyService).value).toEqual('providers');
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should make sure that parent service does not see overrides in child directives', () => {
|
|
|
|
class Greeter {
|
2019-10-15 15:41:30 -04:00
|
|
|
static ɵprov = ɵɵdefineInjectable({
|
2019-06-07 13:12:07 -04:00
|
|
|
token: Greeter,
|
|
|
|
factory: () => new Greeter(ɵɵinject(String)),
|
|
|
|
});
|
2018-11-12 21:40:08 -05:00
|
|
|
constructor(public greeting: String) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [Greeter, {provide: String, useValue: 'parent'}],
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
providers: [{provide: String, useValue: 'view'}],
|
2020-04-13 19:40:21 -04:00
|
|
|
componentAssertion: () => {
|
|
|
|
expect(ɵɵdirectiveInject(Greeter).greeting).toEqual('parent');
|
|
|
|
},
|
2018-11-12 21:40:08 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('injection flags', () => {
|
|
|
|
class MyModule {
|
2019-10-14 18:28:01 -04:00
|
|
|
static ɵinj = ɵɵdefineInjector(
|
2018-11-12 21:40:08 -05:00
|
|
|
{factory: () => new MyModule(), providers: [{provide: String, useValue: 'Module'}]});
|
|
|
|
}
|
|
|
|
it('should not fall through to ModuleInjector if flags limit the scope', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
ngModule: MyModule,
|
|
|
|
parent: {
|
|
|
|
componentAssertion: () => {
|
2019-05-17 21:49:21 -04:00
|
|
|
expect(ɵɵdirectiveInject(String)).toEqual('Module');
|
|
|
|
expect(ɵɵdirectiveInject(String, InjectFlags.Optional | InjectFlags.Self)).toBeNull();
|
|
|
|
expect(ɵɵdirectiveInject(String, InjectFlags.Optional | InjectFlags.Host)).toBeNull();
|
2018-11-12 21:40:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-11-16 09:25:38 -05:00
|
|
|
describe('from a node without injector', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
abstract class Some {
|
|
|
|
abstract location: String;
|
|
|
|
}
|
2018-11-16 09:25:38 -05:00
|
|
|
|
|
|
|
class SomeInj implements Some {
|
|
|
|
constructor(public location: String) {}
|
|
|
|
|
2019-10-15 15:41:30 -04:00
|
|
|
static ɵprov = ɵɵdefineInjectable({
|
2019-06-07 13:12:07 -04:00
|
|
|
token: SomeInj,
|
|
|
|
factory: () => new SomeInj(ɵɵinject(String)),
|
|
|
|
});
|
2018-11-16 09:25:38 -05:00
|
|
|
}
|
|
|
|
|
2019-01-04 09:38:40 -05:00
|
|
|
@Component({
|
|
|
|
template: `<p></p>`,
|
|
|
|
providers: [{provide: String, useValue: 'From my component'}],
|
|
|
|
viewProviders: [{provide: Number, useValue: 123}]
|
|
|
|
})
|
2018-11-16 09:25:38 -05:00
|
|
|
class MyComponent {
|
|
|
|
constructor() {}
|
|
|
|
|
2019-10-11 17:18:45 -04:00
|
|
|
static ɵfac = () => new MyComponent();
|
2019-10-10 17:57:15 -04:00
|
|
|
static ɵcmp = ɵɵdefineComponent({
|
2018-11-16 09:25:38 -05:00
|
|
|
type: MyComponent,
|
|
|
|
selectors: [['my-cmp']],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 1,
|
2018-11-16 09:25:38 -05:00
|
|
|
vars: 0,
|
2020-04-13 19:40:21 -04:00
|
|
|
template:
|
|
|
|
(rf: RenderFlags, cmp: MyComponent) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
|
|
|
ɵɵelement(0, 'p');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
features:
|
|
|
|
[
|
|
|
|
ɵɵProvidersFeature(
|
|
|
|
[{provide: String, useValue: 'From my component'}],
|
|
|
|
[{provide: Number, useValue: 123}]),
|
|
|
|
],
|
2018-11-16 09:25:38 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: `<my-cmp></my-cmp>`,
|
|
|
|
providers:
|
|
|
|
[{provide: String, useValue: 'From app component'}, {provide: Some, useClass: SomeInj}]
|
|
|
|
})
|
|
|
|
class AppComponent {
|
|
|
|
constructor() {}
|
|
|
|
|
2019-10-11 17:18:45 -04:00
|
|
|
static ɵfac = () => new AppComponent();
|
2019-10-10 17:57:15 -04:00
|
|
|
static ɵcmp = ɵɵdefineComponent({
|
2018-11-16 09:25:38 -05:00
|
|
|
type: AppComponent,
|
|
|
|
selectors: [['app-cmp']],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 1,
|
2018-11-16 09:25:38 -05:00
|
|
|
vars: 0,
|
2020-04-13 19:40:21 -04:00
|
|
|
template:
|
|
|
|
(rf: RenderFlags, cmp: AppComponent) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
|
|
|
ɵɵelement(0, 'my-cmp');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
features:
|
|
|
|
[
|
|
|
|
ɵɵProvidersFeature([
|
|
|
|
{provide: String, useValue: 'From app component'},
|
|
|
|
{provide: Some, useClass: SomeInj}
|
|
|
|
]),
|
|
|
|
],
|
2018-11-16 09:25:38 -05:00
|
|
|
directives: [MyComponent]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-04 09:38:40 -05:00
|
|
|
it('should work from within the template', () => {
|
2018-11-16 09:25:38 -05:00
|
|
|
const fixture = new ComponentFixture(AppComponent);
|
|
|
|
expect(fixture.html).toEqual('<my-cmp><p></p></my-cmp>');
|
|
|
|
|
|
|
|
const p = fixture.hostElement.querySelector('p');
|
|
|
|
const injector = getInjector(p as any);
|
2019-01-04 09:38:40 -05:00
|
|
|
expect(injector.get(Number)).toEqual(123);
|
|
|
|
expect(injector.get(String)).toEqual('From my component');
|
|
|
|
expect(injector.get(Some).location).toEqual('From app component');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work from the host of the component', () => {
|
|
|
|
const fixture = new ComponentFixture(AppComponent);
|
|
|
|
expect(fixture.html).toEqual('<my-cmp><p></p></my-cmp>');
|
|
|
|
|
|
|
|
const myCmp = fixture.hostElement.querySelector('my-cmp');
|
|
|
|
const injector = getInjector(myCmp as any);
|
|
|
|
expect(injector.get(Number)).toEqual(123);
|
2018-11-16 09:25:38 -05:00
|
|
|
expect(injector.get(String)).toEqual('From my component');
|
|
|
|
expect(injector.get(Some).location).toEqual('From app component');
|
|
|
|
});
|
|
|
|
});
|
2020-05-09 08:33:54 -04:00
|
|
|
|
|
|
|
// Note: these tests check the behavior of `getInheritedFactory` specifically.
|
|
|
|
// Since `getInheritedFactory` is only generated in AOT, the tests can't be
|
|
|
|
// ported directly to TestBed while running in JIT mode.
|
|
|
|
describe('getInheritedFactory on class with custom decorator', () => {
|
|
|
|
function addFoo() {
|
|
|
|
return (constructor: Type<any>): any => {
|
|
|
|
const decoratedClass = class Extender extends constructor { foo = 'bar'; };
|
|
|
|
return decoratedClass;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should find the correct factories if a parent class has a custom decorator', () => {
|
|
|
|
class GrandParent {
|
|
|
|
static ɵfac = function GrandParent_Factory() {};
|
|
|
|
}
|
|
|
|
|
|
|
|
@addFoo()
|
|
|
|
class Parent extends GrandParent {
|
|
|
|
static ɵfac = function Parent_Factory() {};
|
|
|
|
}
|
|
|
|
|
|
|
|
class Child extends Parent {
|
|
|
|
static ɵfac = function Child_Factory() {};
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(ɵɵgetInheritedFactory(Child).name).toBe('Parent_Factory');
|
|
|
|
expect(ɵɵgetInheritedFactory(Parent).name).toBe('GrandParent_Factory');
|
|
|
|
expect(ɵɵgetInheritedFactory(GrandParent).name).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find the correct factories if a child class has a custom decorator', () => {
|
|
|
|
class GrandParent {
|
|
|
|
static ɵfac = function GrandParent_Factory() {};
|
|
|
|
}
|
|
|
|
|
|
|
|
class Parent extends GrandParent {
|
|
|
|
static ɵfac = function Parent_Factory() {};
|
|
|
|
}
|
|
|
|
|
|
|
|
@addFoo()
|
|
|
|
class Child extends Parent {
|
|
|
|
static ɵfac = function Child_Factory() {};
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(ɵɵgetInheritedFactory(Child).name).toBe('Parent_Factory');
|
|
|
|
expect(ɵɵgetInheritedFactory(Parent).name).toBe('GrandParent_Factory');
|
|
|
|
expect(ɵɵgetInheritedFactory(GrandParent).name).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find the correct factories if a grandparent class has a custom decorator', () => {
|
|
|
|
@addFoo()
|
|
|
|
class GrandParent {
|
|
|
|
static ɵfac = function GrandParent_Factory() {};
|
|
|
|
}
|
|
|
|
|
|
|
|
class Parent extends GrandParent {
|
|
|
|
static ɵfac = function Parent_Factory() {};
|
|
|
|
}
|
|
|
|
|
|
|
|
class Child extends Parent {
|
|
|
|
static ɵfac = function Child_Factory() {};
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(ɵɵgetInheritedFactory(Child).name).toBe('Parent_Factory');
|
|
|
|
expect(ɵɵgetInheritedFactory(Parent).name).toBe('GrandParent_Factory');
|
|
|
|
expect(ɵɵgetInheritedFactory(GrandParent).name).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find the correct factories if all classes have a custom decorator', () => {
|
|
|
|
@addFoo()
|
|
|
|
class GrandParent {
|
|
|
|
static ɵfac = function GrandParent_Factory() {};
|
|
|
|
}
|
|
|
|
|
|
|
|
@addFoo()
|
|
|
|
class Parent extends GrandParent {
|
|
|
|
static ɵfac = function Parent_Factory() {};
|
|
|
|
}
|
|
|
|
|
|
|
|
@addFoo()
|
|
|
|
class Child extends Parent {
|
|
|
|
static ɵfac = function Child_Factory() {};
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(ɵɵgetInheritedFactory(Child).name).toBe('Parent_Factory');
|
|
|
|
expect(ɵɵgetInheritedFactory(Parent).name).toBe('GrandParent_Factory');
|
|
|
|
expect(ɵɵgetInheritedFactory(GrandParent).name).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find the correct factories if parent and grandparent classes have a custom decorator',
|
|
|
|
() => {
|
|
|
|
@addFoo()
|
|
|
|
class GrandParent {
|
|
|
|
static ɵfac = function GrandParent_Factory() {};
|
|
|
|
}
|
|
|
|
|
|
|
|
@addFoo()
|
|
|
|
class Parent extends GrandParent {
|
|
|
|
static ɵfac = function Parent_Factory() {};
|
|
|
|
}
|
|
|
|
|
|
|
|
class Child extends Parent {
|
|
|
|
static ɵfac = function Child_Factory() {};
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(ɵɵgetInheritedFactory(Child).name).toBe('Parent_Factory');
|
|
|
|
expect(ɵɵgetInheritedFactory(Parent).name).toBe('GrandParent_Factory');
|
|
|
|
expect(ɵɵgetInheritedFactory(GrandParent).name).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
2018-11-12 21:40:08 -05:00
|
|
|
});
|
2020-05-09 08:33:54 -04:00
|
|
|
|
2018-11-12 21:40:08 -05:00
|
|
|
interface ComponentTest {
|
|
|
|
providers?: Provider[];
|
|
|
|
viewProviders?: Provider[];
|
|
|
|
directiveProviders?: Provider[];
|
|
|
|
directive2Providers?: Provider[];
|
|
|
|
directiveAssertion?: () => void;
|
|
|
|
componentAssertion?: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectProvidersScenario(defs: {
|
|
|
|
app?: ComponentTest,
|
|
|
|
parent?: ComponentTest,
|
|
|
|
viewChild?: ComponentTest,
|
|
|
|
contentChild?: ComponentTest,
|
|
|
|
ngModule?: InjectorType<any>,
|
|
|
|
}): void {
|
2020-04-13 19:40:21 -04:00
|
|
|
function testComponentInjection<T>(def: ComponentTest|undefined, instance: T): T {
|
2018-11-12 21:40:08 -05:00
|
|
|
if (def) {
|
|
|
|
def.componentAssertion && def.componentAssertion();
|
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
function testDirectiveInjection<T>(def: ComponentTest|undefined, instance: T): T {
|
2018-11-12 21:40:08 -05:00
|
|
|
if (def) {
|
|
|
|
def.directiveAssertion && def.directiveAssertion();
|
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ViewChildComponent {
|
2019-10-11 17:18:45 -04:00
|
|
|
static ɵfac = () => testComponentInjection(defs.viewChild, new ViewChildComponent());
|
2019-10-10 17:57:15 -04:00
|
|
|
static ɵcmp = ɵɵdefineComponent({
|
2018-11-12 21:40:08 -05:00
|
|
|
type: ViewChildComponent,
|
|
|
|
selectors: [['view-child']],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 1,
|
2018-11-12 21:40:08 -05:00
|
|
|
vars: 0,
|
2020-04-13 19:40:21 -04:00
|
|
|
template:
|
|
|
|
function(fs: RenderFlags, ctx: ViewChildComponent) {
|
|
|
|
if (fs & RenderFlags.Create) {
|
|
|
|
ɵɵtext(0, 'view-child');
|
|
|
|
}
|
|
|
|
},
|
2018-11-12 21:40:08 -05:00
|
|
|
features: defs.viewChild &&
|
2019-08-12 02:26:20 -04:00
|
|
|
[ɵɵProvidersFeature(defs.viewChild.providers || [], defs.viewChild.viewProviders || [])]
|
2018-11-12 21:40:08 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class ViewChildDirective {
|
2019-10-11 17:18:45 -04:00
|
|
|
static ɵfac = () => testDirectiveInjection(defs.viewChild, new ViewChildDirective());
|
2019-10-11 15:28:12 -04:00
|
|
|
static ɵdir = ɵɵdefineDirective({
|
2018-11-12 21:40:08 -05:00
|
|
|
type: ViewChildDirective,
|
|
|
|
selectors: [['view-child']],
|
2019-05-17 21:49:21 -04:00
|
|
|
features: defs.viewChild && [ɵɵProvidersFeature(defs.viewChild.directiveProviders || [])],
|
2018-11-12 21:40:08 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class ContentChildComponent {
|
2019-10-11 17:18:45 -04:00
|
|
|
static ɵfac =
|
2020-04-13 19:40:21 -04:00
|
|
|
() => {
|
|
|
|
return testComponentInjection(defs.contentChild, new ContentChildComponent());
|
|
|
|
}
|
2019-08-12 02:26:20 -04:00
|
|
|
|
2019-10-10 17:57:15 -04:00
|
|
|
static ɵcmp = ɵɵdefineComponent({
|
2018-11-12 21:40:08 -05:00
|
|
|
type: ContentChildComponent,
|
|
|
|
selectors: [['content-child']],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 1,
|
2018-11-12 21:40:08 -05:00
|
|
|
vars: 0,
|
2020-04-13 19:40:21 -04:00
|
|
|
template:
|
|
|
|
function(fs: RenderFlags, ctx: ParentComponent) {
|
|
|
|
if (fs & RenderFlags.Create) {
|
|
|
|
ɵɵtext(0, 'content-child');
|
|
|
|
}
|
|
|
|
},
|
2018-11-12 21:40:08 -05:00
|
|
|
features: defs.contentChild &&
|
2019-05-17 21:49:21 -04:00
|
|
|
[ɵɵProvidersFeature(
|
2018-11-12 21:40:08 -05:00
|
|
|
defs.contentChild.providers || [], defs.contentChild.viewProviders || [])],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class ContentChildDirective {
|
2019-10-11 17:18:45 -04:00
|
|
|
static ɵfac =
|
2020-04-13 19:40:21 -04:00
|
|
|
() => {
|
|
|
|
return testDirectiveInjection(defs.contentChild, new ContentChildDirective());
|
|
|
|
}
|
2019-08-12 02:26:20 -04:00
|
|
|
|
2019-10-11 15:28:12 -04:00
|
|
|
static ɵdir = ɵɵdefineDirective({
|
2018-11-12 21:40:08 -05:00
|
|
|
type: ContentChildDirective,
|
|
|
|
selectors: [['content-child']],
|
2020-04-13 19:40:21 -04:00
|
|
|
features: defs.contentChild &&
|
|
|
|
[ɵɵProvidersFeature(defs.contentChild.directiveProviders || [])],
|
2018-11-12 21:40:08 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class ParentComponent {
|
2019-10-11 17:18:45 -04:00
|
|
|
static ɵfac = () => testComponentInjection(defs.parent, new ParentComponent());
|
2019-10-10 17:57:15 -04:00
|
|
|
static ɵcmp = ɵɵdefineComponent({
|
2018-11-12 21:40:08 -05:00
|
|
|
type: ParentComponent,
|
|
|
|
selectors: [['parent']],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 1,
|
2018-11-12 21:40:08 -05:00
|
|
|
vars: 0,
|
2020-04-13 19:40:21 -04:00
|
|
|
template:
|
|
|
|
function(fs: RenderFlags, ctx: ParentComponent) {
|
|
|
|
if (fs & RenderFlags.Create) {
|
|
|
|
ɵɵelement(0, 'view-child');
|
|
|
|
}
|
|
|
|
},
|
2018-11-12 21:40:08 -05:00
|
|
|
features: defs.parent &&
|
2019-05-17 21:49:21 -04:00
|
|
|
[ɵɵProvidersFeature(defs.parent.providers || [], defs.parent.viewProviders || [])],
|
2018-11-12 21:40:08 -05:00
|
|
|
directives: [ViewChildComponent, ViewChildDirective]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class ParentDirective {
|
2019-10-11 17:18:45 -04:00
|
|
|
static ɵfac = () => testDirectiveInjection(defs.parent, new ParentDirective());
|
2019-10-11 15:28:12 -04:00
|
|
|
static ɵdir = ɵɵdefineDirective({
|
2018-11-12 21:40:08 -05:00
|
|
|
type: ParentDirective,
|
|
|
|
selectors: [['parent']],
|
2019-05-17 21:49:21 -04:00
|
|
|
features: defs.parent && [ɵɵProvidersFeature(defs.parent.directiveProviders || [])],
|
2018-11-12 21:40:08 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class ParentDirective2 {
|
2019-10-11 17:18:45 -04:00
|
|
|
static ɵfac = () => testDirectiveInjection(defs.parent, new ParentDirective2());
|
2019-10-11 15:28:12 -04:00
|
|
|
static ɵdir = ɵɵdefineDirective({
|
2018-11-12 21:40:08 -05:00
|
|
|
type: ParentDirective2,
|
|
|
|
selectors: [['parent']],
|
2019-05-17 21:49:21 -04:00
|
|
|
features: defs.parent && [ɵɵProvidersFeature(defs.parent.directive2Providers || [])],
|
2018-11-12 21:40:08 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class App {
|
2019-10-11 17:18:45 -04:00
|
|
|
static ɵfac = () => testComponentInjection(defs.app, new App());
|
2019-10-10 17:57:15 -04:00
|
|
|
static ɵcmp = ɵɵdefineComponent({
|
2018-11-12 21:40:08 -05:00
|
|
|
type: App,
|
|
|
|
selectors: [['app']],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 2,
|
2018-11-12 21:40:08 -05:00
|
|
|
vars: 0,
|
2020-04-13 19:40:21 -04:00
|
|
|
template:
|
|
|
|
function(fs: RenderFlags, ctx: App) {
|
|
|
|
if (fs & RenderFlags.Create) {
|
|
|
|
ɵɵelementStart(0, 'parent');
|
|
|
|
ɵɵelement(1, 'content-child');
|
|
|
|
ɵɵelementEnd();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
features: defs.app &&
|
|
|
|
[ɵɵProvidersFeature(defs.app.providers || [], defs.app.viewProviders || [])],
|
|
|
|
directives:
|
|
|
|
[
|
|
|
|
ParentComponent, ParentDirective2, ParentDirective, ContentChildComponent,
|
|
|
|
ContentChildDirective
|
|
|
|
]
|
2018-11-12 21:40:08 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const fixture = new ComponentFixture(
|
|
|
|
App, {injector: defs.ngModule ? createInjector(defs.ngModule) : undefined});
|
|
|
|
expect(fixture.html).toEqual('<parent><view-child>view-child</view-child></parent>');
|
|
|
|
}
|