Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot write a unit test to make sure that it is not possible to drag some draggable #3624

Open
Nokel81 opened this issue Mar 27, 2024 · 1 comment

Comments

@Nokel81
Copy link

Nokel81 commented Mar 27, 2024

Describe the bug
I am trying to write a unit test to verify that some useDrag cannot be dragged under some conditions however the way that I have found to test throws an Invariant Violation: Cannot call hover while not dragging. error.

Reproduction

This is how I do the test

const firstItem = discover.getSingleElement("slot-index", "1").discovered; // This item has `canDrag: false`
const secondItem = discover.getSingleElement("hotbar-item-id", "some-id").discovered;

fireEvent.dragStart(firstItem);
fireEvent.dragEnter(secondItem);
fireEvent.dragOver(secondItem);
fireEvent.drop(firstItem);

Expected behavior
Nothing to been thrown but also not calls drop.

@Nokel81
Copy link
Author

Nokel81 commented Mar 27, 2024

FYI I have found a workaround for the time being

import type { Backend, BackendFactory } from "dnd-core";
import { HTML5Backend } from "react-dnd-html5-backend";

const HotbarBackend: BackendFactory = (manager, globalContext, configuration) => {
  const base = HTML5Backend(manager, globalContext, configuration) as Backend & {
    handleTopDrop: (e: DragEvent) => void;
  };
  const internalHandleTopDrop = base.handleTopDrop;

  base.handleTopDrop = (e: DragEvent) => {
    try {
      internalHandleTopDrop(e);
    } catch (error) {
      if (process.env.JEST_WORKER_ID) {
        if (process.env.DEBUG) {
          console.warn(error);
        }
      } else {
        throw error;
      }
    }
  };

  return base;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant