@@ -39,27 +39,77 @@ describe('ng-add', () => {
39
39
} ) ;
40
40
} ) ;
41
41
42
+ describe ( 'project selection' , ( ) => {
43
+ it ( 'should select the default project when not explicitly given (for compatibility!)' , async ( ) => {
44
+ const tree = Tree . empty ( ) ;
45
+ const angularJSON = generateAngularJson ( ) ;
46
+
47
+ ( angularJSON as any ) . defaultProject = PROJECT_NAME ; // explicitly set default project
48
+ tree . create ( 'angular.json' , JSON . stringify ( angularJSON ) ) ;
49
+
50
+ const resultTree = await ngAdd ( { project : '' } ) (
51
+ tree ,
52
+ { } as SchematicContext
53
+ ) ;
54
+
55
+ const resultConfig = readJSONFromTree ( resultTree , 'angular.json' ) ;
56
+ expect ( resultConfig . projects [ PROJECT_NAME ] . architect . deploy ) . toBeTruthy ( ) ;
57
+ expect (
58
+ resultConfig . projects [ OTHER_PROJECT_NAME ] . architect . deploy
59
+ ) . toBeFalsy ( ) ;
60
+ } ) ;
61
+
62
+ it ( 'should select the explicitly defined project' , async ( ) => {
63
+ const tree = Tree . empty ( ) ;
64
+ const angularJSON = generateAngularJson ( ) ;
65
+ tree . create ( 'angular.json' , JSON . stringify ( angularJSON ) ) ;
66
+
67
+ const resultTree = await ngAdd ( { project : OTHER_PROJECT_NAME } ) (
68
+ tree ,
69
+ { } as SchematicContext
70
+ ) ;
71
+
72
+ const resultConfig = readJSONFromTree ( resultTree , 'angular.json' ) ;
73
+ expect (
74
+ resultConfig . projects [ OTHER_PROJECT_NAME ] . architect . deploy
75
+ ) . toBeTruthy ( ) ;
76
+ expect ( resultConfig . projects [ PROJECT_NAME ] . architect . deploy ) . toBeFalsy ( ) ;
77
+ } ) ;
78
+
79
+ it ( 'should select the first project if there is only one' , async ( ) => {
80
+ const tree = Tree . empty ( ) ;
81
+ const angularJSON = generateAngularJson ( ) ;
82
+ delete ( angularJSON as any ) . projects [ PROJECT_NAME ] ; // delete one project so that one is left
83
+ tree . create ( 'angular.json' , JSON . stringify ( angularJSON ) ) ;
84
+
85
+ const resultTree = await ngAdd ( { project : '' } ) (
86
+ tree ,
87
+ { } as SchematicContext
88
+ ) ;
89
+
90
+ const resultConfig = readJSONFromTree ( resultTree , 'angular.json' ) ;
91
+ expect (
92
+ resultConfig . projects [ OTHER_PROJECT_NAME ] . architect . deploy
93
+ ) . toBeTruthy ( ) ;
94
+ } ) ;
95
+ } ) ;
96
+
42
97
describe ( 'error handling' , ( ) => {
43
- it ( 'should fail if project not defined' , async ( ) => {
98
+ it ( 'should fail if there are multiple projects in workspace and project is not explicitly defined' , async ( ) => {
44
99
const tree = Tree . empty ( ) ;
45
100
const angularJSON = generateAngularJson ( ) ;
46
- delete angularJSON . defaultProject ;
47
101
tree . create ( 'angular.json' , JSON . stringify ( angularJSON ) ) ;
48
102
49
103
await expect (
50
- ngAdd ( {
51
- project : ''
52
- } ) ( tree , { } as SchematicContext )
104
+ ngAdd ( { project : '' } ) ( tree , { } as SchematicContext )
53
105
) . rejects . toThrowError (
54
- 'No Angular project selected and no default project in the workspace '
106
+ 'There is more than one project in your workspace. Please select it manually by using the --project argument. '
55
107
) ;
56
108
} ) ;
57
109
58
110
it ( 'should throw if angular.json not found' , async ( ) => {
59
111
await expect (
60
- ngAdd ( {
61
- project : PROJECT_NAME
62
- } ) ( Tree . empty ( ) , { } as SchematicContext )
112
+ ngAdd ( { project : PROJECT_NAME } ) ( Tree . empty ( ) , { } as SchematicContext )
63
113
) . rejects . toThrowError ( 'Unable to determine format for workspace path.' ) ;
64
114
} ) ;
65
115
@@ -68,9 +118,7 @@ describe('ng-add', () => {
68
118
tree . create ( 'angular.json' , 'hi' ) ;
69
119
70
120
await expect (
71
- ngAdd ( {
72
- project : PROJECT_NAME
73
- } ) ( tree , { } as SchematicContext )
121
+ ngAdd ( { project : PROJECT_NAME } ) ( tree , { } as SchematicContext )
74
122
) . rejects . toThrowError ( 'Invalid JSON character: "h" at 0:0.' ) ;
75
123
} ) ;
76
124
@@ -79,9 +127,7 @@ describe('ng-add', () => {
79
127
tree . create ( 'angular.json' , JSON . stringify ( { version : 1 , projects : { } } ) ) ;
80
128
81
129
await expect (
82
- ngAdd ( {
83
- project : PROJECT_NAME
84
- } ) ( tree , { } as SchematicContext )
130
+ ngAdd ( { project : PROJECT_NAME } ) ( tree , { } as SchematicContext )
85
131
) . rejects . toThrowError (
86
132
'The specified Angular project is not defined in this workspace'
87
133
) ;
@@ -98,9 +144,7 @@ describe('ng-add', () => {
98
144
) ;
99
145
100
146
await expect (
101
- ngAdd ( {
102
- project : PROJECT_NAME
103
- } ) ( tree , { } as SchematicContext )
147
+ ngAdd ( { project : PROJECT_NAME } ) ( tree , { } as SchematicContext )
104
148
) . rejects . toThrowError (
105
149
'Deploy requires an Angular project type of "application" in angular.json'
106
150
) ;
@@ -131,10 +175,13 @@ function prettifyJSON(json: string) {
131
175
return JSON . stringify ( JSON . parse ( json ) , null , 2 ) ;
132
176
}
133
177
178
+ function readJSONFromTree ( tree : Tree , file : string ) {
179
+ return JSON . parse ( tree . read ( file ) ! . toString ( ) ) ;
180
+ }
181
+
134
182
function generateAngularJson ( ) {
135
183
return {
136
184
version : 1 ,
137
- defaultProject : PROJECT_NAME as string | undefined ,
138
185
projects : {
139
186
[ PROJECT_NAME ] : {
140
187
projectType : 'application' ,
0 commit comments