|
8 | 8 |
|
9 | 9 | import { EmptyTree } from '@angular-devkit/schematics';
|
10 | 10 | import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
|
| 11 | +import { WorkspaceTargets } from '../../utility/workspace-models'; |
| 12 | +import { ANY_COMPONENT_STYLE_BUDGET } from './update-workspace-config'; |
11 | 13 |
|
12 |
| -function readWorkspaceConfig(tree: UnitTestTree) { |
13 |
| - return JSON.parse(tree.readContent('/angular.json')); |
| 14 | +// tslint:disable-next-line: no-any |
| 15 | +function getWorkspaceTargets(tree: UnitTestTree): any { |
| 16 | + return JSON.parse(tree.readContent(workspacePath)) |
| 17 | + .projects['migration-test'].architect; |
| 18 | +} |
| 19 | + |
| 20 | +function updateWorkspaceTargets(tree: UnitTestTree, workspaceTargets: WorkspaceTargets) { |
| 21 | + const config = JSON.parse(tree.readContent(workspacePath)); |
| 22 | + config.projects['migration-test'].architect = workspaceTargets; |
| 23 | + tree.overwrite(workspacePath, JSON.stringify(config, undefined, 2)); |
14 | 24 | }
|
15 | 25 |
|
16 | 26 | const scriptsWithLazy = [
|
@@ -69,60 +79,95 @@ describe('Migration to version 9', () => {
|
69 | 79 | .toPromise();
|
70 | 80 | });
|
71 | 81 |
|
72 |
| - it('should update scripts in build target', () => { |
73 |
| - let config = readWorkspaceConfig(tree); |
74 |
| - let build = config.projects['migration-test'].architect.build; |
75 |
| - build.options.scripts = scriptsWithLazy; |
76 |
| - build.configurations.production.scripts = scriptsWithLazy; |
77 |
| - |
78 |
| - tree.overwrite(workspacePath, JSON.stringify(config)); |
79 |
| - const tree2 = schematicRunner.runSchematic('migration-09', {}, tree.branch()); |
80 |
| - config = readWorkspaceConfig(tree2); |
81 |
| - build = config.projects['migration-test'].architect.build; |
82 |
| - expect(build.options.scripts).toEqual(scriptsExpectWithLazy); |
83 |
| - expect(build.configurations.production.scripts).toEqual(scriptsExpectWithLazy); |
84 |
| - }); |
85 |
| - |
86 |
| - it('should update styles in build target', () => { |
87 |
| - let config = readWorkspaceConfig(tree); |
88 |
| - let build = config.projects['migration-test'].architect.build; |
89 |
| - build.options.styles = stylesWithLazy; |
90 |
| - build.configurations.production.styles = stylesWithLazy; |
91 |
| - |
92 |
| - tree.overwrite(workspacePath, JSON.stringify(config)); |
93 |
| - const tree2 = schematicRunner.runSchematic('migration-09', {}, tree.branch()); |
94 |
| - config = readWorkspaceConfig(tree2); |
95 |
| - build = config.projects['migration-test'].architect.build; |
96 |
| - expect(build.options.styles).toEqual(stylesExpectWithLazy); |
97 |
| - expect(build.configurations.production.styles).toEqual(stylesExpectWithLazy); |
98 |
| - }); |
99 |
| - |
100 |
| - it('should update scripts in test target', () => { |
101 |
| - let config = readWorkspaceConfig(tree); |
102 |
| - let test = config.projects['migration-test'].architect.test; |
103 |
| - test.options.scripts = scriptsWithLazy; |
104 |
| - test.configurations = { production: { scripts: scriptsWithLazy } }; |
105 |
| - |
106 |
| - tree.overwrite(workspacePath, JSON.stringify(config)); |
107 |
| - const tree2 = schematicRunner.runSchematic('migration-09', {}, tree.branch()); |
108 |
| - config = readWorkspaceConfig(tree2); |
109 |
| - test = config.projects['migration-test'].architect.test; |
110 |
| - expect(test.options.scripts).toEqual(scriptsExpectWithLazy); |
111 |
| - expect(test.configurations.production.scripts).toEqual(scriptsExpectWithLazy); |
| 82 | + describe('scripts and style options', () => { |
| 83 | + it('should update scripts in build target', () => { |
| 84 | + let config = getWorkspaceTargets(tree); |
| 85 | + config.build.options.scripts = scriptsWithLazy; |
| 86 | + config.build.configurations.production.scripts = scriptsWithLazy; |
| 87 | + |
| 88 | + updateWorkspaceTargets(tree, config); |
| 89 | + const tree2 = schematicRunner.runSchematic('migration-09', {}, tree.branch()); |
| 90 | + config = getWorkspaceTargets(tree2).build; |
| 91 | + expect(config.options.scripts).toEqual(scriptsExpectWithLazy); |
| 92 | + expect(config.configurations.production.scripts).toEqual(scriptsExpectWithLazy); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should update styles in build target', () => { |
| 96 | + let config = getWorkspaceTargets(tree); |
| 97 | + config.build.options.styles = stylesWithLazy; |
| 98 | + config.build.configurations.production.styles = stylesWithLazy; |
| 99 | + |
| 100 | + updateWorkspaceTargets(tree, config); |
| 101 | + const tree2 = schematicRunner.runSchematic('migration-09', {}, tree.branch()); |
| 102 | + config = getWorkspaceTargets(tree2).build; |
| 103 | + expect(config.options.styles).toEqual(stylesExpectWithLazy); |
| 104 | + expect(config.configurations.production.styles).toEqual(stylesExpectWithLazy); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should update scripts in test target', () => { |
| 108 | + let config = getWorkspaceTargets(tree); |
| 109 | + config.test.options.scripts = scriptsWithLazy; |
| 110 | + config.test.configurations = { production: { scripts: scriptsWithLazy } }; |
| 111 | + |
| 112 | + updateWorkspaceTargets(tree, config); |
| 113 | + const tree2 = schematicRunner.runSchematic('migration-09', {}, tree.branch()); |
| 114 | + config = getWorkspaceTargets(tree2).test; |
| 115 | + expect(config.options.scripts).toEqual(scriptsExpectWithLazy); |
| 116 | + expect(config.configurations.production.scripts).toEqual(scriptsExpectWithLazy); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should update styles in test target', () => { |
| 120 | + let config = getWorkspaceTargets(tree); |
| 121 | + config.test.options.styles = stylesWithLazy; |
| 122 | + config.test.configurations = { production: { styles: stylesWithLazy } }; |
| 123 | + |
| 124 | + updateWorkspaceTargets(tree, config); |
| 125 | + const tree2 = schematicRunner.runSchematic('migration-09', {}, tree.branch()); |
| 126 | + config = getWorkspaceTargets(tree2).test; |
| 127 | + expect(config.options.styles).toEqual(stylesExpectWithLazy); |
| 128 | + expect(config.configurations.production.styles).toEqual(stylesExpectWithLazy); |
| 129 | + }); |
112 | 130 | });
|
113 | 131 |
|
114 |
| - it('should update styles in test target', () => { |
115 |
| - let config = readWorkspaceConfig(tree); |
116 |
| - let test = config.projects['migration-test'].architect.test; |
117 |
| - test.options.styles = stylesWithLazy; |
118 |
| - test.configurations = { production: { styles: stylesWithLazy } }; |
119 |
| - |
120 |
| - tree.overwrite(workspacePath, JSON.stringify(config)); |
121 |
| - const tree2 = schematicRunner.runSchematic('migration-09', {}, tree.branch()); |
122 |
| - config = readWorkspaceConfig(tree2); |
123 |
| - test = config.projects['migration-test'].architect.test; |
124 |
| - expect(test.options.styles).toEqual(stylesExpectWithLazy); |
125 |
| - expect(test.configurations.production.styles).toEqual(stylesExpectWithLazy); |
| 132 | + describe('anyComponentStyle bundle budget', () => { |
| 133 | + it('should not append budget when already exists', () => { |
| 134 | + const defaultBudget = [ |
| 135 | + { type: 'initial', maximumWarning: '2mb', maximumError: '5mb' }, |
| 136 | + { type: 'anyComponentStyle', maximumWarning: '10kb', maximumError: '50kb' }, |
| 137 | + ]; |
| 138 | + |
| 139 | + let config = getWorkspaceTargets(tree); |
| 140 | + config.build.configurations.production.budgets = defaultBudget; |
| 141 | + updateWorkspaceTargets(tree, config); |
| 142 | + |
| 143 | + const tree2 = schematicRunner.runSchematic('migration-09', {}, tree.branch()); |
| 144 | + config = getWorkspaceTargets(tree2).build; |
| 145 | + expect(config.configurations.production.budgets).toEqual(defaultBudget); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should append budget in build target', () => { |
| 149 | + const defaultBudget = [{ type: 'initial', maximumWarning: '2mb', maximumError: '5mb' }]; |
| 150 | + let config = getWorkspaceTargets(tree); |
| 151 | + config.build.configurations.production.budgets = defaultBudget; |
| 152 | + updateWorkspaceTargets(tree, config); |
| 153 | + |
| 154 | + const tree2 = schematicRunner.runSchematic('migration-09', {}, tree.branch()); |
| 155 | + config = getWorkspaceTargets(tree2).build; |
| 156 | + expect(config.configurations.production.budgets).toEqual([ |
| 157 | + ...defaultBudget, |
| 158 | + ANY_COMPONENT_STYLE_BUDGET, |
| 159 | + ]); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should add budget in build target', () => { |
| 163 | + let config = getWorkspaceTargets(tree); |
| 164 | + config.build.configurations.production.budgets = undefined; |
| 165 | + updateWorkspaceTargets(tree, config); |
| 166 | + |
| 167 | + const tree2 = schematicRunner.runSchematic('migration-09', {}, tree.branch()); |
| 168 | + config = getWorkspaceTargets(tree2).build; |
| 169 | + expect(config.configurations.production.budgets).toEqual([ANY_COMPONENT_STYLE_BUDGET]); |
| 170 | + }); |
126 | 171 | });
|
127 | 172 | });
|
128 | 173 | });
|
0 commit comments