forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-guard.spec.ts
More file actions
257 lines (231 loc) · 10 KB
/
generate-guard.spec.ts
File metadata and controls
257 lines (231 loc) · 10 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import * as fs from 'fs-extra';
import * as path from 'path';
import { ng, setupProject } from '../helpers';
const root = process.cwd();
describe('Acceptance: ng generate guard', function () {
setupProject();
it('ng generate guard my-guard', (done) => {
const appRoot = path.join(root, 'tmp/foo');
const testPath = path.join(appRoot, 'src/app/my-guard.guard.ts');
const testSpecPath = path.join(appRoot, 'src/app/my-guard.guard.spec.ts');
const appModulePath = path.join(appRoot, 'src/app/app.module.ts');
return ng(['generate', 'guard', 'my-guard'])
.then(() => {
expect(fs.pathExistsSync(testPath)).toBe(true);
expect(fs.pathExistsSync(testSpecPath)).toBe(true);
})
.then(() => fs.readFile(appModulePath, 'utf-8'))
.then((content: string) => {
expect(content).not.toMatch(/import.*MyGuardGuard.*from '.\/my-guard.guard';/);
expect(content).not.toMatch(/providers:\s*\[MyGuardGuard\]/m);
})
.then(done, done.fail);
});
it('ng generate guard my-guard --no-spec', (done) => {
const appRoot = path.join(root, 'tmp/foo');
const testPath = path.join(appRoot, 'src/app/my-guard.guard.ts');
const testSpecPath = path.join(appRoot, 'src/app/my-guard.guard.spec.ts');
const appModulePath = path.join(appRoot, 'src/app/app.module.ts');
return ng(['generate', 'guard', 'my-guard', '--no-spec'])
.then(() => {
expect(fs.pathExistsSync(testPath)).toBe(true);
expect(fs.pathExistsSync(testSpecPath)).toBe(false);
})
.then(() => fs.readFile(appModulePath, 'utf-8'))
.then((content: string) => {
expect(content).not.toMatch(/import.*MyGuardGuard.*from '.\/my-guard.guard';/);
expect(content).not.toMatch(/providers:\s*\[MyGuardGuard\]/m);
})
.then(done, done.fail);
});
it('ng generate guard test' + path.sep + 'my-guard', (done) => {
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', 'test'));
return ng(['generate', 'guard', 'test' + path.sep + 'my-guard']).then(() => {
const testPath = path.join(root, 'tmp', 'foo', 'src', 'app', 'test', 'my-guard.guard.ts');
expect(fs.pathExistsSync(testPath)).toBe(true);
})
.then(done, done.fail);
});
it('ng generate guard test' + path.sep + '..' + path.sep + 'my-guard', (done) => {
return ng(['generate', 'guard', 'test' + path.sep + '..' + path.sep + 'my-guard']).then(() => {
const testPath = path.join(root, 'tmp', 'foo', 'src', 'app', 'my-guard.guard.ts');
expect(fs.pathExistsSync(testPath)).toBe(true);
})
.then(done, done.fail);
});
it('ng generate guard my-guard from a child dir', (done) => {
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', '1'));
return new Promise(function (resolve) {
process.chdir('./src');
resolve();
})
.then(() => process.chdir('./app'))
.then(() => process.chdir('./1'))
.then(() => {
process.env.CWD = process.cwd();
return ng(['generate', 'guard', 'my-guard']);
})
.then(() => {
const testPath = path.join(root, 'tmp', 'foo', 'src', 'app', '1', 'my-guard.guard.ts');
expect(fs.pathExistsSync(testPath)).toBe(true);
})
.then(done, done.fail);
});
it('ng generate guard child-dir' + path.sep + 'my-guard from a child dir', (done) => {
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', '1', 'child-dir'));
return new Promise(function (resolve) {
process.chdir('./src');
resolve();
})
.then(() => process.chdir('./app'))
.then(() => process.chdir('./1'))
.then(() => {
process.env.CWD = process.cwd();
return ng(['generate', 'guard', 'child-dir' + path.sep + 'my-guard']);
})
.then(() => {
const testPath = path.join(
root, 'tmp', 'foo', 'src', 'app', '1', 'child-dir', 'my-guard.guard.ts');
expect(fs.pathExistsSync(testPath)).toBe(true);
})
.then(done, done.fail);
});
it('ng generate guard child-dir' + path.sep + '..' + path.sep + 'my-guard from a child dir',
(done) => {
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', '1'));
return new Promise(function (resolve) {
process.chdir('./src');
resolve();
})
.then(() => process.chdir('./app'))
.then(() => process.chdir('./1'))
.then(() => {
process.env.CWD = process.cwd();
return ng(
['generate', 'guard', 'child-dir' + path.sep + '..' + path.sep + 'my-guard']);
})
.then(() => {
const testPath =
path.join(root, 'tmp', 'foo', 'src', 'app', '1', 'my-guard.guard.ts');
expect(fs.pathExistsSync(testPath)).toBe(true);
})
.then(done, done.fail);
});
it('ng generate guard ' + path.sep + 'my-guard from a child dir, gens under ' +
path.join('src', 'app'),
(done) => {
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', '1'));
return new Promise(function (resolve) {
process.chdir('./src');
resolve();
})
.then(() => process.chdir('./app'))
.then(() => process.chdir('./1'))
.then(() => {
process.env.CWD = process.cwd();
return ng(['generate', 'guard', path.sep + 'my-guard']);
})
.then(() => {
const testPath = path.join(root, 'tmp', 'foo', 'src', 'app', 'my-guard.guard.ts');
expect(fs.pathExistsSync(testPath)).toBe(true);
})
.then(done, done.fail);
});
it('ng generate guard ..' + path.sep + 'my-guard from root dir will fail', (done) => {
return ng(['generate', 'guard', '..' + path.sep + 'my-guard'])
.then(() => done.fail())
.catch(err => {
// tslint:disable-next-line:max-line-length
expect(err).toBe(`Invalid path: "..${path.sep}my-guard" cannot be above the "src${path.sep}app" directory`);
})
.then(done, done.fail);
});
it('should error out when given an incorrect module path', (done) => {
return Promise.resolve()
.then(() => ng(['generate', 'guard', 'baz', '--module', 'foo']))
.then(() => done.fail())
.catch((error) => {
expect(error).toBe('Specified module does not exist');
})
.then(done, done.fail);
});
describe('should import and add to provider list', () => {
it('when given a root level module with module.ts suffix', (done) => {
const appRoot = path.join(root, 'tmp/foo');
const modulePath = path.join(appRoot, 'src/app/app.module.ts');
return Promise.resolve()
.then(() => ng(['generate', 'guard', 'baz', '--module', 'app.module.ts']))
.then(() => fs.readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).toMatch(/import.*BazGuard.*from '.\/baz.guard';/);
expect(content).toMatch(/providers:\s*\[BazGuard\]/m);
})
.then(done, done.fail);
});
it('when given a root level module with missing module.ts suffix', (done) => {
const appRoot = path.join(root, 'tmp/foo');
const modulePath = path.join(appRoot, 'src/app/app.module.ts');
return Promise.resolve()
.then(() => ng(['generate', 'guard', 'baz', '--module', 'app']))
.then(() => fs.readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).toMatch(/import.*BazGuard.*from '.\/baz.guard';/);
expect(content).toMatch(/providers:\s*\[BazGuard\]/m);
})
.then(done, done.fail);
});
it('when given a submodule with module.ts suffix', (done) => {
const appRoot = path.join(root, 'tmp/foo');
const modulePath = path.join(appRoot, 'src/app/foo/foo.module.ts');
return Promise.resolve()
.then(() => ng(['generate', 'module', 'foo']))
.then(() => ng(['generate', 'guard', 'baz', '--module', path.join('foo', 'foo.module.ts')]))
.then(() => fs.readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).toMatch(/import.*BazGuard.*from '..\/baz.guard';/);
expect(content).toMatch(/providers:\s*\[BazGuard\]/m);
})
.then(done, done.fail);
});
it('when given a submodule with missing module.ts suffix', (done) => {
const appRoot = path.join(root, 'tmp/foo');
const modulePath = path.join(appRoot, 'src/app/foo/foo.module.ts');
return Promise.resolve()
.then(() => ng(['generate', 'module', 'foo']))
.then(() => ng(['generate', 'guard', 'baz', '--module', path.join('foo', 'foo')]))
.then(() => fs.readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).toMatch(/import.*BazGuard.*from '..\/baz.guard';/);
expect(content).toMatch(/providers:\s*\[BazGuard\]/m);
})
.then(done, done.fail);
});
it('when given a submodule folder', (done) => {
const appRoot = path.join(root, 'tmp/foo');
const modulePath = path.join(appRoot, 'src/app/foo/foo.module.ts');
return Promise.resolve()
.then(() => ng(['generate', 'module', 'foo']))
.then(() => ng(['generate', 'guard', 'baz', '--module', 'foo']))
.then(() => fs.readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).toMatch(/import.*BazGuard.*from '..\/baz.guard';/);
expect(content).toMatch(/providers:\s*\[BazGuard\]/m);
})
.then(done, done.fail);
});
it('when given deep submodule folder with missing module.ts suffix', (done) => {
const appRoot = path.join(root, 'tmp/foo');
const modulePath = path.join(appRoot, 'src/app/foo/bar/bar.module.ts');
return Promise.resolve()
.then(() => ng(['generate', 'module', 'foo']))
.then(() => ng(['generate', 'module', path.join('foo', 'bar')]))
.then(() => ng(['generate', 'guard', 'baz', '--module', path.join('foo', 'bar')]))
.then(() => fs.readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).toMatch(/import.*BazGuard.*from '..\/..\/baz.guard';/);
expect(content).toMatch(/providers:\s*\[BazGuard\]/m);
})
.then(done, done.fail);
});
});
});