[Telemetry] Fill in new specs

Complete test coverage for new scripts added to bundle
platform/telemetry in support of autoflow tabular views,
WTD-614. Specifically, these implement a telemetrySubscriber
service which can be used to watch for updates to telemetry
values associated with a specific domain object.
This commit is contained in:
Victor Woeltjen 2014-12-30 14:55:50 -08:00
parent f7e3c9479b
commit 0082b99292
2 changed files with 155 additions and 0 deletions

View File

@ -6,6 +6,48 @@ define(
"use strict";
describe("The telemetry subscriber", function () {
// TelemetrySubscriber just provides a factory
// for TelemetrySubscription, so most real testing
// should happen there.
var mockQ,
mockTimeout,
mockDomainObject,
mockCallback,
mockPromise,
subscriber;
beforeEach(function () {
mockQ = jasmine.createSpyObj("$q", ["when"]);
mockTimeout = jasmine.createSpy("$timeout");
mockDomainObject = jasmine.createSpyObj(
"domainObject",
[ "getCapability", "useCapability", "hasCapability" ]
);
mockCallback = jasmine.createSpy("callback");
mockPromise = jasmine.createSpyObj("promise", ["then"]);
mockQ.when.andReturn(mockPromise);
mockPromise.then.andReturn(mockPromise);
subscriber = new TelemetrySubscriber(mockQ, mockTimeout);
});
it("acts as a factory for subscription objects", function () {
var subscription = subscriber.subscribe(
mockDomainObject,
mockCallback
);
// Just verify that this looks like a TelemetrySubscription
[
"unsubscribe",
"getTelemetryObjects",
"getRangeValue",
"getDomainValue"
].forEach(function (method) {
expect(subscription[method])
.toEqual(jasmine.any(Function));
});
});
});
}

View File

@ -6,7 +6,120 @@ define(
"use strict";
describe("A telemetry subscription", function () {
var mockQ,
mockTimeout,
mockDomainObject,
mockCallback,
mockTelemetry,
mockUnsubscribe,
mockSeries,
subscription;
function mockPromise(value) {
return (value && value.then) ? value : {
then: function (callback) {
return mockPromise(callback(value));
}
};
}
beforeEach(function () {
mockQ = jasmine.createSpyObj("$q", ["when", "all"]);
mockTimeout = jasmine.createSpy("$timeout");
mockDomainObject = jasmine.createSpyObj(
"domainObject",
[ "getCapability", "useCapability", "hasCapability", "getId" ]
);
mockCallback = jasmine.createSpy("callback");
mockTelemetry = jasmine.createSpyObj(
"telemetry",
["subscribe"]
);
mockUnsubscribe = jasmine.createSpy("unsubscribe");
mockSeries = jasmine.createSpyObj(
"series",
[ "getPointCount", "getDomainValue", "getRangeValue" ]
);
mockQ.when.andCallFake(mockPromise);
mockDomainObject.hasCapability.andReturn(true);
mockDomainObject.getCapability.andReturn(mockTelemetry);
mockDomainObject.getId.andReturn('test-id');
mockTelemetry.subscribe.andReturn(mockUnsubscribe);
mockSeries.getPointCount.andReturn(42);
mockSeries.getDomainValue.andReturn(123456);
mockSeries.getRangeValue.andReturn(789);
subscription = new TelemetrySubscription(
mockQ,
mockTimeout,
mockDomainObject,
mockCallback
);
});
it("subscribes to the provided object", function () {
expect(mockTelemetry.subscribe).toHaveBeenCalled();
});
it("unsubscribes on request", function () {
expect(mockUnsubscribe).not.toHaveBeenCalled();
subscription.unsubscribe();
expect(mockUnsubscribe).toHaveBeenCalled();
});
it("fires callbacks when subscriptions update", function () {
expect(mockCallback).not.toHaveBeenCalled();
mockTelemetry.subscribe.mostRecentCall.args[0](mockSeries);
// This gets fired via a timeout, so trigger that
expect(mockTimeout).toHaveBeenCalledWith(
jasmine.any(Function),
0
);
mockTimeout.mostRecentCall.args[0]();
// Should have triggered the callback to alert that
// new data was available
expect(mockCallback).toHaveBeenCalled();
});
it("fires subscription callbacks once per cycle", function () {
var i;
for (i = 0; i < 100; i += 1) {
mockTelemetry.subscribe.mostRecentCall.args[0](mockSeries);
}
// This gets fired via a timeout, so trigger any of those
mockTimeout.calls.forEach(function (call) {
call.args[0]();
});
// Should have only triggered the
expect(mockCallback.calls.length).toEqual(1);
});
it("reports its latest observed data values", function () {
mockTelemetry.subscribe.mostRecentCall.args[0](mockSeries);
// This gets fired via a timeout, so trigger that
mockTimeout.mostRecentCall.args[0]();
// Verify that the last sample was looked at
expect(mockSeries.getDomainValue).toHaveBeenCalledWith(41);
expect(mockSeries.getRangeValue).toHaveBeenCalledWith(41);
// Domain and range values should now be available
expect(subscription.getDomainValue(mockDomainObject))
.toEqual(123456);
expect(subscription.getRangeValue(mockDomainObject))
.toEqual(789);
});
it("provides no objects if no domain object is provided", function () {
// omit last arguments
subscription = new TelemetrySubscription(mockQ, mockTimeout);
// Should have no objects
expect(subscription.getTelemetryObjects()).toEqual([]);
});
});
}
);