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

@@ -34,7 +34,7 @@ define(
"action" + index,
["perform", "getMetadata"]
);
action.getMetadata.andReturn(metadata);
action.getMetadata.and.returnValue(metadata);
return action;
}
@@ -61,7 +61,7 @@ define(
mockScope.action = mockActions;
mockScope.parameters = { category: "test" };
mockActions.getActions.andReturn([
mockActions.getActions.and.returnValue([
{ group: "a", someKey: 0 },
{ group: "a", someKey: 1 },
{ group: "b", someKey: 2 },
@@ -74,7 +74,7 @@ define(
].map(mockAction));
// Call the watch
mockScope.$watch.mostRecentCall.args[1]();
mockScope.$watch.calls.mostRecent().args[1]();
// Should have grouped and ungrouped actions in scope now
expect(mockScope.groups.length).toEqual(2);
@@ -85,7 +85,7 @@ define(
it("provides empty arrays when no action capability is available", function () {
// Call the watch
mockScope.$watch.mostRecentCall.args[1]();
mockScope.$watch.calls.mostRecent().args[1]();
expect(mockScope.groups.length).toEqual(0);
expect(mockScope.ungrouped.length).toEqual(0);

View File

@@ -79,9 +79,9 @@ define(
it("deactivates and detaches listener on document click", function () {
var callback, timeout;
controller.setState(true);
callback = mockDocument.on.mostRecentCall.args[1];
callback = mockDocument.on.calls.mostRecent().args[1];
callback();
timeout = mockTimeout.mostRecentCall.args[0];
timeout = mockTimeout.calls.mostRecent().args[0];
timeout();
expect(controller.isActive()).toEqual(false);
expect(mockDocument.off).toHaveBeenCalledWith("mouseup", callback);

View File

@@ -47,10 +47,10 @@ define(
mockScope.action = mockActions;
mockScope.parameters = { category: "test" };
mockActions.getActions.andReturn(["a", "b", "c"]);
mockActions.getActions.and.returnValue(["a", "b", "c"]);
// Call the watch
mockScope.$watch.mostRecentCall.args[1]();
mockScope.$watch.calls.mostRecent().args[1]();
// Should have grouped and ungrouped actions in scope now
expect(mockScope.menuActions.length).toEqual(3);

View File

@@ -33,7 +33,7 @@ define(
controller;
function fireWatch(expr, value) {
mockScope.$watch.calls.forEach(function (call) {
mockScope.$watch.calls.all().forEach(function (call) {
if (call.args[0] === expr) {
call.args[1](value);
}
@@ -50,15 +50,15 @@ define(
'format'
]);
mockFormatService.getFormat.andReturn(mockFormat);
mockFormatService.getFormat.and.returnValue(mockFormat);
mockFormat.validate.andCallFake(function (text) {
mockFormat.validate.and.callFake(function (text) {
return moment.utc(text, TEST_FORMAT).isValid();
});
mockFormat.parse.andCallFake(function (text) {
mockFormat.parse.and.callFake(function (text) {
return moment.utc(text, TEST_FORMAT).valueOf();
});
mockFormat.format.andCallFake(function (value) {
mockFormat.format.and.callFake(function (value) {
return moment.utc(value).format(TEST_FORMAT);
});
@@ -159,8 +159,8 @@ define(
var newText = "2015-3-3 01:02:04",
oldValue = mockScope.ngModel.testField;
mockFormat.validate.andReturn(true);
mockFormat.parse.andReturn(42);
mockFormat.validate.and.returnValue(true);
mockFormat.parse.and.returnValue(42);
mockScope.textValue = newText;
fireWatch("textValue", newText);
@@ -176,7 +176,7 @@ define(
});
it("throws an error for unknown formats", function () {
mockFormatService.getFormat.andReturn(undefined);
mockFormatService.getFormat.and.returnValue(undefined);
expect(function () {
fireWatch("structure.format", "some-format");
}).toThrow();
@@ -187,9 +187,9 @@ define(
testText = "some text";
beforeEach(function () {
mockFormat.validate.andReturn(true);
mockFormat.parse.andReturn(testValue);
mockFormat.format.andReturn(testText);
mockFormat.validate.and.returnValue(true);
mockFormat.parse.and.returnValue(testValue);
mockFormat.format.and.returnValue(testText);
});
it("parses user input", function () {

View File

@@ -30,7 +30,7 @@ define(
controller;
function fireWatch(expr, value) {
mockScope.$watch.calls.forEach(function (call) {
mockScope.$watch.calls.all().forEach(function (call) {
if (call.args[0] === expr) {
call.args[1](value);
}
@@ -38,7 +38,7 @@ define(
}
function fireWatchCollection(expr, value) {
mockScope.$watchCollection.calls.forEach(function (call) {
mockScope.$watchCollection.calls.all().forEach(function (call) {
if (call.args[0] === expr) {
call.args[1](value);
}

View File

@@ -53,7 +53,7 @@ define(
expect(mockScope.ngModel)
.not.toHaveBeenCalledWith("some new value");
// Fire the matching watcher
mockScope.$watch.calls.forEach(function (call) {
mockScope.$watch.calls.all().forEach(function (call) {
if (call.args[0] === "getterSetter.value") {
call.args[1](mockScope.getterSetter.value);
}
@@ -64,11 +64,11 @@ define(
});
it("updates internal state when external changes are detected", function () {
mockScope.ngModel.andReturn("some other new value");
mockScope.ngModel.and.returnValue("some other new value");
// Verify precondition
expect(mockScope.getterSetter.value).toBeUndefined();
// Fire the matching watcher
mockScope.$watch.calls.forEach(function (call) {
mockScope.$watch.calls.all().forEach(function (call) {
if (call.args[0] === "ngModel()") {
call.args[1]("some other new value");
}

View File

@@ -50,14 +50,14 @@ define(
"promise",
["then"]
);
mockObjectService.getObjects.andReturn(mockPromise);
mockObjectService.getObjects.and.returnValue(mockPromise);
mockDomainObject = jasmine.createSpyObj(
"selectedObject",
["hasCapability", "getCapability", "useCapability", "getModel"]
);
mockDomainObject.getModel.andReturn({location: 'somewhere'});
mockDomainObject.hasCapability.andReturn(true);
mockDomainObject.getModel.and.returnValue({location: 'somewhere'});
mockDomainObject.hasCapability.and.returnValue(true);
mockContextCapability = jasmine.createSpyObj(
"context capability",
@@ -68,7 +68,7 @@ define(
["isLink"]
);
mockDomainObject.getCapability.andCallFake(function (param) {
mockDomainObject.getCapability.and.callFake(function (param) {
if (param === 'location') {
return mockLocationCapability;
} else if (param === 'context') {
@@ -91,21 +91,21 @@ define(
});
it("looks for contextual parent objects", function () {
mockScope.$watch.mostRecentCall.args[1]();
mockScope.$watch.calls.mostRecent().args[1]();
expect(mockContextCapability.getParent).toHaveBeenCalled();
});
it("if link, looks for primary parent objects", function () {
mockLocationCapability.isLink.andReturn(true);
mockLocationCapability.isLink.and.returnValue(true);
mockScope.$watch.mostRecentCall.args[1]();
mockScope.$watch.calls.mostRecent().args[1]();
expect(mockDomainObject.getModel).toHaveBeenCalled();
expect(mockObjectService.getObjects).toHaveBeenCalled();
mockPromise.then.mostRecentCall.args[0]({'somewhere': mockDomainObject});
mockPromise.then.calls.mostRecent().args[0]({'somewhere': mockDomainObject});
});
it("gets metadata", function () {
mockScope.$watch.mostRecentCall.args[1]();
mockScope.$watch.calls.mostRecent().args[1]();
expect(mockDomainObject.useCapability).toHaveBeenCalledWith('metadata');
});
});

View File

@@ -45,7 +45,7 @@ define(
'object-' + id,
['getId']
);
mockObject.getId.andReturn(id);
mockObject.getId.and.returnValue(id);
return mockObject;
}
@@ -72,8 +72,8 @@ define(
mockDomainObjects[id] = makeMockObject(id);
});
mockDomainObject.getCapability.andReturn(mockType);
mockObjectService.getObjects.andReturn(promiseOf(mockDomainObjects));
mockDomainObject.getCapability.and.returnValue(mockType);
mockObjectService.getObjects.and.returnValue(promiseOf(mockDomainObjects));
mockScope.field = "testField";
mockScope.ngModel = {};
@@ -91,27 +91,27 @@ define(
it("watches for changes in selection in left-hand tree", function () {
var testObject = { a: 123, b: 456 };
// This test is sensitive to ordering of watch calls
expect(mockScope.$watch.calls.length).toEqual(1);
expect(mockScope.$watch.calls.count()).toEqual(1);
// Make sure we're watching the correct object
controller.treeModel.selectedObject = testObject;
expect(mockScope.$watch.calls[0].args[0]()).toBe(testObject);
expect(mockScope.$watch.calls.all()[0].args[0]()).toBe(testObject);
});
it("watches for changes in controlled property", function () {
var testValue = ["a", "b", 1, 2];
// This test is sensitive to ordering of watch calls
expect(mockScope.$watchCollection.calls.length).toEqual(1);
expect(mockScope.$watchCollection.calls.count()).toEqual(1);
// Make sure we're watching the correct object
mockScope.ngModel = { testField: testValue };
expect(mockScope.$watchCollection.calls[0].args[0]()).toBe(testValue);
expect(mockScope.$watchCollection.calls.all()[0].args[0]()).toBe(testValue);
});
it("rejects selection of incorrect types", function () {
mockScope.structure = { type: "someType" };
mockType.instanceOf.andReturn(false);
mockType.instanceOf.and.returnValue(false);
controller.treeModel.selectedObject = mockDomainObject;
// Fire the watch
mockScope.$watch.calls[0].args[1](mockDomainObject);
mockScope.$watch.calls.all()[0].args[1](mockDomainObject);
// Should have cleared the selection
expect(controller.treeModel.selectedObject).toBeUndefined();
// Verify interaction (that instanceOf got a useful argument)
@@ -120,10 +120,10 @@ define(
it("permits selection of matching types", function () {
mockScope.structure = { type: "someType" };
mockType.instanceOf.andReturn(true);
mockType.instanceOf.and.returnValue(true);
controller.treeModel.selectedObject = mockDomainObject;
// Fire the watch
mockScope.$watch.calls[0].args[1](mockDomainObject);
mockScope.$watch.calls.all()[0].args[1](mockDomainObject);
// Should have preserved the selection
expect(controller.treeModel.selectedObject).toEqual(mockDomainObject);
// Verify interaction (that instanceOf got a useful argument)
@@ -133,11 +133,11 @@ define(
it("loads objects when the underlying list changes", function () {
var testIds = ["abc", "def", "xyz"];
// This test is sensitive to ordering of watch calls
expect(mockScope.$watchCollection.calls.length).toEqual(1);
expect(mockScope.$watchCollection.calls.count()).toEqual(1);
// Make sure we're watching the correct object
mockScope.ngModel = { testField: testIds };
// Fire the watch
mockScope.$watchCollection.calls[0].args[1](testIds);
mockScope.$watchCollection.calls.all()[0].args[1](testIds);
// Should have loaded the corresponding objects
expect(mockObjectService.getObjects).toHaveBeenCalledWith(testIds);
});
@@ -169,8 +169,8 @@ define(
controller.select(mockDomainObjects.def);
controller.select(mockDomainObjects.abc);
// Fire the watch for the id changes...
mockScope.$watchCollection.calls[0].args[1](
mockScope.$watchCollection.calls[0].args[0]()
mockScope.$watchCollection.calls.all()[0].args[1](
mockScope.$watchCollection.calls.all()[0].args[0]()
);
// Should have loaded and exposed those objects
expect(controller.selected()).toEqual(

View File

@@ -39,7 +39,7 @@ define(
controller;
function fireWatch(expr, value) {
mockScope.$watch.calls.forEach(function (call) {
mockScope.$watch.calls.all().forEach(function (call) {
if (call.args[0] === expr) {
call.args[1](value);
}
@@ -47,7 +47,7 @@ define(
}
function fireWatchCollection(expr, value) {
mockScope.$watchCollection.calls.forEach(function (call) {
mockScope.$watchCollection.calls.all().forEach(function (call) {
if (call.args[0] === expr) {
call.args[1](value);
}
@@ -73,9 +73,9 @@ define(
["validate", "format", "parse"]
);
mockFormatService.getFormat.andReturn(mockFormat);
mockFormatService.getFormat.and.returnValue(mockFormat);
mockFormat.format.andCallFake(function (value) {
mockFormat.format.and.callFake(function (value) {
return moment.utc(value).format("YYYY-MM-DD HH:mm:ss");
});
@@ -300,7 +300,7 @@ define(
});
it("throws an error for unknown formats", function () {
mockFormatService.getFormat.andReturn(undefined);
mockFormatService.getFormat.and.returnValue(undefined);
expect(function () {
fireWatch("parameters.format", "some-format");
}).toThrow();

View File

@@ -62,7 +62,7 @@ define(
// Expansion is tracked on a timeout, because too
// much expansion can result in an unstable digest.
expect(mockTimeout).toHaveBeenCalled();
mockTimeout.mostRecentCall.args[0]();
mockTimeout.calls.mostRecent().args[0]();
expect(controller.hasBeenExpanded()).toBeTruthy();
controller.trackExpansion();
@@ -77,7 +77,7 @@ define(
),
obj = new TestObject("test-object", mockContext);
mockContext.getPath.andReturn([obj]);
mockContext.getPath.and.returnValue([obj]);
// Verify precondition
expect(controller.isSelected()).toBeFalsy();
@@ -86,7 +86,7 @@ define(
mockScope.domainObject = obj;
// Invoke the watch with the new selection
mockScope.$watch.calls[0].args[1](obj);
mockScope.$watch.calls.all()[0].args[1](obj);
expect(controller.isSelected()).toBeTruthy();
});
@@ -103,9 +103,9 @@ define(
parent = new TestObject("parent", mockParentContext),
child = new TestObject("child", mockChildContext);
mockChildContext.getParent.andReturn(parent);
mockChildContext.getPath.andReturn([parent, child]);
mockParentContext.getPath.andReturn([parent]);
mockChildContext.getParent.and.returnValue(parent);
mockChildContext.getPath.and.returnValue([parent, child]);
mockParentContext.getPath.and.returnValue([parent]);
// Set up such that we are on, but not at the end of, a path
mockScope.ngModel = { selectedObject: child };
@@ -113,13 +113,13 @@ define(
mockScope.toggle = jasmine.createSpyObj("toggle", ["setState"]);
// Invoke the watch with the new selection
mockScope.$watch.calls[0].args[1](child);
mockScope.$watch.calls.all()[0].args[1](child);
// Expansion is tracked on a timeout, because too
// much expansion can result in an unstable digest.
// Trigger that timeout.
expect(mockTimeout).toHaveBeenCalled();
mockTimeout.mostRecentCall.args[0]();
mockTimeout.calls.mostRecent().args[0]();
expect(mockScope.toggle.setState).toHaveBeenCalledWith(true);
expect(controller.hasBeenExpanded()).toBeTruthy();
@@ -139,9 +139,9 @@ define(
parent = new TestObject("parent", mockParentContext),
child = new TestObject("child", mockChildContext);
mockChildContext.getParent.andReturn(parent);
mockChildContext.getPath.andReturn([child, child]);
mockParentContext.getPath.andReturn([parent]);
mockChildContext.getParent.and.returnValue(parent);
mockChildContext.getPath.and.returnValue([child, child]);
mockParentContext.getPath.and.returnValue([parent]);
// Set up such that we are on, but not at the end of, a path
mockScope.ngModel = { selectedObject: child };
@@ -149,7 +149,7 @@ define(
mockScope.toggle = jasmine.createSpyObj("toggle", ["setState"]);
// Invoke the watch with the new selection
mockScope.$watch.calls[0].args[1](child);
mockScope.$watch.calls.all()[0].args[1](child);
// Expansion is tracked on a timeout, because too
// much expansion can result in an unstable digest.
@@ -172,9 +172,9 @@ define(
parent = new TestObject("parent", mockParentContext),
child = new TestObject("child", undefined);
mockChildContext.getParent.andReturn(parent);
mockChildContext.getPath.andReturn([parent, child]);
mockParentContext.getPath.andReturn([parent]);
mockChildContext.getParent.and.returnValue(parent);
mockChildContext.getPath.and.returnValue([parent, child]);
mockParentContext.getPath.and.returnValue([parent]);
// Set up such that we are on, but not at the end of, a path
mockScope.ngModel = { selectedObject: child };
@@ -182,7 +182,7 @@ define(
mockScope.toggle = jasmine.createSpyObj("toggle", ["setState"]);
// Invoke the watch with the new selection
mockScope.$watch.calls[0].args[1](child);
mockScope.$watch.calls.all()[0].args[1](child);
expect(mockScope.toggle.setState).not.toHaveBeenCalled();
expect(controller.hasBeenExpanded()).toBeFalsy();

View File

@@ -35,7 +35,7 @@ define(
beforeEach(function () {
mockScope = jasmine.createSpyObj("$scope", ["$watch"]);
mockTimeout = jasmine.createSpy("$timeout");
mockTimeout.andCallFake(function (cb) {
mockTimeout.and.callFake(function (cb) {
cb();
});
mockScope.ngModel = {};
@@ -60,11 +60,11 @@ define(
{ key: "c", name: "View C" },
{ key: "d", name: "View D" }
];
mockScope.$watch.mostRecentCall.args[1](views);
mockScope.$watch.calls.mostRecent().args[1](views);
mockScope.ngModel.selected = views[1];
// Change the set of applicable views
mockScope.$watch.mostRecentCall.args[1]([
mockScope.$watch.calls.mostRecent().args[1]([
{ key: "a", name: "View A" },
{ key: "b", name: "View B" },
{ key: "x", name: "View X" }
@@ -81,11 +81,11 @@ define(
{ key: "c", name: "View C" },
{ key: "d", name: "View D" }
];
mockScope.$watch.mostRecentCall.args[1](views);
mockScope.$watch.calls.mostRecent().args[1](views);
mockScope.ngModel.selected = views[1];
// Change the set of applicable views
mockScope.$watch.mostRecentCall.args[1]([
mockScope.$watch.calls.mostRecent().args[1]([
{ key: "a", name: "View A" },
{ key: "c", name: "View C" },
{ key: "x", name: "View X" }
@@ -102,7 +102,7 @@ define(
expect(mockTimeout).not.toHaveBeenCalled();
// Invoke the watch for set of views
mockScope.$watch.mostRecentCall.args[1]([]);
mockScope.$watch.calls.mostRecent().args[1]([]);
// Should have run on a timeout
expect(mockTimeout).toHaveBeenCalled();