77 */
88
99import { SchematicTestRunner , UnitTestTree } from '@angular-devkit/schematics/testing' ;
10+
1011import { Schema as ApplicationOptions } from '../application/schema' ;
1112import { Schema as WorkspaceOptions } from '../workspace/schema' ;
13+
1214import { Schema as GuardOptions } from './schema' ;
1315
1416describe ( 'Guard Schematic' , ( ) => {
@@ -90,6 +92,37 @@ describe('Guard Schematic', () => {
9092 expect ( fileString ) . not . toContain ( 'canLoad' ) ;
9193 } ) ;
9294
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+
93126 it ( 'should respect the implements values' , async ( ) => {
94127 const implementationOptions = [ 'CanActivate' , 'CanLoad' , 'CanActivateChild' ] ;
95128 const options = { ...defaultOptions , implements : implementationOptions } ;
@@ -104,18 +137,6 @@ describe('Guard Schematic', () => {
104137 } ) ;
105138 } ) ;
106139
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-
119140 it ( 'should add correct imports based on CanLoad implementation' , async ( ) => {
120141 const implementationOptions = [ 'CanLoad' ] ;
121142 const options = { ...defaultOptions , implements : implementationOptions } ;
@@ -136,6 +157,15 @@ describe('Guard Schematic', () => {
136157 expect ( fileString ) . toContain ( expectedImports ) ;
137158 } ) ;
138159
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+
139169 it ( 'should add correct imports based on CanActivate implementation' , async ( ) => {
140170 const implementationOptions = [ 'CanActivate' ] ;
141171 const options = { ...defaultOptions , implements : implementationOptions } ;
@@ -146,6 +176,15 @@ describe('Guard Schematic', () => {
146176 expect ( fileString ) . toContain ( expectedImports ) ;
147177 } ) ;
148178
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+
149188 it ( 'should add correct imports if multiple implementations was selected' , async ( ) => {
150189 const implementationOptions = [ 'CanActivate' , 'CanLoad' , 'CanActivateChild' ] ;
151190 const options = { ...defaultOptions , implements : implementationOptions } ;
0 commit comments