Skip to content

Commit 270cdfd

Browse files
author
Miguel Garcia Lafuente
committed
Finished callbacks support and tests
1 parent d30d592 commit 270cdfd

File tree

2 files changed

+80
-14
lines changed

2 files changed

+80
-14
lines changed

lib/vendor/mockMashupPlatform.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ var MockMP = {};
110110
// });
111111
// Don't need, because setStrategy restore all
112112

113-
// And get back to the default values
113+
// Get back to the default values
114114
if (typeof this.defaultValues === 'undefined' || typeof this.defaultValues !== 'object') {
115115
this.defaultValues = {};
116116
}
@@ -123,13 +123,27 @@ var MockMP = {};
123123
createMocks.call(this);
124124
};
125125

126+
namespace.MockMP.prototype.simulateReceiveEvent = function simulateReceiveEvent(name, value) {
127+
_call_callbacks.call(this, 'wiring', name, value, true);
128+
};
129+
130+
namespace.MockMP.prototype.simulateReceiveContext = function simulateReceiveContext(name, values) {
131+
var single_o = false;
132+
if (typeof values === 'undefined') {
133+
values = name;
134+
name = '';
135+
single_o = true;
136+
}
137+
_call_callbacks.call(this, 'widget', name, values, single_o);
138+
};
139+
126140

127141
/*****************************************************************************/
128142
/********************************** Private **********************************/
129143
/*****************************************************************************/
130144

