UNPKG

vue3-dnd

Version:

Drag and Drop for Vue Composition API

145 lines (144 loc) 5.24 kB
import { DragSourceImpl } from "../DragSourceImpl"; import { describe, it, vi, expect } from "vitest"; describe("The Hooks DragSourceImpl", function() { var monitor = {}; var connector = {}; describe("canDrag()", function() { it("returns true by default", function() { var impl = new DragSourceImpl({}, monitor, connector); expect(impl.canDrag()).toEqual(true); }); it("returns the result of canDrag if it is a boolean", function() { var impl = new DragSourceImpl({ type: "box", canDrag: true }, monitor, connector); expect(impl.canDrag()).toEqual(true); impl = new DragSourceImpl({ type: "box", canDrag: false }, monitor, connector); expect(impl.canDrag()).toEqual(false); }); it("will invoke canDrag if it is a function", function() { var impl = new DragSourceImpl({ canDrag: function(m) { expect(m).toEqual(monitor); return true; } }, monitor, connector); expect(impl.canDrag()).toEqual(true); }); }); describe("beginDrag()", function() { it("returns a drag-item even if an item is not defined in the spec", function() { var impl = new DragSourceImpl({}, monitor, connector); expect(impl.beginDrag()).toEqual({}); }); it("returns the dragItem from the spec", function() { var item = {}; var impl = new DragSourceImpl({ type: "test", item: item }, monitor, connector); expect(impl.beginDrag()).toEqual(item); }); it("will return the result of item() if it is defined", function() { var item = {}; var impl = new DragSourceImpl({ type: "test", item: function() { return item; } }, monitor, connector); expect(impl.beginDrag()).toEqual(item); }); it("will return null if begin() returns a nullish value", function() { var impl = new DragSourceImpl({ type: "test", item: function() { return null; } }, monitor, connector); expect(impl.beginDrag()).toEqual(null); impl = new DragSourceImpl({ type: "test", item: function() { return undefined; } }, monitor, connector); expect(impl.beginDrag()).toEqual(null); }); it("will return an empty object if begin return a nullish value and item is nullish", function() { var impl = new DragSourceImpl({ begin: function() { return null; } }, monitor, connector); expect(impl.beginDrag()).toEqual({}); }); }); describe("isDragging()", function() { it("performs an ID check by default", function() { var globalMon = { getSourceId: function() { return "1"; } }; var impl = new DragSourceImpl({ type: "box" }, monitor, connector); expect(impl.isDragging(globalMon, "1")).toBeTruthy(); expect(impl.isDragging(globalMon, "2")).toBeFalsy(); }); it("will invoke isDragging()", function() { var impl = new DragSourceImpl({ type: "box", isDragging: function() { return true; } }, monitor, connector); expect(impl.isDragging(null, "1")).toBeTruthy(); impl = new DragSourceImpl({ type: "box", isDragging: function() { return false; } }, monitor, connector); expect(impl.isDragging(null, "1")).toBeFalsy(); }); }); describe("endDrag()", function() { it("will reconnect by default", function() { var reconnect = vi.fn(); var impl = new DragSourceImpl({ type: "box" }, monitor, { reconnect: reconnect }); impl.endDrag(); expect(reconnect.mock.calls.length).toEqual(1); }); it("will invoke end() in spec before reconnecting ", function() { var reconnect = vi.fn(); var end = vi.fn(); var item = { x: 1 }; var impl = new DragSourceImpl({ type: "box", end: end }, { getItem: function() { return item; } }, { reconnect: reconnect }); impl.endDrag(); expect(reconnect.mock.calls.length).toEqual(1); expect(end.mock.calls.length).toEqual(1); expect(end.mock.calls[0][0]).toEqual(item); }); }); });