Skip to content

Commit

Permalink
Merge pull request #57 from Tinkoff/import-to-component
Browse files Browse the repository at this point in the history
feat: add new util `addImportToComponent`
  • Loading branch information
IKatsuba authored Apr 26, 2023
2 parents 6113487 + 74bbc00 commit 1f88b0d
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
127 changes: 127 additions & 0 deletions libs/ng-morph/ng/component/add-import-to-component.spec.ts
Original file line number Diff line number Diff line change
@@ -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 {
}`);
});
});
});
13 changes: 13 additions & 0 deletions libs/ng-morph/ng/component/add-import-to-component.ts
Original file line number Diff line number Diff line change
@@ -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,
});
}

0 comments on commit 1f88b0d

Please sign in to comment.