-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from Tinkoff/import-to-component
feat: add new util `addImportToComponent`
- Loading branch information
Showing
2 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
libs/ng-morph/ng/component/add-import-to-component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
}`); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |