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

@@ -37,7 +37,7 @@ define(
mctInclude;
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);
}
@@ -68,8 +68,8 @@ define(
mockScope = jasmine.createSpyObj('$scope', ['$watch', '$on']);
mockElement = jasmine.createSpyObj('element', ['empty']);
mockChangeTemplate = jasmine.createSpy('changeTemplate');
mockLinker.link.andReturn(mockChangeTemplate);
mockLinker.getPath.andCallFake(function (template) {
mockLinker.link.and.returnValue(mockChangeTemplate);
mockLinker.getPath.and.callFake(function (template) {
return testUrls[template.key];
});
mctInclude = new MCTInclude(testTemplates, mockLinker);

View File

@@ -56,7 +56,7 @@ define(
}
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);
}
@@ -107,7 +107,7 @@ define(
"representer" + name,
["represent", "destroy"]
);
constructor.andReturn(representer);
constructor.and.returnValue(representer);
return constructor;
});
@@ -126,13 +126,13 @@ define(
mockElement = jasmine.createSpyObj("element", JQLITE_FUNCTIONS);
mockDomainObject = jasmine.createSpyObj("domainObject", DOMAIN_OBJECT_METHODS);
mockDomainObject.getModel.andReturn(testModel);
mockLinker.link.andReturn(mockChangeTemplate);
mockLinker.getPath.andCallFake(function (ext) {
mockDomainObject.getModel.and.returnValue(testModel);
mockLinker.link.and.returnValue(mockChangeTemplate);
mockLinker.getPath.and.callFake(function (ext) {
return testUrls[ext.key];
});
mockDomainObject.getCapability.andCallFake(function (c) {
mockDomainObject.getCapability.and.callFake(function (c) {
return c === 'mutation' && mockMutationCapability;
});
@@ -212,7 +212,7 @@ define(
mockScope.domainObject = mockDomainObject;
// Trigger the watch
mockScope.$watch.calls[0].args[1]();
mockScope.$watch.calls.all()[0].args[1]();
expect(mockDomainObject.useCapability)
.toHaveBeenCalledWith("testCapability");
@@ -227,7 +227,7 @@ define(
expect(mockLog.warn).not.toHaveBeenCalled();
// Trigger the watch
mockScope.$watch.calls[0].args[1]();
mockScope.$watch.calls.all()[0].args[1]();
// Should have gotten a warning - that's an unknown key
expect(mockLog.warn).toHaveBeenCalled();
@@ -236,17 +236,17 @@ define(
it("clears out obsolete properties from scope", function () {
mockScope.key = "def";
mockScope.domainObject = mockDomainObject;
mockDomainObject.useCapability.andReturn("some value");
mockDomainObject.useCapability.and.returnValue("some value");
// Trigger the watch
mockScope.$watch.calls[0].args[1]();
mockScope.$watch.calls.all()[0].args[1]();
expect(mockScope.testCapability).toBeDefined();
// Change the view
mockScope.key = "xyz";
// Trigger the watch again; should clear capability from scope
mockScope.$watch.calls[0].args[1]();
mockScope.$watch.calls.all()[0].args[1]();
expect(mockScope.testCapability).toBeUndefined();
});
@@ -268,38 +268,38 @@ define(
DOMAIN_OBJECT_METHODS
);
mockDomainObject.getCapability.andCallFake(function (c) {
mockDomainObject.getCapability.and.callFake(function (c) {
return {
context: mockContext,
mutation: mockMutationCapability
}[c];
});
mockLink.getCapability.andCallFake(function (c) {
mockLink.getCapability.and.callFake(function (c) {
return {
context: mockContext2,
mutation: mockMutationCapability
}[c];
});
mockDomainObject.hasCapability.andCallFake(function (c) {
mockDomainObject.hasCapability.and.callFake(function (c) {
return c === 'context';
});
mockLink.hasCapability.andCallFake(function (c) {
mockLink.hasCapability.and.callFake(function (c) {
return c === 'context';
});
mockLink.getModel.andReturn({});
mockLink.getModel.and.returnValue({});
mockContext.getPath.andReturn([mockDomainObject]);
mockContext2.getPath.andReturn([mockParent, mockLink]);
mockContext.getPath.and.returnValue([mockDomainObject]);
mockContext2.getPath.and.returnValue([mockParent, mockLink]);
mockLink.getId.andReturn('test-id');
mockDomainObject.getId.andReturn('test-id');
mockLink.getId.and.returnValue('test-id');
mockDomainObject.getId.and.returnValue('test-id');
mockParent.getId.andReturn('parent-id');
mockParent.getId.and.returnValue('parent-id');
mockScope.key = "abc";
mockScope.domainObject = mockDomainObject;
mockScope.$watch.calls[0].args[1]();
mockScope.$watch.calls.all()[0].args[1]();
});
it("listens for mutation of that object", function () {
@@ -308,19 +308,19 @@ define(
});
it("detects subsequent changes among linked instances", function () {
var callCount = mockChangeTemplate.calls.length;
var callCount = mockChangeTemplate.calls.count();
mockScope.domainObject = mockLink;
mockScope.$watch.calls[0].args[1]();
mockScope.$watch.calls.all()[0].args[1]();
expect(mockChangeTemplate.calls.length)
expect(mockChangeTemplate.calls.count())
.toEqual(callCount + 1);
});
it("does not trigger excess template changes for same instances", function () {
var callCount = mockChangeTemplate.calls.length;
mockScope.$watch.calls[0].args[1]();
expect(mockChangeTemplate.calls.length).toEqual(callCount);
var callCount = mockChangeTemplate.calls.count();
mockScope.$watch.calls.all()[0].args[1]();
expect(mockChangeTemplate.calls.count()).toEqual(callCount);
});
});

View File

@@ -65,28 +65,28 @@ define(
mockElements = {};
mockContents = {};
mockTemplateRequest.andReturn(mockPromise);
mockCompile.andCallFake(function (toCompile) {
mockTemplateRequest.and.returnValue(mockPromise);
mockCompile.and.callFake(function (toCompile) {
var html = typeof toCompile === 'string' ?
toCompile : toCompile.testHtml;
mockTemplates[html] = jasmine.createSpy('template');
mockElements[html] =
jasmine.createSpyObj('templateEl', JQLITE_METHODS);
mockTemplates[html].andReturn(mockElements[html]);
mockTemplates[html].and.returnValue(mockElements[html]);
return mockTemplates[html];
});
mockSce.trustAsResourceUrl.andCallFake(function (url) {
mockSce.trustAsResourceUrl.and.callFake(function (url) {
return { trusted: url };
});
mockScope.$new.andReturn(mockNewScope);
mockElement.html.andCallFake(function (html) {
mockScope.$new.and.returnValue(mockNewScope);
mockElement.html.and.callFake(function (html) {
mockContents[html] =
jasmine.createSpyObj('contentsEl', JQLITE_METHODS);
mockContents[html].testHtml = html;
});
mockElement.contents.andCallFake(function () {
mockElement.contents.and.callFake(function () {
return mockContents[
mockElement.html.mostRecentCall.args[0]
mockElement.html.calls.mostRecent().args[0]
];
});
@@ -108,7 +108,7 @@ define(
commentElement;
function findCommentElement() {
mockCompile.calls.forEach(function (call) {
mockCompile.calls.all().forEach(function (call) {
var html = call.args[0];
if (html.indexOf("<!--") > -1) {
commentElement = mockElements[html];
@@ -144,7 +144,7 @@ define(
testUrl = linker.getPath(testExt);
testTemplate = "<div>Some template!</div>";
changeTemplate(testExt);
mockPromise.then.mostRecentCall
mockPromise.then.calls.mostRecent()
.args[0](testTemplate);
});
@@ -172,12 +172,12 @@ define(
});
it("clears templates when called with undefined", function () {
expect(mockElement.replaceWith.callCount)
expect(mockElement.replaceWith.calls.count())
.toEqual(1);
changeTemplate(undefined);
expect(mockElement.replaceWith.callCount)
expect(mockElement.replaceWith.calls.count())
.toEqual(2);
expect(mockElement.replaceWith.mostRecentCall.args[0])
expect(mockElement.replaceWith.calls.mostRecent().args[0])
.toEqual(commentElement);
});
@@ -191,14 +191,14 @@ define(
testExtension("some", "bad", "template.html")
);
// Reject the template promise
mockPromise.then.mostRecentCall.args[1]();
mockPromise.then.calls.mostRecent().args[1]();
});
it("removes the element from the DOM", function () {
expect(mockElement.replaceWith.callCount)
expect(mockElement.replaceWith.calls.count())
.toEqual(2);
expect(
mockElement.replaceWith.mostRecentCall.args[0]
mockElement.replaceWith.calls.mostRecent().args[0]
).toEqual(commentElement);
});

View File

@@ -39,7 +39,7 @@ define(
['getPath', 'load']
);
mockTemplateLinker.getPath.andCallFake(function (ext) {
mockTemplateLinker.getPath.and.callFake(function (ext) {
return testPathPrefix + ext.templateUrl;
});

View File

@@ -71,11 +71,11 @@ define(
mockEvent.pageX = 123;
mockEvent.pageY = 321;
mockCompile.andReturn(mockCompiledTemplate);
mockCompiledTemplate.andReturn(mockMenu);
mockDocument.find.andReturn(mockBody);
mockRootScope.$new.andReturn(mockScope);
mockPopupService.display.andReturn(mockPopup);
mockCompile.and.returnValue(mockCompiledTemplate);
mockCompiledTemplate.and.returnValue(mockMenu);
mockDocument.find.and.returnValue(mockBody);
mockRootScope.$new.and.returnValue(mockScope);
mockPopupService.display.and.returnValue(mockPopup);
mockActionContext = {key: 'menu', domainObject: mockDomainObject, event: mockEvent};
@@ -108,8 +108,8 @@ define(
booleans.forEach(function (goLeft) {
booleans.forEach(function (goUp) {
mockPopup.goesLeft.andReturn(goLeft);
mockPopup.goesUp.andReturn(goUp);
mockPopup.goesLeft.and.returnValue(goLeft);
mockPopup.goesUp.and.returnValue(goUp);
action.perform();
expect(!!mockScope.menuClass['go-up'])
.toEqual(goUp);
@@ -128,7 +128,7 @@ define(
expect(mockBody.remove).not.toHaveBeenCalled();
// Find and fire body's mousedown listener
mockBody.on.calls.forEach(function (call) {
mockBody.on.calls.all().forEach(function (call) {
if (call.args[0] === 'mousedown') {
call.args[1]();
}
@@ -152,7 +152,7 @@ define(
expect(mockMenu.remove).not.toHaveBeenCalled();
// Find and fire menu's click listener
mockMenu.on.calls.forEach(function (call) {
mockMenu.on.calls.all().forEach(function (call) {
if (call.args[0] === 'click') {
call.args[1]();
}
@@ -166,7 +166,7 @@ define(
// Show the menu
action.perform();
// Find and fire body's mousedown listener
mockMenu.on.calls.forEach(function (call) {
mockMenu.on.calls.all().forEach(function (call) {
if (call.args[0] === 'mousedown') {
call.args[1](mockEvent);
}
@@ -180,7 +180,7 @@ define(
});
it("keeps a menu when menu is clicked on mobile", function () {
mockAgentService.isMobile.andReturn(true);
mockAgentService.isMobile.and.returnValue(true);
action = new ContextMenuAction(
mockCompile,
mockDocument,
@@ -191,7 +191,7 @@ define(
);
action.perform();
mockMenu.on.calls.forEach(function (call) {
mockMenu.on.calls.all().forEach(function (call) {
if (call.args[0] === 'touchstart') {
call.args[1](mockEvent);
}

View File

@@ -59,15 +59,15 @@ define(
);
mockActionContext = {domainObject: mockDomainObject, event: mockEvent};
mockDomainObject.getCapability.andReturn(mockContextMenuAction);
mockContextMenuAction.perform.andReturn(jasmine.any(Function));
mockAgentService.isMobile.andReturn(false);
mockDomainObject.getCapability.and.returnValue(mockContextMenuAction);
mockContextMenuAction.perform.and.returnValue(jasmine.any(Function));
mockAgentService.isMobile.and.returnValue(false);
gesture = new ContextMenuGesture(mockTimeout, mockAgentService, mockElement, mockDomainObject);
// Capture the contextmenu callback
fireGesture = mockElement.on.mostRecentCall.args[1];
fireGesture = mockElement.on.calls.mostRecent().args[1];
});
it("attaches a callback for context menu events", function () {
@@ -86,7 +86,7 @@ define(
expect(mockElement.off).toHaveBeenCalledWith(
"contextmenu",
//mockElement.on.mostRecentCall.args[1]
//mockElement.on.calls.mostRecent().args[1]
mockDomainObject.calls
);
});
@@ -96,15 +96,15 @@ define(
mockTouchEvent = jasmine.createSpyObj("event", ["preventDefault", "touches"]);
mockTouch = jasmine.createSpyObj("touch", ["length"]);
mockTouch.length = 1;
mockTouchEvent.touches.andReturn(mockTouch);
mockAgentService.isMobile.andReturn(true);
mockTouchEvent.touches.and.returnValue(mockTouch);
mockAgentService.isMobile.and.returnValue(true);
// Then create new (mobile) gesture
gesture = new ContextMenuGesture(mockTimeout, mockAgentService, mockElement, mockDomainObject);
// Set calls for the touchstart and touchend gestures
fireTouchStartGesture = mockElement.on.calls[1].args[1];
fireTouchEndGesture = mockElement.on.mostRecentCall.args[1];
fireTouchStartGesture = mockElement.on.calls.all()[1].args[1];
fireTouchEndGesture = mockElement.on.calls.mostRecent().args[1];
// Fire touchstart and expect touch start to begin
fireTouchStartGesture(mockTouchEvent);
@@ -115,7 +115,7 @@ define(
// Expect timeout to begin and then fireTouchEnd
expect(mockTimeout).toHaveBeenCalled();
mockTimeout.mostRecentCall.args[0]();
mockTimeout.calls.mostRecent().args[0]();
fireTouchEndGesture(mockTouchEvent);
});
});

View File

@@ -51,15 +51,15 @@ define(
mockDomainObject = jasmine.createSpyObj("domainObject", DOMAIN_OBJECT_METHODS);
mockDataTransfer = jasmine.createSpyObj("dataTransfer", ["setData"]);
mockDomainObject.getId.andReturn(TEST_ID);
mockDomainObject.getModel.andReturn({});
mockDomainObject.getId.and.returnValue(TEST_ID);
mockDomainObject.getModel.and.returnValue({});
handlers = {};
gesture = new DragGesture(mockLog, mockDndService, mockElement, mockDomainObject);
// Look up all handlers registered by the gesture
mockElement.on.calls.forEach(function (call) {
mockElement.on.calls.all().forEach(function (call) {
handlers[call.args[0]] = call.args[1];
});
});

View File

@@ -76,21 +76,21 @@ define(
mockAction = jasmine.createSpyObj('action', ['getActions']);
mockCompose = jasmine.createSpyObj('compose', ['perform']);
mockDomainObject.getId.andReturn(TEST_ID);
mockDomainObject.getModel.andReturn(testModel);
mockDomainObject.getCapability.andCallFake(function (c) {
mockDomainObject.getId.and.returnValue(TEST_ID);
mockDomainObject.getModel.and.returnValue(testModel);
mockDomainObject.getCapability.and.callFake(function (c) {
return {
persistence: mockPersistence,
action: mockAction
}[c];
});
mockDomainObject.useCapability.andReturn(true);
mockEvent.dataTransfer.getData.andReturn(DROP_ID);
mockDomainObject.useCapability.and.returnValue(true);
mockEvent.dataTransfer.getData.and.returnValue(DROP_ID);
mockElement[0] = mockUnwrappedElement;
mockElement.scope.andReturn(mockScope);
mockUnwrappedElement.getBoundingClientRect.andReturn(testRect);
mockDndService.getData.andReturn(mockDraggedObject);
mockAction.getActions.andReturn([mockCompose]);
mockElement.scope.and.returnValue(mockScope);
mockUnwrappedElement.getBoundingClientRect.and.returnValue(testRect);
mockDndService.getData.and.returnValue(mockDraggedObject);
mockAction.getActions.and.returnValue([mockCompose]);
gesture = new DropGesture(
mockDndService,
@@ -101,7 +101,7 @@ define(
// Get a reference to all callbacks registered during constructor
callbacks = {};
mockElement.on.calls.forEach(function (call) {
mockElement.on.calls.all().forEach(function (call) {
callbacks[call.args[0]] = call.args[1];
});
});
@@ -132,7 +132,7 @@ define(
it("invokes compose on drop in edit mode", function () {
// Set the mockDomainObject to have the editor capability
mockDomainObject.hasCapability.andReturn(true);
mockDomainObject.hasCapability.and.returnValue(true);
callbacks.dragover(mockEvent);
expect(mockAction.getActions).toHaveBeenCalledWith({
@@ -145,9 +145,9 @@ define(
it("invokes compose on drop in browse mode for folders", function () {
// Set the mockDomainObject to not have the editor capability
mockDomainObject.hasCapability.andReturn(false);
mockDomainObject.hasCapability.and.returnValue(false);
// Set the mockDomainObject to have a type of folder
mockDomainObject.getModel.andReturn({type: 'folder'});
mockDomainObject.getModel.and.returnValue({type: 'folder'});
callbacks.dragover(mockEvent);
expect(mockAction.getActions).toHaveBeenCalledWith({
@@ -160,7 +160,7 @@ define(
it("broadcasts drop position (in edit mode)", function () {
// Set the mockDomainObject to have the editor capability
mockDomainObject.hasCapability.andReturn(true);
mockDomainObject.hasCapability.and.returnValue(true);
testRect.left = 42;
testRect.top = 36;

View File

@@ -47,7 +47,7 @@ define(
GESTURE_KEYS.forEach(function (key) {
mockDestroys[key] = jasmine.createSpy("destroy-" + key);
mockGestures[key] = jasmine.createSpy("gesture-" + key);
mockGestures[key].andReturn({ destroy: mockDestroys[key] });
mockGestures[key].and.returnValue({ destroy: mockDestroys[key] });
mockGestures[key].key = key;
});

View File

@@ -42,7 +42,7 @@ define(
mockElement = { someKey: "some value" };
mockGestureService.attachGestures.andReturn(mockGestureHandle);
mockGestureService.attachGestures.and.returnValue(mockGestureHandle);
representer = new GestureRepresenter(
mockGestureService,