@@ -38,12 +38,14 @@ describe('Guard Schematic', () => {
38
38
let appTree : UnitTestTree ;
39
39
beforeEach ( async ( ) => {
40
40
appTree = await schematicRunner . runSchematicAsync ( 'workspace' , workspaceOptions ) . toPromise ( ) ;
41
- appTree = await schematicRunner . runSchematicAsync ( 'application' , appOptions , appTree )
41
+ appTree = await schematicRunner
42
+ . runSchematicAsync ( 'application' , appOptions , appTree )
42
43
. toPromise ( ) ;
43
44
} ) ;
44
45
45
46
it ( 'should create a guard' , async ( ) => {
46
- const tree = await schematicRunner . runSchematicAsync ( 'guard' , defaultOptions , appTree )
47
+ const tree = await schematicRunner
48
+ . runSchematicAsync ( 'guard' , defaultOptions , appTree )
47
49
. toPromise ( ) ;
48
50
const files = tree . files ;
49
51
expect ( files ) . toContain ( '/projects/bar/src/app/foo.guard.spec.ts' ) ;
@@ -53,8 +55,7 @@ describe('Guard Schematic', () => {
53
55
it ( 'should respect the skipTests flag' , async ( ) => {
54
56
const options = { ...defaultOptions , skipTests : true } ;
55
57
56
- const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree )
57
- . toPromise ( ) ;
58
+ const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree ) . toPromise ( ) ;
58
59
const files = tree . files ;
59
60
expect ( files ) . not . toContain ( '/projects/bar/src/app/foo.guard.spec.ts' ) ;
60
61
expect ( files ) . toContain ( '/projects/bar/src/app/foo.guard.ts' ) ;
@@ -63,8 +64,7 @@ describe('Guard Schematic', () => {
63
64
it ( 'should respect the flat flag' , async ( ) => {
64
65
const options = { ...defaultOptions , flat : false } ;
65
66
66
- const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree )
67
- . toPromise ( ) ;
67
+ const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree ) . toPromise ( ) ;
68
68
const files = tree . files ;
69
69
expect ( files ) . toContain ( '/projects/bar/src/app/foo/foo.guard.spec.ts' ) ;
70
70
expect ( files ) . toContain ( '/projects/bar/src/app/foo/foo.guard.ts' ) ;
@@ -74,15 +74,13 @@ describe('Guard Schematic', () => {
74
74
const config = JSON . parse ( appTree . readContent ( '/angular.json' ) ) ;
75
75
config . projects . bar . sourceRoot = 'projects/bar/custom' ;
76
76
appTree . overwrite ( '/angular.json' , JSON . stringify ( config , null , 2 ) ) ;
77
- appTree = await schematicRunner . runSchematicAsync ( 'guard' , defaultOptions , appTree )
78
- . toPromise ( ) ;
77
+ appTree = await schematicRunner . runSchematicAsync ( 'guard' , defaultOptions , appTree ) . toPromise ( ) ;
79
78
expect ( appTree . files ) . toContain ( '/projects/bar/custom/app/foo.guard.ts' ) ;
80
79
} ) ;
81
80
82
81
it ( 'should respect the implements value' , async ( ) => {
83
- const options = { ...defaultOptions , implements : [ 'CanActivate' ] } ;
84
- const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree )
85
- . toPromise ( ) ;
82
+ const options = { ...defaultOptions , implements : [ 'CanActivate' ] } ;
83
+ const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree ) . toPromise ( ) ;
86
84
const fileString = tree . readContent ( '/projects/bar/src/app/foo.guard.ts' ) ;
87
85
expect ( fileString ) . toContain ( 'CanActivate' ) ;
88
86
expect ( fileString ) . toContain ( 'canActivate' ) ;
@@ -94,9 +92,8 @@ describe('Guard Schematic', () => {
94
92
95
93
it ( 'should respect the implements values' , async ( ) => {
96
94
const implementationOptions = [ 'CanActivate' , 'CanLoad' , 'CanActivateChild' ] ;
97
- const options = { ...defaultOptions , implements : implementationOptions } ;
98
- const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree )
99
- . toPromise ( ) ;
95
+ const options = { ...defaultOptions , implements : implementationOptions } ;
96
+ const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree ) . toPromise ( ) ;
100
97
const fileString = tree . readContent ( '/projects/bar/src/app/foo.guard.ts' ) ;
101
98
102
99
// Should contain all implementations
@@ -109,8 +106,7 @@ describe('Guard Schematic', () => {
109
106
110
107
it ( 'should use CanActivate if no implements value' , async ( ) => {
111
108
const options = { ...defaultOptions , implements : undefined } ;
112
- const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree )
113
- . toPromise ( ) ;
109
+ const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree ) . toPromise ( ) ;
114
110
const fileString = tree . readContent ( '/projects/bar/src/app/foo.guard.ts' ) ;
115
111
expect ( fileString ) . toContain ( 'CanActivate' ) ;
116
112
expect ( fileString ) . toContain ( 'canActivate' ) ;
@@ -119,4 +115,37 @@ describe('Guard Schematic', () => {
119
115
expect ( fileString ) . not . toContain ( 'CanLoad' ) ;
120
116
expect ( fileString ) . not . toContain ( 'canLoad' ) ;
121
117
} ) ;
118
+
119
+ it ( 'should add correct imports based on CanLoad implementation' , async ( ) => {
120
+ const implementationOptions = [ 'CanLoad' ] ;
121
+ const options = { ...defaultOptions , implements : implementationOptions } ;
122
+ const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree ) . toPromise ( ) ;
123
+ const fileString = tree . readContent ( '/projects/bar/src/app/foo.guard.ts' ) ;
124
+ const expectedImports = `import { CanLoad, Route, UrlSegment, UrlTree } from '@angular/router';` ;
125
+
126
+ expect ( fileString ) . toContain ( expectedImports ) ;
127
+ } ) ;
128
+
129
+ it ( 'should add correct imports based on CanActivate implementation' , async ( ) => {
130
+ const implementationOptions = [ 'CanActivate' ] ;
131
+ const options = { ...defaultOptions , implements : implementationOptions } ;
132
+ const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree ) . toPromise ( ) ;
133
+ const fileString = tree . readContent ( '/projects/bar/src/app/foo.guard.ts' ) ;
134
+ const expectedImports = `import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, UrlTree } from '@angular/router';` ;
135
+
136
+ expect ( fileString ) . toContain ( expectedImports ) ;
137
+ } ) ;
138
+
139
+ it ( 'should add correct imports if multiple implementations was selected' , async ( ) => {
140
+ const implementationOptions = [ 'CanActivate' , 'CanLoad' , 'CanActivateChild' ] ;
141
+ const options = { ...defaultOptions , implements : implementationOptions } ;
142
+ const tree = await schematicRunner . runSchematicAsync ( 'guard' , options , appTree ) . toPromise ( ) ;
143
+ const fileString = tree . readContent ( '/projects/bar/src/app/foo.guard.ts' ) ;
144
+ const expectedImports =
145
+ `import ` +
146
+ `{ ActivatedRouteSnapshot, CanActivate, CanActivateChild, CanLoad, Route, RouterStateSnapshot, UrlSegment, UrlTree } ` +
147
+ `from '@angular/router';` ;
148
+
149
+ expect ( fileString ) . toContain ( expectedImports ) ;
150
+ } ) ;
122
151
} ) ;
0 commit comments