Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions modules/e2e/cypress/e2e/budget-spec.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,11 @@ describe("Budget Tree Demo", () => {

it("updates folder totals when an expense moves between folders", () => {
// Dropping on the middle of a folder row moves the expense into it.
dragAndDrop(cy.get("@item").contains("Internet"), cy.get("@item").contains("Entertainment"));
cy.dragAndDrop(cy.get("@item").contains("Internet"), cy.get("@item").contains("Entertainment"));

cy.contains("[role=treeitem]", "Entertainment").should("contain.text", "$60");
cy.contains("[role=treeitem]", "Utilities").should("contain.text", "$135");
cy.contains("[role=treeitem]", "Housing").should("contain.text", "$1,935");
cy.get("[data-testid=grand-total]").should("have.text", "$3,175");
});
});

function dragAndDrop(src, dst) {
const dataTransfer = new DataTransfer();
src.trigger("dragstart", { dataTransfer });
dst.trigger("dragover", { dataTransfer });
dst.trigger("drop", { dataTransfer });
dst.trigger("dragend", { dataTransfer });
}
15 changes: 5 additions & 10 deletions modules/e2e/cypress/e2e/drag-out-spec.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("Drag Nodes Out Demo", () => {
it("hands the dragged node's data to an external drop target", () => {
// Drag from the node's text, which lives inside the element carrying the
// drag handle, so react-dnd's source listener runs and builds the item.
dragAndDrop(cy.get("@item").contains("report.txt"), cy.get("[data-testid=drop-zone]"));
cy.dragAndDrop(cy.get("@item").contains("report.txt"), cy.get("[data-testid=drop-zone]"));

// The zone read item.data.name off the drag item and listed it.
cy.get("[data-testid=drop-zone]").should("contain.text", "report.txt");
Expand All @@ -26,19 +26,14 @@ describe("Drag Nodes Out Demo", () => {
it("still reorders nodes within the tree", () => {
// Internal drops keep working because the default "NODE" type is unchanged.
cy.contains("[role=treeitem]", "installer.dmg").should("have.css", "top", "128px");
dragAndDrop(cy.get("@item").contains("installer.dmg"), cy.get("@item").contains("report.txt"));
cy.dragAndDrop(
cy.get("@item").contains("installer.dmg"),
cy.get("@item").contains("report.txt"),
);
// installer.dmg moved up near report.txt, so its offset dropped below 128px.
cy.get("@item").should("have.length", 5);
cy.contains("[role=treeitem]", "installer.dmg").then(($el) => {
expect(parseFloat($el.css("top"))).to.be.lessThan(128);
});
});
});

function dragAndDrop(src, dst) {
const dataTransfer = new DataTransfer();
src.trigger("dragstart", { dataTransfer });
dst.trigger("dragover", { dataTransfer });
dst.trigger("drop", { dataTransfer });
dst.trigger("dragend", { dataTransfer });
}
25 changes: 11 additions & 14 deletions modules/e2e/cypress/e2e/gmail-spec.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,30 @@ describe("Testing the Gmail Demo", () => {
});

it("drags and drops in its list", () => {
dragAndDrop(
// A leaf row's center is where a drop flips from above it to below it, so
// aim at the row's bottom edge to drop below it.
cy.dragAndDrop(
cy.get("@item").contains("Inbox").first(),
cy.get("@item").contains("Sent").first(),
cy.contains("[role=treeitem]", "Sent"),
"bottom",
);

cy.get("@item").contains("Inbox").click();
cy.focused().invoke("index").should("eq", 2);
cy.focused().invoke("index").should("eq", 3);
});

it("drags and drops into folder", () => {
dragAndDrop(
cy.dragAndDrop(
cy.get("@item").contains("Starred").first(),
cy.get("@item").contains("Social").first(),
cy.contains("[role=treeitem]", "Social"),
"bottom",
);
cy.get("@item").contains("Starred").click();
cy.focused().invoke("index").should("eq", 11);
cy.focused().invoke("index").should("eq", 12);
});

it("prevents Inbox from Dragging into Categories", () => {
dragAndDrop(
cy.dragAndDrop(
cy.get("@item").contains("Inbox").first(),
cy.get("@item").contains("Social").first(),
);
Expand Down Expand Up @@ -166,10 +170,3 @@ describe("Testing the Gmail Demo", () => {
.should("have.length", TOTAL_ITEMS - 2);
});
});

function dragAndDrop(src, dst) {
const dataTransfer = new DataTransfer();
src.trigger("dragstart", { dataTransfer });
dst.trigger("drop", { dataTransfer });
dst.trigger("dragend", { dataTransfer });
}
10 changes: 1 addition & 9 deletions modules/e2e/cypress/e2e/variable-height-spec.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("Variable Row Height Demo", () => {
it("recomputes offsets after a drag and drop", () => {
cy.contains("[role=treeitem]", "installer.dmg").should("have.css", "top", "256px");

dragAndDrop(
cy.dragAndDrop(
cy.contains("[role=treeitem]", "installer.dmg"),
cy.contains("[role=treeitem]", "report.txt"),
);
Expand All @@ -63,11 +63,3 @@ describe("Variable Row Height Demo", () => {
});
});
});

function dragAndDrop(src, dst) {
const dataTransfer = new DataTransfer();
src.trigger("dragstart", { dataTransfer });
dst.trigger("dragover", { dataTransfer });
dst.trigger("drop", { dataTransfer });
dst.trigger("dragend", { dataTransfer });
}
20 changes: 20 additions & 0 deletions modules/e2e/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,23 @@ Cypress.Commands.add("visitHydrated", (url) => {
cy.visit(url);
cy.get("html[data-hydrated]");
});

// react-dnd applies a dragover on the next animation frame. On drop, it calls
// our hover() before storing the drop's own pointer position, so hover() reads
// the last stored one. Without a frame between dragover and drop, that's the
// dragstart position.
Cypress.Commands.add("nextFrame", () =>
cy
.window()
.then((win) => new Cypress.Promise((resolve) => win.requestAnimationFrame(() => resolve()))),
);

// `position` is where on dst the dragover and drop fire, as a Cypress position name.
Cypress.Commands.add("dragAndDrop", (src, dst, position = "center") => {
const dataTransfer = new DataTransfer();
src.trigger("dragstart", { dataTransfer });
dst.trigger("dragover", position, { dataTransfer });
cy.nextFrame();
dst.trigger("drop", position, { dataTransfer });
dst.trigger("dragend", { dataTransfer });
});
Loading