-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathng-add.spec.ts
179 lines (151 loc) · 5.19 KB
/
ng-add.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { SchematicContext, Tree } from '@angular-devkit/schematics';
import { ngAdd } from './ng-add';
const PROJECT_NAME = 'THEPROJECT';
const PROJECT_ROOT = 'PROJECTROOT';
const OTHER_PROJECT_NAME = 'OTHERPROJECT';
describe('ng-add', () => {
describe('generating files', () => {
let tree: Tree;
beforeEach(() => {
tree = Tree.empty();
tree.create('angular.json', JSON.stringify(generateAngularJson()));
});
it('generates new files if starting from scratch', async () => {
const result = await ngAdd({
project: PROJECT_NAME
})(tree, {} as SchematicContext);
const actual = result.read('angular.json')!.toString();
expect(prettifyJSON(actual)).toMatchSnapshot();
});
it('overrides existing files', async () => {
const tempTree = await ngAdd({
project: PROJECT_NAME
})(tree, {} as SchematicContext);
const result = await ngAdd({
project: OTHER_PROJECT_NAME
})(tempTree, {} as SchematicContext);
const actual = result.read('angular.json')!.toString();
expect(prettifyJSON(actual)).toMatchSnapshot();
});
});
describe('project selection', () => {
it('should select the first project if there is only one', async () => {
const tree = Tree.empty();
const angularJSON = generateAngularJson();
delete (angularJSON as any).projects[PROJECT_NAME]; // delete one project so that one is left
tree.create('angular.json', JSON.stringify(angularJSON));
const resultTree = await ngAdd({ project: '' })(
tree,
{} as SchematicContext
);
const resultConfig = readJSONFromTree(resultTree, 'angular.json');
expect(
resultConfig.projects[OTHER_PROJECT_NAME].architect.deploy
).toBeTruthy();
});
});
describe('error handling', () => {
it('should fail if there are multiple projects in workspace and project is not explicitly defined', async () => {
const tree = Tree.empty();
const angularJSON = generateAngularJson();
tree.create('angular.json', JSON.stringify(angularJSON));
await expect(
ngAdd({ project: '' })(tree, {} as SchematicContext)
).rejects.toThrowError(
'There is more than one project in your workspace. Please select it manually by using the --project argument.'
);
});
it('should throw if angular.json not found', async () => {
await expect(
ngAdd({ project: PROJECT_NAME })(Tree.empty(), {} as SchematicContext)
).rejects.toThrowError('Unable to determine format for workspace path.');
});
it('should throw if angular.json can not be parsed', async () => {
const tree = Tree.empty();
tree.create('angular.json', 'hi');
await expect(
ngAdd({ project: PROJECT_NAME })(tree, {} as SchematicContext)
).rejects.toThrowError('Invalid workspace file - expected JSON object.');
});
it('should throw if specified project does not exist', async () => {
const tree = Tree.empty();
tree.create('angular.json', JSON.stringify({ version: 1, projects: {} }));
await expect(
ngAdd({ project: PROJECT_NAME })(tree, {} as SchematicContext)
).rejects.toThrowError(
'The specified Angular project is not defined in this workspace'
);
});
it('should throw if specified project is not application', async () => {
const tree = Tree.empty();
tree.create(
'angular.json',
JSON.stringify({
version: 1,
projects: {
[PROJECT_NAME]: { projectType: 'invalid', root: PROJECT_NAME }
}
})
);
await expect(
ngAdd({ project: PROJECT_NAME })(tree, {} as SchematicContext)
).rejects.toThrowError(
'Deploy requires an Angular project type of "application" in angular.json'
);
});
it('should throw if app does not have architect configured', async () => {
const tree = Tree.empty();
tree.create(
'angular.json',
JSON.stringify({
version: 1,
projects: {
[PROJECT_NAME]: { projectType: 'application', root: PROJECT_NAME }
}
})
);
await expect(
ngAdd({
project: PROJECT_NAME
})(tree, {} as SchematicContext)
).rejects.toThrowError(
'Cannot read the output path (architect.build.options.outputPath) of the Angular project "THEPROJECT" in angular.json'
);
});
});
});
function prettifyJSON(json: string) {
return JSON.stringify(JSON.parse(json), null, 2);
}
function readJSONFromTree(tree: Tree, file: string) {
return JSON.parse(tree.read(file)!.toString());
}
function generateAngularJson() {
return {
version: 1,
projects: {
[PROJECT_NAME]: {
projectType: 'application',
root: PROJECT_ROOT,
architect: {
build: {
options: {
outputPath: 'dist/' + PROJECT_NAME
}
}
}
},
[OTHER_PROJECT_NAME]: {
projectType: 'application',
root: PROJECT_ROOT,
architect: {
build: {
options: {
outputPath: 'dist/' + OTHER_PROJECT_NAME
}
}
}
}
}
};
}