|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC 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.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; |
| 10 | +import { Schema as ApplicationOptions } from '../application/schema'; |
| 11 | +import { Schema as WorkspaceOptions } from '../workspace/schema'; |
| 12 | +import { Schema as InjectableOptions } from './schema'; |
| 13 | + |
| 14 | +describe('Injectable Schematic', () => { |
| 15 | + const schematicRunner = new SchematicTestRunner( |
| 16 | + '@schematics/angular', |
| 17 | + require.resolve('../collection.json'), |
| 18 | + ); |
| 19 | + const defaultOptions: InjectableOptions = { |
| 20 | + name: 'foo', |
| 21 | + flat: false, |
| 22 | + project: 'bar', |
| 23 | + }; |
| 24 | + |
| 25 | + const workspaceOptions: WorkspaceOptions = { |
| 26 | + name: 'workspace', |
| 27 | + newProjectRoot: 'projects', |
| 28 | + version: '6.0.0', |
| 29 | + }; |
| 30 | + |
| 31 | + const appOptions: ApplicationOptions = { |
| 32 | + name: 'bar', |
| 33 | + inlineStyle: false, |
| 34 | + inlineTemplate: false, |
| 35 | + routing: false, |
| 36 | + skipPackageJson: false, |
| 37 | + }; |
| 38 | + let appTree: UnitTestTree; |
| 39 | + beforeEach(async () => { |
| 40 | + appTree = await schematicRunner.runSchematic('workspace', workspaceOptions); |
| 41 | + appTree = await schematicRunner.runSchematic('application', appOptions, appTree); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should create an injectable', async () => { |
| 45 | + const options = { ...defaultOptions }; |
| 46 | + |
| 47 | + const tree = await schematicRunner.runSchematic('injectable', options, appTree); |
| 48 | + const files = tree.files; |
| 49 | + expect(files).toContain('/projects/bar/src/app/foo/foo.spec.ts'); |
| 50 | + expect(files).toContain('/projects/bar/src/app/foo/foo.ts'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should use @Injectable decorator', async () => { |
| 54 | + const options = { ...defaultOptions }; |
| 55 | + |
| 56 | + const tree = await schematicRunner.runSchematic('injectable', options, appTree); |
| 57 | + const content = tree.readContent('/projects/bar/src/app/foo/foo.ts'); |
| 58 | + expect(content).toMatch(/@Injectable\(/); |
| 59 | + expect(content).toMatch(/import \{ Injectable \} from '@angular\/core'/); |
| 60 | + }); |
| 61 | + |
| 62 | + it('injectable should be tree-shakeable', async () => { |
| 63 | + const options = { ...defaultOptions }; |
| 64 | + |
| 65 | + const tree = await schematicRunner.runSchematic('injectable', options, appTree); |
| 66 | + const content = tree.readContent('/projects/bar/src/app/foo/foo.ts'); |
| 67 | + expect(content).toMatch(/providedIn: 'root',/); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should respect the skipTests flag', async () => { |
| 71 | + const options = { ...defaultOptions, skipTests: true }; |
| 72 | + |
| 73 | + const tree = await schematicRunner.runSchematic('injectable', options, appTree); |
| 74 | + const files = tree.files; |
| 75 | + expect(files).toContain('/projects/bar/src/app/foo/foo.ts'); |
| 76 | + expect(files).not.toContain('/projects/bar/src/app/foo/foo.spec.ts'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should respect the sourceRoot value', async () => { |
| 80 | + const config = JSON.parse(appTree.readContent('/angular.json')); |
| 81 | + config.projects.bar.sourceRoot = 'projects/bar/custom'; |
| 82 | + appTree.overwrite('/angular.json', JSON.stringify(config, null, 2)); |
| 83 | + appTree = await schematicRunner.runSchematic('injectable', defaultOptions, appTree); |
| 84 | + expect(appTree.files).toContain('/projects/bar/custom/app/foo/foo.ts'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should respect the type option', async () => { |
| 88 | + const options = { ...defaultOptions, type: 'Service' }; |
| 89 | + const tree = await schematicRunner.runSchematic('injectable', options, appTree); |
| 90 | + const content = tree.readContent('/projects/bar/src/app/foo/foo.service.ts'); |
| 91 | + const testContent = tree.readContent('/projects/bar/src/app/foo/foo.service.spec.ts'); |
| 92 | + expect(content).toContain('export class FooService'); |
| 93 | + expect(testContent).toContain("describe('FooService'"); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should allow empty string in the type option', async () => { |
| 97 | + const options = { ...defaultOptions, type: '' }; |
| 98 | + const tree = await schematicRunner.runSchematic('injectable', options, appTree); |
| 99 | + const content = tree.readContent('/projects/bar/src/app/foo/foo.ts'); |
| 100 | + const testContent = tree.readContent('/projects/bar/src/app/foo/foo.spec.ts'); |
| 101 | + expect(content).toContain('export class Foo'); |
| 102 | + expect(testContent).toContain("describe('Foo'"); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should not add type to class name when addTypeToClassName is false', async () => { |
| 106 | + const options = { ...defaultOptions, type: 'Service', addTypeToClassName: false }; |
| 107 | + const tree = await schematicRunner.runSchematic('injectable', options, appTree); |
| 108 | + const content = tree.readContent('/projects/bar/src/app/foo/foo.service.ts'); |
| 109 | + const testContent = tree.readContent('/projects/bar/src/app/foo/foo.service.spec.ts'); |
| 110 | + expect(content).toContain('export class Foo {'); |
| 111 | + expect(content).not.toContain('export class FooService {'); |
| 112 | + expect(testContent).toContain("describe('Foo', () => {"); |
| 113 | + expect(testContent).not.toContain("describe('FooService', () => {"); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should add type to class name when addTypeToClassName is true', async () => { |
| 117 | + const options = { ...defaultOptions, type: 'Service', addTypeToClassName: true }; |
| 118 | + const tree = await schematicRunner.runSchematic('injectable', options, appTree); |
| 119 | + const content = tree.readContent('/projects/bar/src/app/foo/foo.service.ts'); |
| 120 | + const testContent = tree.readContent('/projects/bar/src/app/foo/foo.service.spec.ts'); |
| 121 | + expect(content).toContain('export class FooService {'); |
| 122 | + expect(testContent).toContain("describe('FooService', () => {"); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should add type to class name by default', async () => { |
| 126 | + const options = { ...defaultOptions, type: 'Service', addTypeToClassName: undefined }; |
| 127 | + const tree = await schematicRunner.runSchematic('injectable', options, appTree); |
| 128 | + const content = tree.readContent('/projects/bar/src/app/foo/foo.service.ts'); |
| 129 | + const testContent = tree.readContent('/projects/bar/src/app/foo/foo.service.spec.ts'); |
| 130 | + expect(content).toContain('export class FooService {'); |
| 131 | + expect(testContent).toContain("describe('FooService', () => {"); |
| 132 | + }); |
| 133 | +}); |
0 commit comments