|
1 |
| -import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; |
2 |
| -import * as path from 'path'; |
3 |
| -import { Schema as ApplicationOptions, Style } from '@schematics/angular/application/schema'; |
4 |
| -import { Schema as WorkspaceOptions } from '@schematics/angular/workspace/schema'; |
5 |
| -import { Schema as AngularToasterOptions } from './schema'; |
| 1 | +import { |
| 2 | + SchematicTestRunner, |
| 3 | + UnitTestTree, |
| 4 | +} from "@angular-devkit/schematics/testing"; |
| 5 | +import * as path from "path"; |
| 6 | +import { |
| 7 | + Schema as ApplicationOptions, |
| 8 | + Style, |
| 9 | +} from "@schematics/angular/application/schema"; |
| 10 | +import { Schema as WorkspaceOptions } from "@schematics/angular/workspace/schema"; |
| 11 | +import { Schema as AngularToasterOptions } from "./schema"; |
6 | 12 |
|
7 |
| -describe('angular toaster schematic unit test', () => { |
8 |
| - const schematicRunner = new SchematicTestRunner('angular-toaster', path.join(__dirname, '../collection.json')); |
9 |
| - const defaultOptions: AngularToasterOptions = { |
10 |
| - project: 'angular-toaster-app', |
11 |
| - }; |
| 13 | +describe("angular toaster schematic unit test", () => { |
| 14 | + const schematicRunner = new SchematicTestRunner( |
| 15 | + "angular-toaster", |
| 16 | + path.join(__dirname, "../collection.json") |
| 17 | + ); |
| 18 | + const defaultOptions: AngularToasterOptions = { |
| 19 | + project: "angular-toaster-app", |
| 20 | + }; |
12 | 21 |
|
13 |
| - const workspaceOptions: WorkspaceOptions = { |
14 |
| - name: 'workspace', |
15 |
| - newProjectRoot: 'projects', |
16 |
| - version: '0.0.1' |
17 |
| - }; |
18 |
| - const appOptions: ApplicationOptions = { |
19 |
| - name: 'angular-toaster-app', |
20 |
| - inlineStyle: false, |
21 |
| - inlineTemplate: false, |
22 |
| - routing: false, |
23 |
| - style: Style.Scss, |
24 |
| - skipTests: false, |
25 |
| - skipPackageJson: false, |
26 |
| - standalone: false |
| 22 | + const workspaceOptions: WorkspaceOptions = { |
| 23 | + name: "workspace", |
| 24 | + newProjectRoot: "projects", |
| 25 | + version: "0.0.1", |
| 26 | + }; |
| 27 | + const appOptions: ApplicationOptions = { |
| 28 | + name: "angular-toaster-app", |
| 29 | + inlineStyle: false, |
| 30 | + inlineTemplate: false, |
| 31 | + routing: false, |
| 32 | + style: Style.Scss, |
| 33 | + skipTests: false, |
| 34 | + skipPackageJson: false, |
| 35 | + standalone: false, |
| 36 | + }; |
| 37 | + let appTree: UnitTestTree | undefined; |
| 38 | + |
| 39 | + beforeEach(async () => { |
| 40 | + appTree = await schematicRunner.runExternalSchematic( |
| 41 | + "@schematics/angular", |
| 42 | + "workspace", |
| 43 | + workspaceOptions |
| 44 | + ); |
| 45 | + appTree = await schematicRunner.runExternalSchematic( |
| 46 | + "@schematics/angular", |
| 47 | + "application", |
| 48 | + appOptions, |
| 49 | + appTree |
| 50 | + ); |
| 51 | + const standaloneAppOptions = { |
| 52 | + ...appOptions, |
| 53 | + name: "angular-toaster-standalone-app", |
| 54 | + standalone: true, |
27 | 55 | };
|
28 |
| - let appTree: UnitTestTree | undefined; |
| 56 | + appTree = await schematicRunner.runExternalSchematic( |
| 57 | + "@schematics/angular", |
| 58 | + "application", |
| 59 | + standaloneAppOptions, |
| 60 | + appTree |
| 61 | + ); |
| 62 | + }); |
29 | 63 |
|
30 |
| - beforeEach(async () => { |
31 |
| - appTree = await schematicRunner.runExternalSchematic('@schematics/angular', 'workspace', workspaceOptions); |
32 |
| - appTree = await schematicRunner.runExternalSchematic('@schematics/angular', 'application', appOptions, appTree); |
33 |
| - const standaloneAppOptions = { ...appOptions, name: 'angular-toaster-standalone-app', standalone: true }; |
34 |
| - appTree = await schematicRunner.runExternalSchematic( |
35 |
| - '@schematics/angular', |
36 |
| - 'application', |
37 |
| - standaloneAppOptions, |
38 |
| - appTree |
| 64 | + describe("when project is not a standalone application", () => { |
| 65 | + it("should update app module", async () => { |
| 66 | + const options = { ...defaultOptions }; |
| 67 | + const tree = await schematicRunner.runSchematic( |
| 68 | + "ng-add-setup-project", |
| 69 | + options, |
| 70 | + appTree |
| 71 | + ); |
| 72 | + const content = tree.readContent( |
| 73 | + "/projects/angular-toaster-app/src/app/app-module.ts" |
| 74 | + ); |
| 75 | + expect(content) |
| 76 | + .withContext("Expected the ToasterModule to be imported.") |
| 77 | + .toMatch(/import\s+{\s*ToasterModule\s*}\s+from\s+'angular-toaster'/); |
| 78 | + expect(content) |
| 79 | + .withContext( |
| 80 | + "Expected the ToasterModule to be added to the imports array." |
| 81 | + ) |
| 82 | + .toMatch( |
| 83 | + /imports:\s*\[[^\]]+?,\r?\n\s+ToasterModule\.forRoot\(\)\r?\n/m |
39 | 84 | );
|
40 | 85 | });
|
41 | 86 |
|
42 |
| - describe('when project is not a standalone application', () => { |
43 |
| - it('should update app module', async () => { |
44 |
| - const options = { ...defaultOptions }; |
45 |
| - const tree = await schematicRunner.runSchematic('ng-add-setup-project', options, appTree); |
46 |
| - const content = tree.readContent('/projects/angular-toaster-app/src/app/app.module.ts'); |
47 |
| - expect(content) |
48 |
| - .withContext('Expected the ToasterModule to be imported.') |
49 |
| - .toMatch(/import\s+{\s*ToasterModule\s*}\s+from\s+'angular-toaster'/); |
50 |
| - expect(content) |
51 |
| - .withContext('Expected the ToasterModule to be added to the imports array.') |
52 |
| - .toMatch(/imports:\s*\[[^\]]+?,\r?\n\s+ToasterModule\.forRoot\(\)\r?\n/m,); |
53 |
| - }); |
54 |
| - |
55 |
| - it('should update angular styles', async () => { |
56 |
| - const options = { ...defaultOptions }; |
57 |
| - const tree = await schematicRunner.runSchematic('ng-add-setup-project', options, appTree); |
58 |
| - const angularJsonContent = tree.readContent('/angular.json'); |
59 |
| - const angularJson = JSON.parse(angularJsonContent); |
60 |
| - const angularToasterApp = angularJson.projects['angular-toaster-app']; |
61 |
| - const architect = angularToasterApp.architect; |
62 |
| - const buildArchitect = architect.build; |
63 |
| - const testArchitect = architect.test; |
| 87 | + it("should update angular styles", async () => { |
| 88 | + const options = { ...defaultOptions }; |
| 89 | + const tree = await schematicRunner.runSchematic( |
| 90 | + "ng-add-setup-project", |
| 91 | + options, |
| 92 | + appTree |
| 93 | + ); |
| 94 | + const angularJsonContent = tree.readContent("/angular.json"); |
| 95 | + const angularJson = JSON.parse(angularJsonContent); |
| 96 | + const angularToasterApp = angularJson.projects["angular-toaster-app"]; |
| 97 | + const architect = angularToasterApp.architect; |
| 98 | + const buildArchitect = architect.build; |
| 99 | + const testArchitect = architect.test; |
64 | 100 |
|
65 |
| - expect(buildArchitect.options.styles).withContext('Expect the theme css import styles').toContain('./node_modules/angular-toaster/toaster.css'); |
66 |
| - expect(testArchitect.options.styles).withContext('Expect the theme css import styles').toContain('./node_modules/angular-toaster/toaster.css'); |
67 |
| - }); |
| 101 | + expect(buildArchitect.options.styles) |
| 102 | + .withContext("Expect the theme css import styles") |
| 103 | + .toContain("./node_modules/angular-toaster/toaster.css"); |
| 104 | + expect(testArchitect.options.styles) |
| 105 | + .withContext("Expect the theme css import styles") |
| 106 | + .toContain("./node_modules/angular-toaster/toaster.css"); |
68 | 107 | });
|
| 108 | + }); |
69 | 109 |
|
| 110 | + describe("when project is a standalone application", () => { |
| 111 | + it("should update app config", async () => { |
| 112 | + const options = { |
| 113 | + ...defaultOptions, |
| 114 | + project: "angular-toaster-standalone-app", |
| 115 | + }; |
| 116 | + const tree = await schematicRunner.runSchematic( |
| 117 | + "ng-add-setup-project", |
| 118 | + options, |
| 119 | + appTree |
| 120 | + ); |
| 121 | + const content = tree.readContent( |
| 122 | + "/projects/angular-toaster-standalone-app/src/app/app.config.ts" |
| 123 | + ); |
| 124 | + expect(content) |
| 125 | + .withContext("Expected the provideAngularToaster to be imported.") |
| 126 | + .toMatch( |
| 127 | + /import\s+{\s*provideAngularToaster\s*}\s+from\s+'angular-toaster'/ |
| 128 | + ); |
| 129 | + expect(content) |
| 130 | + .withContext("Expected provideAngularToaster() to be present") |
| 131 | + .toContain("provideAngularToaster()"); |
| 132 | + |
| 133 | + const providersIndex = content.indexOf("providers:"); |
| 134 | + const toasterIndex = content.indexOf("provideAngularToaster()"); |
| 135 | + const nextClosingBrace = content.indexOf("]", providersIndex); |
70 | 136 |
|
71 |
| - describe('when project is a standalone application', () => { |
72 |
| - it('should update app config', async () => { |
73 |
| - const options = { ...defaultOptions, project: 'angular-toaster-standalone-app' }; |
74 |
| - const tree = await schematicRunner.runSchematic('ng-add-setup-project', options, appTree); |
75 |
| - const content = tree.readContent('/projects/angular-toaster-standalone-app/src/app/app.config.ts'); |
76 |
| - expect(content) |
77 |
| - .withContext('Expected the provideAngularToaster to be imported.') |
78 |
| - .toMatch(/import\s+{\s*provideAngularToaster\s*}\s+from\s+'angular-toaster'/); |
79 |
| - expect(content) |
80 |
| - .withContext('Expected the provideAngularToaster to be added to the providers array.') |
81 |
| - .toMatch(/providers:\s*\[\s*.*?provideAngularToaster\(\)/); |
82 |
| - }); |
| 137 | + expect(providersIndex).toBeGreaterThan(-1); |
| 138 | + expect(toasterIndex).toBeGreaterThan(-1); |
| 139 | + expect(toasterIndex).toBeGreaterThan(providersIndex); |
| 140 | + expect(toasterIndex).toBeLessThan(nextClosingBrace); |
| 141 | + }); |
83 | 142 |
|
84 |
| - it('should update angular styles', async () => { |
85 |
| - const options = { ...defaultOptions, project: 'angular-toaster-standalone-app' }; |
86 |
| - const tree = await schematicRunner.runSchematic('ng-add-setup-project', options, appTree); |
87 |
| - const angularJsonContent = tree.readContent('/angular.json'); |
88 |
| - const angularJson = JSON.parse(angularJsonContent); |
89 |
| - const angularToasterApp = angularJson.projects['angular-toaster-standalone-app']; |
90 |
| - const architect = angularToasterApp.architect; |
91 |
| - const buildArchitect = architect.build; |
92 |
| - const testArchitect = architect.test; |
| 143 | + it("should update angular styles", async () => { |
| 144 | + const options = { |
| 145 | + ...defaultOptions, |
| 146 | + project: "angular-toaster-standalone-app", |
| 147 | + }; |
| 148 | + const tree = await schematicRunner.runSchematic( |
| 149 | + "ng-add-setup-project", |
| 150 | + options, |
| 151 | + appTree |
| 152 | + ); |
| 153 | + const angularJsonContent = tree.readContent("/angular.json"); |
| 154 | + const angularJson = JSON.parse(angularJsonContent); |
| 155 | + const angularToasterApp = |
| 156 | + angularJson.projects["angular-toaster-standalone-app"]; |
| 157 | + const architect = angularToasterApp.architect; |
| 158 | + const buildArchitect = architect.build; |
| 159 | + const testArchitect = architect.test; |
93 | 160 |
|
94 |
| - expect(buildArchitect.options.styles).withContext('Expect the theme css import styles').toContain('./node_modules/angular-toaster/toaster.css'); |
95 |
| - expect(testArchitect.options.styles).withContext('Expect the theme css import styles').toContain('./node_modules/angular-toaster/toaster.css'); |
96 |
| - }); |
| 161 | + expect(buildArchitect.options.styles) |
| 162 | + .withContext("Expect the theme css import styles") |
| 163 | + .toContain("./node_modules/angular-toaster/toaster.css"); |
| 164 | + expect(testArchitect.options.styles) |
| 165 | + .withContext("Expect the theme css import styles") |
| 166 | + .toContain("./node_modules/angular-toaster/toaster.css"); |
97 | 167 | });
|
| 168 | + }); |
98 | 169 | });
|
0 commit comments