mirror of
https://github.com/nasa/openmct.git
synced 2025-02-20 17:33:23 +00:00
[Core] Add test case for shared listener
Add test case for sharing listeners across mutation capability instances, WTD-1329.
This commit is contained in:
parent
877461c4a4
commit
e0f672d40d
@ -29,6 +29,8 @@ define(
|
||||
function () {
|
||||
"use strict";
|
||||
|
||||
var TOPIC_PREFIX = "mutation:";
|
||||
|
||||
// Utility function to overwrite a destination object
|
||||
// with the contents of a source object.
|
||||
function copyValues(destination, source) {
|
||||
@ -72,13 +74,7 @@ define(
|
||||
* @constructor
|
||||
*/
|
||||
function MutationCapability(topic, now, domainObject) {
|
||||
var listeners = [];
|
||||
|
||||
function notifyListeners(model) {
|
||||
listeners.forEach(function (listener) {
|
||||
listener(model);
|
||||
});
|
||||
}
|
||||
var t = topic(TOPIC_PREFIX + domainObject.getId());
|
||||
|
||||
function mutate(mutator, timestamp) {
|
||||
// Get the object's model and clone it, so the
|
||||
@ -103,7 +99,7 @@ define(
|
||||
copyValues(model, result);
|
||||
}
|
||||
model.modified = useTimestamp ? timestamp : now();
|
||||
notifyListeners(model);
|
||||
t.notify(model);
|
||||
}
|
||||
|
||||
// Report the result of the mutation
|
||||
@ -116,12 +112,7 @@ define(
|
||||
}
|
||||
|
||||
function listen(listener) {
|
||||
listeners.push(listener);
|
||||
return function unlisten() {
|
||||
listeners = listeners.filter(function (l) {
|
||||
return l !== listener;
|
||||
});
|
||||
};
|
||||
return t.listen(listener);
|
||||
}
|
||||
|
||||
return {
|
||||
|
@ -36,7 +36,10 @@ define(
|
||||
var testModel,
|
||||
topic,
|
||||
mockNow,
|
||||
domainObject = { getModel: function () { return testModel; } },
|
||||
domainObject = {
|
||||
getId: function () { return "test-id"; },
|
||||
getModel: function () { return testModel; }
|
||||
},
|
||||
mutation;
|
||||
|
||||
beforeEach(function () {
|
||||
@ -112,6 +115,22 @@ define(
|
||||
});
|
||||
expect(mockCallback).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("shares listeners across instances", function () {
|
||||
var mockCallback = jasmine.createSpy('callback'),
|
||||
otherMutation = new MutationCapability(
|
||||
topic,
|
||||
mockNow,
|
||||
domainObject
|
||||
);
|
||||
mutation.listen(mockCallback);
|
||||
otherMutation.invoke(function (m) {
|
||||
m.number = 8;
|
||||
});
|
||||
expect(mockCallback).toHaveBeenCalled();
|
||||
expect(mockCallback.mostRecentCall.args[0].number)
|
||||
.toEqual(8);
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user