/
lazy_load.spec.js
144 lines (137 loc) · 6.47 KB
/
lazy_load.spec.js
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
var lazy_load = require('../src/lazy_load'),
config = require('../src/config'),
util = require('../src/util'),
shell = require('shelljs'),
npm = require('npm');
path = require('path'),
hooker = require('../src/hooker'),
request = require('request'),
fs = require('fs'),
platforms = require('../platforms');
describe('lazy_load module', function() {
var custom_path;
beforeEach(function() {
custom_path = spyOn(config, 'has_custom_path').andReturn(false);
});
describe('cordova method (loads stock cordova libs)', function() {
var custom;
beforeEach(function() {
custom = spyOn(lazy_load, 'custom');
});
it('should throw if platform is not a stock cordova platform', function() {
expect(function() {
lazy_load.cordova('atari');
}).toThrow('Cordova library "atari" not recognized.');
});
it('should invoke lazy_load.custom with appropriate url, platform, and version as specified in platforms manifest', function() {
lazy_load.cordova('android');
expect(custom).toHaveBeenCalledWith(platforms.android.url + ';a=snapshot;h=' + platforms.android.version + ';sf=tgz', 'cordova', 'android', platforms.android.version, jasmine.any(Function));
});
});
describe('custom method (loads custom cordova libs)', function() {
var mkdir, exists, fire, rm, sym;
beforeEach(function() {
mkdir = spyOn(shell, 'mkdir');
rm = spyOn(shell, 'rm');
sym = spyOn(fs, 'symlinkSync');
exists = spyOn(fs, 'existsSync').andReturn(false);
fire = spyOn(hooker, 'fire').andCallFake(function(evt, data, cb) {
cb();
});
});
it('should callback with no errors and not fire event hooks if library already exists', function(done) {
exists.andReturn(true);
lazy_load.custom('some url', 'some id', 'platform X', 'three point five', function(err) {
expect(err).not.toBeDefined();
expect(fire).not.toHaveBeenCalled()
done();
});
});
it('should fire a before_library_download event before it starts downloading a library', function() {
lazy_load.custom('some url', 'some id', 'platform X', 'three point five');
expect(fire).toHaveBeenCalledWith('before_library_download', {platform:'platform X', url:'some url', id:'some id', version:'three point five'}, jasmine.any(Function));
});
describe('remote URLs for libraries', function() {
var req,
load_spy,
p1 = jasmine.createSpy().andReturn({
on:function() {
return {
on:function(){}
}
}
});
var p2 = jasmine.createSpy().andReturn({pipe:p1});
beforeEach(function() {
req = spyOn(request, 'get').andReturn({
pipe:p2
});
load_spy = spyOn(npm, 'load').andCallFake(function(cb) { cb(); });
npm.config.get = function() { return null; };
});
it('should call request with appopriate url params', function() {
var url = 'https://github.com/apache/someplugin';
lazy_load.custom(url, 'random', 'android', '1.0');
expect(req).toHaveBeenCalledWith({
uri:url
}, jasmine.any(Function));
});
it('should take into account https-proxy npm configuration var if exists for https:// calls', function() {
var proxy = 'https://somelocalproxy.com';
npm.config.get = function() { return proxy; };
var url = 'https://github.com/apache/someplugin';
lazy_load.custom(url, 'random', 'android', '1.0');
expect(req).toHaveBeenCalledWith({
uri:url,
proxy:proxy
}, jasmine.any(Function));
});
it('should take into account proxy npm config var if exists for http:// calls', function() {
var proxy = 'http://somelocalproxy.com';
npm.config.get = function() { return proxy; };
var url = 'http://github.com/apache/someplugin';
lazy_load.custom(url, 'random', 'android', '1.0');
expect(req).toHaveBeenCalledWith({
uri:url,
proxy:proxy
}, jasmine.any(Function));
});
});
describe('local paths for libraries', function() {
it('should symlink to local path', function() {
lazy_load.custom('/some/random/lib', 'id', 'X', 'three point five')
expect(sym).toHaveBeenCalledWith('/some/random/lib', path.join(util.libDirectory, 'X', 'id', 'three point five'), 'dir');
});
it('should fire after hook once done', function() {
lazy_load.custom('/some/random/lib', 'id', 'X', 'three point five')
expect(fire).toHaveBeenCalledWith('after_library_download', {platform:'X',url:'/some/random/lib',id:'id',version:'three point five',path:path.join(util.libDirectory, 'X', 'id', 'three point five'), symlink:true}, jasmine.any(Function));
});
});
});
describe('based_on_config method', function() {
var cordova, custom;
beforeEach(function() {
cordova = spyOn(lazy_load, 'cordova');
custom = spyOn(lazy_load, 'custom');
});
it('should invoke custom if a custom lib is specified', function() {
var read = spyOn(config, 'read').andReturn({
lib:{
maybe:{
uri:'you or eye?',
id:'eye dee',
version:'four point twenty'
}
}
});
var p = '/some/random/custom/path';
custom_path.andReturn(p);
lazy_load.based_on_config('yup', 'maybe');
expect(custom).toHaveBeenCalledWith('you or eye?', 'eye dee', 'maybe', 'four point twenty', undefined);
});
it('should invoke cordova if no custom lib is specified', function() {
lazy_load.based_on_config('yup', 'ios');
expect(cordova).toHaveBeenCalledWith('ios', undefined);
});
});
});