2018-04-12 15:30:21 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2018-07-12 18:10:55 -04:00
|
|
|
import {MockDirectory, setup} from '@angular/compiler/test/aot/test_util';
|
2018-04-12 15:30:21 -04:00
|
|
|
import {compile, expectEmit} from './mock_compile';
|
|
|
|
|
|
|
|
describe('compiler compliance: dependency injection', () => {
|
|
|
|
const angularFiles = setup({
|
2018-08-01 03:52:34 -04:00
|
|
|
compileAngular: false,
|
|
|
|
compileFakeCore: true,
|
2018-04-12 15:30:21 -04:00
|
|
|
compileAnimations: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create factory methods', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, Injectable, Attribute, Host, SkipSelf, Self, Optional} from '@angular/core';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class MyService {}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`\`
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
constructor(
|
|
|
|
@Attribute('name') name:string,
|
2019-04-04 14:41:52 -04:00
|
|
|
s1: MyService,
|
2018-04-12 15:30:21 -04:00
|
|
|
@Host() s2: MyService,
|
|
|
|
@Self() s4: MyService,
|
|
|
|
@SkipSelf() s3: MyService,
|
|
|
|
@Optional() s5: MyService,
|
|
|
|
@Self() @Optional() s6: MyService,
|
|
|
|
) {}
|
|
|
|
}
|
|
|
|
|
2018-08-01 03:52:34 -04:00
|
|
|
@NgModule({declarations: [MyComponent], providers: [MyService]})
|
2018-04-12 15:30:21 -04:00
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const factory = `
|
2019-10-11 17:18:45 -04:00
|
|
|
MyComponent.ɵfac = function MyComponent_Factory(t) {
|
2018-07-16 19:36:31 -04:00
|
|
|
return new (t || MyComponent)(
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵinjectAttribute('name'),
|
|
|
|
$r3$.ɵɵdirectiveInject(MyService),
|
|
|
|
$r3$.ɵɵdirectiveInject(MyService, 1),
|
|
|
|
$r3$.ɵɵdirectiveInject(MyService, 2),
|
|
|
|
$r3$.ɵɵdirectiveInject(MyService, 4),
|
|
|
|
$r3$.ɵɵdirectiveInject(MyService, 8),
|
|
|
|
$r3$.ɵɵdirectiveInject(MyService, 10)
|
2018-04-12 15:30:21 -04:00
|
|
|
);
|
|
|
|
}`;
|
|
|
|
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
|
|
|
|
expectEmit(result.source, factory, 'Incorrect factory');
|
|
|
|
});
|
|
|
|
|
2019-09-01 06:26:04 -04:00
|
|
|
it('should create a factory definition for an injectable', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
|
|
|
|
class MyDependency {}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class MyService {
|
|
|
|
constructor(dep: MyDependency) {}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const factory = `
|
2019-10-11 17:18:45 -04:00
|
|
|
MyService.ɵfac = function MyService_Factory(t) {
|
2019-09-01 06:26:04 -04:00
|
|
|
return new (t || MyService)($r3$.ɵɵinject(MyDependency));
|
|
|
|
}`;
|
|
|
|
|
|
|
|
const def = `
|
2019-10-15 15:41:30 -04:00
|
|
|
MyService.ɵprov = $r3$.ɵɵdefineInjectable({
|
2019-09-01 06:26:04 -04:00
|
|
|
token: MyService,
|
2019-11-27 19:25:47 -05:00
|
|
|
factory: MyService.ɵfac
|
2019-09-01 06:26:04 -04:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, factory, 'Incorrect factory definition');
|
|
|
|
expectEmit(result.source, def, 'Incorrect injectable definition');
|
|
|
|
});
|
|
|
|
|
2019-12-29 03:50:19 -05:00
|
|
|
it('should create a factory definition for an injectable with an overloaded constructor', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Injectable, Optional} from '@angular/core';
|
|
|
|
|
|
|
|
class MyDependency {}
|
|
|
|
class MyOptionalDependency {}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class MyService {
|
|
|
|
constructor(dep: MyDependency);
|
|
|
|
constructor(dep: MyDependency, @Optional() optionalDep?: MyOptionalDependency) {}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const factory = `
|
|
|
|
MyService.ɵfac = function MyService_Factory(t) {
|
|
|
|
return new (t || MyService)($r3$.ɵɵinject(MyDependency), $r3$.ɵɵinject(MyOptionalDependency, 8));
|
|
|
|
}`;
|
|
|
|
|
|
|
|
const def = `
|
|
|
|
MyService.ɵprov = $r3$.ɵɵdefineInjectable({
|
|
|
|
token: MyService,
|
|
|
|
factory: MyService.ɵfac
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, factory, 'Incorrect factory definition');
|
|
|
|
expectEmit(result.source, def, 'Incorrect injectable definition');
|
|
|
|
});
|
|
|
|
|
2019-10-11 17:18:45 -04:00
|
|
|
it('should create a single factory def if the class has more than one decorator', () => {
|
2019-09-01 06:26:04 -04:00
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Injectable, Pipe} from '@angular/core';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
@Pipe({name: 'my-pipe'})
|
|
|
|
export class MyPipe {
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles).source;
|
2019-10-11 17:18:45 -04:00
|
|
|
const matches = result.match(/MyPipe\.ɵfac = function MyPipe_Factory/g);
|
2019-09-01 06:26:04 -04:00
|
|
|
expect(matches ? matches.length : 0).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should delegate directly to the alternate factory when setting `useFactory` without `deps`',
|
|
|
|
() => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
|
|
|
|
class MyAlternateService {}
|
|
|
|
|
|
|
|
function alternateFactory() {
|
|
|
|
return new MyAlternateService();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
useFactory: alternateFactory
|
|
|
|
})
|
|
|
|
export class MyService {
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const def = `
|
2019-10-15 15:41:30 -04:00
|
|
|
MyService.ɵprov = $r3$.ɵɵdefineInjectable({
|
2019-09-01 06:26:04 -04:00
|
|
|
token: MyService,
|
|
|
|
factory: function() {
|
|
|
|
return alternateFactory();
|
2019-11-27 19:25:47 -05:00
|
|
|
}
|
2019-09-01 06:26:04 -04:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, def, 'Incorrect injectable definition');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not delegate directly to the alternate factory when setting `useFactory` with `deps`',
|
|
|
|
() => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
|
|
|
|
class SomeDep {}
|
|
|
|
class MyAlternateService {}
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
useFactory: () => new MyAlternateFactory(),
|
|
|
|
deps: [SomeDep]
|
|
|
|
})
|
|
|
|
export class MyService {
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const def = `
|
2019-10-15 15:41:30 -04:00
|
|
|
MyService.ɵprov = $r3$.ɵɵdefineInjectable({
|
2019-09-01 06:26:04 -04:00
|
|
|
token: MyService,
|
|
|
|
factory: function MyService_Factory(t) {
|
|
|
|
var r = null;
|
|
|
|
if (t) {
|
2019-10-30 19:09:38 -04:00
|
|
|
r = new t();
|
2019-09-01 06:26:04 -04:00
|
|
|
} else {
|
2019-10-30 19:09:38 -04:00
|
|
|
r = (() => new MyAlternateFactory())($r3$.ɵɵinject(SomeDep));
|
2019-09-01 06:26:04 -04:00
|
|
|
}
|
|
|
|
return r;
|
2019-11-27 19:25:47 -05:00
|
|
|
}
|
2019-09-01 06:26:04 -04:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, def, 'Incorrect injectable definition');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should delegate directly to the alternate class factory when setting `useClass` without `deps`',
|
|
|
|
() => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
class MyAlternateService {}
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
useClass: MyAlternateService
|
|
|
|
})
|
|
|
|
export class MyService {
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const factory = `
|
2019-10-15 15:41:30 -04:00
|
|
|
MyService.ɵprov = $r3$.ɵɵdefineInjectable({
|
2019-09-01 06:26:04 -04:00
|
|
|
token: MyService,
|
|
|
|
factory: function(t) {
|
2019-10-11 17:18:45 -04:00
|
|
|
return MyAlternateService.ɵfac(t);
|
2019-11-27 19:25:47 -05:00
|
|
|
}
|
2019-09-01 06:26:04 -04:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, factory, 'Incorrect factory definition');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not delegate directly to the alternate class when setting `useClass` with `deps`',
|
|
|
|
() => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
|
|
|
|
class SomeDep {}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
class MyAlternateService {}
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
useClass: MyAlternateService,
|
|
|
|
deps: [SomeDep]
|
|
|
|
})
|
|
|
|
export class MyService {
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const factory = `
|
2019-10-15 15:41:30 -04:00
|
|
|
MyService.ɵprov = $r3$.ɵɵdefineInjectable({
|
2019-09-01 06:26:04 -04:00
|
|
|
token: MyService,
|
|
|
|
factory: function MyService_Factory(t) {
|
|
|
|
var r = null;
|
|
|
|
if (t) {
|
2019-10-30 19:09:38 -04:00
|
|
|
r = new t();
|
2019-09-01 06:26:04 -04:00
|
|
|
} else {
|
2019-10-30 19:09:38 -04:00
|
|
|
r = new MyAlternateService($r3$.ɵɵinject(SomeDep));
|
2019-09-01 06:26:04 -04:00
|
|
|
}
|
|
|
|
return r;
|
2019-11-27 19:25:47 -05:00
|
|
|
}
|
2019-09-01 06:26:04 -04:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, factory, 'Incorrect factory definition');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should unwrap forward refs when delegating to a different class', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Injectable, forwardRef} from '@angular/core';
|
|
|
|
|
|
|
|
@Injectable({providedIn: 'root', useClass: forwardRef(() => SomeProviderImpl)})
|
|
|
|
abstract class SomeProvider {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
class SomeProviderImpl extends SomeProvider {
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const factory = `
|
2019-10-15 15:41:30 -04:00
|
|
|
SomeProvider.ɵprov = $r3$.ɵɵdefineInjectable({
|
2019-09-01 06:26:04 -04:00
|
|
|
token: SomeProvider,
|
|
|
|
factory: function(t) {
|
2019-10-11 17:18:45 -04:00
|
|
|
return SomeProviderImpl.ɵfac(t);
|
2019-09-01 06:26:04 -04:00
|
|
|
},
|
|
|
|
providedIn: 'root'
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, factory, 'Incorrect factory definition');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have the pipe factory take precedence over the injectable factory, if a class has multiple decorators',
|
|
|
|
() => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, Pipe, PipeTransform, Injectable} from '@angular/core';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
class Service {}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
@Pipe({name: 'myPipe'})
|
|
|
|
export class MyPipe implements PipeTransform {
|
|
|
|
constructor(service: Service) {}
|
|
|
|
transform(value: any, ...args: any[]) { return value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Pipe({name: 'myOtherPipe'})
|
|
|
|
@Injectable()
|
|
|
|
export class MyOtherPipe implements PipeTransform {
|
|
|
|
constructor(service: Service) {}
|
|
|
|
transform(value: any, ...args: any[]) { return value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-app',
|
|
|
|
template: '{{0 | myPipe | myOtherPipe}}'
|
|
|
|
})
|
|
|
|
export class MyApp {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyPipe, MyOtherPipe, MyApp], declarations: [Service]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
2019-11-27 18:52:34 -05:00
|
|
|
// The prov definition must be last so MyPipe.fac is defined
|
|
|
|
const MyPipeDefs = `
|
|
|
|
MyPipe.ɵfac = function MyPipe_Factory(t) { return new (t || MyPipe)(i0.ɵɵdirectiveInject(Service)); };
|
|
|
|
MyPipe.ɵpipe = i0.ɵɵdefinePipe({ name: "myPipe", type: MyPipe, pure: true });
|
2019-11-27 19:25:47 -05:00
|
|
|
MyPipe.ɵprov = i0.ɵɵdefineInjectable({ token: MyPipe, factory: MyPipe.ɵfac });
|
2019-09-01 06:26:04 -04:00
|
|
|
`;
|
|
|
|
|
2019-11-27 18:52:34 -05:00
|
|
|
// The prov definition must be last so MyOtherPipe.fac is defined
|
|
|
|
const MyOtherPipeDefs = `
|
2019-10-11 17:18:45 -04:00
|
|
|
MyOtherPipe.ɵfac = function MyOtherPipe_Factory(t) { return new (t || MyOtherPipe)($r3$.ɵɵdirectiveInject(Service)); };
|
2019-11-27 18:52:34 -05:00
|
|
|
MyOtherPipe.ɵpipe = i0.ɵɵdefinePipe({ name: "myOtherPipe", type: MyOtherPipe, pure: true });
|
2019-11-27 19:25:47 -05:00
|
|
|
MyOtherPipe.ɵprov = i0.ɵɵdefineInjectable({ token: MyOtherPipe, factory: MyOtherPipe.ɵfac });
|
2019-09-01 06:26:04 -04:00
|
|
|
`;
|
|
|
|
|
2019-11-27 18:52:34 -05:00
|
|
|
expectEmit(source, MyPipeDefs, 'Invalid pipe factory function');
|
|
|
|
expectEmit(source, MyOtherPipeDefs, 'Invalid pipe factory function');
|
2019-10-11 17:18:45 -04:00
|
|
|
expect(source.match(/MyPipe\.ɵfac =/g) !.length).toBe(1);
|
|
|
|
expect(source.match(/MyOtherPipe\.ɵfac =/g) !.length).toBe(1);
|
2019-09-01 06:26:04 -04:00
|
|
|
});
|
|
|
|
|
2018-04-12 15:30:21 -04:00
|
|
|
});
|