-
Notifications
You must be signed in to change notification settings - Fork 71.9k
/
headless.js
186 lines (154 loc) · 4.99 KB
/
headless.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
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
var read = require('fs').readFileSync;
var _ = require('lodash');
function headless (benv, binding) {
var self = binding;
function root ( ) {
return benv;
}
function init (opts, callback) {
var localStorage = opts.localStorage || './localstorage';
const t = Date.now();
console.log('Headless init');
var htmlFile = opts.htmlFile || __dirname + '/../../views/index.html';
var serverSettings = opts.serverSettings || require('./default-server-settings');
var someData = opts.mockAjax || { };
console.log('Entering setup', Date.now() - t);
benv.setup(function() {
console.log('Setting up benv', Date.now() - t);
benv.require(__dirname + '/../../node_modules/.cache/_ns_cache/public/js/bundle.app.js');
console.log('Bundle loaded', Date.now() - t);
self.$ = $;
self.localCookieStorage = self.localStorage = self.$.localStorage = require(localStorage);
self.$.fn.tooltip = function mockTooltip ( ) { };
var indexHtml = read(htmlFile, 'utf8');
self.$('body').html(indexHtml);
console.log('HTML set', Date.now() - t);
var d3 = require('d3');
//disable all d3 transitions so most of the other code can run with jsdom
d3.timer = function mockTimer() { };
if (opts.mockProfileEditor) {
self.$.plot = function mockPlot () {
};
self.$.fn.tooltip = function mockTooltip ( ) { };
self.$.fn.dialog = function mockDialog (opts) {
function maybeCall (name, obj) {
if (obj[name] && obj[name].call) {
obj[name]();
}
}
maybeCall('open', opts);
_.forEach(opts.buttons, function (button) {
maybeCall('click', button);
});
};
}
if (opts.mockSimpleAjax) {
someData = opts.mockSimpleAjax;
self.$.ajax = function mockAjax (url, opts) {
if (url && url.url) {
url = url.url;
}
var returnVal = someData[url] || [];
if (opts && typeof opts.success === 'function') {
opts.success(returnVal);
return self.$.Deferred().resolveWith(returnVal);
} else {
return {
done: function mockDone (fn) {
if (url.indexOf('status.json') > -1) {
fn(serverSettings);
} else {
fn({message: 'OK'});
}
return self.$.ajax();
},
fail: function mockFail () {
return self.$.ajax();
}
};
}
};
}
if (opts.mockAjax) {
self.$.ajax = function mockAjax (url, opts) {
if (url && url.url) {
url = url.url;
}
//logfile.write(url+'\n');
//console.log(url,opts);
if (opts && opts.success && opts.success.call) {
return {
done: function mockDone (fn) {
if (someData[url]) {
console.log('+++++Data for ' + url + ' sent');
opts.success(someData[url]);
} else {
console.log('-----Data for ' + url + ' missing');
opts.success([]);
}
fn();
return self.$.ajax();
},
fail: function mockFail () {
return self.$.ajax();
}
};
}
return {
done: function mockDone (fn) {
if (url.indexOf('status.json') > -1) {
fn(serverSettings);
} else {
fn({message: 'OK'});
}
return self.$.ajax();
},
fail: function mockFail () {
return self.$.ajax();
}
};
};
}
console.log('Benv expose', Date.now() - t);
benv.expose({
$: self.$
, jQuery: self.$
, d3: d3
, serverSettings: serverSettings
, localCookieStorage: self.localStorage
, cookieStorageType: self.localStorage
, localStorage: self.localStorage
, io: {
connect: function mockConnect ( ) {
return {
on: function mockOn (event, callback) {
if ('connect' === event && callback) {
callback();
}
}
, emit: function mockEmit (event, data, callback) {
if ('authorize' === event && callback) {
callback({
read: true
});
}
}
};
}
}
});
var extraRequires = opts.benvRequires || [ ];
extraRequires.forEach(function (req) {
benv.require(req);
});
callback( );
});
}
function teardown ( ) {
benv.teardown();
}
root.setup = init;
root.teardown = teardown;
return root;
}
module.exports = headless;