This repository was archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathstate.spec.js
More file actions
478 lines (418 loc) · 21.3 KB
/
state.spec.js
File metadata and controls
478 lines (418 loc) · 21.3 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
var State = require('../lib/state'),
events = require('../lib/events'),
helpers = require('./helpers'),
path = require('path'),
Q = require('q'),
shelljs = require('shelljs'),
logging = require('../lib/logging');
logging.logger = helpers.testingLogger;
var tempDirectory = '/test/dev/ionic',
testPluginId = 'com.ionic.keyboard',
defaultPackageJson = { cordovaPlatforms: [], cordovaPlugins: [] };
describe('State', function() {
it('should have state defined', function() {
expect(State).toBeDefined();
});
it('should get the package json by app directory', function() {
spyOn(State, 'readInPackageJson').andReturn({});
var packageJson = State.getPackageJson(tempDirectory);
expect(State.readInPackageJson).toHaveBeenCalledWith(path.normalize('/test/dev/ionic/package.json'));
expect(packageJson.cordovaPlugins.length).toBe([].length);
expect(packageJson.cordovaPlatforms.length).toBe([].length);
});
describe('#savePlatform', function() {
beforeEach(function() {
defaultPackageJson = { cordovaPlatforms: [], cordovaPlugins: [] };
spyOn(State, 'getPackageJson').andReturn(defaultPackageJson);
spyOn(State, 'savePackageJson');
});
// Locator may be:
// Name: ios, android
// Name with version: ios@3.8.0, android@4.0.0
// Local path: ./engine/cordova-android-c0.6.1
// Http url: https://github.com/apache/cordova-android.git
it('should call getPackageJson with the correct directory', function() {
spyOn(State, 'addOrUpdatePlatformToPackageJson');
State.savePlatform(tempDirectory, 'ios');
expect(State.getPackageJson).toHaveBeenCalledWith(tempDirectory);
// expect(State.addOrUpdatePlatformToPackageJson).toHaveBeenCalledWith()
});
it('should call addOrUpdatePlatformToPackageJson with directory and ios when ios is passed', function() {
spyOn(State, 'addOrUpdatePlatformToPackageJson');
State.savePlatform(tempDirectory, 'ios');
expect(State.addOrUpdatePlatformToPackageJson).toHaveBeenCalledWith(defaultPackageJson, 'ios');
});
it('should call savePackageJson with app directory and packageJson data', function() {
spyOn(State, 'addOrUpdatePlatformToPackageJson').andCallFake(function(packageJson){
packageJson.cordovaPlatforms = ['ios'];
});
State.savePlatform(tempDirectory, 'ios');
expect(State.addOrUpdatePlatformToPackageJson).toHaveBeenCalledWith(defaultPackageJson, 'ios');
expect(State.savePackageJson).toHaveBeenCalledWith(tempDirectory, { cordovaPlatforms: ['ios'], cordovaPlugins: [] });
});
it('should save ios and version with both are passed', function() {
var packageJson =
spyOn(State, 'addOrUpdatePlatformToPackageJson').andCallFake(function(packageJson) {
packageJson.cordovaPlatforms = [{ platform: 'ios', locator: 'ios@3.8.0', version: '3.8.0' }];
});
State.savePlatform(tempDirectory, 'ios@3.8.0');
expect(State.addOrUpdatePlatformToPackageJson).toHaveBeenCalledWith(defaultPackageJson, 'ios', {platform: 'ios', locator: 'ios@3.8.0', version: '3.8.0'});
expect(State.savePackageJson).toHaveBeenCalledWith(tempDirectory, defaultPackageJson);
});
it('should save android version from remote URL', function() {
spyOn(State, 'addOrUpdatePlatformToPackageJson').andCallFake(function(packageJson) {
packageJson.cordovaPlatforms = [{ platform: 'android', locator: 'https://github.com/apache/cordova-android', version: '4.0.0' }];
});
State.savePlatform(tempDirectory, 'https://github.com/apache/cordova-android');
expect(State.addOrUpdatePlatformToPackageJson).toHaveBeenCalledWith(defaultPackageJson, 'android', {platform: 'android', locator: 'https://github.com/apache/cordova-android'});
expect(State.savePackageJson).toHaveBeenCalledWith(tempDirectory, defaultPackageJson);
});
it('should save android version from local path', function() {
var fileLocator = './engine/cordova-android';
spyOn(State, 'addOrUpdatePlatformToPackageJson').andCallFake(function(packageJson) {
packageJson.cordovaPlatforms = [{ platform: 'android', locator: fileLocator }];
});
State.savePlatform(tempDirectory, fileLocator);
expect(State.addOrUpdatePlatformToPackageJson).toHaveBeenCalledWith(defaultPackageJson, 'android', {platform: 'android', locator: fileLocator});
expect(State.savePackageJson).toHaveBeenCalledWith(tempDirectory, defaultPackageJson);
});
});
describe('#addOrUpdatePlatformToPackageJson', function() {
it('should overwrite default android when local android is added', function() {
var fileLocator = './engine/cordova-android';
defaultPackageJson = { cordovaPlatforms: ['android'], cordovaPlugins: [] };
var afterPackageJson = { cordovaPlatforms: [{platform: 'android', locator: fileLocator}], cordovaPlugins: [] };
var platformInfo = { platform: 'android', locator: fileLocator };
State.addOrUpdatePlatformToPackageJson(defaultPackageJson, 'android', platformInfo);
expect(defaultPackageJson).toEqual(afterPackageJson);
});
it('should overwrite ios version when ios and version are added', function() {
defaultPackageJson = { cordovaPlatforms: [{platform: 'ios', locator: 'ios@3.7.0', version: '3.7.0'}], cordovaPlugins: [] };
var afterPackageJson = { cordovaPlatforms: [{platform: 'ios', locator: 'ios@3.8.0', version: '3.8.0'}], cordovaPlugins: [] };
var platformInfo = { platform: 'ios', locator: 'ios@3.8.0', version: '3.8.0' };
State.addOrUpdatePlatformToPackageJson(defaultPackageJson, 'ios', platformInfo);
expect(defaultPackageJson).toEqual(afterPackageJson);
});
});
describe('#removePlatform', function() {
beforeEach(function() {
//Start with ios in package.json
spyOn(State, 'savePackageJson');
});
it('should call getPackageJson with the correct directory', function() {
defaultPackageJson = { cordovaPlatforms: ['ios'] };
spyOn(State, 'getPackageJson').andReturn(defaultPackageJson);
State.removePlatform(tempDirectory, 'ios');
expect(State.getPackageJson).toHaveBeenCalledWith(tempDirectory);
});
it('should remove platform from packageJson when plain ios exists', function() {
defaultPackageJson = { cordovaPlatforms: ['ios'] };
spyOn(State, 'getPackageJson').andReturn(defaultPackageJson);
State.removePlatform(tempDirectory, 'ios');
expect(State.savePackageJson).toHaveBeenCalledWith(tempDirectory, { cordovaPlatforms: []});
});
it('should remove platform from packageJson when android with locator exists', function() {
defaultPackageJson = { cordovaPlatforms: [ { platform: 'android', locator: 'https://github.com/apache/cordova-android' } ] };
spyOn(State, 'getPackageJson').andReturn(defaultPackageJson);
State.removePlatform(tempDirectory, 'android');
expect(State.savePackageJson).toHaveBeenCalledWith(tempDirectory, { cordovaPlatforms: []});
});
it('should remove platform from packageJson when android with file locator exists', function() {
defaultPackageJson = { cordovaPlatforms: [ { platform: 'android', locator: './engine/cordova-android' } ] };
spyOn(State, 'getPackageJson').andReturn(defaultPackageJson);
State.removePlatform(tempDirectory, 'android');
expect(State.savePackageJson).toHaveBeenCalledWith(tempDirectory, { cordovaPlatforms: []});
});
it('should remove only one platforms from packageJson when both exist', function() {
defaultPackageJson = { cordovaPlatforms: [ 'ios', { platform: 'android', locator: './engine/cordova-android' } ] };
spyOn(State, 'getPackageJson').andReturn(defaultPackageJson);
State.removePlatform(tempDirectory, 'android');
expect(State.savePackageJson).toHaveBeenCalledWith(tempDirectory, { cordovaPlatforms: ['ios']});
});
});
describe('#savePlugin', function() {
beforeEach(function() {
spyOn(State, 'savePackageJson');
defaultPackageJson = { cordovaPlatforms: [], cordovaPlugins: [testPluginId] };
});
//Expects - either simple ID for plugin registry
//or a local path, with or without variables
//ionic plugin add org.apache.cordova.splashscreen
//ionic plugin add ../phonegap-facebook-plugin --variable APP_ID="123456789" --variable APP_NAME="myApplication"
it('should call getPackageJson with the correct directory', function() {
spyOn(State, 'addOrUpdatePluginToPackageJson');
defaultPackageJson = { cordovaPlatforms: [], cordovaPlugins: [testPluginId] };
spyOn(State, 'getPackageJson').andReturn(defaultPackageJson);
State.savePlugin(tempDirectory, testPluginId);
expect(State.getPackageJson).toHaveBeenCalledWith(tempDirectory);
});
it('should save the plugin ID to the packageJson for a simple ID', function() {
defaultPackageJson = { cordovaPlatforms: [], cordovaPlugins: [] };
spyOn(State, 'getPackageJson').andReturn(defaultPackageJson);
spyOn(State, 'addOrUpdatePluginToPackageJson').andCallFake(function(packageJson) {
packageJson.cordovaPlugins = [testPluginId];
})
State.savePlugin(tempDirectory, testPluginId);
expect(State.addOrUpdatePluginToPackageJson).toHaveBeenCalledWith(defaultPackageJson, testPluginId);
expect(State.savePackageJson).toHaveBeenCalledWith(tempDirectory, defaultPackageJson);
});
it('should save the plugin ID to the packageJson for a local ID', function() {
var testLocalPluginId = './engine/cordova-crosswalk-plugin';
defaultPackageJson = { cordovaPlatforms: [], cordovaPlugins: [] };
spyOn(State, 'getPackageJson').andReturn(defaultPackageJson);
spyOn(State, 'getPluginFromFetchJsonByLocator').andReturn('cordova-crosswalk-engine');
spyOn(State, 'addOrUpdatePluginToPackageJson').andCallFake(function(packageJson) {
packageJson.cordovaPlugins = [{locator: testLocalPluginId, id: 'cordova-crosswalk-engine'}];
})
State.savePlugin(tempDirectory, testLocalPluginId);
expect(State.addOrUpdatePluginToPackageJson).toHaveBeenCalledWith(defaultPackageJson, 'cordova-crosswalk-engine', {locator: testLocalPluginId, id: 'cordova-crosswalk-engine'});
expect(State.savePackageJson).toHaveBeenCalledWith(tempDirectory, defaultPackageJson);
});
it('should save the plugin ID to the packageJson for a local ID', function() {
var testLocalPluginId = './engine/cordova-facebook-plugin';
var testVariables = [ 'APP_ID=123456789', 'APP_NAME=myApplication' ];
var variablesHash = {APP_ID: '123456789', APP_NAME: 'myApplication'};
defaultPackageJson = { cordovaPlatforms: [], cordovaPlugins: [] };
var modifiedPackageJson = { cordovaPlatforms: [], cordovaPlugins: [{locator: testLocalPluginId, id: 'cordova-facebook-plugin', variables: variablesHash}]};
spyOn(State, 'getPackageJson').andReturn(defaultPackageJson);
spyOn(State, 'getPluginFromFetchJsonByLocator').andReturn('cordova-facebook-plugin');
spyOn(State, 'addOrUpdatePluginToPackageJson').andCallFake(function(packageJson) {
packageJson.cordovaPlugins = [{locator: testLocalPluginId, id: 'cordova-facebook-plugin', variables: variablesHash}];
});
State.savePlugin(tempDirectory, testLocalPluginId, testVariables);
expect(State.addOrUpdatePluginToPackageJson).toHaveBeenCalledWith(defaultPackageJson, 'cordova-facebook-plugin', {locator: testLocalPluginId, id: 'cordova-facebook-plugin', variables: variablesHash});
expect(State.savePackageJson).toHaveBeenCalledWith(tempDirectory, modifiedPackageJson);
});
it('should call getPluginFromFetchJsonByLocator with correct parameters', function() {
var testRemoteUrl = 'https://github.com/apache/cordova-plugin-whitelist.git#r1.0.0';
defaultPackageJson = { cordovaPlatforms: [], cordovaPlugins: [] };
spyOn(State, 'getPluginFromFetchJsonByLocator');
spyOn(State, 'addOrUpdatePluginToPackageJson');
spyOn(State, 'getPackageJson').andReturn(defaultPackageJson);
try {
State.savePlugin(tempDirectory, testRemoteUrl, null);
} catch (ex) {
console.log(ex);
}
expect(State.getPluginFromFetchJsonByLocator).toHaveBeenCalledWith(tempDirectory, testRemoteUrl);
expect(State.savePackageJson).toHaveBeenCalled();
});
});
// describe('getPluginFromFetchJsonByLocator', function() {
// });
describe('#removePlugin', function() {
beforeEach(function() {
spyOn(State, 'savePackageJson');
});
it('should call getPackageJson with the correct directory', function() {
defaultPackageJson = { cordovaPlatforms: [], cordovaPlugins: [testPluginId] };
spyOn(State, 'getPackageJson').andReturn(defaultPackageJson);
State.removePlugin(tempDirectory, testPluginId);
expect(State.getPackageJson).toHaveBeenCalledWith(tempDirectory);
});
it('should remove the pluginId from the packageJson from simple plugin IDs in cordovaPlugins', function() {
defaultPackageJson = { cordovaPlatforms: [], cordovaPlugins: [testPluginId] };
spyOn(State, 'getPackageJson').andReturn(defaultPackageJson);
State.removePlugin(tempDirectory, testPluginId);
expect(State.savePackageJson).toHaveBeenCalledWith(tempDirectory, { cordovaPlatforms: [], cordovaPlugins: [] });
});
it('should remove the pluginId from the packageJson when its an object', function() {
defaultPackageJson = { cordovaPlatforms: [], cordovaPlugins: [{ id: 'cordova-crosswalk-engine', locator: './engine/cordova-crosswalk-engine'}] };
spyOn(State, 'getPackageJson').andReturn(defaultPackageJson);
State.removePlugin(tempDirectory, 'cordova-crosswalk-engine');
expect(State.savePackageJson).toHaveBeenCalledWith(tempDirectory, { cordovaPlatforms: [], cordovaPlugins: [] });
});
});
describe('#addOrUpdatePluginToPackageJson', function() {
it('should add the plugin ID with http url', function() {
defaultPackageJson = {
"cordovaPlugins": [
"org.apache.cordova.device",
"org.apache.cordova.console",
"com.ionic.keyboard",
{
"locator": "engine/cordova-crosswalk-engine-c0.7.1",
"id": "cordova-plugin-crosswalk-webview"
},
"org.apache.cordova.splashscreen"
],
"cordovaPlatforms": [
"ios",
{
"platform": "android",
"locator": "./engine/cordova-android-c0.6.1/"
}
]
};
expect(defaultPackageJson.cordovaPlugins.length).toBe(5);
State.addOrUpdatePluginToPackageJson(defaultPackageJson, {id:'cordova-plugin-whitelist', locator: 'https://github.com/apache/cordova-plugin-whitelist.git#r1.0.0'});
//we had 5 plugins, we should have 6 now.
expect(defaultPackageJson.cordovaPlugins.length).toBe(6);
});
});
describe('#restoreState', function(){
it('should only restore plugins when plugin option passed', function(done) {
var options = { plugins: true, platforms: false };
spyOn(State, 'restorePlatforms');
spyOn(State, 'restorePlugins');
defaultPackageJson = { cordovaPlatforms: [], cordovaPlugins: [] };
spyOn(State, 'getPackageJson').andReturn(defaultPackageJson);
Q()
.then(function(){
return State.restoreState(tempDirectory, options);
})
.then(function(){
expect(State.restorePlugins).toHaveBeenCalledWith(tempDirectory, defaultPackageJson);
expect(State.restorePlatforms).not.toHaveBeenCalledWith(tempDirectory);
})
.catch(function(ex) {
expect('this').toBe('not this');
})
.fin(done);
});
it('should only restore platforms when platform option passed', function(done) {
var options = { plugins: false, platforms: true };
spyOn(State, 'restorePlatforms').andReturn(Q());
spyOn(State, 'restorePlugins').andReturn(Q());
defaultPackageJson = { cordovaPlatforms: [], cordovaPlugins: [] };
spyOn(State, 'getPackageJson').andReturn(defaultPackageJson);
Q()
.then(function(){
return State.restoreState(tempDirectory, options);
})
.then(function(){
expect(State.restorePlugins).not.toHaveBeenCalledWith(tempDirectory, defaultPackageJson);
expect(State.restorePlatforms).toHaveBeenCalledWith(tempDirectory, defaultPackageJson);
})
.catch(function(ex) {
expect('this').toBe('not this');
console.log(ex.stack)
})
.fin(done);
});
it('should restore platforms and plugins when both options passed', function(done) {
var options = { plugins: true, platforms: true };
spyOn(State, 'restorePlatforms').andReturn(Q());
spyOn(State, 'restorePlugins').andReturn(Q());
defaultPackageJson = { cordovaPlatforms: [], cordovaPlugins: [] };
spyOn(State, 'getPackageJson').andReturn(defaultPackageJson);
Q()
.then(function(){
return State.restoreState(tempDirectory, options);
})
.then(function(){
expect(State.restorePlugins).toHaveBeenCalledWith(tempDirectory, defaultPackageJson);
expect(State.restorePlatforms).toHaveBeenCalledWith(tempDirectory, defaultPackageJson);
})
.catch(function(ex) {
expect('this').toBe('not this');
console.log(ex.stack)
})
.fin(done);
});
it('should check for undefined options', function() {
expect(State.restoreState).toThrow('You must pass an application directory and options to restore state.');
});
});
describe('#resetState', function() {
it('should call call rm on the platforms path', function() {
spyOn(shelljs, 'rm');
spyOn(State, 'restoreState').andReturn(Q());
State.resetState(tempDirectory, {});
var platformPath = path.join(tempDirectory, 'platforms');
var pluginPath = path.join(tempDirectory, 'plugins');
expect(shelljs.rm).toHaveBeenCalledWith('-rf', [platformPath, pluginPath]);
expect(State.restoreState).toHaveBeenCalledWith(tempDirectory, {});
});
});
describe('#restorePlugins', function(){
it('should call processPlugin with the correct app directory, index, and a promise', function(done){
var promise = Q.defer();
spyOn(Q, 'defer').andReturn(promise);
spyOn(State, 'processPlugin');
Q()
.then(function(){
promise.resolve();
return State.restorePlugins(tempDirectory, defaultPackageJson);
})
.then(function() {
expect(State.processPlugin).toHaveBeenCalledWith(tempDirectory, 0, defaultPackageJson, promise);
})
.catch(function(ex) {
console.log(ex.stack);
expect('this').toBe('not this');
})
.fin(done);
});
});
describe('#restorePlatforms', function(){
it('should call processPlatform with the correct app directory, index, and a promise', function(done){
var promise = Q.defer();
spyOn(Q, 'defer').andReturn(promise);
spyOn(State, 'processPlatform');
Q()
.then(function(){
promise.resolve();
return State.restorePlatforms(tempDirectory, defaultPackageJson);
})
.then(function() {
expect(State.processPlatform).toHaveBeenCalledWith(tempDirectory, 0, defaultPackageJson, promise);
})
.catch(function(ex) {
console.log(ex.stack);
expect('this').toBe('not this');
})
.fin(done);
});
});
describe('#clearState', function (){
it('should clear our the packageJson entries and remove platforms and plugins', function(done) {
spyOn(State, 'getPackageJson').andReturn({cordovaPlatforms: ['ios'], cordovaPlugins: ['org.apache.cordova.device']});
spyOn(shelljs, 'rm');
spyOn(State, 'savePackageJson');
Q()
.then(function(){
return State.clearState(tempDirectory);
})
.then(function() {
expect(shelljs.rm).toHaveBeenCalledWith('-rf', [path.join(tempDirectory, 'platforms'), path.join(tempDirectory, 'plugins')]);
expect(State.savePackageJson).toHaveBeenCalledWith(tempDirectory, {cordovaPlatforms: [], cordovaPlugins: []});
})
.catch(function(ex) {
console.log(ex.stack);
expect('this').toBe('not this');
})
.fin(done);
})
});
describe('#saveState', function (){
it('should save the state of our application', function(done) {
var packageJson = {cordovaPlatforms: [], cordovaPlugins: []};
spyOn(State, 'getPackageJson').andReturn(packageJson);
spyOn(State, 'saveExistingPlatforms');
spyOn(State, 'saveExistingPlugins');
spyOn(State, 'savePackageJson');
Q()
.then(function(){
return State.saveState(tempDirectory);
})
.then(function() {
expect(State.saveExistingPlugins).toHaveBeenCalledWith(tempDirectory, packageJson);
expect(State.savePackageJson).toHaveBeenCalledWith(tempDirectory, packageJson);
expect(State.savePackageJson).toHaveBeenCalledWith(tempDirectory, packageJson);
})
.catch(function(ex) {
console.log(ex.stack);
expect('this').toBe('not this');
})
.fin(done);
})
});
describe('#getPluginFromFetchJsonByLocator', function() {
it('should get the correct plugin by ID from fetch.json', function() {
var plugin = State.getPluginFromFetchJsonByLocator(__dirname, 'cordova-plugin-googleplus');
// console.log('plugin yo', plugin);
expect(plugin).toBe('cordova-plugin-googleplus');
});
});
});