|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import { tags } from '@angular-devkit/core'; // tslint:disable-line:no-implicit-dependencies |
| 9 | +import { createTypescriptContext, transformTypescript } from './ast_helpers'; |
| 10 | +import { downlevelConstructorParameters } from './ctor-parameters'; |
| 11 | + |
| 12 | +function transform(input: string, additionalFiles?: Record<string, string>) { |
| 13 | + const { program, compilerHost } = createTypescriptContext(input, additionalFiles); |
| 14 | + const transformer = downlevelConstructorParameters(() => program.getTypeChecker()); |
| 15 | + const result = transformTypescript(undefined, [transformer], program, compilerHost); |
| 16 | + |
| 17 | + return result; |
| 18 | +} |
| 19 | + |
| 20 | +describe('Constructor Parameter Transformer', () => { |
| 21 | + it('supports Inject decorators with interfaces in same module', () => { |
| 22 | + const input = tags.stripIndent` |
| 23 | + export interface InterInject {}; |
| 24 | + export const INTERFACE_INJECT = new InjectionToken<InterInject>('interface-inject'); |
| 25 | +
|
| 26 | + export class MyService { |
| 27 | + constructor(@Inject(INTERFACE_INJECT) config: InterInject) {} |
| 28 | + } |
| 29 | + `; |
| 30 | + |
| 31 | + const output = `import * as tslib_1 from "tslib"; ; export const INTERFACE_INJECT = new InjectionToken('interface-inject'); let MyService = class MyService { constructor(config) { } }; MyService.ctorParameters = () => [ { type: undefined, decorators: [{ type: Inject, args: [INTERFACE_INJECT,] }] } ]; MyService = tslib_1.__decorate([ tslib_1.__param(0, Inject(INTERFACE_INJECT)) ], MyService); export { MyService };`; |
| 32 | + |
| 33 | + const result = transform(input); |
| 34 | + |
| 35 | + expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); |
| 36 | + }); |
| 37 | + |
| 38 | + it('supports Inject decorators with interfaces in another module', () => { |
| 39 | + const injectedModule = { |
| 40 | + 'module-inject': ` |
| 41 | + export interface InterInject {}; |
| 42 | + export const INTERFACE_INJECT = new InjectionToken<InterInject>('interface-inject'); |
| 43 | + `, |
| 44 | + }; |
| 45 | + |
| 46 | + const input = tags.stripIndent` |
| 47 | + import { INTERFACE_INJECT, InterInject } from './module-inject'; |
| 48 | +
|
| 49 | + export class MyService { |
| 50 | + constructor(@Inject(INTERFACE_INJECT) config: InterInject) {} |
| 51 | + } |
| 52 | + `; |
| 53 | + |
| 54 | + const output = `import * as tslib_1 from "tslib"; import { INTERFACE_INJECT } from './module-inject'; let MyService = class MyService { constructor(config) { } }; MyService.ctorParameters = () => [ { type: undefined, decorators: [{ type: Inject, args: [INTERFACE_INJECT,] }] } ]; MyService = tslib_1.__decorate([ tslib_1.__param(0, Inject(INTERFACE_INJECT)) ], MyService); export { MyService };`; |
| 55 | + |
| 56 | + const result = transform(input, injectedModule); |
| 57 | + |
| 58 | + expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); |
| 59 | + }); |
| 60 | +}); |
0 commit comments