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:
Andrew Henry
2018-06-29 17:32:59 -07:00
committed by Pete Richards
parent 013eba744d
commit 433dee0314
305 changed files with 2866 additions and 3324 deletions

View File

@@ -38,8 +38,8 @@ define([
'useCapability'
]) };
testAdaptedObject = { foo: 'bar' };
testContext.domainObject.getModel.andReturn(testModel);
testContext.domainObject.useCapability.andCallFake(function (c) {
testContext.domainObject.getModel.and.returnValue(testModel);
testContext.domainObject.useCapability.and.callFake(function (c) {
return c === 'adapter' && testAdaptedObject;
});
});

View File

@@ -58,13 +58,13 @@ define(
['getCapability', 'useCapability', 'getModel']
);
mockDomainObject.useCapability.andCallFake(function (c, v) {
mockDomainObject.useCapability.and.callFake(function (c, v) {
if (c === 'mutation') {
testModel = v(testModel) || testModel;
return asPromise(true);
}
});
mockDomainObject.getModel.andCallFake(function () {
mockDomainObject.getModel.and.callFake(function () {
return testModel;
});
@@ -82,7 +82,7 @@ define(
it("updates the model with a pausedTime", function () {
testModel.pausedTime = undefined;
mockNow.andReturn(12000);
mockNow.and.returnValue(12000);
action.perform();
expect(testModel.pausedTime).toEqual(12000);
});

View File

@@ -58,13 +58,13 @@ define(
['getCapability', 'useCapability', 'getModel']
);
mockDomainObject.useCapability.andCallFake(function (c, v) {
mockDomainObject.useCapability.and.callFake(function (c, v) {
if (c === 'mutation') {
testModel = v(testModel) || testModel;
return asPromise(true);
}
});
mockDomainObject.getModel.andCallFake(function () {
mockDomainObject.getModel.and.callFake(function () {
return testModel;
});
@@ -76,7 +76,7 @@ define(
it("updates the model with a timestamp", function () {
testModel.pausedTime = 12000;
mockNow.andReturn(12000);
mockNow.and.returnValue(12000);
action.perform();
expect(testModel.timestamp).toEqual(12000);
});

View File

@@ -58,13 +58,13 @@ define(
['getCapability', 'useCapability', 'getModel']
);
mockDomainObject.useCapability.andCallFake(function (c, v) {
mockDomainObject.useCapability.and.callFake(function (c, v) {
if (c === 'mutation') {
testModel = v(testModel) || testModel;
return asPromise(true);
}
});
mockDomainObject.getModel.andCallFake(function () {
mockDomainObject.getModel.and.callFake(function () {
return testModel;
});
@@ -75,7 +75,7 @@ define(
});
it("updates the model with a timestamp", function () {
mockNow.andReturn(12000);
mockNow.and.returnValue(12000);
action.perform();
expect(testModel.timestamp).toEqual(12000);
});

View File

@@ -58,13 +58,13 @@ define(
['getCapability', 'useCapability', 'getModel']
);
mockDomainObject.useCapability.andCallFake(function (c, v) {
mockDomainObject.useCapability.and.callFake(function (c, v) {
if (c === 'mutation') {
testModel = v(testModel) || testModel;
return asPromise(true);
}
});
mockDomainObject.getModel.andCallFake(function () {
mockDomainObject.getModel.and.callFake(function () {
return testModel;
});
@@ -75,7 +75,7 @@ define(
});
it("updates the model with a timestamp", function () {
mockNow.andReturn(12000);
mockNow.and.returnValue(12000);
action.perform();
expect(testModel.timestamp).toEqual(undefined);
});

View File

@@ -38,7 +38,7 @@ define(
mockTicker = jasmine.createSpyObj('ticker', ['listen']);
mockUnticker = jasmine.createSpy('unticker');
mockTicker.listen.andReturn(mockUnticker);
mockTicker.listen.and.returnValue(mockUnticker);
controller = new ClockController(mockScope, mockTicker);
});
@@ -57,17 +57,17 @@ define(
it("unsubscribes to ticks when destroyed", function () {
// Make sure $destroy is being listened for...
expect(mockScope.$on.mostRecentCall.args[0]).toEqual('$destroy');
expect(mockScope.$on.calls.mostRecent().args[0]).toEqual('$destroy');
expect(mockUnticker).not.toHaveBeenCalled();
// ...and makes sure that its listener unsubscribes from ticker
mockScope.$on.mostRecentCall.args[1]();
mockScope.$on.calls.mostRecent().args[1]();
expect(mockUnticker).toHaveBeenCalled();
});
it("formats using the format string from the model", function () {
mockTicker.listen.mostRecentCall.args[0](TEST_TIMESTAMP);
mockScope.$watch.mostRecentCall.args[1]({
mockTicker.listen.calls.mostRecent().args[0](TEST_TIMESTAMP);
mockScope.$watch.calls.mostRecent().args[1]({
"clockFormat": [
"YYYY-DDD hh:mm:ss",
"clock24"
@@ -81,8 +81,8 @@ define(
});
it("formats 12-hour time", function () {
mockTicker.listen.mostRecentCall.args[0](TEST_TIMESTAMP);
mockScope.$watch.mostRecentCall.args[1]({
mockTicker.listen.calls.mostRecent().args[0](TEST_TIMESTAMP);
mockScope.$watch.calls.mostRecent().args[1]({
"clockFormat": [
"YYYY-DDD hh:mm:ss",
"clock12"
@@ -96,9 +96,9 @@ define(
});
it("does not throw exceptions when model is undefined", function () {
mockTicker.listen.mostRecentCall.args[0](TEST_TIMESTAMP);
mockTicker.listen.calls.mostRecent().args[0](TEST_TIMESTAMP);
expect(function () {
mockScope.$watch.mostRecentCall.args[1](undefined);
mockScope.$watch.calls.mostRecent().args[1](undefined);
}).not.toThrow();
});

View File

@@ -37,7 +37,7 @@ define(
mockTicker = jasmine.createSpyObj('ticker', ['listen']);
mockUnticker = jasmine.createSpy('unticker');
mockTicker.listen.andReturn(mockUnticker);
mockTicker.listen.and.returnValue(mockUnticker);
controller = new RefreshingController(mockScope, mockTicker);
});
@@ -52,13 +52,13 @@ define(
['persist', 'refresh']
);
mockDomainObject.getCapability.andCallFake(function (c) {
mockDomainObject.getCapability.and.callFake(function (c) {
return (c === 'persistence') && mockPersistence;
});
mockScope.domainObject = mockDomainObject;
mockTicker.listen.mostRecentCall.args[0](12321);
mockTicker.listen.calls.mostRecent().args[0](12321);
expect(mockPersistence.refresh).toHaveBeenCalled();
expect(mockPersistence.persist).not.toHaveBeenCalled();
});
@@ -70,11 +70,11 @@ define(
it("unsubscribes to ticks when destroyed", function () {
// Make sure $destroy is being listened for...
expect(mockScope.$on.mostRecentCall.args[0]).toEqual('$destroy');
expect(mockScope.$on.calls.mostRecent().args[0]).toEqual('$destroy');
expect(mockUnticker).not.toHaveBeenCalled();
// ...and makes sure that its listener unsubscribes from ticker
mockScope.$on.mostRecentCall.args[1]();
mockScope.$on.calls.mostRecent().args[1]();
expect(mockUnticker).toHaveBeenCalled();
});
});

View File

@@ -40,7 +40,7 @@ define(
controller;
function invokeWatch(expr, value) {
mockScope.$watch.calls.forEach(function (call) {
mockScope.$watch.calls.all().forEach(function (call) {
if (call.args[0] === expr) {
call.args[1](value);
}
@@ -78,13 +78,13 @@ define(
);
mockNow = jasmine.createSpy('now');
mockDomainObject.getCapability.andCallFake(function (c) {
mockDomainObject.getCapability.and.callFake(function (c) {
return (c === 'action') && mockActionCapability;
});
mockDomainObject.getModel.andCallFake(function () {
mockDomainObject.getModel.and.callFake(function () {
return testModel;
});
mockActionCapability.getActions.andCallFake(function (k) {
mockActionCapability.getActions.and.callFake(function (k) {
return [{
'timer.start': mockStart,
'timer.pause': mockPause,
@@ -92,9 +92,9 @@ define(
}[k]];
});
mockStart.getMetadata.andReturn({cssClass: "icon-play", name: "Start"});
mockPause.getMetadata.andReturn({cssClass: "icon-pause", name: "Pause"});
mockStop.getMetadata.andReturn({cssClass: "icon-box", name: "Stop"});
mockStart.getMetadata.and.returnValue({cssClass: "icon-play", name: "Start"});
mockPause.getMetadata.and.returnValue({cssClass: "icon-pause", name: "Pause"});
mockStop.getMetadata.and.returnValue({cssClass: "icon-box", name: "Stop"});
mockScope.domainObject = mockDomainObject;
testModel = {};
@@ -124,8 +124,8 @@ define(
it("displays nothing when there is no target", function () {
// Notify that domain object is available via scope
invokeWatch('domainObject', mockDomainObject);
mockNow.andReturn(TEST_TIMESTAMP);
mockWindow.requestAnimationFrame.mostRecentCall.args[0]();
mockNow.and.returnValue(TEST_TIMESTAMP);
mockWindow.requestAnimationFrame.calls.mostRecent().args[0]();
expect(controller.sign()).toEqual("");
expect(controller.signClass()).toEqual("");
expect(controller.text()).toEqual("");
@@ -137,20 +137,20 @@ define(
// Notify that domain object is available via scope
invokeWatch('domainObject', mockDomainObject);
mockNow.andReturn(TEST_TIMESTAMP + 121000);
mockWindow.requestAnimationFrame.mostRecentCall.args[0]();
mockNow.and.returnValue(TEST_TIMESTAMP + 121000);
mockWindow.requestAnimationFrame.calls.mostRecent().args[0]();
expect(controller.sign()).toEqual("+");
expect(controller.signClass()).toEqual("icon-plus");
expect(controller.text()).toEqual("0D 00:02:01");
mockNow.andReturn(TEST_TIMESTAMP - 121000);
mockWindow.requestAnimationFrame.mostRecentCall.args[0]();
mockNow.and.returnValue(TEST_TIMESTAMP - 121000);
mockWindow.requestAnimationFrame.calls.mostRecent().args[0]();
expect(controller.sign()).toEqual("-");
expect(controller.signClass()).toEqual("icon-minus");
expect(controller.text()).toEqual("0D 00:02:01");
mockNow.andReturn(TEST_TIMESTAMP);
mockWindow.requestAnimationFrame.mostRecentCall.args[0]();
mockNow.and.returnValue(TEST_TIMESTAMP);
mockWindow.requestAnimationFrame.calls.mostRecent().args[0]();
expect(controller.sign()).toEqual("");
expect(controller.signClass()).toEqual("");
expect(controller.text()).toEqual("0D 00:00:00");
@@ -190,27 +190,27 @@ define(
});
it("stops requesting animation frames when destroyed", function () {
var initialCount = mockWindow.requestAnimationFrame.calls.length;
var initialCount = mockWindow.requestAnimationFrame.calls.count();
// First, check that normally new frames keep getting requested
mockWindow.requestAnimationFrame.mostRecentCall.args[0]();
expect(mockWindow.requestAnimationFrame.calls.length)
mockWindow.requestAnimationFrame.calls.mostRecent().args[0]();
expect(mockWindow.requestAnimationFrame.calls.count())
.toEqual(initialCount + 1);
mockWindow.requestAnimationFrame.mostRecentCall.args[0]();
expect(mockWindow.requestAnimationFrame.calls.length)
mockWindow.requestAnimationFrame.calls.mostRecent().args[0]();
expect(mockWindow.requestAnimationFrame.calls.count())
.toEqual(initialCount + 2);
// Now, verify that it stops after $destroy
expect(mockScope.$on.mostRecentCall.args[0])
expect(mockScope.$on.calls.mostRecent().args[0])
.toEqual('$destroy');
mockScope.$on.mostRecentCall.args[1]();
mockScope.$on.calls.mostRecent().args[1]();
// Frames should no longer get requested
mockWindow.requestAnimationFrame.mostRecentCall.args[0]();
expect(mockWindow.requestAnimationFrame.calls.length)
mockWindow.requestAnimationFrame.calls.mostRecent().args[0]();
expect(mockWindow.requestAnimationFrame.calls.count())
.toEqual(initialCount + 2);
mockWindow.requestAnimationFrame.mostRecentCall.args[0]();
expect(mockWindow.requestAnimationFrame.calls.length)
mockWindow.requestAnimationFrame.calls.mostRecent().args[0]();
expect(mockWindow.requestAnimationFrame.calls.count())
.toEqual(initialCount + 2);
});
});

View File

@@ -37,13 +37,13 @@ define(
mockTicker = jasmine.createSpyObj('ticker', ['listen']);
mockUnticker = jasmine.createSpy('unticker');
mockTicker.listen.andReturn(mockUnticker);
mockTicker.listen.and.returnValue(mockUnticker);
indicator = new ClockIndicator(mockTicker, TEST_FORMAT);
});
it("displays the current time", function () {
mockTicker.listen.mostRecentCall.args[0](TEST_TIMESTAMP);
mockTicker.listen.calls.mostRecent().args[0](TEST_TIMESTAMP);
expect(indicator.getText()).toEqual("2015-154 17:56:14 UTC");
});

View File

@@ -46,7 +46,7 @@ define(["../../src/indicators/FollowIndicator"], function (FollowIndicator) {
beforeEach(function () {
testObject = { name: "some timer!" };
mockTimerService.getTimer.andReturn(testObject);
mockTimerService.getTimer.and.returnValue(testObject);
});
it("displays the timer's name", function () {

View File

@@ -37,23 +37,23 @@ define(
mockNow = jasmine.createSpy('now');
mockCallback = jasmine.createSpy('callback');
mockNow.andReturn(TEST_TIMESTAMP);
mockNow.and.returnValue(TEST_TIMESTAMP);
tickerService = new TickerService(mockTimeout, mockNow);
});
it("notifies listeners of clock ticks", function () {
tickerService.listen(mockCallback);
mockNow.andReturn(TEST_TIMESTAMP + 12321);
mockTimeout.mostRecentCall.args[0]();
mockNow.and.returnValue(TEST_TIMESTAMP + 12321);
mockTimeout.calls.mostRecent().args[0]();
expect(mockCallback)
.toHaveBeenCalledWith(TEST_TIMESTAMP + 12321);
});
it("allows listeners to unregister", function () {
tickerService.listen(mockCallback)(); // Unregister immediately
mockNow.andReturn(TEST_TIMESTAMP + 12321);
mockTimeout.mostRecentCall.args[0]();
mockNow.and.returnValue(TEST_TIMESTAMP + 12321);
mockTimeout.calls.mostRecent().args[0]();
expect(mockCallback).not
.toHaveBeenCalledWith(TEST_TIMESTAMP + 12321);
});

View File

@@ -69,7 +69,7 @@ define([
'*',
jasmine.any(Function)
);
mockmct.objects.observe.mostRecentCall.args[2](newTimer);
mockmct.objects.observe.calls.mostRecent().args[2](newTimer);
expect(timerService.getTimer()).toBe(newTimer);
});
});