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