forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_spec.ts
205 lines (175 loc) · 9.19 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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { Schema as ApplicationOptions } from '../application/schema';
import { Schema as WorkspaceOptions } from '../workspace/schema';
import { Schema as GuardOptions } from './schema';
describe('Guard Schematic', () => {
const schematicRunner = new SchematicTestRunner(
'@schematics/angular',
require.resolve('../collection.json'),
);
const defaultOptions: GuardOptions = {
name: 'foo',
flat: true,
project: 'bar',
};
const workspaceOptions: WorkspaceOptions = {
name: 'workspace',
newProjectRoot: 'projects',
version: '6.0.0',
};
const appOptions: ApplicationOptions = {
name: 'bar',
inlineStyle: false,
inlineTemplate: false,
routing: false,
skipTests: false,
skipPackageJson: false,
};
let appTree: UnitTestTree;
beforeEach(async () => {
appTree = await schematicRunner.runSchematic('workspace', workspaceOptions);
appTree = await schematicRunner.runSchematic('application', appOptions, appTree);
});
it('should create a (deprecated) class-based guard with --no-functional', async () => {
const tree = await schematicRunner.runSchematic(
'guard',
{ ...defaultOptions, functional: false },
appTree,
);
const files = tree.files;
expect(files).toContain('/projects/bar/src/app/foo-guard.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo-guard.ts');
});
it('should respect the skipTests flag', async () => {
const options = { ...defaultOptions, skipTests: true };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const files = tree.files;
expect(files).not.toContain('/projects/bar/src/app/foo-guard.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo-guard.ts');
});
it('should use a `.` type separator when specified', async () => {
const options = { ...defaultOptions, typeSeparator: '.' };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const files = tree.files;
expect(files).toContain('/projects/bar/src/app/foo.guard.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo.guard.ts');
const specContent = tree.readContent('/projects/bar/src/app/foo.guard.spec.ts');
expect(specContent).toContain(`'./foo.guard'`);
});
it('should use a `-` type separator when specified', async () => {
const options = { ...defaultOptions, typeSeparator: '-' };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const files = tree.files;
expect(files).toContain('/projects/bar/src/app/foo-guard.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo-guard.ts');
const specContent = tree.readContent('/projects/bar/src/app/foo-guard.spec.ts');
expect(specContent).toContain(`'./foo-guard'`);
});
it('should respect the flat flag', async () => {
const options = { ...defaultOptions, flat: false };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const files = tree.files;
expect(files).toContain('/projects/bar/src/app/foo/foo-guard.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo/foo-guard.ts');
});
it('should respect the sourceRoot value', async () => {
const config = JSON.parse(appTree.readContent('/angular.json'));
config.projects.bar.sourceRoot = 'projects/bar/custom';
appTree.overwrite('/angular.json', JSON.stringify(config, null, 2));
appTree = await schematicRunner.runSchematic('guard', defaultOptions, appTree);
expect(appTree.files).toContain('/projects/bar/custom/app/foo-guard.ts');
});
it('should respect the implements value', async () => {
const options = { ...defaultOptions, implements: ['CanActivate'], functional: false };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.ts');
expect(fileString).toContain('CanActivate');
expect(fileString).toContain('canActivate');
expect(fileString).not.toContain('CanActivateChild');
expect(fileString).not.toContain('canActivateChild');
expect(fileString).not.toContain('CanMatch');
expect(fileString).not.toContain('canMatch');
});
it('should generate a functional guard by default', async () => {
const options = { ...defaultOptions, implements: ['CanActivate'] };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.ts');
expect(fileString).toContain('export const fooGuard: CanActivateFn = (route, state) => {');
expect(fileString).not.toContain('CanActivateChild');
expect(fileString).not.toContain('canActivateChild');
expect(fileString).not.toContain('CanMatch');
expect(fileString).not.toContain('canMatch');
});
it('should generate a helper function to execute the guard in a test', async () => {
const options = { ...defaultOptions, implements: ['CanActivate'] };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.spec.ts');
expect(fileString).toContain('const executeGuard: CanActivateFn = (...guardParameters) => ');
expect(fileString).toContain(
'TestBed.runInInjectionContext(() => fooGuard(...guardParameters));',
);
});
it('should generate CanDeactivateFn with unknown functional guard', async () => {
const options = { ...defaultOptions, implements: ['CanDeactivate'] };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.ts');
expect(fileString).toContain(
'export const fooGuard: CanDeactivateFn<unknown> = ' +
'(component, currentRoute, currentState, nextState) => {',
);
});
it('should respect the implements values in (deprecated) class-based guards', async () => {
const implementationOptions = ['CanActivate', 'CanDeactivate', 'CanActivateChild'];
const options = { ...defaultOptions, implements: implementationOptions, functional: false };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.ts');
// Should contain all implementations
implementationOptions.forEach((implementation: string) => {
expect(fileString).toContain(implementation);
const functionName = `${implementation.charAt(0).toLowerCase()}${implementation.slice(1)}`;
expect(fileString).toContain(functionName);
});
});
it('should add correct imports based on CanMatch implementation in (deprecated) class-based guards', async () => {
const implementationOptions = ['CanMatch'];
const options = { ...defaultOptions, implements: implementationOptions, functional: false };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.ts');
const expectedImports = `import { CanMatch, GuardResult, MaybeAsync, Route, subPath } from '@angular/router';`;
expect(fileString).toContain(expectedImports);
});
it('should add correct imports based on CanActivate implementation in (deprecated) class-based guards', async () => {
const implementationOptions = ['CanActivate'];
const options = { ...defaultOptions, implements: implementationOptions, functional: false };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.ts');
const expectedImports =
`import { ActivatedRouteSnapshot, CanActivate, GuardResult, ` +
`MaybeAsync, RouterStateSnapshot } from '@angular/router';`;
expect(fileString).toContain(expectedImports);
});
it('should add correct imports based on canActivate functional guard', async () => {
const options = { ...defaultOptions, implements: ['CanActivate'] };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.ts');
const expectedImports = `import { CanActivateFn } from '@angular/router';`;
expect(fileString).toContain(expectedImports);
});
it('should add correct imports if multiple implementations was selected in (deprecated) class-based guards', async () => {
const implementationOptions = ['CanActivate', 'CanMatch', 'CanActivateChild'];
const options = { ...defaultOptions, implements: implementationOptions, functional: false };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.ts');
const expectedImports =
`import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, CanMatch, GuardResult, ` +
`MaybeAsync, Route, RouterStateSnapshot, subPath } from '@angular/router';`;
expect(fileString).toContain(expectedImports);
});
});