|
1 | | -import React, { ReactElement } from "react"; |
| 1 | +import React, { ReactElement, ReactNode } from "react"; |
2 | 2 | import cn from "classnames"; |
3 | 3 | import { fireEvent, render } from "@testing-library/react"; |
4 | 4 |
|
5 | 5 | import { DropzoneHanders, useDropzone } from "../useDropzone"; |
6 | 6 |
|
7 | | -function Test(props: DropzoneHanders<HTMLElement>): ReactElement { |
8 | | - const [isOver, handlers] = useDropzone(props); |
| 7 | +function Test({ |
| 8 | + children, |
| 9 | + ...options |
| 10 | +}: DropzoneHanders<HTMLElement> & { children?: ReactNode }): ReactElement { |
| 11 | + const [isOver, handlers] = useDropzone(options); |
9 | 12 |
|
10 | 13 | return ( |
11 | | - <div |
12 | | - data-testid="dropzone" |
13 | | - {...handlers} |
14 | | - className={cn(isOver && "over")} |
15 | | - /> |
| 14 | + <div data-testid="dropzone" {...handlers} className={cn(isOver && "over")}> |
| 15 | + {children} |
| 16 | + </div> |
16 | 17 | ); |
17 | 18 | } |
18 | 19 |
|
@@ -95,4 +96,33 @@ describe("useDropzone", () => { |
95 | 96 | expect(onDragLeave).not.toBeCalled(); |
96 | 97 | expect(onDrop).not.toBeCalled(); |
97 | 98 | }); |
| 99 | + |
| 100 | + it("should not disable the isOver state if a dragleave event is called on a child element", () => { |
| 101 | + const { getByTestId } = render( |
| 102 | + <Test> |
| 103 | + <div data-testid="child1" /> |
| 104 | + <div data-testid="child2" /> |
| 105 | + </Test> |
| 106 | + ); |
| 107 | + |
| 108 | + const dropzone = getByTestId("dropzone"); |
| 109 | + const child1 = getByTestId("child1"); |
| 110 | + const child2 = getByTestId("child2"); |
| 111 | + expect(dropzone).not.toHaveClass("over"); |
| 112 | + |
| 113 | + fireEvent.dragEnter(dropzone); |
| 114 | + expect(dropzone).toHaveClass("over"); |
| 115 | + |
| 116 | + fireEvent.dragOver(child1); |
| 117 | + expect(dropzone).toHaveClass("over"); |
| 118 | + |
| 119 | + fireEvent.dragLeave(child1); |
| 120 | + expect(dropzone).toHaveClass("over"); |
| 121 | + |
| 122 | + fireEvent.dragOver(child2); |
| 123 | + expect(dropzone).toHaveClass("over"); |
| 124 | + |
| 125 | + fireEvent.dragLeave(dropzone); |
| 126 | + expect(dropzone).not.toHaveClass("over"); |
| 127 | + }); |
98 | 128 | }); |
0 commit comments