From 74bbc00a7f446a9eff6ac041b63a9c045e2e278d Mon Sep 17 00:00:00 2001 From: "v.potekhin" Date: Wed, 19 Apr 2023 18:16:55 +0300 Subject: [PATCH] feat: add new `addImportToComponent` util --- .../component/add-import-to-component.spec.ts | 127 ++++++++++++++++++ .../ng/component/add-import-to-component.ts | 13 ++ 2 files changed, 140 insertions(+) create mode 100644 libs/ng-morph/ng/component/add-import-to-component.spec.ts create mode 100644 libs/ng-morph/ng/component/add-import-to-component.ts diff --git a/libs/ng-morph/ng/component/add-import-to-component.spec.ts b/libs/ng-morph/ng/component/add-import-to-component.spec.ts new file mode 100644 index 00000000..ff214e7b --- /dev/null +++ b/libs/ng-morph/ng/component/add-import-to-component.spec.ts @@ -0,0 +1,127 @@ +import { UnitTestTree } from '@angular-devkit/schematics/testing'; +import { HostTree } from '@angular-devkit/schematics'; +import { + createProject, + saveActiveProject, + setActiveProject, +} from 'ng-morph/project'; +import { createSourceFile } from 'ng-morph/source-file'; +import { getClasses } from 'ng-morph/classes'; +import { addImportToComponent } from './add-import-to-component'; + +describe('addProviderToComponent', () => { + let host: UnitTestTree; + + beforeEach(() => { + host = new UnitTestTree(new HostTree()); + + setActiveProject(createProject(host)); + }); + + describe('No providers property', () => { + beforeEach(() => { + createSourceFile( + 'src/main.ts', + `import { Component } from '@angular/core'; + +@Component({}) +export class SomeComponent { + +}` + ); + }); + + it('should create the providers property', () => { + addImportToComponent( + getClasses('src/main.ts', { + name: 'SomeComponent', + })[0], + 'TestImport' + ); + + saveActiveProject(); + + expect(host.readContent('src/main.ts')) + .toStrictEqual(`import { Component } from '@angular/core'; + +@Component({ + imports: [TestImport] + }) +export class SomeComponent { + +}`); + }); + }); + + describe('No decorator arguments', () => { + beforeEach(() => { + createSourceFile( + 'src/main.ts', + `import { Component } from '@angular/core'; + +@Component() +export class SomeComponent { + +}` + ); + }); + + it('should create the providers property', () => { + addImportToComponent( + getClasses('src/main.ts', { + name: 'SomeComponent', + })[0], + 'TestImport' + ); + + saveActiveProject(); + + expect(host.readContent('src/main.ts')) + .toStrictEqual(`import { Component } from '@angular/core'; + +@Component({imports: [TestImport]}) +export class SomeComponent { + +}`); + }); + }); + + describe('The providers property is exists', () => { + beforeEach(() => { + createSourceFile( + 'src/main.ts', + `import { Component } from '@angular/core'; +import { TestImport } from '@angular/common'; + +@Component({ + imports: [TestImport] +}) +export class SomeComponent { + +}` + ); + }); + + it('should add module to providers', () => { + addImportToComponent( + getClasses('src/main.ts', { + name: 'SomeComponent', + })[0], + 'NewTestImport' + ); + + saveActiveProject(); + + expect(host.readContent('src/main.ts')) + .toStrictEqual(`import { Component } from '@angular/core'; +import { TestImport } from '@angular/common'; + +@Component({ + imports: [TestImport, NewTestImport] +}) +export class SomeComponent { + +}`); + }); + }); +}); diff --git a/libs/ng-morph/ng/component/add-import-to-component.ts b/libs/ng-morph/ng/component/add-import-to-component.ts new file mode 100644 index 00000000..f3ed1826 --- /dev/null +++ b/libs/ng-morph/ng/component/add-import-to-component.ts @@ -0,0 +1,13 @@ +import { ClassDeclaration } from 'ts-morph'; +import { pushToArrayProperty } from '../helpers/push-to-array-property'; + +export function addImportToComponent( + classDeclaration: ClassDeclaration, + importName: string, + { unique = false }: { unique?: boolean } = {} +) { + pushToArrayProperty(classDeclaration, 'Component', 'imports', importName, { + unique, + forceToArray: true, + }); +}