This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index_spec.ts
324 lines (263 loc) · 13.5 KB
/
index_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { Schema as WorkspaceOptions } from '@schematics/angular/workspace/schema';
import {
NodeDependencyType,
addPackageJsonDependency,
removePackageJsonDependency
} from '@schematics/angular/utility/dependencies';
import * as path from 'path';
import { modifyJSONFile } from '../utility/modify-json-file';
const collectionPath = path.join(__dirname, '../collection.json');
describe('layout', () => {
const appOptions: any = {
name: 'testApp',
projectRoot: '',
inlineStyle: false,
inlineTemplate: false,
routing: true,
style: 'scss',
skipTests: false,
skipPackageJson: false
};
const workspaceOptions: WorkspaceOptions = {
name: 'workspace',
version: '6.0.0'
};
const options: any = {
layout: 'side-nav-outer-toolbar',
resolveConflicts: 'override'
};
const angularSchematicsCollection = require.resolve('../../node_modules/@schematics/angular/collection.json');
const schematicRunner = new SchematicTestRunner('@schematics/angular', angularSchematicsCollection);
let appTree: UnitTestTree;
beforeEach(async () => {
appTree = await schematicRunner.runSchematicAsync('workspace', workspaceOptions).toPromise();
appTree = await schematicRunner.runSchematicAsync('application', appOptions, appTree).toPromise();
});
it('should add layout with override', async () => {
const runner = new SchematicTestRunner('schematics', collectionPath);
const tree = await runner.runSchematicAsync('add-layout', options, appTree).toPromise();
expect(tree.files)
.toContain('/devextreme.json');
expect(tree.files)
.toContain('/src/app/app-navigation.ts');
expect(tree.files)
.toContain('/src/app/shared/components/header/header.component.ts');
expect(tree.files)
.toContain('/src/app/shared/components/login-form/login-form.component.ts');
expect(tree.files)
.toContain('/src/app/shared/components/side-navigation-menu/side-navigation-menu.component.ts');
expect(tree.files)
.toContain('/src/app/shared/services/app-info.service.ts');
expect(tree.files)
.toContain('/src/app/shared/services/auth.service.ts');
expect(tree.files)
.toContain('/src/app/shared/services/screen.service.ts');
expect(tree.files)
.toContain('/src/app/layouts/side-nav-outer-toolbar/side-nav-outer-toolbar.component.ts');
expect(tree.files)
.toContain('/src/app/layouts/side-nav-inner-toolbar/side-nav-inner-toolbar.component.ts');
expect(tree.files)
.toContain('/src/app/layouts/single-card/single-card.component.ts');
expect(tree.files)
.toContain('/src/themes/metadata.base.json');
expect(tree.files)
.toContain('/src/themes/metadata.additional.json');
const devextremeConfigContent = tree.readContent('/devextreme.json');
expect(devextremeConfigContent).toContain('"applicationEngine": "angular"');
expect(devextremeConfigContent).toContain('"inputFile": "src/themes/metadata.additional.json"');
const componentContent = tree.readContent('/src/app/app.component.html');
expect(componentContent).toContain('app-side-nav-outer-toolbar title="{{appInfo.title}}"');
const stylesContent = tree.readContent('/src/dx-styles.scss');
expect(stylesContent).toMatch(/html, body {/);
const indexContent = tree.readContent('/src/index.html');
expect(indexContent).toMatch(/<body class="dx-viewport">/);
const angularContent = JSON.parse(tree.readContent('/angular.json'));
const styles = angularContent.projects.testApp.architect.build.options.styles;
expect(styles[0]).toBe('node_modules/devextreme/dist/css/dx.common.css');
expect(styles[1]).toBe('src/themes/generated/theme.base.css');
expect(styles[2]).toBe('src/themes/generated/theme.additional.css');
const moduleContent = tree.readContent('/src/app/app.module.ts');
expect(moduleContent)
.toContain('import { SideNavOuterToolbarModule, SideNavInnerToolbarModule, SingleCardModule }');
expect(moduleContent)
.toContain(`import { AuthService, ScreenService, AppInfoService } from './shared/services';`);
expect(moduleContent).toContain('import { AppRoutingModule }');
const testContent = tree.readContent('/e2e/src/app.e2e-spec.ts');
expect(testContent).toMatch(/'Welcome to TestApp!'/);
const testUtilsContent = tree.readContent('/e2e/src/app.po.ts');
expect(testUtilsContent).toMatch(/'app-root .dx-drawer-content .dx-card p:nth-child\(2\)'/);
const appContent = tree.readContent('/src/app/app.component.ts');
expect(appContent).toContain('templateUrl: \'./app.component.html\',');
expect(appContent).toContain('styleUrls: [\'./app.component.scss\']');
expect(appContent).toContain('selector: \'app-root\',');
expect(appContent).toContain(`import { AuthService, ScreenService, AppInfoService } from './shared/services';`);
const navigationMenu = tree.readContent(
'/src/app/shared/components/side-navigation-menu/side-navigation-menu.component.ts');
expect(navigationMenu).toContain('@ViewChild(DxTreeViewComponent, { static: true })');
});
it('should add npm scripts', async () => {
const runner = new SchematicTestRunner('schematics', collectionPath);
const tree = await runner.runSchematicAsync('add-layout', options, appTree).toPromise();
const packageConfig = JSON.parse(tree.readContent('package.json'));
expect(packageConfig.scripts['build-themes']).toBe('devextreme build');
expect(packageConfig.scripts['postinstall']).toBe('npm run build-themes');
});
it('should set static flag', async () => {
removePackageJsonDependency(appTree, '@angular/core');
addPackageJsonDependency(appTree, {
type: NodeDependencyType.Default,
name: '@angular/core',
version: '7.0.0'
});
const runner = new SchematicTestRunner('schematics', collectionPath);
const tree = await runner.runSchematicAsync('add-layout', options, appTree).toPromise();
const navigationMenu = tree.readContent(
'/src/app/shared/components/side-navigation-menu/side-navigation-menu.component.ts');
expect(navigationMenu).toContain('@ViewChild(DxTreeViewComponent)');
});
it('should add npm scripts safely', async () => {
modifyJSONFile(appTree, './package.json', config => {
const scripts = config['scripts'];
scripts['build-themes'] = 'prev value 1';
scripts['postinstall'] = 'prev value 2';
return config;
});
const runner = new SchematicTestRunner('schematics', collectionPath);
const tree = await runner.runSchematicAsync('add-layout', options, appTree).toPromise();
const packageConfig = JSON.parse(tree.readContent('package.json'));
expect(packageConfig.scripts['origin-build-themes']).toBe('prev value 1');
expect(packageConfig.scripts['origin-postinstall']).toBe('prev value 2');
expect(packageConfig.scripts['build-themes']).toBe('npm run origin-build-themes && devextreme build');
expect(packageConfig.scripts['postinstall']).toBe('npm run origin-postinstall && npm run build-themes');
});
it('should add angular/cdk dependency', async () => {
const runner = new SchematicTestRunner('schematics', collectionPath);
const tree = await runner.runSchematicAsync('add-layout', options, appTree).toPromise();
const packageConfig = JSON.parse(tree.readContent('package.json'));
expect(packageConfig.dependencies['@angular/cdk']).toBeDefined();
});
it('should update budgets if updateBudgets option is true', async () => {
const runner = new SchematicTestRunner('schematics', collectionPath);
const tree = await runner.runSchematicAsync('add-layout', {
...options,
updateBudgets: true
}, appTree).toPromise();
const angularContent = JSON.parse(tree.readContent('/angular.json'));
const budgets = angularContent.projects.testApp.architect.build.configurations.production.budgets;
expect(budgets.length).toBe(2);
expect(budgets[0]).toEqual({
type: 'initial',
maximumWarning: '4mb',
maximumError: '6mb'
});
});
it('should not update budgets if updateBudgets option is not defined or false', async () => {
const runner = new SchematicTestRunner('schematics', collectionPath);
const tree = await runner.runSchematicAsync('add-layout', options, appTree).toPromise();
const angularContent = JSON.parse(tree.readContent('/angular.json'));
const budgets = angularContent.projects.testApp.architect.build.configurations.production.budgets;
const defaultBudget = {
type: 'initial',
maximumWarning: '2mb',
maximumError: '5mb'
};
expect(budgets.length).toBe(2);
expect(budgets[0]).toEqual(defaultBudget);
});
it('should add layout without override', async () => {
const runner = new SchematicTestRunner('schematics', collectionPath);
options.resolveConflicts = 'createNew';
const tree = await runner.runSchematicAsync('add-layout', options, appTree).toPromise();
expect(tree.files).toContain('/src/app/app1.component.ts');
const componentContent = tree.readContent('/src/app/app1.component.html');
expect(componentContent).toContain('app-side-nav-outer-toolbar title="{{appInfo.title}}"');
const appContent = tree.readContent('/src/app/app.component.ts');
expect(appContent).toMatch(/templateUrl: '.\/app.component.html',/);
expect(appContent).toMatch(/styleUrls: \['.\/app.component.scss'\]/);
const newAppContent = tree.readContent('/src/app/app1.component.ts');
expect(newAppContent).toMatch(/templateUrl: '.\/app1.component.html',/);
expect(newAppContent).toMatch(/styleUrls: \['.\/app1.component.scss'\]/);
expect(newAppContent).toContain(`import { AuthService, ScreenService, AppInfoService } from './shared/services';`);
const appInfo = tree.readContent('/src/app/shared/services/app-info.service.ts');
expect(appInfo).toContain(`return 'TestApp';`);
});
it('should add routing to layout', async () => {
let newAppTree = await schematicRunner.runSchematicAsync('workspace', workspaceOptions).toPromise();
appOptions.routing = false;
newAppTree = await schematicRunner.runSchematicAsync(
'application', appOptions, newAppTree).toPromise();
const runner = new SchematicTestRunner('schematics', collectionPath);
const tree = await runner.runSchematicAsync('add-layout', options, appTree).toPromise();
expect(tree.files).toContain('/src/app/app-routing.module.ts');
const moduleContent = tree.readContent('/src/app/app-routing.module.ts');
expect(moduleContent)
.toMatch(/imports:\s\[RouterModule\.forRoot\(routes\)\],/);
expect(moduleContent)
.toContain(`{
path: 'login-form',
component: LoginFormComponent,
canActivate: [ AuthGuardService ]
}`);
});
it('should use selected layout', async () => {
const runner = new SchematicTestRunner('schematics', collectionPath);
options.layout = 'side-nav-inner-toolbar';
options.resolveConflicts = 'override';
const tree = await runner.runSchematicAsync('add-layout', options, appTree).toPromise();
const content = tree.readContent('/src/app/app.component.html');
expect(content).toContain('app-side-nav-inner-toolbar title="{{appInfo.title}}"');
});
it('should consider the `project` option', async () => {
appTree = await schematicRunner.runSchematicAsync('application', {
...appOptions,
name: 'testApp2',
projectRoot: 'projects/testApp2'
}, appTree).toPromise();
const runner = new SchematicTestRunner('schematics', collectionPath);
const tree = await runner.runSchematicAsync('add-layout', {
...options,
project: 'testApp2'
}, appTree).toPromise();
expect(tree.files)
.toContain('/devextreme.json');
expect(tree.files)
.toContain('/projects/testApp2/src/themes/metadata.base.json');
});
it('should merge build commands in devextreme.json file', async () => {
appTree = await schematicRunner.runSchematicAsync('application', {
...appOptions,
name: 'testApp2',
prefix: 'app2',
projectRoot: 'projects/testApp2'
}, appTree).toPromise();
const runner = new SchematicTestRunner('schematics', collectionPath);
let tree = await runner.runSchematicAsync('add-layout', options, appTree).toPromise();
tree = await runner.runSchematicAsync('add-layout', {
...options,
project: 'testApp2'
}, appTree).toPromise();
const appContent = tree.readContent('projects/testApp2/src/app/app.component.ts');
expect(appContent).toContain('selector: \'app2-root\',');
const content = tree.readContent('/devextreme.json');
expect(content).toContain('"inputFile": "src/themes/metadata.base.json",');
expect(content).toContain('"inputFile": "projects/testApp2/src/themes/metadata.base.json",');
});
it('should add e2e tests only for default project', async () => {
appTree = await schematicRunner.runSchematicAsync('application', {
...appOptions,
name: 'testApp2',
projectRoot: 'projects/testApp2'
}, appTree).toPromise();
const runner = new SchematicTestRunner('schematics', collectionPath);
let tree = await runner.runSchematicAsync('add-layout', options, appTree).toPromise();
tree = await runner.runSchematicAsync('add-layout', {
...options,
project: 'testApp2'
}, appTree).toPromise();
const testContent = tree.readContent('/e2e/src/app.e2e-spec.ts');
expect(testContent).toContain('Welcome to TestApp!');
const testUtilsContent = tree.readContent('/e2e/src/app.po.ts');
expect(testUtilsContent).toMatch(/'app-root .dx-drawer-content .dx-card p:nth-child\(2\)'/);
});
});