From 929e5014089beee9b44a115c85d985ea9e1ec7c3 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Sat, 22 Nov 2014 10:26:44 -0800 Subject: [PATCH] [Representation] Spec for DragGesture Add spec for the drag gesture, as attached to a representation of a domain object. WTD-521. --- .../src/gestures/DragGesture.js | 2 +- .../test/gestures/ContextMenuGestureSpec.js | 1 - .../test/gestures/DragGestureSpec.js | 72 ++++++++++++++++++- 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/platform/representation/src/gestures/DragGesture.js b/platform/representation/src/gestures/DragGesture.js index 9832010cc9..f44f7d20d2 100644 --- a/platform/representation/src/gestures/DragGesture.js +++ b/platform/representation/src/gestures/DragGesture.js @@ -49,7 +49,7 @@ define( return { destroy: function () { // Detach listener - element.attr('draggable', false); + element.removeAttr('draggable'); element.off('dragstart', startDrag); } }; diff --git a/platform/representation/test/gestures/ContextMenuGestureSpec.js b/platform/representation/test/gestures/ContextMenuGestureSpec.js index 1c5b0b3c83..245cb7f47c 100644 --- a/platform/representation/test/gestures/ContextMenuGestureSpec.js +++ b/platform/representation/test/gestures/ContextMenuGestureSpec.js @@ -12,7 +12,6 @@ define( var JQLITE_FUNCTIONS = [ "on", "off", "find", "append", "remove" ], DOMAIN_OBJECT_METHODS = [ "getId", "getModel", "getCapability", "hasCapability", "useCapability" ]; - // ContextMenuGesture($compile, $document, $window, $rootScope, element, domainObject) describe("The 'context menu' gesture", function () { var mockCompile, diff --git a/platform/representation/test/gestures/DragGestureSpec.js b/platform/representation/test/gestures/DragGestureSpec.js index 1affab23d4..9628183158 100644 --- a/platform/representation/test/gestures/DragGestureSpec.js +++ b/platform/representation/test/gestures/DragGestureSpec.js @@ -4,11 +4,77 @@ * DragGestureSpec. Created by vwoeltje on 11/6/14. */ define( - ["../../src/gestures/DragGesture"], - function (DragGesture) { + ["../../src/gestures/DragGesture", "../../src/gestures/GestureConstants"], + function (DragGesture, GestureConstants) { "use strict"; - describe("", function () { + var JQLITE_FUNCTIONS = [ "on", "off", "attr", "removeAttr" ], + LOG_FUNCTIONS = [ "error", "warn", "info", "debug"], + DOMAIN_OBJECT_METHODS = [ "getId", "getModel", "getCapability", "hasCapability", "useCapability"], + TEST_ID = "test-id"; + + + + describe("The drag gesture", function () { + var mockLog, + mockElement, + mockDomainObject, + mockDataTransfer, + gesture, + fireGesture; + + beforeEach(function () { + mockLog = jasmine.createSpyObj("$log", LOG_FUNCTIONS); + mockElement = jasmine.createSpyObj("element", JQLITE_FUNCTIONS); + mockDomainObject = jasmine.createSpyObj("domainObject", DOMAIN_OBJECT_METHODS); + mockDataTransfer = jasmine.createSpyObj("dataTransfer", ["setData"]); + + mockDomainObject.getId.andReturn(TEST_ID); + mockDomainObject.getModel.andReturn({}); + + gesture = new DragGesture(mockLog, mockElement, mockDomainObject); + fireGesture = mockElement.on.mostRecentCall.args[1]; + }); + + it("listens for dragstart on the element", function () { + expect(mockElement.on.mostRecentCall.args[0]).toEqual("dragstart"); + }); + + it("marks an element as draggable", function () { + expect(mockElement.attr).toHaveBeenCalledWith("draggable", "true"); + }); + + it("places data in a dataTransfer object", function () { + fireGesture({ dataTransfer: mockDataTransfer }); + expect(mockDataTransfer.setData).toHaveBeenCalledWith( + GestureConstants.MCT_DRAG_TYPE, + TEST_ID + ); + }); + + it("logs a warning if dataTransfer cannot be set", function () { + // Verify precondition + expect(mockLog.warn).not.toHaveBeenCalled(); + + // Fire the gesture without a dataTransfer field + fireGesture({}); + + // Should have logged a warning + expect(mockLog.warn).toHaveBeenCalled(); + }); + + it("removes draggable attribute and listener when destroyed", function () { + // Verify preconditions + expect(mockElement.removeAttr).not.toHaveBeenCalled(); + expect(mockElement.off).not.toHaveBeenCalled(); + + // Notify the gesture that its scope is being destroyed + gesture.destroy(); + + // Verify that attribute/listener were removed + expect(mockElement.removeAttr).toHaveBeenCalledWith("draggable"); + expect(mockElement.off).toHaveBeenCalledWith("dragstart", fireGesture); + }); }); }