131145
var default_preferences_set = function default_preferences_set(name, value) {
132-
/* istanbul ignore else */ // ignore else because it should never lead to that
146+
/* istanbul ignore next: This function is private, shouldn't be called outside */
133147
if (this.methodValues && this.methodValues['prefs.get']) {
134148
var old_f = this.methodValues['prefs.get'];
135149
this.methodValues['prefs.get'] = function (name2) {
@@ -152,7 +166,7 @@ var MockMP = {};
152166

153167
var default_register_callback = function default_register_callback(name) {
154168
var valid_names = ['prefs', 'widget', 'wiring'];
155-
/* istanbul ignore next */ // This function is private, shouldn't be called outside
169+
/* istanbul ignore next: This function is private, shouldn't be called outside */
156170
if (valid_names.indexOf(name) == -1) {
157171
throw new TypeError('You are trying to register in a non existant name: ' + name);
158172
}
@@ -172,7 +186,6 @@ var MockMP = {};
172186
if (typeof this.callbacks[name][setted] === 'undefined') {
173187
this.callbacks[name][setted] = [];
174188
}
175-
176189
this.callbacks[name][setted].push(cb);
177190
return null;
178191
};
@@ -192,14 +205,18 @@ var MockMP = {};
192205
if (typeof onlyValue === 'undefined') {
193206
onlyValue = false;
194207
}
208+
var toget = vname;
195209
if (!onlyValue) {
196210
var nv = {};
197211
nv[vname] = value; // Send as object
198212
value = nv;
213+
toget = '';
199214
}
200215

201-
var calls = _get_callbacks.call(this, 'prefs', 'vname');
216+
var calls = _get_callbacks.call(this, cname, toget);
217+
202218
for (var c in calls) {
219+
/* istanbul ignore else: The callback initializer ensures that this will be a function */
203220
if (typeof calls[c] == 'function') {
204221
calls[c](value);
205222
}
@@ -208,16 +225,16 @@ var MockMP = {};
208225

209226
var _get_callbacks = function _get_callbacks(name, sname) {
210227
_initialize_callbacks.call(this);
228+
/* istanbul ignore next: This function is private, shouldn't be called outside */
211229
if (!this.callbacks[name]) {
212230
return [];
213231
}
214232
var callsn = this.callbacks[name];
215233

216-
var allcalls = callsn[''] || [];
217-
234+
var allcalls = callsn[''] || /* istanbul ignore next: We've already initialized before */ [];
218235
if (typeof sname !== 'undefined' && sname !== '') {
219236
var speccalls = callsn[sname] || [];
220-
allcalls.concat(speccalls);
237+
allcalls = allcalls.concat(speccalls);
221238
}
222239

223240
return allcalls;
@@ -231,7 +248,6 @@ var MockMP = {};
231248
};
232249

233250
var mergeDefault = function mergeDefault(methodValues) {
234-
235251
var defaultValues = {
236252
"context.get": namespace.strategy.constant("not set yet"),
237253
"http.buildProxyURL": namespace.strategy.constant(null),
@@ -241,17 +257,17 @@ var MockMP = {};
241257
"log.INFO": 3,
242258
"mashup.context.get": namespace.strategy.constant(null),
243259
"prefs.get": namespace.strategy.constant("value"),
244-
"prefs.set": default_functionality['prefs.set'].bind(this), // namespace.strategy.constant(null),
245-
"prefs.registerCallback": default_functionality['prefs.registerCallback'].bind(this), //namespace.strategy.constant(null),
260+
"prefs.set": default_functionality['prefs.set'].bind(this),
261+
"prefs.registerCallback": default_functionality['prefs.registerCallback'].bind(this),
246262
"widget.getVariable": namespace.strategy.constant("value"),
247263
"widget.drawAttention": namespace.strategy.constant(null),
248264
"widget.id": "id33",
249265
"widget.log": namespace.strategy.constant("something"),
250266
"widget.context.get": namespace.strategy.constant(null),
251-
"widget.context.registerCallback": default_functionality['widget.context.registerCallback'].bind(this), //namespace.strategy.constant(null),
267+
"widget.context.registerCallback": default_functionality['widget.context.registerCallback'].bind(this),
252268
"wiring.getReachableEndpoints": namespace.strategy.constant([]),
253269
"wiring.pushEvent": namespace.strategy.constant(null),
254-
"wiring.registerCallback": default_functionality['wiring.registerCallback'].bind(this) //namespace.strategy.constant(null)
270+
"wiring.registerCallback": default_functionality['wiring.registerCallback'].bind(this)
255271
};
256272

257273
if (methodValues) {

test/js/mockMashupPlatformSpec.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,57 @@
294294
expect(cb).toHaveBeenCalledWith({'test': 'value2'});
295295
});
296296

297-
});
297+
it("Wiring callback", function() {
298+
var cb = jasmine.createSpy('cb');
299+
MashupPlatform.wiring.registerCallback('testv', cb);
300+
expect(cb).not.toHaveBeenCalled();
301+
302+
MashupPlatform.simulateReceiveEvent('testv', 'myvalue!');
303+
expect(cb).toHaveBeenCalledWith('myvalue!');
304+
});
305+
306+
it("Don't call in other wiring event", function() {
307+
var cb = jasmine.createSpy('cb');
308+
MashupPlatform.wiring.registerCallback('testv', cb);
309+
expect(cb).not.toHaveBeenCalled();
310+
311+
MashupPlatform.simulateReceiveEvent('othertestv', 'myvalue!');
312+
expect(cb).not.toHaveBeenCalled();
313+
});
314+
315+
it("Don't call the callback if you remove all callbacks, but don't break :)", function() {
316+
var cb = jasmine.createSpy('cb');
317+
MashupPlatform.wiring.registerCallback('testv', cb);
318+
expect(cb).not.toHaveBeenCalled();
319+
320+
delete MashupPlatform.callbacks;
298321

322+
MashupPlatform.simulateReceiveEvent('testv', 'myvalue!');
323+
expect(cb).not.toHaveBeenCalled();
324+
325+
MashupPlatform.wiring.registerCallback('testv', cb);
326+
expect(cb).not.toHaveBeenCalled();
327+
328+
MashupPlatform.simulateReceiveEvent('testv', 'myvalue!');
329+
expect(cb).toHaveBeenCalledWith('myvalue!');
330+
});
331+
332+
it("Widget callback with object", function() {
333+
var cb = jasmine.createSpy('cb');
334+
MashupPlatform.widget.context.registerCallback(cb);
335+
expect(cb).not.toHaveBeenCalled();
299336

337+
MashupPlatform.simulateReceiveContext({'test': 'value', 'test2': 'value2'});
338+
expect(cb).toHaveBeenCalledWith({'test': 'value', 'test2': 'value2'});
339+
});
340+
341+
it("Widget callback simulate only one value", function() {
342+
var cb = jasmine.createSpy('cb');
343+
MashupPlatform.widget.context.registerCallback(cb);
344+
expect(cb).not.toHaveBeenCalled();
345+
346+
MashupPlatform.simulateReceiveContext('test', 'value');
347+
expect(cb).toHaveBeenCalledWith({'test': 'value'});
348+
});
349+
});
300350
})();

0 commit comments

Comments
 (0)