-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathactions.jasmine.ts
302 lines (265 loc) · 8.94 KB
/
actions.jasmine.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* eslint-disable @typescript-eslint/no-empty-function */
import { join } from 'path';
import { BuilderContext, BuilderRun, ScheduleOptions, Target } from '@angular-devkit/architect';
import { JsonObject, logging } from '@angular-devkit/core';
import { BuildTarget, FSHost, FirebaseDeployConfig, FirebaseTools } from '../interfaces';
import deploy, { deployToFunction } from './actions'
import 'jasmine';
let context: BuilderContext;
let firebaseMock: FirebaseTools;
let fsHost: FSHost;
const FIREBASE_PROJECT = 'ikachu-aa3ef';
const PROJECT = 'pirojok-project';
const STATIC_BUILD_TARGET: BuildTarget = {
name: `${PROJECT}:build:production`
};
const FIREBASE_TOKEN = 'kkasllkascnkjnskjsdcskdckskdksdkjc';
const SERVER_BUILD_TARGET: BuildTarget = {
name: `${PROJECT}:server:production`
};
const login = () => Promise.resolve({ user: { email: '[email protected]' }});
login.list = () => Promise.resolve([{ user: { email: '[email protected]' }}]);
login.add = () => Promise.resolve([{ user: { email: '[email protected]' }}]);
login.use = () => Promise.resolve('[email protected]');
const workspaceRoot = join('home', 'user');
const initMocks = () => {
fsHost = {
moveSync(_: string, __: string) {
},
renameSync(_: string, __: string) {
},
writeFileSync(_: string, __: string) {
},
copySync(_: string, __: string) {
},
removeSync(_: string) {
},
existsSync(_: string) {
return false;
},
};
firebaseMock = {
login,
projects: {
list: () => Promise.resolve([]),
create: () => Promise.reject(),
},
apps: {
list: () => Promise.resolve([]),
create: () => Promise.reject(),
sdkconfig: () => Promise.resolve({ fileName: '_', fileContents: '', sdkConfig: {}, }),
},
hosting: {
sites: {
list: () => Promise.resolve({sites: []}),
create: () => Promise.reject(),
}
},
init() {
return Promise.resolve()
},
deploy: (_: FirebaseDeployConfig) => Promise.resolve(),
use: () => Promise.resolve(),
logger: {
add: () => { },
logger: {
add: () => { }
}
},
cli: { version: () => '9.0.0' },
serve: () => Promise.resolve()
};
context = ({
target: {
configuration: 'production',
project: PROJECT,
target: 'foo'
},
builder: {
builderName: 'mock',
description: 'mock',
optionSchema: false
},
currentDirectory: 'cwd',
id: 1,
logger: new logging.NullLogger() as any,
workspaceRoot: 'cwd',
getTargetOptions: (target: Target) => {
if (target.target === 'build') {
return { outputPath: 'dist/browser' };
} else if (target.target === 'server') {
return { outputPath: 'dist/server' };
}
},
reportProgress: (_: number, __?: number, ___?: string) => {
},
reportStatus: (_: string) => {
},
reportRunning: () => {
},
scheduleBuilder: (_: string, __?: JsonObject, ___?: ScheduleOptions) => Promise.resolve({} as BuilderRun),
scheduleTarget: (_: Target, __?: JsonObject, ___?: ScheduleOptions) => Promise.resolve({} as BuilderRun)
} as any);
};
describe('Deploy Angular apps', () => {
beforeEach(() => initMocks());
it('should call login', async () => {
const spy = spyOn(firebaseMock, 'login').and.resolveTo({ email: '[email protected]' });
await deploy(
firebaseMock, context, STATIC_BUILD_TARGET, undefined,
undefined, undefined, { projectId: FIREBASE_PROJECT, preview: false }
);
expect(spy).toHaveBeenCalled();
});
it('should not call login', async () => {
const spy = spyOn(firebaseMock, 'login');
await deploy(firebaseMock, context, STATIC_BUILD_TARGET, undefined, undefined, undefined, { preview: false }, FIREBASE_TOKEN);
expect(spy).not.toHaveBeenCalled();
});
it('should invoke the builder', async () => {
const spy = spyOn(context, 'scheduleTarget').and.callThrough();
await deploy(firebaseMock, context, STATIC_BUILD_TARGET, undefined, undefined, undefined, { preview: false });
expect(spy).toHaveBeenCalled();
expect(spy).toHaveBeenCalledWith({
target: 'build',
configuration: 'production',
project: PROJECT
}, undefined);
});
it('should allow the buildTarget to be specified', async () => {
const buildTarget = {
name: `${PROJECT}:prerender`,
options: {}
};
const spy = spyOn(context, 'scheduleTarget').and.callThrough();
await deploy(firebaseMock, context, buildTarget, undefined, undefined, undefined, { preview: false });
expect(spy).toHaveBeenCalled();
expect(spy).toHaveBeenCalledWith({ target: 'prerender', project: PROJECT }, {});
});
it('should invoke firebase.deploy', async () => {
const spy = spyOn(firebaseMock, 'deploy').and.callThrough();
await deploy(firebaseMock, context, STATIC_BUILD_TARGET, undefined, undefined, undefined, { preview: false }, FIREBASE_TOKEN);
expect(spy).toHaveBeenCalled();
expect(spy).toHaveBeenCalledWith({
cwd: 'cwd',
only: 'hosting:' + PROJECT,
token: FIREBASE_TOKEN,
nonInteractive: true,
projectRoot: 'cwd',
});
});
describe('error handling', () => {
it('throws if there is no firebase project', async () => {
try {
await deploy(firebaseMock, context, STATIC_BUILD_TARGET, undefined, undefined, undefined, { preview: false });
} catch (e) {
expect(e.message).toMatch(/Cannot find firebase project/);
}
});
it('throws if there is no target project', async () => {
context.target = undefined;
try {
await deploy(firebaseMock, context, STATIC_BUILD_TARGET, undefined, undefined, undefined, { preview: false });
} catch (e) {
expect(e.message).toMatch(/Cannot execute the build target/);
}
});
});
});
describe('universal deployment', () => {
beforeEach(() => initMocks());
it('should create a firebase function', async () => {
const spy = spyOn(fsHost, 'writeFileSync');
await deployToFunction(
firebaseMock,
context,
workspaceRoot,
STATIC_BUILD_TARGET,
SERVER_BUILD_TARGET,
{ preview: false },
undefined,
fsHost
);
expect(spy).toHaveBeenCalledTimes(2);
const packageArgs = spy.calls.argsFor(0);
const functionArgs = spy.calls.argsFor(1);
expect(packageArgs[0]).toBe(join(workspaceRoot, 'dist', 'package.json'));
expect(functionArgs[0]).toBe(join(workspaceRoot, 'dist', 'index.js'));
});
it('should create a firebase function (new)', async () => {
const spy = spyOn(fsHost, 'writeFileSync');
await deployToFunction(
firebaseMock,
context,
workspaceRoot,
STATIC_BUILD_TARGET,
SERVER_BUILD_TARGET,
{ preview: false, outputPath: join('dist', 'functions') },
undefined,
fsHost
);
expect(spy).toHaveBeenCalledTimes(2);
const packageArgs = spy.calls.argsFor(0);
const functionArgs = spy.calls.argsFor(1);
expect(packageArgs[0]).toBe(join(workspaceRoot, 'dist', 'functions', 'package.json'));
expect(functionArgs[0]).toBe(join(workspaceRoot, 'dist', 'functions', 'index.js'));
});
it('should rename the index.html file in the nested dist', async () => {
const spy = spyOn(fsHost, 'renameSync');
await deployToFunction(
firebaseMock,
context,
workspaceRoot,
STATIC_BUILD_TARGET,
SERVER_BUILD_TARGET,
{ preview: false },
undefined,
fsHost
);
expect(spy).toHaveBeenCalledTimes(1);
const packageArgs = spy.calls.argsFor(0);
expect(packageArgs).toEqual([
join(workspaceRoot, 'dist', 'dist', 'browser', 'index.html'),
join(workspaceRoot, 'dist', 'dist', 'browser', 'index.original.html')
]);
});
it('should rename the index.html file in the nested dist (new)', async () => {
const spy = spyOn(fsHost, 'renameSync');
await deployToFunction(
firebaseMock,
context,
workspaceRoot,
STATIC_BUILD_TARGET,
SERVER_BUILD_TARGET,
{ preview: false, outputPath: join('dist', 'functions') },
undefined,
fsHost
);
expect(spy).toHaveBeenCalledTimes(1);
const packageArgs = spy.calls.argsFor(0);
expect(packageArgs).toEqual([
join(workspaceRoot, 'dist', 'functions', 'dist', 'browser', 'index.html'),
join(workspaceRoot, 'dist', 'functions', 'dist', 'browser', 'index.original.html')
]);
});
it('should invoke firebase.deploy', async () => {
const spy = spyOn(firebaseMock, 'deploy');
await deployToFunction(
firebaseMock,
context,
workspaceRoot,
STATIC_BUILD_TARGET,
SERVER_BUILD_TARGET,
{ preview: false },
undefined,
fsHost
);
expect(spy).toHaveBeenCalledTimes(1);
});
/* TODO figure out how to stub the prompt
it('should not deploy if the command is invoked with --preview', async () => {
const spy = spyOn(firebaseMock, 'deploy');
await deployToFunction(firebaseMock, context, '/home/user', projectTargets, true, fsHost);
expect(spy).not.toHaveBeenCalled();
});*/
});