Update test specs to use Jasmine 3 (#2089)
* Updated Karma and Jasmine versions * Added DOMObserver class. Supports promise-based testing of DOM changes Update asynchronous test specs to use promises or done() instead of waitsFor/runs * Modified ActionCapability to duplicate context object properties as own properties for better object equality comparisons * Global find + replace to fix syntax issues * Fixed various issues caused by non-deterministic runtime order of tests in Jasmine 3. Fixed issues caused by changes to determination of object equality * Addressed review comments * Resolved merge conflicts with master * Fixed style errors * Use spy.calls.count() instead of manually tracking
This commit is contained in:
committed by
Pete Richards
parent
013eba744d
commit
433dee0314
@@ -38,8 +38,8 @@ define(
|
||||
['getModel', 'useCapability', 'getCapability', 'hasCapability']
|
||||
);
|
||||
mockModel = {};
|
||||
mockDomainObject.getModel.andReturn(mockModel);
|
||||
mockDomainObject.getCapability.andCallFake(function (name) {
|
||||
mockDomainObject.getModel.and.returnValue(mockModel);
|
||||
mockDomainObject.getCapability.and.callFake(function (name) {
|
||||
return name === 'editor' && {
|
||||
isEditContextRoot: function () {
|
||||
return true;
|
||||
@@ -53,7 +53,7 @@ define(
|
||||
mockAPI = {
|
||||
telemetry: mockTelemetryAPI
|
||||
};
|
||||
mockTelemetryAPI.getValueFormatter.andCallFake(function (metadata) {
|
||||
mockTelemetryAPI.getValueFormatter.and.callFake(function (metadata) {
|
||||
var formatter = jasmine.createSpyObj(
|
||||
'telemetryFormatter:' + metadata.key,
|
||||
[
|
||||
@@ -64,8 +64,8 @@ define(
|
||||
var getter = function (datum) {
|
||||
return datum[metadata.key];
|
||||
};
|
||||
formatter.format.andCallFake(getter);
|
||||
formatter.parse.andCallFake(getter);
|
||||
formatter.format.and.callFake(getter);
|
||||
formatter.parse.and.callFake(getter);
|
||||
return formatter;
|
||||
});
|
||||
|
||||
|
||||
@@ -84,7 +84,7 @@ define(
|
||||
|
||||
it("discards telemetry data with a time stamp " +
|
||||
"before specified start bound", function () {
|
||||
var discarded = discardedCallback.mostRecentCall.args[0];
|
||||
var discarded = discardedCallback.calls.mostRecent().args[0];
|
||||
|
||||
// Expect 5 because as an optimization, the TelemetryCollection
|
||||
// will not consider telemetry values that exceed the upper
|
||||
|
||||
@@ -47,7 +47,7 @@ define(
|
||||
mockFormat;
|
||||
|
||||
function getCallback(target, event) {
|
||||
return target.calls.filter(function (call) {
|
||||
return target.calls.all().filter(function (call) {
|
||||
return call.args[0] === event;
|
||||
})[0].args[1];
|
||||
}
|
||||
@@ -61,7 +61,7 @@ define(
|
||||
'$watchCollection',
|
||||
'$digest'
|
||||
]);
|
||||
mockScope.$watchCollection.andCallFake(function (event, callback) {
|
||||
mockScope.$watchCollection.and.callFake(function (event, callback) {
|
||||
watches[event] = callback;
|
||||
});
|
||||
|
||||
@@ -80,7 +80,7 @@ define(
|
||||
|
||||
mockScope.displayHeaders = true;
|
||||
mockWindow = jasmine.createSpyObj('$window', ['requestAnimationFrame']);
|
||||
mockWindow.requestAnimationFrame.andCallFake(function (f) {
|
||||
mockWindow.requestAnimationFrame.and.callFake(function (f) {
|
||||
return f();
|
||||
});
|
||||
|
||||
@@ -91,7 +91,7 @@ define(
|
||||
mockFormatService = jasmine.createSpyObj('formatService', [
|
||||
'getFormat'
|
||||
]);
|
||||
mockFormatService.getFormat.andReturn(mockFormat);
|
||||
mockFormatService.getFormat.and.returnValue(mockFormat);
|
||||
|
||||
controller = new MCTTableController(
|
||||
mockScope,
|
||||
@@ -101,7 +101,7 @@ define(
|
||||
mockFormatService,
|
||||
{time: mockConductor}
|
||||
);
|
||||
spyOn(controller, 'setVisibleRows').andCallThrough();
|
||||
spyOn(controller, 'setVisibleRows').and.callThrough();
|
||||
});
|
||||
|
||||
it('Reacts to changes to filters, headers, and rows', function () {
|
||||
@@ -186,8 +186,8 @@ define(
|
||||
mockScope.sortDirection = 'asc';
|
||||
|
||||
var toi = moment.utc(testDate).valueOf();
|
||||
mockFormat.parse.andReturn(toi);
|
||||
mockFormat.format.andReturn(testDate);
|
||||
mockFormat.parse.and.returnValue(toi);
|
||||
mockFormat.format.and.returnValue(testDate);
|
||||
|
||||
//mock setting the timeColumns parameter
|
||||
getCallback(mockScope.$watch, 'timeColumns')(['col2']);
|
||||
@@ -204,8 +204,8 @@ define(
|
||||
mockScope.sortDirection = 'desc';
|
||||
|
||||
var toi = moment.utc(testDate).valueOf();
|
||||
mockFormat.parse.andReturn(toi);
|
||||
mockFormat.format.andReturn(testDate);
|
||||
mockFormat.parse.and.returnValue(toi);
|
||||
mockFormat.format.and.returnValue(testDate);
|
||||
|
||||
//mock setting the timeColumns parameter
|
||||
getCallback(mockScope.$watch, 'timeColumns')(['col2']);
|
||||
@@ -220,9 +220,9 @@ define(
|
||||
mockScope.sortDirection = 'asc';
|
||||
|
||||
var toi = moment.utc(testDate).valueOf();
|
||||
mockFormat.parse.andReturn(toi);
|
||||
mockFormat.format.andReturn(testDate);
|
||||
mockConductor.timeOfInterest.andReturn(toi);
|
||||
mockFormat.parse.and.returnValue(toi);
|
||||
mockFormat.format.and.returnValue(testDate);
|
||||
mockConductor.timeOfInterest.and.returnValue(toi);
|
||||
|
||||
//mock setting the timeColumns parameter
|
||||
getCallback(mockScope.$watch, 'timeColumns')(['col2']);
|
||||
@@ -230,19 +230,10 @@ define(
|
||||
//Mock setting the rows on scope
|
||||
var rowsCallback = getCallback(mockScope.$watch, 'rows');
|
||||
var setRowsPromise = rowsCallback(rowsAsc);
|
||||
var promiseResolved = false;
|
||||
setRowsPromise.then(function () {
|
||||
promiseResolved = true;
|
||||
});
|
||||
|
||||
waitsFor(function () {
|
||||
return promiseResolved;
|
||||
}, "promise to resolve", 100);
|
||||
|
||||
runs(function () {
|
||||
return setRowsPromise.then(function () {
|
||||
expect(mockScope.toiRowIndex).toBe(2);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
@@ -322,7 +313,7 @@ define(
|
||||
mockScope.exportAsCSV();
|
||||
expect(mockExportService.exportCSV)
|
||||
.toHaveBeenCalled();
|
||||
mockExportService.exportCSV.mostRecentCall.args[0]
|
||||
mockExportService.exportCSV.calls.mostRecent().args[0]
|
||||
.forEach(function (row, i) {
|
||||
Object.keys(row).forEach(function (k) {
|
||||
expect(row[k]).toEqual(
|
||||
@@ -389,18 +380,11 @@ define(
|
||||
var oldRows;
|
||||
mockScope.rows = testRows;
|
||||
var setRowsPromise = controller.setRows(testRows);
|
||||
var promiseResolved = false;
|
||||
setRowsPromise.then(function () {
|
||||
promiseResolved = true;
|
||||
});
|
||||
|
||||
oldRows = mockScope.visibleRows;
|
||||
mockScope.toggleSort('col2');
|
||||
|
||||
waitsFor(function () {
|
||||
return promiseResolved;
|
||||
}, "promise to resolve", 100);
|
||||
|
||||
runs(function () {
|
||||
return setRowsPromise.then(function () {
|
||||
expect(mockScope.visibleRows).not.toEqual(oldRows);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -40,8 +40,8 @@ define(
|
||||
'getCapability',
|
||||
'getModel'
|
||||
]);
|
||||
mockDomainObject.getCapability.andReturn(mockCapability);
|
||||
mockDomainObject.getModel.andReturn({});
|
||||
mockDomainObject.getCapability.and.returnValue(mockCapability);
|
||||
mockDomainObject.getModel.and.returnValue({});
|
||||
|
||||
mockScope = jasmine.createSpyObj('scope', [
|
||||
'$watchCollection',
|
||||
@@ -61,12 +61,12 @@ define(
|
||||
var unlistenFunc = jasmine.createSpy("unlisten");
|
||||
controller.listeners.push(unlistenFunc);
|
||||
expect(mockScope.$on).toHaveBeenCalledWith('$destroy', jasmine.any(Function));
|
||||
mockScope.$on.mostRecentCall.args[1]();
|
||||
mockScope.$on.calls.mostRecent().args[1]();
|
||||
expect(unlistenFunc).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('Registers a listener for mutation events on the object', function () {
|
||||
mockScope.$watch.mostRecentCall.args[1](mockDomainObject);
|
||||
mockScope.$watch.calls.mostRecent().args[1](mockDomainObject);
|
||||
expect(mockCapability.listen).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ define(
|
||||
mockBounds;
|
||||
|
||||
function getCallback(target, event) {
|
||||
return target.calls.filter(function (call) {
|
||||
return target.calls.all().filter(function (call) {
|
||||
return call.args[0] === event;
|
||||
})[0].args[1];
|
||||
}
|
||||
@@ -60,8 +60,8 @@ define(
|
||||
"off",
|
||||
"timeSystem"
|
||||
]);
|
||||
mockConductor.bounds.andReturn(mockBounds);
|
||||
mockConductor.clock.andReturn(undefined);
|
||||
mockConductor.bounds.and.returnValue(mockBounds);
|
||||
mockConductor.clock.and.returnValue(undefined);
|
||||
|
||||
mockDomainObject = jasmine.createSpyObj("domainObject", [
|
||||
"getModel",
|
||||
@@ -69,9 +69,9 @@ define(
|
||||
"useCapability",
|
||||
"hasCapability"
|
||||
]);
|
||||
mockDomainObject.getModel.andReturn({});
|
||||
mockDomainObject.getId.andReturn("mockId");
|
||||
mockDomainObject.useCapability.andReturn(true);
|
||||
mockDomainObject.getModel.and.returnValue({});
|
||||
mockDomainObject.getId.and.returnValue("mockId");
|
||||
mockDomainObject.useCapability.and.returnValue(true);
|
||||
|
||||
mockCompositionAPI = jasmine.createSpyObj("compositionAPI", [
|
||||
"get"
|
||||
@@ -81,7 +81,7 @@ define(
|
||||
"observe"
|
||||
]);
|
||||
unobserve = jasmine.createSpy("unobserve");
|
||||
mockObjectAPI.observe.andReturn(unobserve);
|
||||
mockObjectAPI.observe.and.returnValue(unobserve);
|
||||
|
||||
mockScope = jasmine.createSpyObj("scope", [
|
||||
"$on",
|
||||
@@ -99,9 +99,14 @@ define(
|
||||
"limitEvaluator",
|
||||
"getValueFormatter"
|
||||
]);
|
||||
mockTelemetryAPI.commonValuesForHints.andReturn([]);
|
||||
mockTelemetryAPI.request.andReturn(Promise.resolve([]));
|
||||
mockTelemetryAPI.getValueFormatter.andCallFake(function (metadata) {
|
||||
mockTelemetryAPI.commonValuesForHints.and.returnValue([]);
|
||||
mockTelemetryAPI.request.and.returnValue(Promise.resolve([]));
|
||||
mockTelemetryAPI.getMetadata.and.returnValue({
|
||||
values: function () {
|
||||
return [];
|
||||
}
|
||||
});
|
||||
mockTelemetryAPI.getValueFormatter.and.callFake(function (metadata) {
|
||||
var formatter = jasmine.createSpyObj(
|
||||
'telemetryFormatter:' + metadata.key,
|
||||
[
|
||||
@@ -112,19 +117,15 @@ define(
|
||||
var getter = function (datum) {
|
||||
return datum[metadata.key];
|
||||
};
|
||||
formatter.format.andCallFake(getter);
|
||||
formatter.parse.andCallFake(getter);
|
||||
formatter.format.and.callFake(getter);
|
||||
formatter.parse.and.callFake(getter);
|
||||
return formatter;
|
||||
});
|
||||
mockTelemetryAPI.getMetadata.andReturn({
|
||||
values: function () {
|
||||
return [];
|
||||
}
|
||||
});
|
||||
mockTelemetryAPI.isTelemetryObject.andReturn(false);
|
||||
|
||||
mockTelemetryAPI.isTelemetryObject.and.returnValue(false);
|
||||
|
||||
mockTimeout = jasmine.createSpy("timeout");
|
||||
mockTimeout.andReturn(1); // Return something
|
||||
mockTimeout.and.returnValue(1); // Return something
|
||||
mockTimeout.cancel = jasmine.createSpy("cancel");
|
||||
|
||||
mockAPI = {
|
||||
@@ -141,7 +142,7 @@ define(
|
||||
controller.registerChangeListeners();
|
||||
});
|
||||
it('object mutation', function () {
|
||||
var calledObject = mockObjectAPI.observe.mostRecentCall.args[0];
|
||||
var calledObject = mockObjectAPI.observe.calls.mostRecent().args[0];
|
||||
|
||||
expect(mockObjectAPI.observe).toHaveBeenCalled();
|
||||
expect(calledObject.identifier.key).toEqual(mockDomainObject.getId());
|
||||
@@ -197,13 +198,13 @@ define(
|
||||
};
|
||||
|
||||
unsubscribe = jasmine.createSpy("unsubscribe");
|
||||
mockTelemetryAPI.subscribe.andReturn(unsubscribe);
|
||||
mockTelemetryAPI.subscribe.and.returnValue(unsubscribe);
|
||||
|
||||
mockChildren = [mockTelemetryObject];
|
||||
mockComposition.load.andReturn(Promise.resolve(mockChildren));
|
||||
mockCompositionAPI.get.andReturn(mockComposition);
|
||||
mockComposition.load.and.returnValue(Promise.resolve(mockChildren));
|
||||
mockCompositionAPI.get.and.returnValue(mockComposition);
|
||||
|
||||
mockTelemetryAPI.isTelemetryObject.andCallFake(function (obj) {
|
||||
mockTelemetryAPI.isTelemetryObject.and.callFake(function (obj) {
|
||||
return obj.identifier.key === mockTelemetryObject.identifier.key;
|
||||
});
|
||||
|
||||
@@ -211,28 +212,13 @@ define(
|
||||
});
|
||||
|
||||
it('fetches historical data for the time period specified by the conductor bounds', function () {
|
||||
controller.getData().then(function () {
|
||||
done = true;
|
||||
});
|
||||
waitsFor(function () {
|
||||
return done;
|
||||
}, "getData to return", 100);
|
||||
|
||||
runs(function () {
|
||||
return controller.getData().then(function () {
|
||||
expect(mockTelemetryAPI.request).toHaveBeenCalledWith(mockTelemetryObject, mockBounds);
|
||||
});
|
||||
});
|
||||
|
||||
it('unsubscribes on view destruction', function () {
|
||||
controller.getData().then(function () {
|
||||
done = true;
|
||||
});
|
||||
|
||||
waitsFor(function () {
|
||||
return done;
|
||||
}, "getData to return", 100);
|
||||
|
||||
runs(function () {
|
||||
return controller.getData().then(function () {
|
||||
var destroy = getCallback(mockScope.$on, "$destroy");
|
||||
destroy();
|
||||
|
||||
@@ -240,40 +226,19 @@ define(
|
||||
});
|
||||
});
|
||||
it('fetches historical data for the time period specified by the conductor bounds', function () {
|
||||
controller.getData().then(function () {
|
||||
done = true;
|
||||
});
|
||||
waitsFor(function () {
|
||||
return done;
|
||||
}, "getData to return", 100);
|
||||
|
||||
runs(function () {
|
||||
return controller.getData().then(function () {
|
||||
expect(mockTelemetryAPI.request).toHaveBeenCalledWith(mockTelemetryObject, mockBounds);
|
||||
});
|
||||
});
|
||||
|
||||
it('fetches data for, and subscribes to parent object if it is a telemetry object', function () {
|
||||
controller.getData().then(function () {
|
||||
done = true;
|
||||
});
|
||||
waitsFor(function () {
|
||||
return done;
|
||||
}, "getData to return", 100);
|
||||
|
||||
runs(function () {
|
||||
return controller.getData().then(function () {
|
||||
expect(mockTelemetryAPI.subscribe).toHaveBeenCalledWith(mockTelemetryObject, jasmine.any(Function), {});
|
||||
expect(mockTelemetryAPI.request).toHaveBeenCalledWith(mockTelemetryObject, jasmine.any(Object));
|
||||
});
|
||||
});
|
||||
it('fetches data for, and subscribes to parent object if it is a telemetry object', function () {
|
||||
controller.getData().then(function () {
|
||||
done = true;
|
||||
});
|
||||
waitsFor(function () {
|
||||
return done;
|
||||
}, "getData to return", 100);
|
||||
|
||||
runs(function () {
|
||||
return controller.getData().then(function () {
|
||||
expect(mockTelemetryAPI.subscribe).toHaveBeenCalledWith(mockTelemetryObject, jasmine.any(Function), {});
|
||||
expect(mockTelemetryAPI.request).toHaveBeenCalledWith(mockTelemetryObject, jasmine.any(Object));
|
||||
});
|
||||
@@ -289,9 +254,9 @@ define(
|
||||
{name: "child 4"}
|
||||
];
|
||||
mockChildren = mockChildren.concat(mockTelemetryChildren);
|
||||
mockComposition.load.andReturn(Promise.resolve(mockChildren));
|
||||
mockComposition.load.and.returnValue(Promise.resolve(mockChildren));
|
||||
|
||||
mockTelemetryAPI.isTelemetryObject.andCallFake(function (object) {
|
||||
mockTelemetryAPI.isTelemetryObject.and.callFake(function (object) {
|
||||
if (object === mockTelemetryObject) {
|
||||
return false;
|
||||
} else {
|
||||
@@ -299,15 +264,7 @@ define(
|
||||
}
|
||||
});
|
||||
|
||||
controller.getData().then(function () {
|
||||
done = true;
|
||||
});
|
||||
|
||||
waitsFor(function () {
|
||||
return done;
|
||||
}, "getData to return", 100);
|
||||
|
||||
runs(function () {
|
||||
return controller.getData().then(function () {
|
||||
mockTelemetryChildren.forEach(function (child) {
|
||||
expect(mockTelemetryAPI.subscribe).toHaveBeenCalledWith(child, jasmine.any(Function), {});
|
||||
});
|
||||
@@ -364,13 +321,13 @@ define(
|
||||
key: "column1"
|
||||
};
|
||||
|
||||
mockTelemetryAPI.commonValuesForHints.andCallFake(function (metadata, hints) {
|
||||
mockTelemetryAPI.commonValuesForHints.and.callFake(function (metadata, hints) {
|
||||
if (_.eq(hints, ["domain"])) {
|
||||
return domainMetadata;
|
||||
}
|
||||
});
|
||||
|
||||
mockTelemetryAPI.getMetadata.andReturn({
|
||||
mockTelemetryAPI.getMetadata.and.returnValue({
|
||||
values: function () {
|
||||
return allMetadata;
|
||||
}
|
||||
@@ -409,21 +366,21 @@ define(
|
||||
"column3": 9
|
||||
}
|
||||
];
|
||||
|
||||
controller.batchSize = 2;
|
||||
mockTelemetryAPI.request.andReturn(Promise.resolve(mockHistoricalData));
|
||||
mockTelemetryAPI.request.and.returnValue(Promise.resolve(mockHistoricalData));
|
||||
controller.getHistoricalData([mockDomainObject]);
|
||||
|
||||
waitsFor(function () {
|
||||
return !!controller.timeoutHandle;
|
||||
}, "first batch to be processed", 100);
|
||||
|
||||
runs(function () {
|
||||
//Verify that timeout is being used to yield process
|
||||
expect(mockTimeout).toHaveBeenCalled();
|
||||
mockTimeout.mostRecentCall.args[0]();
|
||||
expect(mockTimeout.calls.length).toBe(2);
|
||||
mockTimeout.mostRecentCall.args[0]();
|
||||
return new Promise(function (resolve) {
|
||||
mockTimeout.and.callFake(function () {
|
||||
resolve();
|
||||
});
|
||||
}).then(function () {
|
||||
mockTimeout.calls.mostRecent().args[0]();
|
||||
expect(mockTimeout.calls.count()).toBe(2);
|
||||
mockTimeout.calls.mostRecent().args[0]();
|
||||
expect(mockScope.rows.length).toBe(3);
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -435,7 +392,7 @@ define(
|
||||
{"column3": "value 3"}
|
||||
];
|
||||
|
||||
spyOn(controller.telemetry, "on").andCallThrough();
|
||||
spyOn(controller.telemetry, "on").and.callThrough();
|
||||
|
||||
controller.registerChangeListeners();
|
||||
expect(controller.telemetry.on).toHaveBeenCalledWith("discarded", jasmine.any(Function));
|
||||
@@ -453,10 +410,10 @@ define(
|
||||
mockScope.rows = [{ a: -1 }];
|
||||
expectedRows = mockScope.rows.concat(testRows);
|
||||
|
||||
spyOn(controller.telemetry, "on").andCallThrough();
|
||||
spyOn(controller.telemetry, "on").and.callThrough();
|
||||
controller.registerChangeListeners();
|
||||
|
||||
controller.telemetry.on.calls.forEach(function (call) {
|
||||
controller.telemetry.on.calls.all().forEach(function (call) {
|
||||
if (call.args[0] === 'added') {
|
||||
call.args[1](testRows);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user