7
7
*/
8
8
9
9
import { SchematicTestRunner , UnitTestTree } from '@angular-devkit/schematics/testing' ;
10
+
10
11
import { Schema as ApplicationOptions } from '../application/schema' ;
11
12
import { Schema as WorkspaceOptions } from '../workspace/schema' ;
13
+
12
14
import { Schema as GuardOptions } from './schema' ;
13
15
14
16
describe ( 'Guard Schematic' , ( ) => {
@@ -90,6 +92,37 @@ describe('Guard Schematic', () => {
90
92
expect ( fileString ) . not . toContain ( 'canLoad' ) ;
91
93
} ) ;
92
94
95
+ it ( 'should respect the guardType value' , async ( ) => {
96
+ const options = { ...defaultOptions , guardType : 'canActivate' } ;
97
+ const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree ) . toPromise ( ) ;
98
+ const fileString = tree . readContent ( '/projects/bar/src/app/foo.guard.ts' ) ;
99
+ expect ( fileString ) . toContain ( 'export const fooGuard: CanActivateFn = (route, state) => {' ) ;
100
+ expect ( fileString ) . not . toContain ( 'CanActivateChild' ) ;
101
+ expect ( fileString ) . not . toContain ( 'canActivateChild' ) ;
102
+ expect ( fileString ) . not . toContain ( 'CanLoad' ) ;
103
+ expect ( fileString ) . not . toContain ( 'canLoad' ) ;
104
+ } ) ;
105
+
106
+ it ( 'should generate a helper function to execute the guard in a test' , async ( ) => {
107
+ const options = { ...defaultOptions , guardType : 'canActivate' } ;
108
+ const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree ) . toPromise ( ) ;
109
+ const fileString = tree . readContent ( '/projects/bar/src/app/foo.guard.spec.ts' ) ;
110
+ expect ( fileString ) . toContain ( 'const executeGuard: CanActivateFn = (...guardParameters) => ' ) ;
111
+ expect ( fileString ) . toContain (
112
+ 'TestBed.inject(EnvironmentInjector).runInContext(() => fooGuard(...guardParameters));' ,
113
+ ) ;
114
+ } ) ;
115
+
116
+ it ( 'should generate CanDeactivateFn with unknown guardType' , async ( ) => {
117
+ const options = { ...defaultOptions , guardType : 'canDeactivate' } ;
118
+ const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree ) . toPromise ( ) ;
119
+ const fileString = tree . readContent ( '/projects/bar/src/app/foo.guard.ts' ) ;
120
+ expect ( fileString ) . toContain (
121
+ 'export const fooGuard: CanDeactivateFn<unknown> = ' +
122
+ '(component, currentRoute, currentState, nextState) => {' ,
123
+ ) ;
124
+ } ) ;
125
+
93
126
it ( 'should respect the implements values' , async ( ) => {
94
127
const implementationOptions = [ 'CanActivate' , 'CanLoad' , 'CanActivateChild' ] ;
95
128
const options = { ...defaultOptions , implements : implementationOptions } ;
@@ -104,18 +137,6 @@ describe('Guard Schematic', () => {
104
137
} ) ;
105
138
} ) ;
106
139
107
- it ( 'should use CanActivate if no implements value' , async ( ) => {
108
- const options = { ...defaultOptions , implements : undefined } ;
109
- const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree ) . toPromise ( ) ;
110
- const fileString = tree . readContent ( '/projects/bar/src/app/foo.guard.ts' ) ;
111
- expect ( fileString ) . toContain ( 'CanActivate' ) ;
112
- expect ( fileString ) . toContain ( 'canActivate' ) ;
113
- expect ( fileString ) . not . toContain ( 'CanActivateChild' ) ;
114
- expect ( fileString ) . not . toContain ( 'canActivateChild' ) ;
115
- expect ( fileString ) . not . toContain ( 'CanLoad' ) ;
116
- expect ( fileString ) . not . toContain ( 'canLoad' ) ;
117
- } ) ;
118
-
119
140
it ( 'should add correct imports based on CanLoad implementation' , async ( ) => {
120
141
const implementationOptions = [ 'CanLoad' ] ;
121
142
const options = { ...defaultOptions , implements : implementationOptions } ;
@@ -136,6 +157,15 @@ describe('Guard Schematic', () => {
136
157
expect ( fileString ) . toContain ( expectedImports ) ;
137
158
} ) ;
138
159
160
+ it ( 'should add correct imports based on canLoad guardType' , async ( ) => {
161
+ const options = { ...defaultOptions , guardType : 'canLoad' } ;
162
+ const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree ) . toPromise ( ) ;
163
+ const fileString = tree . readContent ( '/projects/bar/src/app/foo.guard.ts' ) ;
164
+ const expectedImports = `import { CanLoadFn } from '@angular/router';` ;
165
+
166
+ expect ( fileString ) . toContain ( expectedImports ) ;
167
+ } ) ;
168
+
139
169
it ( 'should add correct imports based on CanActivate implementation' , async ( ) => {
140
170
const implementationOptions = [ 'CanActivate' ] ;
141
171
const options = { ...defaultOptions , implements : implementationOptions } ;
@@ -146,6 +176,15 @@ describe('Guard Schematic', () => {
146
176
expect ( fileString ) . toContain ( expectedImports ) ;
147
177
} ) ;
148
178
179
+ it ( 'should add correct imports based on canActivate guardType' , async ( ) => {
180
+ const options = { ...defaultOptions , guardType : 'canActivate' } ;
181
+ const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree ) . toPromise ( ) ;
182
+ const fileString = tree . readContent ( '/projects/bar/src/app/foo.guard.ts' ) ;
183
+ const expectedImports = `import { CanActivateFn } from '@angular/router';` ;
184
+
185
+ expect ( fileString ) . toContain ( expectedImports ) ;
186
+ } ) ;
187
+
149
188
it ( 'should add correct imports if multiple implementations was selected' , async ( ) => {
150
189
const implementationOptions = [ 'CanActivate' , 'CanLoad' , 'CanActivateChild' ] ;
151
190
const options = { ...defaultOptions , implements : implementationOptions } ;
0 commit comments