| 1 |
/* eslint react/prop-types: 0, jsx-a11y/label-has-for: 0 */ |
| 2 |
import React, { createRef } from "react"; |
| 3 |
import { act, cleanup, fireEvent, render } from "@testing-library/react"; |
| 4 |
import { renderHook } from "@testing-library/react-hooks"; |
| 5 |
import { fromEvent } from "file-selector"; |
| 6 |
import * as utils from "./utils"; |
| 7 |
import Dropzone, { useDropzone } from "./index"; |
| 8 |
|
| 9 |
describe("useDropzone() hook", () => { |
| 10 |
let files; |
| 11 |
let images; |
| 12 |
|
| 13 |
beforeEach(() => { |
| 14 |
files = [createFile("file1.pdf", 1111, "application/pdf")]; |
| 15 |
images = [ |
| 16 |
createFile("cats.gif", 1234, "image/gif"), |
| 17 |
createFile("dogs.gif", 2345, "image/jpeg"), |
| 18 |
]; |
| 19 |
}); |
| 20 |
|
| 21 |
afterEach(cleanup); |
| 22 |
|
| 23 |
describe("behavior", () => { |
| 24 |
it("renders the root and input nodes with the necessary props", () => { |
| 25 |
const { container } = render( |
| 26 |
<Dropzone> |
| 27 |
{({ getRootProps, getInputProps }) => ( |
| 28 |
<div {...getRootProps()}> |
| 29 |
<input {...getInputProps()} /> |
| 30 |
</div> |
| 31 |
)} |
| 32 |
</Dropzone> |
| 33 |
); |
| 34 |
expect(container.innerHTML).toMatchSnapshot(); |
| 35 |
}); |
| 36 |
|
| 37 |
it("sets {accept} prop on the <input>", () => { |
| 38 |
const accept = { |
| 39 |
"image/jpeg": [], |
| 40 |
}; |
| 41 |
const { container } = render( |
| 42 |
<Dropzone accept={accept}> |
| 43 |
{({ getRootProps, getInputProps }) => ( |
| 44 |
<div {...getRootProps()}> |
| 45 |
<input {...getInputProps()} /> |
| 46 |
</div> |
| 47 |
)} |
| 48 |
</Dropzone> |
| 49 |
); |
| 50 |
|
| 51 |
expect(container.querySelector("input")).toHaveAttribute( |
| 52 |
"accept", |
| 53 |
"image/jpeg" |
| 54 |
); |
| 55 |
}); |
| 56 |
|
| 57 |
it("updates {multiple} prop on the <input> when it changes", () => { |
| 58 |
const { container, rerender } = render( |
| 59 |
<Dropzone |
| 60 |
accept={{ |
| 61 |
"image/jpeg": [], |
| 62 |
}} |
| 63 |
> |
| 64 |
{({ getRootProps, getInputProps }) => ( |
| 65 |
<div {...getRootProps()}> |
| 66 |
<input {...getInputProps()} /> |
| 67 |
</div> |
| 68 |
)} |
| 69 |
</Dropzone> |
| 70 |
); |
| 71 |
|
| 72 |
expect(container.querySelector("input")).toHaveAttribute( |
| 73 |
"accept", |
| 74 |
"image/jpeg" |
| 75 |
); |
| 76 |
|
| 77 |
rerender( |
| 78 |
<Dropzone |
| 79 |
accept={{ |
| 80 |
"image/png": [], |
| 81 |
}} |
| 82 |
> |
| 83 |
{({ getRootProps, getInputProps }) => ( |
| 84 |
<div {...getRootProps()}> |
| 85 |
<input {...getInputProps()} /> |
| 86 |
</div> |
| 87 |
)} |
| 88 |
</Dropzone> |
| 89 |
); |
| 90 |
|
| 91 |
expect(container.querySelector("input")).toHaveAttribute( |
| 92 |
"accept", |
| 93 |
"image/png" |
| 94 |
); |
| 95 |
}); |
| 96 |
|
| 97 |
it("sets {multiple} prop on the <input>", () => { |
| 98 |
const { container } = render( |
| 99 |
<Dropzone multiple> |
| 100 |
{({ getRootProps, getInputProps }) => ( |
| 101 |
<div {...getRootProps()}> |
| 102 |
<input {...getInputProps()} /> |
| 103 |
</div> |
| 104 |
)} |
| 105 |
</Dropzone> |
| 106 |
); |
| 107 |
|
| 108 |
expect(container.querySelector("input")).toHaveAttribute("multiple"); |
| 109 |
}); |
| 110 |
|
| 111 |
it("updates {multiple} prop on the <input> when it changes", () => { |
| 112 |
const { container, rerender } = render( |
| 113 |
<Dropzone multiple={false}> |
| 114 |
{({ getRootProps, getInputProps }) => ( |
| 115 |
<div {...getRootProps()}> |
| 116 |
<input {...getInputProps()} /> |
| 117 |
</div> |
| 118 |
)} |
| 119 |
</Dropzone> |
| 120 |
); |
| 121 |
|
| 122 |
expect(container.querySelector("input")).not.toHaveAttribute("multiple"); |
| 123 |
|
| 124 |
rerender( |
| 125 |
<Dropzone multiple> |
| 126 |
{({ getRootProps, getInputProps }) => ( |
| 127 |
<div {...getRootProps()}> |
| 128 |
<input {...getInputProps()} /> |
| 129 |
</div> |
| 130 |
)} |
| 131 |
</Dropzone> |
| 132 |
); |
| 133 |
|
| 134 |
expect(container.querySelector("input")).toHaveAttribute("multiple"); |
| 135 |
}); |
| 136 |
|
| 137 |
it("sets any props passed to the input props getter on the <input>", () => { |
| 138 |
const name = "dropzone-input"; |
| 139 |
const { container } = render( |
| 140 |
<Dropzone multiple> |
| 141 |
{({ getRootProps, getInputProps }) => ( |
| 142 |
<div {...getRootProps()}> |
| 143 |
<input {...getInputProps({ name })} /> |
| 144 |
</div> |
| 145 |
)} |
| 146 |
</Dropzone> |
| 147 |
); |
| 148 |
|
| 149 |
expect(container.querySelector("input")).toHaveAttribute("name", name); |
| 150 |
}); |
| 151 |
|
| 152 |
it("sets any props passed to the root props getter on the root node", () => { |
| 153 |
const ariaLabel = "Dropzone area"; |
| 154 |
const { container } = render( |
| 155 |
<Dropzone multiple> |
| 156 |
{({ getRootProps, getInputProps }) => ( |
| 157 |
<div {...getRootProps({ "aria-label": ariaLabel })}> |
| 158 |
<input {...getInputProps()} /> |
| 159 |
</div> |
| 160 |
)} |
| 161 |
</Dropzone> |
| 162 |
); |
| 163 |
|
| 164 |
expect(container.querySelector("div")).toHaveAttribute( |
| 165 |
"aria-label", |
| 166 |
ariaLabel |
| 167 |
); |
| 168 |
}); |
| 169 |
|
| 170 |
it("runs the custom callback handlers provided to the root props getter", async () => { |
| 171 |
const event = createDtWithFiles(files); |
| 172 |
|
| 173 |
const rootProps = { |
| 174 |
onClick: jest.fn(), |
| 175 |
onKeyDown: jest.fn(), |
| 176 |
onFocus: jest.fn(), |
| 177 |
onBlur: jest.fn(), |
| 178 |
onDragEnter: jest.fn(), |
| 179 |
onDragOver: jest.fn(), |
| 180 |
onDragLeave: jest.fn(), |
| 181 |
onDrop: jest.fn(), |
| 182 |
}; |
| 183 |
|
| 184 |
const { container } = render( |
| 185 |
<Dropzone> |
| 186 |
{({ getRootProps, getInputProps }) => ( |
| 187 |
<div {...getRootProps(rootProps)}> |
| 188 |
<input {...getInputProps()} /> |
| 189 |
</div> |
| 190 |
)} |
| 191 |
</Dropzone> |
| 192 |
); |
| 193 |
|
| 194 |
const dropzone = container.querySelector("div"); |
| 195 |
|
| 196 |
fireEvent.click(dropzone); |
| 197 |
expect(rootProps.onClick).toHaveBeenCalled(); |
| 198 |
|
| 199 |
fireEvent.focus(dropzone); |
| 200 |
fireEvent.keyDown(dropzone); |
| 201 |
expect(rootProps.onFocus).toHaveBeenCalled(); |
| 202 |
expect(rootProps.onKeyDown).toHaveBeenCalled(); |
| 203 |
|
| 204 |
fireEvent.blur(dropzone); |
| 205 |
expect(rootProps.onBlur).toHaveBeenCalled(); |
| 206 |
|
| 207 |
await act(() => fireEvent.dragEnter(dropzone, event)); |
| 208 |
expect(rootProps.onDragEnter).toHaveBeenCalled(); |
| 209 |
|
| 210 |
fireEvent.dragOver(dropzone, event); |
| 211 |
expect(rootProps.onDragOver).toHaveBeenCalled(); |
| 212 |
|
| 213 |
fireEvent.dragLeave(dropzone, event); |
| 214 |
expect(rootProps.onDragLeave).toHaveBeenCalled(); |
| 215 |
|
| 216 |
await act(() => fireEvent.drop(dropzone, event)); |
| 217 |
expect(rootProps.onDrop).toHaveBeenCalled(); |
| 218 |
}); |
| 219 |
|
| 220 |
it("runs the custom callback handlers provided to the input props getter", async () => { |
| 221 |
const inputProps = { |
| 222 |
onClick: jest.fn(), |
| 223 |
onChange: jest.fn(), |
| 224 |
}; |
| 225 |
|
| 226 |
const { container } = render( |
| 227 |
<Dropzone> |
| 228 |
{({ getRootProps, getInputProps }) => ( |
| 229 |
<div {...getRootProps()}> |
| 230 |
<input {...getInputProps(inputProps)} /> |
| 231 |
</div> |
| 232 |
)} |
| 233 |
</Dropzone> |
| 234 |
); |
| 235 |
|
| 236 |
const input = container.querySelector("input"); |
| 237 |
|
| 238 |
fireEvent.click(input); |
| 239 |
expect(inputProps.onClick).toHaveBeenCalled(); |
| 240 |
|
| 241 |
await act(async () => fireEvent.change(input, { target: { files: [] } })); |
| 242 |
expect(inputProps.onChange).toHaveBeenCalled(); |
| 243 |
}); |
| 244 |
|
| 245 |
it("runs no callback handlers if {disabled} is true", async () => { |
| 246 |
const event = createDtWithFiles(files); |
| 247 |
|
| 248 |
const rootProps = { |
| 249 |
onClick: jest.fn(), |
| 250 |
onKeyDown: jest.fn(), |
| 251 |
onFocus: jest.fn(), |
| 252 |
onBlur: jest.fn(), |
| 253 |
onDragEnter: jest.fn(), |
| 254 |
onDragOver: jest.fn(), |
| 255 |
onDragLeave: jest.fn(), |
| 256 |
onDrop: jest.fn(), |
| 257 |
}; |
| 258 |
|
| 259 |
const inputProps = { |
| 260 |
onClick: jest.fn(), |
| 261 |
onChange: jest.fn(), |
| 262 |
}; |
| 263 |
|
| 264 |
const { container } = render( |
| 265 |
<Dropzone disabled> |
| 266 |
{({ getRootProps, getInputProps }) => ( |
| 267 |
<div {...getRootProps(rootProps)}> |
| 268 |
<input {...getInputProps(inputProps)} /> |
| 269 |
</div> |
| 270 |
)} |
| 271 |
</Dropzone> |
| 272 |
); |
| 273 |
|
| 274 |
const dropzone = container.querySelector("div"); |
| 275 |
|
| 276 |
fireEvent.click(dropzone); |
| 277 |
expect(rootProps.onClick).not.toHaveBeenCalled(); |
| 278 |
|
| 279 |
fireEvent.focus(dropzone); |
| 280 |
fireEvent.keyDown(dropzone); |
| 281 |
expect(rootProps.onFocus).not.toHaveBeenCalled(); |
| 282 |
expect(rootProps.onKeyDown).not.toHaveBeenCalled(); |
| 283 |
|
| 284 |
fireEvent.blur(dropzone); |
| 285 |
expect(rootProps.onBlur).not.toHaveBeenCalled(); |
| 286 |
|
| 287 |
await act(() => fireEvent.dragEnter(dropzone, event)); |
| 288 |
expect(rootProps.onDragEnter).not.toHaveBeenCalled(); |
| 289 |
|
| 290 |
fireEvent.dragOver(dropzone, event); |
| 291 |
expect(rootProps.onDragOver).not.toHaveBeenCalled(); |
| 292 |
|
| 293 |
fireEvent.dragLeave(dropzone, event); |
| 294 |
expect(rootProps.onDragLeave).not.toHaveBeenCalled(); |
| 295 |
|
| 296 |
await act(() => fireEvent.drop(dropzone, event)); |
| 297 |
expect(rootProps.onDrop).not.toHaveBeenCalled(); |
| 298 |
|
| 299 |
const input = container.querySelector("input"); |
| 300 |
|
| 301 |
fireEvent.click(input); |
| 302 |
expect(inputProps.onClick).not.toHaveBeenCalled(); |
| 303 |
|
| 304 |
await act(() => fireEvent.change(input)); |
| 305 |
expect(inputProps.onChange).not.toHaveBeenCalled(); |
| 306 |
}); |
| 307 |
|
| 308 |
test("{rootRef, inputRef} are exposed", () => { |
| 309 |
const { result } = renderHook(() => useDropzone()); |
| 310 |
const { rootRef, inputRef, getRootProps, getInputProps } = result.current; |
| 311 |
|
| 312 |
const { container } = render( |
| 313 |
<div {...getRootProps()}> |
| 314 |
<input {...getInputProps()} /> |
| 315 |
</div> |
| 316 |
); |
| 317 |
|
| 318 |
expect(container.querySelector("div")).toEqual(rootRef.current); |
| 319 |
expect(container.querySelector("input")).toEqual(inputRef.current); |
| 320 |
}); |
| 321 |
|
| 322 |
test("<Dropzone> exposes and sets the ref if using a ref object", () => { |
| 323 |
const dropzoneRef = createRef(); |
| 324 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 325 |
|
| 326 |
const ui = ( |
| 327 |
<Dropzone ref={dropzoneRef}> |
| 328 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 329 |
<div {...getRootProps()}> |
| 330 |
<input {...getInputProps()} /> |
| 331 |
{isFocused && <div id="focus" />} |
| 332 |
</div> |
| 333 |
)} |
| 334 |
</Dropzone> |
| 335 |
); |
| 336 |
|
| 337 |
const { rerender } = render(ui); |
| 338 |
|
| 339 |
expect(dropzoneRef.current).not.toBeNull(); |
| 340 |
expect(typeof dropzoneRef.current.open).toEqual("function"); |
| 341 |
|
| 342 |
act(() => dropzoneRef.current.open()); |
| 343 |
expect(onClickSpy).toHaveBeenCalled(); |
| 344 |
|
| 345 |
rerender(null); |
| 346 |
|
| 347 |
expect(dropzoneRef.current).toBeNull(); |
| 348 |
}); |
| 349 |
|
| 350 |
test("<Dropzone> exposes and sets the ref if using a ref fn", () => { |
| 351 |
let dropzoneRef; |
| 352 |
const setRef = (ref) => (dropzoneRef = ref); |
| 353 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 354 |
|
| 355 |
const ui = ( |
| 356 |
<Dropzone ref={setRef}> |
| 357 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 358 |
<div {...getRootProps()}> |
| 359 |
<input {...getInputProps()} /> |
| 360 |
{isFocused && <div id="focus" />} |
| 361 |
</div> |
| 362 |
)} |
| 363 |
</Dropzone> |
| 364 |
); |
| 365 |
|
| 366 |
const { rerender } = render(ui); |
| 367 |
|
| 368 |
expect(dropzoneRef).not.toBeNull(); |
| 369 |
expect(typeof dropzoneRef.open).toEqual("function"); |
| 370 |
|
| 371 |
act(() => dropzoneRef.open()); |
| 372 |
expect(onClickSpy).toHaveBeenCalled(); |
| 373 |
|
| 374 |
rerender(null); |
| 375 |
expect(dropzoneRef).toBeNull(); |
| 376 |
}); |
| 377 |
|
| 378 |
test("<Dropzone> doesn't invoke the ref fn if it hasn't changed", () => { |
| 379 |
const setRef = jest.fn(); |
| 380 |
|
| 381 |
const { rerender } = render( |
| 382 |
<Dropzone ref={setRef}> |
| 383 |
{({ getRootProps, getInputProps }) => ( |
| 384 |
<div {...getRootProps()}> |
| 385 |
<input {...getInputProps()} /> |
| 386 |
</div> |
| 387 |
)} |
| 388 |
</Dropzone> |
| 389 |
); |
| 390 |
|
| 391 |
rerender( |
| 392 |
<Dropzone ref={setRef}> |
| 393 |
{({ getRootProps }) => <div {...getRootProps()} />} |
| 394 |
</Dropzone> |
| 395 |
); |
| 396 |
|
| 397 |
expect(setRef).toHaveBeenCalledTimes(1); |
| 398 |
}); |
| 399 |
|
| 400 |
it("sets {isFocused} to false if {disabled} is true", () => { |
| 401 |
const { container, rerender } = render( |
| 402 |
<Dropzone> |
| 403 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 404 |
<div {...getRootProps()}> |
| 405 |
<input {...getInputProps()} /> |
| 406 |
{isFocused && <div id="focus" />} |
| 407 |
</div> |
| 408 |
)} |
| 409 |
</Dropzone> |
| 410 |
); |
| 411 |
|
| 412 |
const dropzone = container.querySelector("div"); |
| 413 |
|
| 414 |
fireEvent.focus(dropzone); |
| 415 |
expect(dropzone.querySelector("#focus")).not.toBeNull(); |
| 416 |
|
| 417 |
rerender( |
| 418 |
<Dropzone disabled> |
| 419 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 420 |
<div {...getRootProps()}> |
| 421 |
<input {...getInputProps()} /> |
| 422 |
{isFocused && <div id="focus" />} |
| 423 |
</div> |
| 424 |
)} |
| 425 |
</Dropzone> |
| 426 |
); |
| 427 |
|
| 428 |
expect(dropzone.querySelector("#focus")).toBeNull(); |
| 429 |
}); |
| 430 |
|
| 431 |
test("{tabindex} is 0 if {disabled} is false", () => { |
| 432 |
const { container } = render( |
| 433 |
<Dropzone> |
| 434 |
{({ getRootProps, getInputProps }) => ( |
| 435 |
<div {...getRootProps()}> |
| 436 |
<input {...getInputProps()} /> |
| 437 |
</div> |
| 438 |
)} |
| 439 |
</Dropzone> |
| 440 |
); |
| 441 |
expect(container.querySelector("div")).toHaveAttribute("tabindex", "0"); |
| 442 |
}); |
| 443 |
|
| 444 |
test("{tabindex} is not set if {disabled} is true", () => { |
| 445 |
const { container, rerender } = render( |
| 446 |
<Dropzone> |
| 447 |
{({ getRootProps, getInputProps }) => ( |
| 448 |
<div {...getRootProps()}> |
| 449 |
<input {...getInputProps()} /> |
| 450 |
</div> |
| 451 |
)} |
| 452 |
</Dropzone> |
| 453 |
); |
| 454 |
|
| 455 |
expect(container.querySelector("div")).toHaveAttribute("tabindex", "0"); |
| 456 |
|
| 457 |
rerender( |
| 458 |
<Dropzone disabled> |
| 459 |
{({ getRootProps, getInputProps }) => ( |
| 460 |
<div {...getRootProps()}> |
| 461 |
<input {...getInputProps()} /> |
| 462 |
</div> |
| 463 |
)} |
| 464 |
</Dropzone> |
| 465 |
); |
| 466 |
|
| 467 |
expect(container.querySelector("div")).not.toHaveAttribute("tabindex"); |
| 468 |
}); |
| 469 |
|
| 470 |
test("{tabindex} is not set if {noKeyboard} is true", () => { |
| 471 |
const { container, rerender } = render( |
| 472 |
<Dropzone> |
| 473 |
{({ getRootProps, getInputProps }) => ( |
| 474 |
<div {...getRootProps()}> |
| 475 |
<input {...getInputProps()} /> |
| 476 |
</div> |
| 477 |
)} |
| 478 |
</Dropzone> |
| 479 |
); |
| 480 |
|
| 481 |
expect(container.querySelector("div")).toHaveAttribute("tabindex", "0"); |
| 482 |
|
| 483 |
rerender( |
| 484 |
<Dropzone noKeyboard> |
| 485 |
{({ getRootProps, getInputProps }) => ( |
| 486 |
<div {...getRootProps()}> |
| 487 |
<input {...getInputProps()} /> |
| 488 |
</div> |
| 489 |
)} |
| 490 |
</Dropzone> |
| 491 |
); |
| 492 |
|
| 493 |
expect(container.querySelector("div")).not.toHaveAttribute("tabindex"); |
| 494 |
}); |
| 495 |
|
| 496 |
test("refs are set when {refKey} is set to a different value", (done) => { |
| 497 |
const data = createDtWithFiles(files); |
| 498 |
|
| 499 |
class MyView extends React.Component { |
| 500 |
render() { |
| 501 |
const { children, innerRef, ...rest } = this.props; |
| 502 |
return ( |
| 503 |
<div id="dropzone" ref={innerRef} {...rest}> |
| 504 |
<div>{children}</div> |
| 505 |
</div> |
| 506 |
); |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
const ui = ( |
| 511 |
<Dropzone> |
| 512 |
{({ getRootProps }) => ( |
| 513 |
<MyView {...getRootProps({ refKey: "innerRef" })}> |
| 514 |
<span>Drop some files here ...</span> |
| 515 |
</MyView> |
| 516 |
)} |
| 517 |
</Dropzone> |
| 518 |
); |
| 519 |
|
| 520 |
const { container, rerender } = render(ui); |
| 521 |
const dropzone = container.querySelector("#dropzone"); |
| 522 |
|
| 523 |
const fn = async () => { |
| 524 |
await act(() => fireEvent.drop(dropzone, data)); |
| 525 |
rerender(ui); |
| 526 |
done(); |
| 527 |
}; |
| 528 |
|
| 529 |
expect(fn).not.toThrow(); |
| 530 |
}); |
| 531 |
|
| 532 |
test("click events originating from <label> should not trigger file dialog open twice", () => { |
| 533 |
const activeRef = createRef(); |
| 534 |
const active = <span ref={activeRef}>I am active</span>; |
| 535 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 536 |
|
| 537 |
const { container } = render( |
| 538 |
<Dropzone> |
| 539 |
{({ getRootProps, getInputProps, isFileDialogActive }) => ( |
| 540 |
<label {...getRootProps()}> |
| 541 |
<input {...getInputProps()} /> |
| 542 |
{isFileDialogActive && active} |
| 543 |
</label> |
| 544 |
)} |
| 545 |
</Dropzone> |
| 546 |
); |
| 547 |
|
| 548 |
const dropzone = container.querySelector("label"); |
| 549 |
|
| 550 |
fireEvent.click(dropzone, { bubbles: true, cancelable: true }); |
| 551 |
|
| 552 |
expect(activeRef.current).not.toBeNull(); |
| 553 |
expect(dropzone).toContainElement(activeRef.current); |
| 554 |
expect(onClickSpy).toHaveBeenCalledTimes(1); |
| 555 |
}); |
| 556 |
}); |
| 557 |
|
| 558 |
describe("document drop protection", () => { |
| 559 |
const addEventListenerSpy = jest.spyOn(document, "addEventListener"); |
| 560 |
const removeEventListenerSpy = jest.spyOn(document, "removeEventListener"); |
| 561 |
// Collect the list of addEventListener/removeEventListener spy calls into an object keyed by event name |
| 562 |
const collectEventListenerCalls = (spy) => |
| 563 |
spy.mock.calls.reduce( |
| 564 |
(acc, [eventName, ...rest]) => ({ |
| 565 |
...acc, |
| 566 |
[eventName]: rest, |
| 567 |
}), |
| 568 |
{} |
| 569 |
); |
| 570 |
|
| 571 |
it("installs hooks to prevent stray drops from taking over the browser window", () => { |
| 572 |
render( |
| 573 |
<Dropzone> |
| 574 |
{({ getRootProps, getInputProps }) => ( |
| 575 |
<div {...getRootProps()}> |
| 576 |
<input {...getInputProps()} /> |
| 577 |
</div> |
| 578 |
)} |
| 579 |
</Dropzone> |
| 580 |
); |
| 581 |
|
| 582 |
expect(addEventListenerSpy).toHaveBeenCalledTimes(6); |
| 583 |
|
| 584 |
const addEventCalls = collectEventListenerCalls(addEventListenerSpy); |
| 585 |
const events = Object.keys(addEventCalls); |
| 586 |
|
| 587 |
expect(events).toContain("dragover"); |
| 588 |
expect(events).toContain("drop"); |
| 589 |
expect(events).toContain("dragenter"); |
| 590 |
expect(events).toContain("dragleave"); |
| 591 |
expect(events).toContain("dragend"); |
| 592 |
|
| 593 |
events.forEach((eventName) => { |
| 594 |
const [fn, options] = addEventCalls[eventName]; |
| 595 |
expect(fn).toBeDefined(); |
| 596 |
expect(options).toBe(false); |
| 597 |
}); |
| 598 |
}); |
| 599 |
|
| 600 |
it("removes document hooks when unmounted", () => { |
| 601 |
const { unmount } = render( |
| 602 |
<Dropzone> |
| 603 |
{({ getRootProps, getInputProps }) => ( |
| 604 |
<div {...getRootProps()}> |
| 605 |
<input {...getInputProps()} /> |
| 606 |
</div> |
| 607 |
)} |
| 608 |
</Dropzone> |
| 609 |
); |
| 610 |
|
| 611 |
unmount(); |
| 612 |
|
| 613 |
expect(removeEventListenerSpy).toHaveBeenCalledTimes(6); |
| 614 |
|
| 615 |
const addEventCalls = collectEventListenerCalls(addEventListenerSpy); |
| 616 |
const removeEventCalls = collectEventListenerCalls( |
| 617 |
removeEventListenerSpy |
| 618 |
); |
| 619 |
const events = Object.keys(removeEventCalls); |
| 620 |
|
| 621 |
expect(events).toContain("dragover"); |
| 622 |
expect(events).toContain("drop"); |
| 623 |
expect(events).toContain("dragenter"); |
| 624 |
expect(events).toContain("dragleave"); |
| 625 |
expect(events).toContain("dragend"); |
| 626 |
|
| 627 |
events.forEach((eventName) => { |
| 628 |
const [a] = addEventCalls[eventName]; |
| 629 |
const [b] = removeEventCalls[eventName]; |
| 630 |
expect(a).toEqual(b); |
| 631 |
}); |
| 632 |
}); |
| 633 |
|
| 634 |
it("terminates drags and drops on elements outside our dropzone", () => { |
| 635 |
render( |
| 636 |
<Dropzone> |
| 637 |
{({ getRootProps, getInputProps }) => ( |
| 638 |
<div {...getRootProps()}> |
| 639 |
<input {...getInputProps()} /> |
| 640 |
</div> |
| 641 |
)} |
| 642 |
</Dropzone> |
| 643 |
); |
| 644 |
|
| 645 |
const dragEvt = new Event("dragover", { bubbles: true }); |
| 646 |
const dragEvtPreventDefaultSpy = jest.spyOn(dragEvt, "preventDefault"); |
| 647 |
fireEvent(document.body, dragEvt); |
| 648 |
expect(dragEvtPreventDefaultSpy).toHaveBeenCalled(); |
| 649 |
|
| 650 |
const dropEvt = new Event("drop", { bubbles: true }); |
| 651 |
const dropEvtPreventDefaultSpy = jest.spyOn(dropEvt, "preventDefault"); |
| 652 |
fireEvent(document.body, dropEvt); |
| 653 |
expect(dropEvtPreventDefaultSpy).toHaveBeenCalled(); |
| 654 |
}); |
| 655 |
|
| 656 |
it("permits drags and drops on elements inside our dropzone", () => { |
| 657 |
const { container } = render( |
| 658 |
<Dropzone> |
| 659 |
{({ getRootProps, getInputProps }) => ( |
| 660 |
<div {...getRootProps()}> |
| 661 |
<input {...getInputProps()} /> |
| 662 |
</div> |
| 663 |
)} |
| 664 |
</Dropzone> |
| 665 |
); |
| 666 |
|
| 667 |
const dropEvt = new Event("drop", { bubbles: true }); |
| 668 |
const dropEvtPreventDefaultSpy = jest.spyOn(dropEvt, "preventDefault"); |
| 669 |
|
| 670 |
fireEvent(container.querySelector("div"), dropEvt); |
| 671 |
// A call is from the onDrop handler for the dropzone, |
| 672 |
// but there should be no more than 1 |
| 673 |
expect(dropEvtPreventDefaultSpy).toHaveBeenCalled(); |
| 674 |
}); |
| 675 |
|
| 676 |
it("does not prevent stray drops when {preventDropOnDocument} is false", () => { |
| 677 |
render( |
| 678 |
<Dropzone preventDropOnDocument={false}> |
| 679 |
{({ getRootProps, getInputProps }) => ( |
| 680 |
<div {...getRootProps()}> |
| 681 |
<input {...getInputProps()} /> |
| 682 |
</div> |
| 683 |
)} |
| 684 |
</Dropzone> |
| 685 |
); |
| 686 |
|
| 687 |
const dropEvt = new Event("drop", { bubbles: true }); |
| 688 |
const dropEvtPreventDefaultSpy = jest.spyOn(dropEvt, "preventDefault"); |
| 689 |
fireEvent(document.body, dropEvt); |
| 690 |
expect(dropEvtPreventDefaultSpy).toHaveBeenCalledTimes(0); |
| 691 |
}); |
| 692 |
}); |
| 693 |
|
| 694 |
describe("event propagation", () => { |
| 695 |
const data = createDtWithFiles(files); |
| 696 |
|
| 697 |
test("drag events propagate from the inner dropzone to parents", async () => { |
| 698 |
const innerProps = { |
| 699 |
onDragEnter: jest.fn(), |
| 700 |
onDragOver: jest.fn(), |
| 701 |
onDragLeave: jest.fn(), |
| 702 |
onDrop: jest.fn(), |
| 703 |
}; |
| 704 |
|
| 705 |
const InnerDropzone = () => ( |
| 706 |
<Dropzone {...innerProps}> |
| 707 |
{({ getRootProps, getInputProps }) => ( |
| 708 |
<div id="inner-dropzone" {...getRootProps()}> |
| 709 |
<input {...getInputProps()} /> |
| 710 |
</div> |
| 711 |
)} |
| 712 |
</Dropzone> |
| 713 |
); |
| 714 |
|
| 715 |
const parentProps = { |
| 716 |
onDragEnter: jest.fn(), |
| 717 |
onDragOver: jest.fn(), |
| 718 |
onDragLeave: jest.fn(), |
| 719 |
onDrop: jest.fn(), |
| 720 |
}; |
| 721 |
|
| 722 |
const { container } = render( |
| 723 |
<Dropzone {...parentProps}> |
| 724 |
{({ getRootProps, getInputProps }) => ( |
| 725 |
<div {...getRootProps()}> |
| 726 |
<input {...getInputProps()} /> |
| 727 |
<InnerDropzone /> |
| 728 |
</div> |
| 729 |
)} |
| 730 |
</Dropzone> |
| 731 |
); |
| 732 |
|
| 733 |
const innerDropzone = container.querySelector("#inner-dropzone"); |
| 734 |
|
| 735 |
await act(() => fireEvent.dragEnter(innerDropzone, data)); |
| 736 |
expect(innerProps.onDragEnter).toHaveBeenCalled(); |
| 737 |
expect(parentProps.onDragEnter).toHaveBeenCalled(); |
| 738 |
|
| 739 |
fireEvent.dragOver(innerDropzone, data); |
| 740 |
expect(innerProps.onDragOver).toHaveBeenCalled(); |
| 741 |
expect(parentProps.onDragOver).toHaveBeenCalled(); |
| 742 |
|
| 743 |
fireEvent.dragLeave(innerDropzone, data); |
| 744 |
expect(innerProps.onDragLeave).toHaveBeenCalled(); |
| 745 |
expect(parentProps.onDragLeave).toHaveBeenCalled(); |
| 746 |
|
| 747 |
await act(() => fireEvent.drop(innerDropzone, data)); |
| 748 |
expect(innerProps.onDrop).toHaveBeenCalled(); |
| 749 |
expect(parentProps.onDrop).toHaveBeenCalled(); |
| 750 |
}); |
| 751 |
|
| 752 |
test("drag events do not propagate from the inner dropzone to parent dropzone if user invoked stopPropagation() on the events", async () => { |
| 753 |
const innerProps = { |
| 754 |
onDragEnter: jest.fn(), |
| 755 |
onDragOver: jest.fn(), |
| 756 |
onDragLeave: jest.fn(), |
| 757 |
onDrop: jest.fn(), |
| 758 |
}; |
| 759 |
|
| 760 |
Object.keys(innerProps).forEach((prop) => |
| 761 |
innerProps[prop].mockImplementation((...args) => { |
| 762 |
const event = prop === "onDrop" ? args.pop() : args.shift(); |
| 763 |
event.stopPropagation(); |
| 764 |
}) |
| 765 |
); |
| 766 |
|
| 767 |
const InnerDropzone = () => ( |
| 768 |
<Dropzone {...innerProps}> |
| 769 |
{({ getRootProps, getInputProps }) => ( |
| 770 |
<div id="inner-dropzone" {...getRootProps()}> |
| 771 |
<input {...getInputProps()} /> |
| 772 |
</div> |
| 773 |
)} |
| 774 |
</Dropzone> |
| 775 |
); |
| 776 |
|
| 777 |
const parentProps = { |
| 778 |
onDragEnter: jest.fn(), |
| 779 |
onDragOver: jest.fn(), |
| 780 |
onDragLeave: jest.fn(), |
| 781 |
onDrop: jest.fn(), |
| 782 |
}; |
| 783 |
|
| 784 |
const { container } = render( |
| 785 |
<Dropzone {...parentProps}> |
| 786 |
{({ getRootProps, getInputProps }) => ( |
| 787 |
<div {...getRootProps()}> |
| 788 |
<input {...getInputProps()} /> |
| 789 |
<InnerDropzone /> |
| 790 |
</div> |
| 791 |
)} |
| 792 |
</Dropzone> |
| 793 |
); |
| 794 |
|
| 795 |
const innerDropzone = container.querySelector("#inner-dropzone"); |
| 796 |
|
| 797 |
await act(() => fireEvent.dragEnter(innerDropzone, data)); |
| 798 |
expect(innerProps.onDragEnter).toHaveBeenCalled(); |
| 799 |
expect(parentProps.onDragEnter).not.toHaveBeenCalled(); |
| 800 |
|
| 801 |
fireEvent.dragOver(innerDropzone, data); |
| 802 |
expect(innerProps.onDragOver).toHaveBeenCalled(); |
| 803 |
expect(parentProps.onDragOver).not.toHaveBeenCalled(); |
| 804 |
|
| 805 |
fireEvent.dragLeave(innerDropzone, data); |
| 806 |
expect(innerProps.onDragLeave).toHaveBeenCalled(); |
| 807 |
expect(parentProps.onDragLeave).not.toHaveBeenCalled(); |
| 808 |
|
| 809 |
await act(() => fireEvent.drop(innerDropzone, data)); |
| 810 |
expect(innerProps.onDrop).toHaveBeenCalled(); |
| 811 |
expect(parentProps.onDrop).not.toHaveBeenCalled(); |
| 812 |
}); |
| 813 |
|
| 814 |
test("drag events do not propagate from the inner dropzone to parent dropzone if {noDragEventsBubbling} is true", async () => { |
| 815 |
const innerProps = { |
| 816 |
onDragEnter: jest.fn(), |
| 817 |
onDragOver: jest.fn(), |
| 818 |
onDragLeave: jest.fn(), |
| 819 |
onDrop: jest.fn(), |
| 820 |
}; |
| 821 |
|
| 822 |
const InnerDropzone = () => ( |
| 823 |
<Dropzone {...innerProps} noDragEventsBubbling> |
| 824 |
{({ getRootProps, getInputProps }) => ( |
| 825 |
<div id="inner-dropzone" {...getRootProps()}> |
| 826 |
<input {...getInputProps()} /> |
| 827 |
</div> |
| 828 |
)} |
| 829 |
</Dropzone> |
| 830 |
); |
| 831 |
|
| 832 |
const parentProps = { |
| 833 |
onDragEnter: jest.fn(), |
| 834 |
onDragOver: jest.fn(), |
| 835 |
onDragLeave: jest.fn(), |
| 836 |
onDrop: jest.fn(), |
| 837 |
}; |
| 838 |
|
| 839 |
const { container } = render( |
| 840 |
<Dropzone {...parentProps}> |
| 841 |
{({ getRootProps, getInputProps }) => ( |
| 842 |
<div id="outer-dropzone" {...getRootProps()}> |
| 843 |
<input {...getInputProps()} /> |
| 844 |
<InnerDropzone /> |
| 845 |
</div> |
| 846 |
)} |
| 847 |
</Dropzone> |
| 848 |
); |
| 849 |
|
| 850 |
const outerDropzone = container.querySelector("#outer-dropzone"); |
| 851 |
const innerDropzone = container.querySelector("#inner-dropzone"); |
| 852 |
|
| 853 |
// Sets drag targets on the outer dropzone |
| 854 |
await act(() => fireEvent.dragEnter(outerDropzone, data)); |
| 855 |
|
| 856 |
await act(() => fireEvent.dragEnter(innerDropzone, data)); |
| 857 |
expect(innerProps.onDragEnter).toHaveBeenCalled(); |
| 858 |
expect(parentProps.onDragEnter).toHaveBeenCalledTimes(1); |
| 859 |
|
| 860 |
fireEvent.dragOver(innerDropzone, data); |
| 861 |
expect(innerProps.onDragOver).toHaveBeenCalled(); |
| 862 |
expect(parentProps.onDragOver).not.toHaveBeenCalled(); |
| 863 |
|
| 864 |
fireEvent.dragLeave(innerDropzone, data); |
| 865 |
expect(innerProps.onDragLeave).toHaveBeenCalled(); |
| 866 |
expect(parentProps.onDragLeave).not.toHaveBeenCalled(); |
| 867 |
|
| 868 |
await act(() => fireEvent.drop(innerDropzone, data)); |
| 869 |
expect(innerProps.onDrop).toHaveBeenCalled(); |
| 870 |
expect(parentProps.onDrop).not.toHaveBeenCalled(); |
| 871 |
}); |
| 872 |
|
| 873 |
test("onDragLeave is not invoked for the parent dropzone if it was invoked for an inner dropzone", async () => { |
| 874 |
const innerDragLeave = jest.fn(); |
| 875 |
const InnerDropzone = () => ( |
| 876 |
<Dropzone onDragLeave={innerDragLeave}> |
| 877 |
{({ getRootProps, getInputProps }) => ( |
| 878 |
<div id="inner-dropzone" {...getRootProps()}> |
| 879 |
<input {...getInputProps()} /> |
| 880 |
</div> |
| 881 |
)} |
| 882 |
</Dropzone> |
| 883 |
); |
| 884 |
|
| 885 |
const parentDragLeave = jest.fn(); |
| 886 |
|
| 887 |
const { container } = render( |
| 888 |
<Dropzone onDragLeave={parentDragLeave}> |
| 889 |
{({ getRootProps, getInputProps }) => ( |
| 890 |
<div id="parent-dropzone" {...getRootProps()}> |
| 891 |
<input {...getInputProps()} /> |
| 892 |
<InnerDropzone /> |
| 893 |
</div> |
| 894 |
)} |
| 895 |
</Dropzone> |
| 896 |
); |
| 897 |
|
| 898 |
const parentDropzone = container.querySelector("#parent-dropzone"); |
| 899 |
|
| 900 |
await act(() => fireEvent.dragEnter(parentDropzone, data)); |
| 901 |
|
| 902 |
const innerDropzone = container.querySelector("#inner-dropzone"); |
| 903 |
await act(() => fireEvent.dragEnter(innerDropzone, data)); |
| 904 |
|
| 905 |
fireEvent.dragLeave(innerDropzone, data); |
| 906 |
expect(innerDragLeave).toHaveBeenCalled(); |
| 907 |
expect(parentDragLeave).not.toHaveBeenCalled(); |
| 908 |
}); |
| 909 |
}); |
| 910 |
|
| 911 |
describe("plugin integration", () => { |
| 912 |
it("uses provided getFilesFromEvent()", async () => { |
| 913 |
const data = createDtWithFiles(files); |
| 914 |
|
| 915 |
const props = { |
| 916 |
getFilesFromEvent: jest |
| 917 |
.fn() |
| 918 |
.mockImplementation((event) => fromEvent(event)), |
| 919 |
onDragEnter: jest.fn(), |
| 920 |
onDragOver: jest.fn(), |
| 921 |
onDragLeave: jest.fn(), |
| 922 |
onDrop: jest.fn(), |
| 923 |
}; |
| 924 |
|
| 925 |
const { container } = render( |
| 926 |
<Dropzone {...props}> |
| 927 |
{({ getRootProps, getInputProps }) => ( |
| 928 |
<div {...getRootProps()}> |
| 929 |
<input {...getInputProps()} /> |
| 930 |
</div> |
| 931 |
)} |
| 932 |
</Dropzone> |
| 933 |
); |
| 934 |
const dropzone = container.querySelector("div"); |
| 935 |
|
| 936 |
await act(() => fireEvent.dragEnter(dropzone, data)); |
| 937 |
expect(props.onDragEnter).toHaveBeenCalled(); |
| 938 |
|
| 939 |
fireEvent.dragOver(dropzone, data); |
| 940 |
expect(props.onDragOver).toHaveBeenCalled(); |
| 941 |
|
| 942 |
fireEvent.dragLeave(dropzone, data); |
| 943 |
expect(props.onDragLeave).toHaveBeenCalled(); |
| 944 |
|
| 945 |
await act(() => fireEvent.drop(dropzone, data)); |
| 946 |
expect(props.onDrop).toHaveBeenCalled(); |
| 947 |
expect(props.getFilesFromEvent).toHaveBeenCalledTimes(2); |
| 948 |
}); |
| 949 |
|
| 950 |
it("calls {onError} when getFilesFromEvent() rejects", async () => { |
| 951 |
const data = createDtWithFiles(files); |
| 952 |
|
| 953 |
const props = { |
| 954 |
getFilesFromEvent: jest |
| 955 |
.fn() |
| 956 |
.mockImplementation(() => Promise.reject("oops :(")), |
| 957 |
onDragEnter: jest.fn(), |
| 958 |
onDrop: jest.fn(), |
| 959 |
onError: jest.fn(), |
| 960 |
}; |
| 961 |
|
| 962 |
const ui = ( |
| 963 |
<Dropzone {...props}> |
| 964 |
{({ getRootProps, getInputProps }) => ( |
| 965 |
<div {...getRootProps()}> |
| 966 |
<input {...getInputProps()} /> |
| 967 |
</div> |
| 968 |
)} |
| 969 |
</Dropzone> |
| 970 |
); |
| 971 |
const { container } = render(ui); |
| 972 |
const dropzone = container.querySelector("div"); |
| 973 |
|
| 974 |
await act(() => fireEvent.dragEnter(dropzone, data)); |
| 975 |
expect(props.onDragEnter).not.toHaveBeenCalled(); |
| 976 |
|
| 977 |
await act(() => fireEvent.drop(dropzone, data)); |
| 978 |
expect(props.onDrop).not.toHaveBeenCalled(); |
| 979 |
|
| 980 |
expect(props.getFilesFromEvent).toHaveBeenCalledTimes(2); |
| 981 |
expect(props.onError).toHaveBeenCalledTimes(2); |
| 982 |
}); |
| 983 |
}); |
| 984 |
|
| 985 |
describe("onFocus", () => { |
| 986 |
it("sets focus state", () => { |
| 987 |
const { container } = render( |
| 988 |
<Dropzone> |
| 989 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 990 |
<div {...getRootProps()}> |
| 991 |
<input {...getInputProps()} /> |
| 992 |
{isFocused && <div id="focus" />} |
| 993 |
</div> |
| 994 |
)} |
| 995 |
</Dropzone> |
| 996 |
); |
| 997 |
|
| 998 |
const dropzone = container.querySelector("div"); |
| 999 |
|
| 1000 |
fireEvent.focus(dropzone); |
| 1001 |
expect(dropzone.querySelector("#focus")).not.toBeNull(); |
| 1002 |
}); |
| 1003 |
|
| 1004 |
it("does not set focus state if user stopped event propagation", () => { |
| 1005 |
const { container } = render( |
| 1006 |
<Dropzone> |
| 1007 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 1008 |
<div |
| 1009 |
{...getRootProps({ onFocus: (event) => event.stopPropagation() })} |
| 1010 |
> |
| 1011 |
<input {...getInputProps()} /> |
| 1012 |
{isFocused && <div id="focus" />} |
| 1013 |
</div> |
| 1014 |
)} |
| 1015 |
</Dropzone> |
| 1016 |
); |
| 1017 |
|
| 1018 |
const dropzone = container.querySelector("div"); |
| 1019 |
|
| 1020 |
fireEvent.focus(dropzone); |
| 1021 |
expect(dropzone.querySelector("#focus")).toBeNull(); |
| 1022 |
}); |
| 1023 |
|
| 1024 |
it("does not set focus state if {noKeyboard} is true", () => { |
| 1025 |
const { container } = render( |
| 1026 |
<Dropzone noKeyboard> |
| 1027 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 1028 |
<div {...getRootProps()}> |
| 1029 |
<input {...getInputProps()} /> |
| 1030 |
{isFocused && <div id="focus" />} |
| 1031 |
</div> |
| 1032 |
)} |
| 1033 |
</Dropzone> |
| 1034 |
); |
| 1035 |
|
| 1036 |
const dropzone = container.querySelector("div"); |
| 1037 |
|
| 1038 |
fireEvent.focus(dropzone); |
| 1039 |
expect(dropzone.querySelector("#focus")).toBeNull(); |
| 1040 |
}); |
| 1041 |
|
| 1042 |
it("restores focus behavior if {noKeyboard} is set back to false", () => { |
| 1043 |
const { container, rerender } = render( |
| 1044 |
<Dropzone noKeyboard> |
| 1045 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 1046 |
<div {...getRootProps()}> |
| 1047 |
<input {...getInputProps()} /> |
| 1048 |
{isFocused && <div id="focus" />} |
| 1049 |
</div> |
| 1050 |
)} |
| 1051 |
</Dropzone> |
| 1052 |
); |
| 1053 |
|
| 1054 |
const dropzone = container.querySelector("div"); |
| 1055 |
|
| 1056 |
fireEvent.focus(dropzone); |
| 1057 |
expect(dropzone.querySelector("#focus")).toBeNull(); |
| 1058 |
|
| 1059 |
rerender( |
| 1060 |
<Dropzone noKeyboard={false}> |
| 1061 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 1062 |
<div {...getRootProps()}> |
| 1063 |
<input {...getInputProps()} /> |
| 1064 |
{isFocused && <div id="focus" />} |
| 1065 |
</div> |
| 1066 |
)} |
| 1067 |
</Dropzone> |
| 1068 |
); |
| 1069 |
|
| 1070 |
fireEvent.focus(dropzone); |
| 1071 |
expect(dropzone.querySelector("#focus")).not.toBeNull(); |
| 1072 |
}); |
| 1073 |
|
| 1074 |
it("{autoFocus} sets the focus state on render", () => { |
| 1075 |
const { container, rerender } = render( |
| 1076 |
<Dropzone> |
| 1077 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 1078 |
<div {...getRootProps()}> |
| 1079 |
<input {...getInputProps()} /> |
| 1080 |
{isFocused && <div id="focus" />} |
| 1081 |
</div> |
| 1082 |
)} |
| 1083 |
</Dropzone> |
| 1084 |
); |
| 1085 |
|
| 1086 |
const dropzone = container.querySelector("div"); |
| 1087 |
|
| 1088 |
expect(dropzone.querySelector("#focus")).toBeNull(); |
| 1089 |
|
| 1090 |
rerender( |
| 1091 |
/* eslint-disable-next-line jsx-a11y/no-autofocus */ |
| 1092 |
<Dropzone autoFocus> |
| 1093 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 1094 |
<div {...getRootProps()}> |
| 1095 |
<input {...getInputProps()} /> |
| 1096 |
{isFocused && <div id="focus" />} |
| 1097 |
</div> |
| 1098 |
)} |
| 1099 |
</Dropzone> |
| 1100 |
); |
| 1101 |
|
| 1102 |
expect(dropzone.querySelector("#focus")).not.toBeNull(); |
| 1103 |
|
| 1104 |
rerender( |
| 1105 |
/* eslint-disable-next-line jsx-a11y/no-autofocus */ |
| 1106 |
<Dropzone autoFocus disabled> |
| 1107 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 1108 |
<div {...getRootProps()}> |
| 1109 |
<input {...getInputProps()} /> |
| 1110 |
{isFocused && <div id="focus" />} |
| 1111 |
</div> |
| 1112 |
)} |
| 1113 |
</Dropzone> |
| 1114 |
); |
| 1115 |
|
| 1116 |
expect(dropzone.querySelector("#focus")).toBeNull(); |
| 1117 |
}); |
| 1118 |
}); |
| 1119 |
|
| 1120 |
describe("onBlur", () => { |
| 1121 |
it("unsets focus state", () => { |
| 1122 |
const { container } = render( |
| 1123 |
<Dropzone> |
| 1124 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 1125 |
<div {...getRootProps()}> |
| 1126 |
<input {...getInputProps()} /> |
| 1127 |
{isFocused && <div id="focus" />} |
| 1128 |
</div> |
| 1129 |
)} |
| 1130 |
</Dropzone> |
| 1131 |
); |
| 1132 |
|
| 1133 |
const dropzone = container.querySelector("div"); |
| 1134 |
|
| 1135 |
fireEvent.focus(dropzone); |
| 1136 |
expect(dropzone.querySelector("#focus")).not.toBeNull(); |
| 1137 |
|
| 1138 |
fireEvent.blur(dropzone); |
| 1139 |
expect(dropzone.querySelector("#focus")).toBeNull(); |
| 1140 |
}); |
| 1141 |
|
| 1142 |
it("does not unset focus state if user stopped event propagation", () => { |
| 1143 |
const { container } = render( |
| 1144 |
<Dropzone> |
| 1145 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 1146 |
<div |
| 1147 |
{...getRootProps({ onBlur: (event) => event.stopPropagation() })} |
| 1148 |
> |
| 1149 |
<input {...getInputProps()} /> |
| 1150 |
{isFocused && <div id="focus" />} |
| 1151 |
</div> |
| 1152 |
)} |
| 1153 |
</Dropzone> |
| 1154 |
); |
| 1155 |
|
| 1156 |
const dropzone = container.querySelector("div"); |
| 1157 |
|
| 1158 |
fireEvent.focus(dropzone); |
| 1159 |
expect(dropzone.querySelector("#focus")).not.toBeNull(); |
| 1160 |
fireEvent.blur(dropzone); |
| 1161 |
expect(dropzone.querySelector("#focus")).not.toBeNull(); |
| 1162 |
}); |
| 1163 |
|
| 1164 |
it("does not unset focus state if {noKeyboard} is true", () => { |
| 1165 |
const { container, rerender } = render( |
| 1166 |
<Dropzone> |
| 1167 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 1168 |
<div {...getRootProps()}> |
| 1169 |
<input {...getInputProps()} /> |
| 1170 |
{isFocused && <div id="focus" />} |
| 1171 |
</div> |
| 1172 |
)} |
| 1173 |
</Dropzone> |
| 1174 |
); |
| 1175 |
|
| 1176 |
const dropzone = container.querySelector("div"); |
| 1177 |
|
| 1178 |
fireEvent.focus(dropzone); |
| 1179 |
expect(dropzone.querySelector("#focus")).not.toBeNull(); |
| 1180 |
|
| 1181 |
rerender( |
| 1182 |
<Dropzone noKeyboard> |
| 1183 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 1184 |
<div {...getRootProps()}> |
| 1185 |
<input {...getInputProps()} /> |
| 1186 |
{isFocused && <div id="focus" />} |
| 1187 |
</div> |
| 1188 |
)} |
| 1189 |
</Dropzone> |
| 1190 |
); |
| 1191 |
|
| 1192 |
fireEvent.blur(dropzone); |
| 1193 |
expect(dropzone.querySelector("#focus")).not.toBeNull(); |
| 1194 |
}); |
| 1195 |
|
| 1196 |
it("restores blur behavior if {noKeyboard} is set back to false", () => { |
| 1197 |
const { container, rerender } = render( |
| 1198 |
<Dropzone> |
| 1199 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 1200 |
<div {...getRootProps()}> |
| 1201 |
<input {...getInputProps()} /> |
| 1202 |
{isFocused && <div id="focus" />} |
| 1203 |
</div> |
| 1204 |
)} |
| 1205 |
</Dropzone> |
| 1206 |
); |
| 1207 |
|
| 1208 |
const dropzone = container.querySelector("div"); |
| 1209 |
|
| 1210 |
fireEvent.focus(dropzone); |
| 1211 |
expect(dropzone.querySelector("#focus")).not.toBeNull(); |
| 1212 |
|
| 1213 |
rerender( |
| 1214 |
<Dropzone noKeyboard> |
| 1215 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 1216 |
<div {...getRootProps()}> |
| 1217 |
<input {...getInputProps()} /> |
| 1218 |
{isFocused && <div id="focus" />} |
| 1219 |
</div> |
| 1220 |
)} |
| 1221 |
</Dropzone> |
| 1222 |
); |
| 1223 |
|
| 1224 |
fireEvent.blur(dropzone); |
| 1225 |
expect(dropzone.querySelector("#focus")).not.toBeNull(); |
| 1226 |
|
| 1227 |
rerender( |
| 1228 |
<Dropzone noKeyboard={false}> |
| 1229 |
{({ getRootProps, getInputProps, isFocused }) => ( |
| 1230 |
<div {...getRootProps()}> |
| 1231 |
<input {...getInputProps()} /> |
| 1232 |
{isFocused && <div id="focus" />} |
| 1233 |
</div> |
| 1234 |
)} |
| 1235 |
</Dropzone> |
| 1236 |
); |
| 1237 |
|
| 1238 |
fireEvent.blur(dropzone); |
| 1239 |
expect(dropzone.querySelector("#focus")).toBeNull(); |
| 1240 |
}); |
| 1241 |
}); |
| 1242 |
|
| 1243 |
describe("onClick", () => { |
| 1244 |
let currentShowOpenFilePicker; |
| 1245 |
|
| 1246 |
beforeEach(() => { |
| 1247 |
currentShowOpenFilePicker = window.showOpenFilePicker; |
| 1248 |
}); |
| 1249 |
|
| 1250 |
afterEach(() => { |
| 1251 |
if (currentShowOpenFilePicker) { |
| 1252 |
window.showOpenFilePicker = currentShowOpenFilePicker; |
| 1253 |
} else { |
| 1254 |
delete window.showOpenFilePicker; |
| 1255 |
} |
| 1256 |
}); |
| 1257 |
|
| 1258 |
it("should proxy the click event to the input", () => { |
| 1259 |
const activeRef = createRef(); |
| 1260 |
const active = <span ref={activeRef}>I am active</span>; |
| 1261 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 1262 |
|
| 1263 |
const { container } = render( |
| 1264 |
<Dropzone> |
| 1265 |
{({ getRootProps, getInputProps, isFileDialogActive }) => ( |
| 1266 |
<div {...getRootProps()}> |
| 1267 |
<input {...getInputProps()} /> |
| 1268 |
{isFileDialogActive && active} |
| 1269 |
</div> |
| 1270 |
)} |
| 1271 |
</Dropzone> |
| 1272 |
); |
| 1273 |
|
| 1274 |
const dropzone = container.querySelector("div"); |
| 1275 |
|
| 1276 |
fireEvent.click(dropzone); |
| 1277 |
expect(activeRef.current).not.toBeNull(); |
| 1278 |
expect(dropzone).toContainElement(activeRef.current); |
| 1279 |
expect(onClickSpy).toHaveBeenCalled(); |
| 1280 |
}); |
| 1281 |
|
| 1282 |
it("should not not proxy the click event to the input if event propagation was stopped", () => { |
| 1283 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 1284 |
const { container } = render( |
| 1285 |
<Dropzone> |
| 1286 |
{({ getRootProps, getInputProps }) => ( |
| 1287 |
<div |
| 1288 |
{...getRootProps({ onClick: (event) => event.stopPropagation() })} |
| 1289 |
> |
| 1290 |
<input {...getInputProps()} /> |
| 1291 |
</div> |
| 1292 |
)} |
| 1293 |
</Dropzone> |
| 1294 |
); |
| 1295 |
|
| 1296 |
fireEvent.click(container.querySelector("div")); |
| 1297 |
expect(onClickSpy).not.toHaveBeenCalled(); |
| 1298 |
}); |
| 1299 |
|
| 1300 |
it("should not not proxy the click event to the input if {noClick} is true", () => { |
| 1301 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 1302 |
const { container } = render( |
| 1303 |
<Dropzone noClick> |
| 1304 |
{({ getRootProps, getInputProps }) => ( |
| 1305 |
<div {...getRootProps()}> |
| 1306 |
<input {...getInputProps()} /> |
| 1307 |
</div> |
| 1308 |
)} |
| 1309 |
</Dropzone> |
| 1310 |
); |
| 1311 |
|
| 1312 |
fireEvent.click(container.querySelector("div")); |
| 1313 |
expect(onClickSpy).not.toHaveBeenCalled(); |
| 1314 |
}); |
| 1315 |
|
| 1316 |
it("restores click behavior if {noClick} is set back to false", () => { |
| 1317 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 1318 |
const { container, rerender } = render( |
| 1319 |
<Dropzone noClick> |
| 1320 |
{({ getRootProps, getInputProps }) => ( |
| 1321 |
<div {...getRootProps()}> |
| 1322 |
<input {...getInputProps()} /> |
| 1323 |
</div> |
| 1324 |
)} |
| 1325 |
</Dropzone> |
| 1326 |
); |
| 1327 |
|
| 1328 |
const dropzone = container.querySelector("div"); |
| 1329 |
|
| 1330 |
fireEvent.click(dropzone); |
| 1331 |
expect(onClickSpy).not.toHaveBeenCalled(); |
| 1332 |
|
| 1333 |
rerender( |
| 1334 |
<Dropzone noClick={false}> |
| 1335 |
{({ getRootProps, getInputProps }) => ( |
| 1336 |
<div {...getRootProps()}> |
| 1337 |
<input {...getInputProps()} /> |
| 1338 |
</div> |
| 1339 |
)} |
| 1340 |
</Dropzone> |
| 1341 |
); |
| 1342 |
|
| 1343 |
fireEvent.click(dropzone); |
| 1344 |
expect(onClickSpy).toHaveBeenCalled(); |
| 1345 |
}); |
| 1346 |
|
| 1347 |
// https://github.com/react-dropzone/react-dropzone/issues/783 |
| 1348 |
it("should continue event propagation if {noClick} is true", () => { |
| 1349 |
const btnClickSpy = jest.fn(); |
| 1350 |
const inputClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 1351 |
const { container } = render( |
| 1352 |
<Dropzone noClick> |
| 1353 |
{({ getRootProps, getInputProps }) => ( |
| 1354 |
<div {...getRootProps()}> |
| 1355 |
<input {...getInputProps()} /> |
| 1356 |
<button onClick={btnClickSpy} /> |
| 1357 |
</div> |
| 1358 |
)} |
| 1359 |
</Dropzone> |
| 1360 |
); |
| 1361 |
|
| 1362 |
fireEvent.click(container.querySelector("div")); |
| 1363 |
expect(inputClickSpy).not.toHaveBeenCalled(); |
| 1364 |
|
| 1365 |
fireEvent.click(container.querySelector("button")); |
| 1366 |
expect(btnClickSpy).toHaveBeenCalled(); |
| 1367 |
}); |
| 1368 |
|
| 1369 |
it("should schedule input click on next tick in Edge", () => { |
| 1370 |
jest.useFakeTimers(); |
| 1371 |
|
| 1372 |
const isIeOrEdgeSpy = jest |
| 1373 |
.spyOn(utils, "isIeOrEdge") |
| 1374 |
.mockReturnValueOnce(true); |
| 1375 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 1376 |
|
| 1377 |
const { container } = render( |
| 1378 |
<Dropzone> |
| 1379 |
{({ getRootProps, getInputProps }) => ( |
| 1380 |
<div {...getRootProps()}> |
| 1381 |
<input {...getInputProps()} /> |
| 1382 |
</div> |
| 1383 |
)} |
| 1384 |
</Dropzone> |
| 1385 |
); |
| 1386 |
|
| 1387 |
fireEvent.click(container.querySelector("div")); |
| 1388 |
drainPendingTimers(); |
| 1389 |
|
| 1390 |
expect(onClickSpy).toHaveBeenCalled(); |
| 1391 |
jest.useRealTimers(); |
| 1392 |
isIeOrEdgeSpy.mockClear(); |
| 1393 |
}); |
| 1394 |
|
| 1395 |
it("should not use showOpenFilePicker() if supported and {useFsAccessApi} is not true", () => { |
| 1396 |
jest.useFakeTimers(); |
| 1397 |
|
| 1398 |
const activeRef = createRef(); |
| 1399 |
const active = <span ref={activeRef}>I am active</span>; |
| 1400 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 1401 |
|
| 1402 |
const showOpenFilePickerMock = jest.fn(); |
| 1403 |
|
| 1404 |
window.showOpenFilePicker = showOpenFilePickerMock; |
| 1405 |
|
| 1406 |
const onDropSpy = jest.fn(); |
| 1407 |
const onFileDialogOpenSpy = jest.fn(); |
| 1408 |
|
| 1409 |
const { container } = render( |
| 1410 |
<Dropzone |
| 1411 |
onDrop={onDropSpy} |
| 1412 |
onFileDialogOpen={onFileDialogOpenSpy} |
| 1413 |
accept={{ |
| 1414 |
"application/pdf": [], |
| 1415 |
}} |
| 1416 |
multiple |
| 1417 |
> |
| 1418 |
{({ getRootProps, getInputProps, isFileDialogActive }) => ( |
| 1419 |
<div {...getRootProps()}> |
| 1420 |
<input {...getInputProps()} /> |
| 1421 |
{isFileDialogActive && active} |
| 1422 |
</div> |
| 1423 |
)} |
| 1424 |
</Dropzone> |
| 1425 |
); |
| 1426 |
|
| 1427 |
const dropzone = container.querySelector("div"); |
| 1428 |
|
| 1429 |
fireEvent.click(dropzone); |
| 1430 |
|
| 1431 |
expect(showOpenFilePickerMock).not.toHaveBeenCalled(); |
| 1432 |
expect(onClickSpy).toHaveBeenCalled(); |
| 1433 |
expect(onFileDialogOpenSpy).toHaveBeenCalled(); |
| 1434 |
expect(activeRef.current).not.toBeNull(); |
| 1435 |
expect(dropzone).toContainElement(activeRef.current); |
| 1436 |
|
| 1437 |
focusWindow(); |
| 1438 |
drainPendingTimers(); |
| 1439 |
|
| 1440 |
expect(activeRef.current).toBeNull(); |
| 1441 |
expect(dropzone).not.toContainElement(activeRef.current); |
| 1442 |
expect(onDropSpy).not.toHaveBeenCalled(); |
| 1443 |
|
| 1444 |
jest.useRealTimers(); |
| 1445 |
}); |
| 1446 |
|
| 1447 |
it("should not use showOpenFilePicker() if supported and {isSecureContext} is not true", () => { |
| 1448 |
jest.useFakeTimers(); |
| 1449 |
|
| 1450 |
const activeRef = createRef(); |
| 1451 |
const active = <span ref={activeRef}>I am active</span>; |
| 1452 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 1453 |
|
| 1454 |
const showOpenFilePickerMock = jest.fn(); |
| 1455 |
|
| 1456 |
window.showOpenFilePicker = showOpenFilePickerMock; |
| 1457 |
window.isSecureContext = false; |
| 1458 |
|
| 1459 |
const onDropSpy = jest.fn(); |
| 1460 |
const onFileDialogOpenSpy = jest.fn(); |
| 1461 |
|
| 1462 |
const { container } = render( |
| 1463 |
<Dropzone |
| 1464 |
onDrop={onDropSpy} |
| 1465 |
onFileDialogOpen={onFileDialogOpenSpy} |
| 1466 |
accept={{ |
| 1467 |
"application/pdf": [], |
| 1468 |
}} |
| 1469 |
multiple |
| 1470 |
> |
| 1471 |
{({ getRootProps, getInputProps, isFileDialogActive }) => ( |
| 1472 |
<div {...getRootProps()}> |
| 1473 |
<input {...getInputProps()} /> |
| 1474 |
{isFileDialogActive && active} |
| 1475 |
</div> |
| 1476 |
)} |
| 1477 |
</Dropzone> |
| 1478 |
); |
| 1479 |
|
| 1480 |
const dropzone = container.querySelector("div"); |
| 1481 |
|
| 1482 |
fireEvent.click(dropzone); |
| 1483 |
|
| 1484 |
expect(showOpenFilePickerMock).not.toHaveBeenCalled(); |
| 1485 |
expect(onClickSpy).toHaveBeenCalled(); |
| 1486 |
expect(onFileDialogOpenSpy).toHaveBeenCalled(); |
| 1487 |
expect(activeRef.current).not.toBeNull(); |
| 1488 |
expect(dropzone).toContainElement(activeRef.current); |
| 1489 |
|
| 1490 |
focusWindow(); |
| 1491 |
drainPendingTimers(); |
| 1492 |
|
| 1493 |
expect(activeRef.current).toBeNull(); |
| 1494 |
expect(dropzone).not.toContainElement(activeRef.current); |
| 1495 |
expect(onDropSpy).not.toHaveBeenCalled(); |
| 1496 |
|
| 1497 |
jest.useRealTimers(); |
| 1498 |
|
| 1499 |
window.isSecureContext = true; |
| 1500 |
}); |
| 1501 |
|
| 1502 |
it("should use showOpenFilePicker() if supported and {useFsAccessApi} is true, and not trigger click on input", async () => { |
| 1503 |
const activeRef = createRef(); |
| 1504 |
const active = <span ref={activeRef}>I am active</span>; |
| 1505 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 1506 |
|
| 1507 |
const handlers = files.map((f) => createFileSystemFileHandle(f)); |
| 1508 |
const thenable = createThenable(); |
| 1509 |
const showOpenFilePickerMock = jest |
| 1510 |
.fn() |
| 1511 |
.mockReturnValue(thenable.promise); |
| 1512 |
|
| 1513 |
window.showOpenFilePicker = showOpenFilePickerMock; |
| 1514 |
|
| 1515 |
const onDropSpy = jest.fn(); |
| 1516 |
const onFileDialogOpenSpy = jest.fn(); |
| 1517 |
|
| 1518 |
const { container } = render( |
| 1519 |
<Dropzone |
| 1520 |
onDrop={onDropSpy} |
| 1521 |
onFileDialogOpen={onFileDialogOpenSpy} |
| 1522 |
accept={{ |
| 1523 |
"application/pdf": [], |
| 1524 |
}} |
| 1525 |
multiple |
| 1526 |
useFsAccessApi |
| 1527 |
> |
| 1528 |
{({ getRootProps, getInputProps, isFileDialogActive }) => ( |
| 1529 |
<div {...getRootProps()}> |
| 1530 |
<input {...getInputProps()} /> |
| 1531 |
{isFileDialogActive && active} |
| 1532 |
</div> |
| 1533 |
)} |
| 1534 |
</Dropzone> |
| 1535 |
); |
| 1536 |
|
| 1537 |
const dropzone = container.querySelector("div"); |
| 1538 |
|
| 1539 |
fireEvent.click(dropzone); |
| 1540 |
|
| 1541 |
expect(showOpenFilePickerMock).toHaveBeenCalledWith({ |
| 1542 |
multiple: true, |
| 1543 |
types: [ |
| 1544 |
{ |
| 1545 |
description: "Files", |
| 1546 |
accept: { "application/pdf": [] }, |
| 1547 |
}, |
| 1548 |
], |
| 1549 |
}); |
| 1550 |
expect(onClickSpy).not.toHaveBeenCalled(); |
| 1551 |
expect(onFileDialogOpenSpy).toHaveBeenCalled(); |
| 1552 |
|
| 1553 |
expect(activeRef.current).not.toBeNull(); |
| 1554 |
expect(dropzone).toContainElement(activeRef.current); |
| 1555 |
|
| 1556 |
await act(() => thenable.done(handlers)); |
| 1557 |
|
| 1558 |
expect(activeRef.current).toBeNull(); |
| 1559 |
expect(dropzone).not.toContainElement(activeRef.current); |
| 1560 |
|
| 1561 |
expect(onDropSpy).toHaveBeenCalledWith(files, [], null); |
| 1562 |
}); |
| 1563 |
|
| 1564 |
test("if showOpenFilePicker() is supported and {useFsAccessApi} is true, it should work without the <input>", async () => { |
| 1565 |
const activeRef = createRef(); |
| 1566 |
const active = <span ref={activeRef}>I am active</span>; |
| 1567 |
|
| 1568 |
const handlers = files.map((f) => createFileSystemFileHandle(f)); |
| 1569 |
const thenable = createThenable(); |
| 1570 |
const showOpenFilePickerMock = jest |
| 1571 |
.fn() |
| 1572 |
.mockReturnValue(thenable.promise); |
| 1573 |
|
| 1574 |
window.showOpenFilePicker = showOpenFilePickerMock; |
| 1575 |
|
| 1576 |
const onDropSpy = jest.fn(); |
| 1577 |
const onFileDialogOpenSpy = jest.fn(); |
| 1578 |
|
| 1579 |
const { container } = render( |
| 1580 |
<Dropzone |
| 1581 |
onDrop={onDropSpy} |
| 1582 |
onFileDialogOpen={onFileDialogOpenSpy} |
| 1583 |
useFsAccessApi |
| 1584 |
> |
| 1585 |
{({ getRootProps, isFileDialogActive }) => ( |
| 1586 |
<div {...getRootProps()}>{isFileDialogActive && active}</div> |
| 1587 |
)} |
| 1588 |
</Dropzone> |
| 1589 |
); |
| 1590 |
|
| 1591 |
const dropzone = container.querySelector("div"); |
| 1592 |
|
| 1593 |
fireEvent.click(dropzone); |
| 1594 |
|
| 1595 |
expect(showOpenFilePickerMock).toHaveBeenCalled(); |
| 1596 |
expect(onFileDialogOpenSpy).toHaveBeenCalled(); |
| 1597 |
|
| 1598 |
await act(() => thenable.done(handlers)); |
| 1599 |
|
| 1600 |
expect(activeRef.current).toBeNull(); |
| 1601 |
expect(dropzone).not.toContainElement(activeRef.current); |
| 1602 |
expect(onDropSpy).toHaveBeenCalledWith(files, [], null); |
| 1603 |
}); |
| 1604 |
|
| 1605 |
test("if showOpenFilePicker() is supported and {useFsAccessApi} is true, and the user cancels it should call onFileDialogCancel", async () => { |
| 1606 |
const activeRef = createRef(); |
| 1607 |
const active = <span ref={activeRef}>I am active</span>; |
| 1608 |
|
| 1609 |
const thenable = createThenable(); |
| 1610 |
const showOpenFilePickerMock = jest |
| 1611 |
.fn() |
| 1612 |
.mockReturnValue(thenable.promise); |
| 1613 |
|
| 1614 |
window.showOpenFilePicker = showOpenFilePickerMock; |
| 1615 |
|
| 1616 |
const onDropSpy = jest.fn(); |
| 1617 |
const onFileDialogCancelSpy = jest.fn(); |
| 1618 |
|
| 1619 |
const { container } = render( |
| 1620 |
<Dropzone |
| 1621 |
onDrop={onDropSpy} |
| 1622 |
onFileDialogCancel={onFileDialogCancelSpy} |
| 1623 |
useFsAccessApi |
| 1624 |
> |
| 1625 |
{({ getRootProps, isFileDialogActive }) => ( |
| 1626 |
<div {...getRootProps()}>{isFileDialogActive && active}</div> |
| 1627 |
)} |
| 1628 |
</Dropzone> |
| 1629 |
); |
| 1630 |
|
| 1631 |
const dropzone = container.querySelector("div"); |
| 1632 |
|
| 1633 |
fireEvent.click(dropzone); |
| 1634 |
|
| 1635 |
expect(showOpenFilePickerMock).toHaveBeenCalled(); |
| 1636 |
|
| 1637 |
await act(() => |
| 1638 |
thenable.cancel(new DOMException("user aborted request", "AbortError")) |
| 1639 |
); |
| 1640 |
|
| 1641 |
expect(activeRef.current).toBeNull(); |
| 1642 |
expect(dropzone).not.toContainElement(activeRef.current); |
| 1643 |
expect(onFileDialogCancelSpy).toHaveBeenCalled(); |
| 1644 |
expect(onDropSpy).not.toHaveBeenCalled(); |
| 1645 |
}); |
| 1646 |
|
| 1647 |
test("window focus evt is not bound if showOpenFilePicker() is supported and {useFsAccessApi} is true", async () => { |
| 1648 |
jest.useFakeTimers(); |
| 1649 |
|
| 1650 |
const activeRef = createRef(); |
| 1651 |
const active = <span ref={activeRef}>I am active</span>; |
| 1652 |
const onFileDialogCancelSpy = jest.fn(); |
| 1653 |
|
| 1654 |
const thenable = createThenable(); |
| 1655 |
const showOpenFilePickerMock = jest |
| 1656 |
.fn() |
| 1657 |
.mockReturnValue(thenable.promise); |
| 1658 |
|
| 1659 |
window.showOpenFilePicker = showOpenFilePickerMock; |
| 1660 |
|
| 1661 |
const { container } = render( |
| 1662 |
<Dropzone onFileDialogCancel={onFileDialogCancelSpy} useFsAccessApi> |
| 1663 |
{({ getRootProps, isFileDialogActive }) => ( |
| 1664 |
<div {...getRootProps()}>{isFileDialogActive && active}</div> |
| 1665 |
)} |
| 1666 |
</Dropzone> |
| 1667 |
); |
| 1668 |
|
| 1669 |
const dropzone = container.querySelector("div"); |
| 1670 |
|
| 1671 |
fireEvent.click(dropzone); |
| 1672 |
|
| 1673 |
expect(activeRef.current).not.toBeNull(); |
| 1674 |
expect(dropzone).toContainElement(activeRef.current); |
| 1675 |
|
| 1676 |
await act(() => |
| 1677 |
thenable.cancel(new DOMException("user aborted request", "AbortError")) |
| 1678 |
); |
| 1679 |
|
| 1680 |
// Try to focus window and run timers |
| 1681 |
focusWindow(); |
| 1682 |
drainPendingTimers(); |
| 1683 |
|
| 1684 |
expect(activeRef.current).toBeNull(); |
| 1685 |
expect(dropzone).not.toContainElement(activeRef.current); |
| 1686 |
expect(onFileDialogCancelSpy).toHaveBeenCalledTimes(1); |
| 1687 |
|
| 1688 |
jest.useRealTimers(); |
| 1689 |
}); |
| 1690 |
|
| 1691 |
it("should try to use showOpenFilePicker() and fallback to input in case of a security error", async () => { |
| 1692 |
const activeRef = createRef(); |
| 1693 |
const active = <span ref={activeRef}>I am active</span>; |
| 1694 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 1695 |
|
| 1696 |
const thenable = createThenable(); |
| 1697 |
const showOpenFilePickerMock = jest |
| 1698 |
.fn() |
| 1699 |
.mockReturnValue(thenable.promise); |
| 1700 |
|
| 1701 |
window.showOpenFilePicker = showOpenFilePickerMock; |
| 1702 |
|
| 1703 |
const onDropSpy = jest.fn(); |
| 1704 |
const onFileDialogOpenSpy = jest.fn(); |
| 1705 |
|
| 1706 |
const { container } = render( |
| 1707 |
<Dropzone |
| 1708 |
onDrop={onDropSpy} |
| 1709 |
onFileDialogOpen={onFileDialogOpenSpy} |
| 1710 |
accept={{ |
| 1711 |
"application/pdf": [], |
| 1712 |
}} |
| 1713 |
multiple |
| 1714 |
useFsAccessApi |
| 1715 |
> |
| 1716 |
{({ getRootProps, getInputProps, isFileDialogActive }) => ( |
| 1717 |
<div {...getRootProps()}> |
| 1718 |
<input {...getInputProps()} /> |
| 1719 |
{isFileDialogActive && active} |
| 1720 |
</div> |
| 1721 |
)} |
| 1722 |
</Dropzone> |
| 1723 |
); |
| 1724 |
|
| 1725 |
const dropzone = container.querySelector("div"); |
| 1726 |
|
| 1727 |
fireEvent.click(dropzone); |
| 1728 |
|
| 1729 |
expect(activeRef.current).not.toBeNull(); |
| 1730 |
expect(dropzone).toContainElement(activeRef.current); |
| 1731 |
expect(onFileDialogOpenSpy).toHaveBeenCalled(); |
| 1732 |
|
| 1733 |
await act(() => |
| 1734 |
thenable.cancel( |
| 1735 |
new DOMException("Cannot use this API cross-origin", "SecurityError") |
| 1736 |
) |
| 1737 |
); |
| 1738 |
|
| 1739 |
expect(activeRef.current).not.toBeNull(); |
| 1740 |
expect(dropzone).toContainElement(activeRef.current); |
| 1741 |
expect(onClickSpy).toHaveBeenCalled(); |
| 1742 |
}); |
| 1743 |
|
| 1744 |
test("window focus evt is bound if showOpenFilePicker() is supported but errors due to a security error", async () => { |
| 1745 |
jest.useFakeTimers(); |
| 1746 |
|
| 1747 |
const activeRef = createRef(); |
| 1748 |
const active = <span ref={activeRef}>I am active</span>; |
| 1749 |
const onFileDialogCancelSpy = jest.fn(); |
| 1750 |
|
| 1751 |
const thenable = createThenable(); |
| 1752 |
const showOpenFilePickerMock = jest |
| 1753 |
.fn() |
| 1754 |
.mockReturnValue(thenable.promise); |
| 1755 |
|
| 1756 |
window.showOpenFilePicker = showOpenFilePickerMock; |
| 1757 |
|
| 1758 |
const { container } = render( |
| 1759 |
<Dropzone onFileDialogCancel={onFileDialogCancelSpy} useFsAccessApi> |
| 1760 |
{({ getRootProps, getInputProps, isFileDialogActive }) => ( |
| 1761 |
<div {...getRootProps()}> |
| 1762 |
<input {...getInputProps()} /> |
| 1763 |
{isFileDialogActive && active} |
| 1764 |
</div> |
| 1765 |
)} |
| 1766 |
</Dropzone> |
| 1767 |
); |
| 1768 |
|
| 1769 |
const dropzone = container.querySelector("div"); |
| 1770 |
|
| 1771 |
fireEvent.click(dropzone); |
| 1772 |
|
| 1773 |
expect(activeRef.current).not.toBeNull(); |
| 1774 |
expect(dropzone).toContainElement(activeRef.current); |
| 1775 |
|
| 1776 |
await act(() => |
| 1777 |
thenable.cancel( |
| 1778 |
new DOMException("Cannot use this API cross-origin", "SecurityError") |
| 1779 |
) |
| 1780 |
); |
| 1781 |
|
| 1782 |
focusWindow(); |
| 1783 |
drainPendingTimers(); |
| 1784 |
|
| 1785 |
expect(onFileDialogCancelSpy).toHaveBeenCalled(); |
| 1786 |
expect(dropzone).not.toContainElement(activeRef.current); |
| 1787 |
|
| 1788 |
jest.useRealTimers(); |
| 1789 |
}); |
| 1790 |
|
| 1791 |
test("showOpenFilePicker() should call {onError} when an unexpected error occurs", async () => { |
| 1792 |
const activeRef = createRef(); |
| 1793 |
const active = <span ref={activeRef}>I am active</span>; |
| 1794 |
|
| 1795 |
const thenable = createThenable(); |
| 1796 |
const showOpenFilePickerMock = jest |
| 1797 |
.fn() |
| 1798 |
.mockReturnValue(thenable.promise); |
| 1799 |
|
| 1800 |
window.showOpenFilePicker = showOpenFilePickerMock; |
| 1801 |
|
| 1802 |
const onErrorSpy = jest.fn(); |
| 1803 |
const onDropSpy = jest.fn(); |
| 1804 |
const onFileDialogOpenSpy = jest.fn(); |
| 1805 |
|
| 1806 |
const ui = ( |
| 1807 |
<Dropzone |
| 1808 |
onError={onErrorSpy} |
| 1809 |
onDrop={onDropSpy} |
| 1810 |
onFileDialogOpen={onFileDialogOpenSpy} |
| 1811 |
accept={{ |
| 1812 |
"application/pdf": [], |
| 1813 |
}} |
| 1814 |
multiple |
| 1815 |
useFsAccessApi |
| 1816 |
> |
| 1817 |
{({ getRootProps, getInputProps, isFileDialogActive }) => ( |
| 1818 |
<div {...getRootProps()}> |
| 1819 |
<input {...getInputProps()} /> |
| 1820 |
{isFileDialogActive && active} |
| 1821 |
</div> |
| 1822 |
)} |
| 1823 |
</Dropzone> |
| 1824 |
); |
| 1825 |
|
| 1826 |
const { container } = render(ui); |
| 1827 |
|
| 1828 |
const dropzone = container.querySelector("div"); |
| 1829 |
|
| 1830 |
fireEvent.click(dropzone); |
| 1831 |
|
| 1832 |
expect(activeRef.current).not.toBeNull(); |
| 1833 |
expect(dropzone).toContainElement(activeRef.current); |
| 1834 |
expect(onFileDialogOpenSpy).toHaveBeenCalled(); |
| 1835 |
|
| 1836 |
const err = new Error("oops :("); |
| 1837 |
await act(() => thenable.cancel(err)); |
| 1838 |
expect(activeRef.current).not.toBeNull(); |
| 1839 |
expect(dropzone).toContainElement(activeRef.current); |
| 1840 |
expect(onErrorSpy).toHaveBeenCalledWith(err); |
| 1841 |
}); |
| 1842 |
|
| 1843 |
test("showOpenFilePicker() should call {onError} when a security error occurs and no <input> was provided", async () => { |
| 1844 |
const activeRef = createRef(); |
| 1845 |
const active = <span ref={activeRef}>I am active</span>; |
| 1846 |
|
| 1847 |
const thenable = createThenable(); |
| 1848 |
const showOpenFilePickerMock = jest |
| 1849 |
.fn() |
| 1850 |
.mockReturnValue(thenable.promise); |
| 1851 |
|
| 1852 |
window.showOpenFilePicker = showOpenFilePickerMock; |
| 1853 |
|
| 1854 |
const onErrorSpy = jest.fn(); |
| 1855 |
const onDropSpy = jest.fn(); |
| 1856 |
const onFileDialogOpenSpy = jest.fn(); |
| 1857 |
|
| 1858 |
const ui = ( |
| 1859 |
<Dropzone |
| 1860 |
onError={onErrorSpy} |
| 1861 |
onDrop={onDropSpy} |
| 1862 |
onFileDialogOpen={onFileDialogOpenSpy} |
| 1863 |
accept={{ |
| 1864 |
"application/pdf": [], |
| 1865 |
}} |
| 1866 |
multiple |
| 1867 |
useFsAccessApi |
| 1868 |
> |
| 1869 |
{({ getRootProps, isFileDialogActive }) => ( |
| 1870 |
<div {...getRootProps()}>{isFileDialogActive && active}</div> |
| 1871 |
)} |
| 1872 |
</Dropzone> |
| 1873 |
); |
| 1874 |
|
| 1875 |
const { container } = render(ui); |
| 1876 |
|
| 1877 |
const dropzone = container.querySelector("div"); |
| 1878 |
|
| 1879 |
fireEvent.click(dropzone); |
| 1880 |
|
| 1881 |
expect(activeRef.current).not.toBeNull(); |
| 1882 |
expect(dropzone).toContainElement(activeRef.current); |
| 1883 |
expect(onFileDialogOpenSpy).toHaveBeenCalled(); |
| 1884 |
|
| 1885 |
const err = new DOMException("oops :(", "SecurityError"); |
| 1886 |
await act(() => thenable.cancel(err)); |
| 1887 |
expect(activeRef.current).not.toBeNull(); |
| 1888 |
expect(dropzone).toContainElement(activeRef.current); |
| 1889 |
expect(onErrorSpy).toHaveBeenCalled(); |
| 1890 |
}); |
| 1891 |
}); |
| 1892 |
|
| 1893 |
describe("onKeyDown", () => { |
| 1894 |
it("triggers the click event on the input if the SPACE/ENTER keys are pressed", () => { |
| 1895 |
const activeRef = createRef(); |
| 1896 |
const active = <span ref={activeRef}>I am active</span>; |
| 1897 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 1898 |
const { container } = render( |
| 1899 |
<Dropzone> |
| 1900 |
{({ getRootProps, getInputProps, isFileDialogActive }) => ( |
| 1901 |
<div {...getRootProps()}> |
| 1902 |
<input {...getInputProps()} /> |
| 1903 |
{isFileDialogActive && active} |
| 1904 |
</div> |
| 1905 |
)} |
| 1906 |
</Dropzone> |
| 1907 |
); |
| 1908 |
|
| 1909 |
const dropzone = container.querySelector("div"); |
| 1910 |
|
| 1911 |
fireEvent.keyDown(dropzone, { |
| 1912 |
keyCode: 32, // Space |
| 1913 |
}); |
| 1914 |
|
| 1915 |
fireEvent.keyDown(dropzone, { |
| 1916 |
keyCode: 13, // Enter |
| 1917 |
}); |
| 1918 |
|
| 1919 |
fireEvent.keyDown(dropzone, { |
| 1920 |
key: " ", // Space |
| 1921 |
}); |
| 1922 |
|
| 1923 |
fireEvent.keyDown(dropzone, { |
| 1924 |
key: "Enter", |
| 1925 |
}); |
| 1926 |
|
| 1927 |
const ref = activeRef.current; |
| 1928 |
expect(ref).not.toBeNull(); |
| 1929 |
expect(dropzone).toContainElement(ref); |
| 1930 |
expect(onClickSpy).toHaveBeenCalledTimes(4); |
| 1931 |
}); |
| 1932 |
|
| 1933 |
it("does not trigger the click event on the input if the dropzone is not in focus", () => { |
| 1934 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 1935 |
const { container } = render( |
| 1936 |
<Dropzone> |
| 1937 |
{({ getRootProps, getInputProps }) => ( |
| 1938 |
<div {...getRootProps()}> |
| 1939 |
<input {...getInputProps()} /> |
| 1940 |
</div> |
| 1941 |
)} |
| 1942 |
</Dropzone> |
| 1943 |
); |
| 1944 |
|
| 1945 |
const input = container.querySelector("input"); |
| 1946 |
|
| 1947 |
fireEvent.keyDown(input, { |
| 1948 |
keyCode: 32, // Space |
| 1949 |
}); |
| 1950 |
|
| 1951 |
fireEvent.keyDown(input, { |
| 1952 |
key: " ", // Space |
| 1953 |
}); |
| 1954 |
|
| 1955 |
expect(onClickSpy).not.toHaveBeenCalled(); |
| 1956 |
}); |
| 1957 |
|
| 1958 |
it("does not trigger the click event on the input if event propagation was stopped", () => { |
| 1959 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 1960 |
const { container } = render( |
| 1961 |
<Dropzone> |
| 1962 |
{({ getRootProps, getInputProps }) => ( |
| 1963 |
<div |
| 1964 |
{...getRootProps({ |
| 1965 |
onKeyDown: (event) => event.stopPropagation(), |
| 1966 |
})} |
| 1967 |
> |
| 1968 |
<input {...getInputProps()} /> |
| 1969 |
</div> |
| 1970 |
)} |
| 1971 |
</Dropzone> |
| 1972 |
); |
| 1973 |
|
| 1974 |
const dropzone = container.querySelector("div"); |
| 1975 |
|
| 1976 |
fireEvent.keyDown(dropzone, { |
| 1977 |
keyCode: 32, // Space |
| 1978 |
}); |
| 1979 |
fireEvent.keyDown(dropzone, { |
| 1980 |
key: " ", // Space |
| 1981 |
}); |
| 1982 |
expect(onClickSpy).not.toHaveBeenCalled(); |
| 1983 |
}); |
| 1984 |
|
| 1985 |
it("does not trigger the click event on the input if {noKeyboard} is true", () => { |
| 1986 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 1987 |
const { container } = render( |
| 1988 |
<Dropzone noKeyboard> |
| 1989 |
{({ getRootProps, getInputProps }) => ( |
| 1990 |
<div {...getRootProps()}> |
| 1991 |
<input {...getInputProps()} /> |
| 1992 |
</div> |
| 1993 |
)} |
| 1994 |
</Dropzone> |
| 1995 |
); |
| 1996 |
|
| 1997 |
const dropzone = container.querySelector("div"); |
| 1998 |
|
| 1999 |
fireEvent.keyDown(dropzone, { |
| 2000 |
keyCode: 32, // Space |
| 2001 |
}); |
| 2002 |
fireEvent.keyDown(dropzone, { |
| 2003 |
key: " ", // Space |
| 2004 |
}); |
| 2005 |
expect(onClickSpy).not.toHaveBeenCalled(); |
| 2006 |
}); |
| 2007 |
|
| 2008 |
it("restores the keydown behavior when {noKeyboard} is set back to false", () => { |
| 2009 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 2010 |
const { container, rerender } = render( |
| 2011 |
<Dropzone noKeyboard> |
| 2012 |
{({ getRootProps, getInputProps }) => ( |
| 2013 |
<div {...getRootProps()}> |
| 2014 |
<input {...getInputProps()} /> |
| 2015 |
</div> |
| 2016 |
)} |
| 2017 |
</Dropzone> |
| 2018 |
); |
| 2019 |
|
| 2020 |
const dropzone = container.querySelector("div"); |
| 2021 |
|
| 2022 |
fireEvent.keyDown(dropzone, { |
| 2023 |
keyCode: 32, // Space |
| 2024 |
}); |
| 2025 |
fireEvent.keyDown(dropzone, { |
| 2026 |
key: " ", // Space |
| 2027 |
}); |
| 2028 |
expect(onClickSpy).not.toHaveBeenCalled(); |
| 2029 |
|
| 2030 |
rerender( |
| 2031 |
<Dropzone noKeyboard={false}> |
| 2032 |
{({ getRootProps, getInputProps }) => ( |
| 2033 |
<div {...getRootProps()}> |
| 2034 |
<input {...getInputProps()} /> |
| 2035 |
</div> |
| 2036 |
)} |
| 2037 |
</Dropzone> |
| 2038 |
); |
| 2039 |
|
| 2040 |
fireEvent.keyDown(dropzone, { |
| 2041 |
keyCode: 32, // Space |
| 2042 |
}); |
| 2043 |
fireEvent.keyDown(dropzone, { |
| 2044 |
key: " ", // Space |
| 2045 |
}); |
| 2046 |
expect(onClickSpy).toHaveBeenCalledTimes(2); |
| 2047 |
}); |
| 2048 |
|
| 2049 |
it("does not trigger the click event on the input for other keys", () => { |
| 2050 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 2051 |
const { container } = render( |
| 2052 |
<Dropzone> |
| 2053 |
{({ getRootProps, getInputProps }) => ( |
| 2054 |
<div {...getRootProps()}> |
| 2055 |
<input {...getInputProps()} /> |
| 2056 |
</div> |
| 2057 |
)} |
| 2058 |
</Dropzone> |
| 2059 |
); |
| 2060 |
|
| 2061 |
const dropzone = container.querySelector("div"); |
| 2062 |
|
| 2063 |
fireEvent.keyDown(dropzone, { |
| 2064 |
keyCode: 97, // Numpad1 |
| 2065 |
}); |
| 2066 |
fireEvent.keyDown(dropzone, { |
| 2067 |
key: "1", |
| 2068 |
}); |
| 2069 |
expect(onClickSpy).not.toHaveBeenCalled(); |
| 2070 |
}); |
| 2071 |
}); |
| 2072 |
|
| 2073 |
describe("onDrag*", () => { |
| 2074 |
it("invokes callbacks for the appropriate events", async () => { |
| 2075 |
const data = createDtWithFiles(files); |
| 2076 |
|
| 2077 |
const props = { |
| 2078 |
onDragEnter: jest.fn(), |
| 2079 |
onDragOver: jest.fn(), |
| 2080 |
onDragLeave: jest.fn(), |
| 2081 |
onDrop: jest.fn(), |
| 2082 |
}; |
| 2083 |
|
| 2084 |
const { container } = render( |
| 2085 |
<Dropzone {...props}> |
| 2086 |
{({ getRootProps, getInputProps }) => ( |
| 2087 |
<div {...getRootProps()}> |
| 2088 |
<input {...getInputProps()} /> |
| 2089 |
</div> |
| 2090 |
)} |
| 2091 |
</Dropzone> |
| 2092 |
); |
| 2093 |
const dropzone = container.querySelector("div"); |
| 2094 |
|
| 2095 |
await act(() => fireEvent.dragEnter(dropzone, data)); |
| 2096 |
expect(props.onDragEnter).toHaveBeenCalled(); |
| 2097 |
|
| 2098 |
fireEvent.dragOver(dropzone, data); |
| 2099 |
expect(props.onDragOver).toHaveBeenCalled(); |
| 2100 |
|
| 2101 |
fireEvent.dragLeave(dropzone, data); |
| 2102 |
expect(props.onDragLeave).toHaveBeenCalled(); |
| 2103 |
|
| 2104 |
await act(() => fireEvent.drop(dropzone, data)); |
| 2105 |
expect(props.onDrop).toHaveBeenCalled(); |
| 2106 |
}); |
| 2107 |
|
| 2108 |
it("invokes callbacks for the appropriate events even if it cannot access the data", async () => { |
| 2109 |
const emptyData = createDtWithFiles([]); |
| 2110 |
|
| 2111 |
const props = { |
| 2112 |
onDragEnter: jest.fn(), |
| 2113 |
onDragOver: jest.fn(), |
| 2114 |
onDragLeave: jest.fn(), |
| 2115 |
onDrop: jest.fn(), |
| 2116 |
onDropAccepted: jest.fn(), |
| 2117 |
onDropRejected: jest.fn(), |
| 2118 |
}; |
| 2119 |
|
| 2120 |
const { container } = render( |
| 2121 |
<Dropzone {...props}> |
| 2122 |
{({ getRootProps, getInputProps }) => ( |
| 2123 |
<div {...getRootProps()}> |
| 2124 |
<input {...getInputProps()} /> |
| 2125 |
</div> |
| 2126 |
)} |
| 2127 |
</Dropzone> |
| 2128 |
); |
| 2129 |
const dropzone = container.querySelector("div"); |
| 2130 |
|
| 2131 |
await act(() => fireEvent.dragEnter(dropzone, emptyData)); |
| 2132 |
expect(props.onDragEnter).toHaveBeenCalled(); |
| 2133 |
|
| 2134 |
fireEvent.dragOver(dropzone, emptyData); |
| 2135 |
expect(props.onDragOver).toHaveBeenCalled(); |
| 2136 |
|
| 2137 |
fireEvent.dragLeave(dropzone, emptyData); |
| 2138 |
expect(props.onDragLeave).toHaveBeenCalled(); |
| 2139 |
|
| 2140 |
const data = createDtWithFiles(files); |
| 2141 |
await act(() => fireEvent.drop(dropzone, data)); |
| 2142 |
expect(props.onDrop).toHaveBeenCalled(); |
| 2143 |
expect(props.onDropAccepted).toHaveBeenCalledWith( |
| 2144 |
files, |
| 2145 |
expect.any(Object) |
| 2146 |
); |
| 2147 |
expect(props.onDropRejected).not.toHaveBeenCalled(); |
| 2148 |
}); |
| 2149 |
|
| 2150 |
it("does not invoke callbacks if no files are detected", async () => { |
| 2151 |
const data = { |
| 2152 |
dataTransfer: { |
| 2153 |
items: [], |
| 2154 |
types: ["text/html", "text/plain"], |
| 2155 |
}, |
| 2156 |
}; |
| 2157 |
|
| 2158 |
const props = { |
| 2159 |
onDragEnter: jest.fn(), |
| 2160 |
onDragOver: jest.fn(), |
| 2161 |
onDragLeave: jest.fn(), |
| 2162 |
onDrop: jest.fn(), |
| 2163 |
onDropAccepted: jest.fn(), |
| 2164 |
onDropRejected: jest.fn(), |
| 2165 |
}; |
| 2166 |
|
| 2167 |
const { container } = render( |
| 2168 |
<Dropzone {...props}> |
| 2169 |
{({ getRootProps, getInputProps }) => ( |
| 2170 |
<div {...getRootProps()}> |
| 2171 |
<input {...getInputProps()} /> |
| 2172 |
</div> |
| 2173 |
)} |
| 2174 |
</Dropzone> |
| 2175 |
); |
| 2176 |
const dropzone = container.querySelector("div"); |
| 2177 |
|
| 2178 |
await act(() => fireEvent.dragEnter(dropzone, data)); |
| 2179 |
expect(props.onDragEnter).not.toHaveBeenCalled(); |
| 2180 |
|
| 2181 |
fireEvent.dragOver(dropzone, data); |
| 2182 |
expect(props.onDragOver).not.toHaveBeenCalled(); |
| 2183 |
|
| 2184 |
fireEvent.dragLeave(dropzone, data); |
| 2185 |
expect(props.onDragLeave).not.toHaveBeenCalled(); |
| 2186 |
|
| 2187 |
await act(() => fireEvent.drop(dropzone, data)); |
| 2188 |
expect(props.onDrop).not.toHaveBeenCalled(); |
| 2189 |
expect(props.onDropAccepted).not.toHaveBeenCalled(); |
| 2190 |
expect(props.onDropRejected).not.toHaveBeenCalled(); |
| 2191 |
}); |
| 2192 |
|
| 2193 |
it("does not invoke callbacks if {noDrag} is true", async () => { |
| 2194 |
const data = createDtWithFiles(files); |
| 2195 |
|
| 2196 |
const props = { |
| 2197 |
onDragEnter: jest.fn(), |
| 2198 |
onDragOver: jest.fn(), |
| 2199 |
onDragLeave: jest.fn(), |
| 2200 |
onDrop: jest.fn(), |
| 2201 |
onDropAccepted: jest.fn(), |
| 2202 |
onDropRejected: jest.fn(), |
| 2203 |
}; |
| 2204 |
|
| 2205 |
const { container } = render( |
| 2206 |
<Dropzone {...props} noDrag> |
| 2207 |
{({ getRootProps, getInputProps }) => ( |
| 2208 |
<div {...getRootProps()}> |
| 2209 |
<input {...getInputProps()} /> |
| 2210 |
</div> |
| 2211 |
)} |
| 2212 |
</Dropzone> |
| 2213 |
); |
| 2214 |
const dropzone = container.querySelector("div"); |
| 2215 |
|
| 2216 |
await act(() => fireEvent.dragEnter(dropzone, data)); |
| 2217 |
expect(props.onDragEnter).not.toHaveBeenCalled(); |
| 2218 |
|
| 2219 |
fireEvent.dragOver(dropzone, data); |
| 2220 |
expect(props.onDragOver).not.toHaveBeenCalled(); |
| 2221 |
|
| 2222 |
fireEvent.dragLeave(dropzone, data); |
| 2223 |
expect(props.onDragLeave).not.toHaveBeenCalled(); |
| 2224 |
|
| 2225 |
await act(() => fireEvent.drop(dropzone, data)); |
| 2226 |
expect(props.onDrop).not.toHaveBeenCalled(); |
| 2227 |
expect(props.onDropAccepted).not.toHaveBeenCalled(); |
| 2228 |
expect(props.onDropRejected).not.toHaveBeenCalled(); |
| 2229 |
}); |
| 2230 |
|
| 2231 |
it("restores drag behavior if {noDrag} is set back to false", async () => { |
| 2232 |
const data = createDtWithFiles(files); |
| 2233 |
|
| 2234 |
const props = { |
| 2235 |
onDragEnter: jest.fn(), |
| 2236 |
onDragOver: jest.fn(), |
| 2237 |
onDragLeave: jest.fn(), |
| 2238 |
onDrop: jest.fn(), |
| 2239 |
}; |
| 2240 |
|
| 2241 |
const { container, rerender } = render( |
| 2242 |
<Dropzone {...props} noDrag> |
| 2243 |
{({ getRootProps, getInputProps }) => ( |
| 2244 |
<div {...getRootProps()}> |
| 2245 |
<input {...getInputProps()} /> |
| 2246 |
</div> |
| 2247 |
)} |
| 2248 |
</Dropzone> |
| 2249 |
); |
| 2250 |
const dropzone = container.querySelector("div"); |
| 2251 |
|
| 2252 |
await act(() => fireEvent.dragEnter(dropzone, data)); |
| 2253 |
expect(props.onDragEnter).not.toHaveBeenCalled(); |
| 2254 |
|
| 2255 |
fireEvent.dragOver(dropzone, data); |
| 2256 |
expect(props.onDragOver).not.toHaveBeenCalled(); |
| 2257 |
|
| 2258 |
fireEvent.dragLeave(dropzone, data); |
| 2259 |
expect(props.onDragLeave).not.toHaveBeenCalled(); |
| 2260 |
|
| 2261 |
await act(() => fireEvent.drop(dropzone, data)); |
| 2262 |
expect(props.onDrop).not.toHaveBeenCalled(); |
| 2263 |
|
| 2264 |
rerender( |
| 2265 |
<Dropzone {...props} noDrag={false}> |
| 2266 |
{({ getRootProps, getInputProps }) => ( |
| 2267 |
<div {...getRootProps()}> |
| 2268 |
<input {...getInputProps()} /> |
| 2269 |
</div> |
| 2270 |
)} |
| 2271 |
</Dropzone> |
| 2272 |
); |
| 2273 |
|
| 2274 |
await act(() => fireEvent.dragEnter(dropzone, data)); |
| 2275 |
expect(props.onDragEnter).toHaveBeenCalled(); |
| 2276 |
|
| 2277 |
fireEvent.dragOver(dropzone, data); |
| 2278 |
expect(props.onDragOver).toHaveBeenCalled(); |
| 2279 |
|
| 2280 |
fireEvent.dragLeave(dropzone, data); |
| 2281 |
expect(props.onDragLeave).toHaveBeenCalled(); |
| 2282 |
|
| 2283 |
await act(() => fireEvent.drop(dropzone, data)); |
| 2284 |
expect(props.onDrop).toHaveBeenCalled(); |
| 2285 |
}); |
| 2286 |
|
| 2287 |
it("sets {isDragActive} and {isDragAccept} if some files are accepted on dragenter", async () => { |
| 2288 |
const { container } = render( |
| 2289 |
<Dropzone> |
| 2290 |
{({ |
| 2291 |
getRootProps, |
| 2292 |
getInputProps, |
| 2293 |
isDragActive, |
| 2294 |
isDragAccept, |
| 2295 |
isDragReject, |
| 2296 |
}) => ( |
| 2297 |
<div {...getRootProps()}> |
| 2298 |
<input {...getInputProps()} /> |
| 2299 |
{isDragActive && "dragActive"} |
| 2300 |
{isDragAccept && "dragAccept"} |
| 2301 |
{isDragReject && "dragReject"} |
| 2302 |
</div> |
| 2303 |
)} |
| 2304 |
</Dropzone> |
| 2305 |
); |
| 2306 |
const dropzone = container.querySelector("div"); |
| 2307 |
|
| 2308 |
await act(() => fireEvent.dragEnter(dropzone, createDtWithFiles(files))); |
| 2309 |
|
| 2310 |
expect(dropzone).toHaveTextContent("dragActive"); |
| 2311 |
expect(dropzone).toHaveTextContent("dragAccept"); |
| 2312 |
expect(dropzone).not.toHaveTextContent("dragReject"); |
| 2313 |
}); |
| 2314 |
|
| 2315 |
it("sets {isDragActive} and {isDragReject} of some files are not accepted on dragenter", async () => { |
| 2316 |
const { container } = render( |
| 2317 |
<Dropzone |
| 2318 |
accept={{ |
| 2319 |
"image/*": [], |
| 2320 |
}} |
| 2321 |
> |
| 2322 |
{({ |
| 2323 |
getRootProps, |
| 2324 |
getInputProps, |
| 2325 |
isDragActive, |
| 2326 |
isDragAccept, |
| 2327 |
isDragReject, |
| 2328 |
}) => ( |
| 2329 |
<div {...getRootProps()}> |
| 2330 |
<input {...getInputProps()} /> |
| 2331 |
{isDragActive && "dragActive"} |
| 2332 |
{isDragAccept && "dragAccept"} |
| 2333 |
{isDragReject && "dragReject"} |
| 2334 |
</div> |
| 2335 |
)} |
| 2336 |
</Dropzone> |
| 2337 |
); |
| 2338 |
const dropzone = container.querySelector("div"); |
| 2339 |
|
| 2340 |
await act(() => |
| 2341 |
fireEvent.dragEnter(dropzone, createDtWithFiles([...files, ...images])) |
| 2342 |
); |
| 2343 |
|
| 2344 |
expect(dropzone).toHaveTextContent("dragActive"); |
| 2345 |
expect(dropzone).not.toHaveTextContent("dragAccept"); |
| 2346 |
expect(dropzone).toHaveTextContent("dragReject"); |
| 2347 |
}); |
| 2348 |
|
| 2349 |
it("sets {isDragReject} if some files are too large", async () => { |
| 2350 |
const { container } = render( |
| 2351 |
<Dropzone maxSize={0}> |
| 2352 |
{({ getRootProps, getInputProps, isDragAccept, isDragReject }) => ( |
| 2353 |
<div {...getRootProps()}> |
| 2354 |
<input {...getInputProps()} /> |
| 2355 |
{isDragAccept && "dragAccept"} |
| 2356 |
{isDragReject && "dragReject"} |
| 2357 |
</div> |
| 2358 |
)} |
| 2359 |
</Dropzone> |
| 2360 |
); |
| 2361 |
const dropzone = container.querySelector("div"); |
| 2362 |
|
| 2363 |
await act(() => fireEvent.dragEnter(dropzone, createDtWithFiles(files))); |
| 2364 |
|
| 2365 |
expect(dropzone).not.toHaveTextContent("dragAccept"); |
| 2366 |
expect(dropzone).toHaveTextContent("dragReject"); |
| 2367 |
}); |
| 2368 |
|
| 2369 |
it("accepts files with empty type during dragenter (Chrome .md file issue)", async () => { |
| 2370 |
const markdownFiles = [createFile("README.md", 1234, "text/markdown")]; |
| 2371 |
|
| 2372 |
const { container } = render( |
| 2373 |
<Dropzone |
| 2374 |
accept={{ |
| 2375 |
"text/markdown": [".md"], |
| 2376 |
}} |
| 2377 |
> |
| 2378 |
{({ |
| 2379 |
getRootProps, |
| 2380 |
getInputProps, |
| 2381 |
isDragActive, |
| 2382 |
isDragAccept, |
| 2383 |
isDragReject, |
| 2384 |
}) => ( |
| 2385 |
<div {...getRootProps()}> |
| 2386 |
<input {...getInputProps()} /> |
| 2387 |
{isDragActive && "dragActive"} |
| 2388 |
{isDragAccept && "dragAccept"} |
| 2389 |
{isDragReject && "dragReject"} |
| 2390 |
</div> |
| 2391 |
)} |
| 2392 |
</Dropzone> |
| 2393 |
); |
| 2394 |
const dropzone = container.querySelector("div"); |
| 2395 |
|
| 2396 |
// Simulate Chrome's behavior: during drag, .md files have empty type |
| 2397 |
await act(() => |
| 2398 |
fireEvent.dragEnter( |
| 2399 |
dropzone, |
| 2400 |
createDtWithFiles(markdownFiles, { emptyTypes: true }) |
| 2401 |
) |
| 2402 |
); |
| 2403 |
|
| 2404 |
expect(dropzone).toHaveTextContent("dragActive"); |
| 2405 |
expect(dropzone).toHaveTextContent("dragAccept"); |
| 2406 |
expect(dropzone).not.toHaveTextContent("dragReject"); |
| 2407 |
}); |
| 2408 |
|
| 2409 |
it("sets {isDragActive, isDragAccept, isDragReject} if any files are rejected and {multiple} is false on dragenter", async () => { |
| 2410 |
const { container } = render( |
| 2411 |
<Dropzone |
| 2412 |
accept={{ |
| 2413 |
"image/*": [], |
| 2414 |
}} |
| 2415 |
multiple={false} |
| 2416 |
> |
| 2417 |
{({ |
| 2418 |
getRootProps, |
| 2419 |
getInputProps, |
| 2420 |
isDragActive, |
| 2421 |
isDragAccept, |
| 2422 |
isDragReject, |
| 2423 |
}) => ( |
| 2424 |
<div {...getRootProps()}> |
| 2425 |
<input {...getInputProps()} /> |
| 2426 |
{isDragActive && "dragActive"} |
| 2427 |
{isDragAccept && "dragAccept"} |
| 2428 |
{isDragReject && "dragReject"} |
| 2429 |
</div> |
| 2430 |
)} |
| 2431 |
</Dropzone> |
| 2432 |
); |
| 2433 |
const dropzone = container.querySelector("div"); |
| 2434 |
|
| 2435 |
await act(() => fireEvent.dragEnter(dropzone, createDtWithFiles(images))); |
| 2436 |
|
| 2437 |
expect(dropzone).toHaveTextContent("dragActive"); |
| 2438 |
expect(dropzone).not.toHaveTextContent("dragAccept"); |
| 2439 |
expect(dropzone).toHaveTextContent("dragReject"); |
| 2440 |
}); |
| 2441 |
|
| 2442 |
it("keeps {isDragActive} if dragleave is triggered for some arbitrary node", async () => { |
| 2443 |
const { container: overlayContainer } = render(<div />); |
| 2444 |
|
| 2445 |
const { container } = render( |
| 2446 |
<Dropzone> |
| 2447 |
{({ |
| 2448 |
getRootProps, |
| 2449 |
getInputProps, |
| 2450 |
isDragActive, |
| 2451 |
isDragAccept, |
| 2452 |
isDragReject, |
| 2453 |
}) => ( |
| 2454 |
<div {...getRootProps()}> |
| 2455 |
<input {...getInputProps()} /> |
| 2456 |
{isDragActive && "dragActive"} |
| 2457 |
{isDragAccept && "dragAccept"} |
| 2458 |
{isDragReject && "dragReject"} |
| 2459 |
</div> |
| 2460 |
)} |
| 2461 |
</Dropzone> |
| 2462 |
); |
| 2463 |
const dropzone = container.querySelector("div"); |
| 2464 |
|
| 2465 |
await act(() => fireEvent.dragEnter(dropzone, createDtWithFiles(files))); |
| 2466 |
|
| 2467 |
fireEvent.dragLeave(dropzone, { |
| 2468 |
bubbles: true, |
| 2469 |
target: overlayContainer.querySelector("div"), |
| 2470 |
}); |
| 2471 |
|
| 2472 |
expect(dropzone).toHaveTextContent("dragActive"); |
| 2473 |
}); |
| 2474 |
|
| 2475 |
it("resets {isDragActive, isDragAccept, isDragReject} on dragleave", async () => { |
| 2476 |
const { container } = render( |
| 2477 |
<Dropzone |
| 2478 |
accept={{ |
| 2479 |
"image/*": [], |
| 2480 |
}} |
| 2481 |
> |
| 2482 |
{({ |
| 2483 |
getRootProps, |
| 2484 |
getInputProps, |
| 2485 |
isDragActive, |
| 2486 |
isDragAccept, |
| 2487 |
isDragReject, |
| 2488 |
}) => ( |
| 2489 |
<div {...getRootProps()}> |
| 2490 |
<input {...getInputProps()} /> |
| 2491 |
{isDragActive && "dragActive"} |
| 2492 |
{isDragAccept && "dragAccept"} |
| 2493 |
{isDragReject && "dragReject"} |
| 2494 |
{!isDragActive && ( |
| 2495 |
<span |
| 2496 |
id="child" |
| 2497 |
data-accept={isDragAccept} |
| 2498 |
data-reject={isDragReject} |
| 2499 |
/> |
| 2500 |
)} |
| 2501 |
</div> |
| 2502 |
)} |
| 2503 |
</Dropzone> |
| 2504 |
); |
| 2505 |
const dropzone = container.querySelector("div"); |
| 2506 |
|
| 2507 |
const data = createDtWithFiles(images); |
| 2508 |
|
| 2509 |
await act(() => |
| 2510 |
fireEvent.dragEnter(container.querySelector("#child"), data) |
| 2511 |
); |
| 2512 |
await act(() => fireEvent.dragEnter(dropzone, data)); |
| 2513 |
|
| 2514 |
await act(() => fireEvent.dragEnter(dropzone, data)); |
| 2515 |
|
| 2516 |
expect(dropzone).toHaveTextContent("dragActive"); |
| 2517 |
expect(dropzone).toHaveTextContent("dragAccept"); |
| 2518 |
expect(dropzone).not.toHaveTextContent("dragReject"); |
| 2519 |
|
| 2520 |
fireEvent.dragLeave(dropzone, data); |
| 2521 |
expect(dropzone).toHaveTextContent("dragActive"); |
| 2522 |
expect(dropzone).toHaveTextContent("dragAccept"); |
| 2523 |
expect(dropzone).not.toHaveTextContent("dragReject"); |
| 2524 |
|
| 2525 |
fireEvent.dragLeave(dropzone, data); |
| 2526 |
expect(dropzone).not.toHaveTextContent("dragActive"); |
| 2527 |
expect(dropzone).not.toHaveTextContent("dragAccept"); |
| 2528 |
expect(dropzone).not.toHaveTextContent("dragReject"); |
| 2529 |
|
| 2530 |
const child = container.querySelector("#child"); |
| 2531 |
expect(child).toHaveAttribute("data-accept", "false"); |
| 2532 |
expect(child).toHaveAttribute("data-reject", "false"); |
| 2533 |
}); |
| 2534 |
}); |
| 2535 |
|
| 2536 |
describe("onDrop", () => { |
| 2537 |
test("callback is invoked when <input> change event occurs", async () => { |
| 2538 |
const onDropSpy = jest.fn(); |
| 2539 |
|
| 2540 |
const { container } = render( |
| 2541 |
<Dropzone onDrop={onDropSpy}> |
| 2542 |
{({ getRootProps, getInputProps }) => ( |
| 2543 |
<div {...getRootProps()}> |
| 2544 |
<input {...getInputProps()} /> |
| 2545 |
</div> |
| 2546 |
)} |
| 2547 |
</Dropzone> |
| 2548 |
); |
| 2549 |
|
| 2550 |
await act(async () => |
| 2551 |
fireEvent.change(container.querySelector("input"), { |
| 2552 |
target: { files }, |
| 2553 |
}) |
| 2554 |
); |
| 2555 |
|
| 2556 |
expect(onDropSpy).toHaveBeenCalledWith(files, [], expect.anything()); |
| 2557 |
}); |
| 2558 |
|
| 2559 |
it("sets {acceptedFiles, fileRejections, isDragReject}", async () => { |
| 2560 |
const FileList = ({ files = [] }) => ( |
| 2561 |
<ul> |
| 2562 |
{files.map((file) => ( |
| 2563 |
<li key={file.name} data-type={"accepted"}> |
| 2564 |
{file.name} |
| 2565 |
</li> |
| 2566 |
))} |
| 2567 |
</ul> |
| 2568 |
); |
| 2569 |
|
| 2570 |
const RejectedFileList = ({ fileRejections = [] }) => ( |
| 2571 |
<ul> |
| 2572 |
{fileRejections.map(({ file, errors }) => ( |
| 2573 |
<li key={file.name}> |
| 2574 |
<span data-type={"rejected"}>{file.name}</span> |
| 2575 |
<ul> |
| 2576 |
{errors.map((e) => ( |
| 2577 |
<li key={e.code} data-type={"error"}> |
| 2578 |
{e.code} |
| 2579 |
</li> |
| 2580 |
))} |
| 2581 |
</ul> |
| 2582 |
</li> |
| 2583 |
))} |
| 2584 |
</ul> |
| 2585 |
); |
| 2586 |
|
| 2587 |
const getAcceptedFiles = (node) => |
| 2588 |
node.querySelectorAll(`[data-type="accepted"]`); |
| 2589 |
const getRejectedFiles = (node) => |
| 2590 |
node.querySelectorAll(`[data-type="rejected"]`); |
| 2591 |
const getRejectedFilesErrors = (node) => |
| 2592 |
node.querySelectorAll(`[data-type="error"]`); |
| 2593 |
|
| 2594 |
const matchToFiles = (fileList, files) => |
| 2595 |
Array.from(fileList).every( |
| 2596 |
(item) => !!files.find((file) => file.name === item.textContent) |
| 2597 |
); |
| 2598 |
const matchToErrorCode = (errorList, code) => |
| 2599 |
Array.from(errorList).every((item) => item.textContent === code); |
| 2600 |
|
| 2601 |
const { container } = render( |
| 2602 |
<Dropzone |
| 2603 |
accept={{ |
| 2604 |
"image/*": [], |
| 2605 |
}} |
| 2606 |
> |
| 2607 |
{({ |
| 2608 |
getRootProps, |
| 2609 |
getInputProps, |
| 2610 |
acceptedFiles, |
| 2611 |
fileRejections, |
| 2612 |
isDragReject, |
| 2613 |
}) => ( |
| 2614 |
<div {...getRootProps()}> |
| 2615 |
<input {...getInputProps()} /> |
| 2616 |
<FileList files={acceptedFiles} /> |
| 2617 |
<RejectedFileList fileRejections={fileRejections} /> |
| 2618 |
{isDragReject && "dragReject"} |
| 2619 |
</div> |
| 2620 |
)} |
| 2621 |
</Dropzone> |
| 2622 |
); |
| 2623 |
const dropzone = container.querySelector("div"); |
| 2624 |
|
| 2625 |
await act(() => fireEvent.drop(dropzone, createDtWithFiles(images))); |
| 2626 |
|
| 2627 |
const acceptedFileList = getAcceptedFiles(dropzone); |
| 2628 |
expect(acceptedFileList).toHaveLength(images.length); |
| 2629 |
expect(matchToFiles(acceptedFileList, images)).toBe(true); |
| 2630 |
expect(dropzone).not.toHaveTextContent("dragReject"); |
| 2631 |
|
| 2632 |
await act(() => fireEvent.drop(dropzone, createDtWithFiles(files))); |
| 2633 |
|
| 2634 |
const rejectedFileList = getRejectedFiles(dropzone); |
| 2635 |
expect(rejectedFileList).toHaveLength(files.length); |
| 2636 |
expect(matchToFiles(rejectedFileList, files)).toBe(true); |
| 2637 |
const rejectedFileErrorList = getRejectedFilesErrors(dropzone); |
| 2638 |
expect(rejectedFileErrorList).toHaveLength(files.length); |
| 2639 |
expect(matchToErrorCode(rejectedFileErrorList, "file-invalid-type")).toBe( |
| 2640 |
true |
| 2641 |
); |
| 2642 |
expect(dropzone).toHaveTextContent("dragReject"); |
| 2643 |
}); |
| 2644 |
|
| 2645 |
it("resets {isDragActive, isDragAccept, isDragReject}", async () => { |
| 2646 |
const { container } = render( |
| 2647 |
<Dropzone> |
| 2648 |
{({ |
| 2649 |
getRootProps, |
| 2650 |
getInputProps, |
| 2651 |
isDragActive, |
| 2652 |
isDragAccept, |
| 2653 |
isDragReject, |
| 2654 |
}) => ( |
| 2655 |
<div {...getRootProps()}> |
| 2656 |
<input {...getInputProps()} /> |
| 2657 |
{isDragActive && "dragActive"} |
| 2658 |
{isDragAccept && "dragAccept"} |
| 2659 |
{isDragReject && "dragReject"} |
| 2660 |
</div> |
| 2661 |
)} |
| 2662 |
</Dropzone> |
| 2663 |
); |
| 2664 |
const dropzone = container.querySelector("div"); |
| 2665 |
|
| 2666 |
const data = createDtWithFiles(files); |
| 2667 |
|
| 2668 |
await act(() => fireEvent.dragEnter(dropzone, data)); |
| 2669 |
|
| 2670 |
expect(dropzone).toHaveTextContent("dragActive"); |
| 2671 |
expect(dropzone).toHaveTextContent("dragAccept"); |
| 2672 |
expect(dropzone).not.toHaveTextContent("dragReject"); |
| 2673 |
|
| 2674 |
await act(() => fireEvent.drop(dropzone, data)); |
| 2675 |
|
| 2676 |
expect(dropzone).not.toHaveTextContent("dragActive"); |
| 2677 |
expect(dropzone).not.toHaveTextContent("dragAccept"); |
| 2678 |
expect(dropzone).not.toHaveTextContent("dragReject"); |
| 2679 |
}); |
| 2680 |
|
| 2681 |
it("sets {isDragGlobal} to true when drag event is detected on document", async () => { |
| 2682 |
const { container } = render( |
| 2683 |
<Dropzone> |
| 2684 |
{({ getRootProps, getInputProps, isDragGlobal }) => ( |
| 2685 |
<div {...getRootProps()}> |
| 2686 |
<input {...getInputProps()} /> |
| 2687 |
{isDragGlobal && "dragGlobal"} |
| 2688 |
</div> |
| 2689 |
)} |
| 2690 |
</Dropzone> |
| 2691 |
); |
| 2692 |
const dropzone = container.querySelector("div"); |
| 2693 |
|
| 2694 |
await act(() => |
| 2695 |
fireEvent.dragEnter(document.body, createDtWithFiles(files)) |
| 2696 |
); |
| 2697 |
|
| 2698 |
expect(dropzone).toHaveTextContent("dragGlobal"); |
| 2699 |
}); |
| 2700 |
|
| 2701 |
it("sets {isDragGlobal} to false when drag leaves document", async () => { |
| 2702 |
const { container } = render( |
| 2703 |
<Dropzone> |
| 2704 |
{({ getRootProps, getInputProps, isDragGlobal }) => ( |
| 2705 |
<div {...getRootProps()}> |
| 2706 |
<input {...getInputProps()} /> |
| 2707 |
{isDragGlobal && "dragGlobal"} |
| 2708 |
</div> |
| 2709 |
)} |
| 2710 |
</Dropzone> |
| 2711 |
); |
| 2712 |
const dropzone = container.querySelector("div"); |
| 2713 |
|
| 2714 |
await act(() => |
| 2715 |
fireEvent.dragEnter(document.body, createDtWithFiles(files)) |
| 2716 |
); |
| 2717 |
expect(dropzone).toHaveTextContent("dragGlobal"); |
| 2718 |
|
| 2719 |
await act(() => |
| 2720 |
fireEvent.dragLeave(document.body, createDtWithFiles(files)) |
| 2721 |
); |
| 2722 |
expect(dropzone).not.toHaveTextContent("dragGlobal"); |
| 2723 |
}); |
| 2724 |
|
| 2725 |
it("sets {isDragGlobal} to false when drop occurs on document", async () => { |
| 2726 |
const { container } = render( |
| 2727 |
<Dropzone> |
| 2728 |
{({ getRootProps, getInputProps, isDragGlobal }) => ( |
| 2729 |
<div {...getRootProps()}> |
| 2730 |
<input {...getInputProps()} /> |
| 2731 |
{isDragGlobal && "dragGlobal"} |
| 2732 |
</div> |
| 2733 |
)} |
| 2734 |
</Dropzone> |
| 2735 |
); |
| 2736 |
const dropzone = container.querySelector("div"); |
| 2737 |
|
| 2738 |
await act(() => |
| 2739 |
fireEvent.dragEnter(document.body, createDtWithFiles(files)) |
| 2740 |
); |
| 2741 |
expect(dropzone).toHaveTextContent("dragGlobal"); |
| 2742 |
|
| 2743 |
await act(() => fireEvent.drop(document.body, createDtWithFiles(files))); |
| 2744 |
expect(dropzone).not.toHaveTextContent("dragGlobal"); |
| 2745 |
}); |
| 2746 |
|
| 2747 |
it("sets {isDragGlobal} to false when dragend occurs on document", async () => { |
| 2748 |
const { container } = render( |
| 2749 |
<Dropzone> |
| 2750 |
{({ getRootProps, getInputProps, isDragGlobal }) => ( |
| 2751 |
<div {...getRootProps()}> |
| 2752 |
<input {...getInputProps()} /> |
| 2753 |
{isDragGlobal && "dragGlobal"} |
| 2754 |
</div> |
| 2755 |
)} |
| 2756 |
</Dropzone> |
| 2757 |
); |
| 2758 |
const dropzone = container.querySelector("div"); |
| 2759 |
|
| 2760 |
await act(() => |
| 2761 |
fireEvent.dragEnter(document.body, createDtWithFiles(files)) |
| 2762 |
); |
| 2763 |
expect(dropzone).toHaveTextContent("dragGlobal"); |
| 2764 |
|
| 2765 |
fireEvent.dragEnd(document.body, createDtWithFiles(files)); |
| 2766 |
expect(dropzone).not.toHaveTextContent("dragGlobal"); |
| 2767 |
}); |
| 2768 |
|
| 2769 |
it("rejects all files if {multiple} is false and {accept} criteria is not met", async () => { |
| 2770 |
const onDropSpy = jest.fn(); |
| 2771 |
|
| 2772 |
const { container } = render( |
| 2773 |
<Dropzone |
| 2774 |
accept={{ |
| 2775 |
"image/*": [], |
| 2776 |
}} |
| 2777 |
onDrop={onDropSpy} |
| 2778 |
multiple={false} |
| 2779 |
> |
| 2780 |
{({ getRootProps, getInputProps }) => ( |
| 2781 |
<div {...getRootProps()}> |
| 2782 |
<input {...getInputProps()} /> |
| 2783 |
</div> |
| 2784 |
)} |
| 2785 |
</Dropzone> |
| 2786 |
); |
| 2787 |
const dropzone = container.querySelector("div"); |
| 2788 |
|
| 2789 |
await act(() => fireEvent.drop(dropzone, createDtWithFiles(files))); |
| 2790 |
|
| 2791 |
expect(onDropSpy).toHaveBeenCalledWith( |
| 2792 |
[], |
| 2793 |
[ |
| 2794 |
{ |
| 2795 |
file: files[0], |
| 2796 |
errors: [ |
| 2797 |
{ |
| 2798 |
code: "file-invalid-type", |
| 2799 |
message: "File type must be image/*", |
| 2800 |
}, |
| 2801 |
], |
| 2802 |
}, |
| 2803 |
], |
| 2804 |
expect.anything() |
| 2805 |
); |
| 2806 |
}); |
| 2807 |
|
| 2808 |
it("rejects all files if {multiple} is false and {accept} criteria is met", async () => { |
| 2809 |
const onDropSpy = jest.fn(); |
| 2810 |
|
| 2811 |
const { container } = render( |
| 2812 |
<Dropzone |
| 2813 |
accept={{ |
| 2814 |
"image/*": [], |
| 2815 |
}} |
| 2816 |
onDrop={onDropSpy} |
| 2817 |
multiple={false} |
| 2818 |
> |
| 2819 |
{({ getRootProps, getInputProps }) => ( |
| 2820 |
<div {...getRootProps()}> |
| 2821 |
<input {...getInputProps()} /> |
| 2822 |
</div> |
| 2823 |
)} |
| 2824 |
</Dropzone> |
| 2825 |
); |
| 2826 |
const dropzone = container.querySelector("div"); |
| 2827 |
|
| 2828 |
await act(() => fireEvent.drop(dropzone, createDtWithFiles(images))); |
| 2829 |
|
| 2830 |
expect(onDropSpy).toHaveBeenCalledWith( |
| 2831 |
[], |
| 2832 |
[ |
| 2833 |
{ |
| 2834 |
file: images[0], |
| 2835 |
errors: [ |
| 2836 |
{ |
| 2837 |
code: "too-many-files", |
| 2838 |
message: "Too many files", |
| 2839 |
}, |
| 2840 |
], |
| 2841 |
}, |
| 2842 |
{ |
| 2843 |
file: images[1], |
| 2844 |
errors: [ |
| 2845 |
{ |
| 2846 |
code: "too-many-files", |
| 2847 |
message: "Too many files", |
| 2848 |
}, |
| 2849 |
], |
| 2850 |
}, |
| 2851 |
], |
| 2852 |
expect.anything() |
| 2853 |
); |
| 2854 |
}); |
| 2855 |
|
| 2856 |
it("rejects all files if {multiple} is true and maxFiles is less than files and {accept} criteria is met", async () => { |
| 2857 |
const onDropSpy = jest.fn(); |
| 2858 |
const onDropRejectedSpy = jest.fn(); |
| 2859 |
|
| 2860 |
const { container } = render( |
| 2861 |
<Dropzone |
| 2862 |
accept={{ |
| 2863 |
"image/*": [], |
| 2864 |
}} |
| 2865 |
onDrop={onDropSpy} |
| 2866 |
onDropRejected={onDropRejectedSpy} |
| 2867 |
multiple={true} |
| 2868 |
maxFiles={1} |
| 2869 |
> |
| 2870 |
{({ getRootProps, getInputProps, isDragReject, isDragAccept }) => ( |
| 2871 |
<div {...getRootProps()}> |
| 2872 |
<input {...getInputProps()} /> |
| 2873 |
{isDragReject && "dragReject"} |
| 2874 |
{isDragAccept && "dragAccept"} |
| 2875 |
</div> |
| 2876 |
)} |
| 2877 |
</Dropzone> |
| 2878 |
); |
| 2879 |
const dropzone = container.querySelector("div"); |
| 2880 |
|
| 2881 |
await act(() => fireEvent.drop(dropzone, createDtWithFiles(images))); |
| 2882 |
|
| 2883 |
expect(onDropRejectedSpy).toHaveBeenCalled(); |
| 2884 |
|
| 2885 |
await act(() => fireEvent.dragEnter(dropzone, createDtWithFiles(images))); |
| 2886 |
|
| 2887 |
expect(dropzone).toHaveTextContent("dragReject"); |
| 2888 |
expect(dropzone).not.toHaveTextContent("dragAccept"); |
| 2889 |
expect(onDropSpy).toHaveBeenCalledWith( |
| 2890 |
[], |
| 2891 |
[ |
| 2892 |
{ |
| 2893 |
file: images[0], |
| 2894 |
errors: [ |
| 2895 |
{ |
| 2896 |
code: "too-many-files", |
| 2897 |
message: "Too many files", |
| 2898 |
}, |
| 2899 |
], |
| 2900 |
}, |
| 2901 |
{ |
| 2902 |
file: images[1], |
| 2903 |
errors: [ |
| 2904 |
{ |
| 2905 |
code: "too-many-files", |
| 2906 |
message: "Too many files", |
| 2907 |
}, |
| 2908 |
], |
| 2909 |
}, |
| 2910 |
], |
| 2911 |
expect.anything() |
| 2912 |
); |
| 2913 |
}); |
| 2914 |
|
| 2915 |
it("rejects all files if {multiple} is true and maxFiles has been updated so that it is less than files", async () => { |
| 2916 |
const onDropSpy = jest.fn(); |
| 2917 |
const onDropRejectedSpy = jest.fn(); |
| 2918 |
const ui = (maxFiles) => ( |
| 2919 |
<Dropzone |
| 2920 |
accept={{ |
| 2921 |
"image/*": [], |
| 2922 |
}} |
| 2923 |
onDrop={onDropSpy} |
| 2924 |
multiple={true} |
| 2925 |
maxFiles={maxFiles} |
| 2926 |
onDropRejected={onDropRejectedSpy} |
| 2927 |
> |
| 2928 |
{({ getRootProps, getInputProps }) => ( |
| 2929 |
<div {...getRootProps()}> |
| 2930 |
<input {...getInputProps()} /> |
| 2931 |
</div> |
| 2932 |
)} |
| 2933 |
</Dropzone> |
| 2934 |
); |
| 2935 |
const { container, rerender } = render(ui(3)); |
| 2936 |
const dropzone = container.querySelector("div"); |
| 2937 |
|
| 2938 |
await act(() => fireEvent.drop(dropzone, createDtWithFiles(images))); |
| 2939 |
rerender(ui(3)); |
| 2940 |
|
| 2941 |
expect(onDropRejectedSpy).not.toHaveBeenCalled(); |
| 2942 |
expect(onDropSpy).toHaveBeenCalledWith(images, [], expect.anything()); |
| 2943 |
|
| 2944 |
rerender(ui(1)); |
| 2945 |
|
| 2946 |
await act(() => fireEvent.drop(dropzone, createDtWithFiles(images))); |
| 2947 |
rerender(ui(1)); |
| 2948 |
|
| 2949 |
expect(onDropRejectedSpy).toHaveBeenCalledWith( |
| 2950 |
expect.arrayContaining( |
| 2951 |
images.map((image) => |
| 2952 |
expect.objectContaining({ errors: expect.any(Array), file: image }) |
| 2953 |
) |
| 2954 |
), |
| 2955 |
expect.anything() |
| 2956 |
); |
| 2957 |
}); |
| 2958 |
|
| 2959 |
it("accepts multiple files if {multiple} is true and {accept} criteria is met", async () => { |
| 2960 |
const onDropSpy = jest.fn(); |
| 2961 |
const onDropRejectedSpy = jest.fn(); |
| 2962 |
|
| 2963 |
const { container } = render( |
| 2964 |
<Dropzone |
| 2965 |
accept={{ |
| 2966 |
"image/*": [], |
| 2967 |
}} |
| 2968 |
onDrop={onDropSpy} |
| 2969 |
multiple={true} |
| 2970 |
maxFiles={3} |
| 2971 |
onDropRejected={onDropRejectedSpy} |
| 2972 |
> |
| 2973 |
{({ getRootProps, getInputProps }) => ( |
| 2974 |
<div {...getRootProps()}> |
| 2975 |
<input {...getInputProps()} /> |
| 2976 |
</div> |
| 2977 |
)} |
| 2978 |
</Dropzone> |
| 2979 |
); |
| 2980 |
|
| 2981 |
await act(() => |
| 2982 |
fireEvent.drop( |
| 2983 |
container.querySelector("div"), |
| 2984 |
createDtWithFiles(images) |
| 2985 |
) |
| 2986 |
); |
| 2987 |
|
| 2988 |
expect(onDropRejectedSpy).not.toHaveBeenCalled(); |
| 2989 |
expect(onDropSpy).toHaveBeenCalledWith(images, [], expect.anything()); |
| 2990 |
}); |
| 2991 |
|
| 2992 |
it("accepts a single files if {multiple} is false and {accept} criteria is met", async () => { |
| 2993 |
const onDropSpy = jest.fn(); |
| 2994 |
|
| 2995 |
const { container } = render( |
| 2996 |
<Dropzone |
| 2997 |
accept={{ |
| 2998 |
"image/*": [], |
| 2999 |
}} |
| 3000 |
onDrop={onDropSpy} |
| 3001 |
multiple={false} |
| 3002 |
> |
| 3003 |
{({ getRootProps, getInputProps }) => ( |
| 3004 |
<div {...getRootProps()}> |
| 3005 |
<input {...getInputProps()} /> |
| 3006 |
</div> |
| 3007 |
)} |
| 3008 |
</Dropzone> |
| 3009 |
); |
| 3010 |
|
| 3011 |
const [image] = images; |
| 3012 |
await act(() => |
| 3013 |
fireEvent.drop( |
| 3014 |
container.querySelector("div"), |
| 3015 |
createDtWithFiles([image]) |
| 3016 |
) |
| 3017 |
); |
| 3018 |
|
| 3019 |
expect(onDropSpy).toHaveBeenCalledWith([image], [], expect.anything()); |
| 3020 |
}); |
| 3021 |
|
| 3022 |
it("accepts all files if {multiple} is true", async () => { |
| 3023 |
const onDropSpy = jest.fn(); |
| 3024 |
|
| 3025 |
const { container } = render( |
| 3026 |
<Dropzone onDrop={onDropSpy}> |
| 3027 |
{({ getRootProps, getInputProps }) => ( |
| 3028 |
<div {...getRootProps()}> |
| 3029 |
<input {...getInputProps()} /> |
| 3030 |
</div> |
| 3031 |
)} |
| 3032 |
</Dropzone> |
| 3033 |
); |
| 3034 |
|
| 3035 |
await act(() => |
| 3036 |
fireEvent.drop(container.querySelector("div"), createDtWithFiles(files)) |
| 3037 |
); |
| 3038 |
|
| 3039 |
expect(onDropSpy).toHaveBeenCalledWith(files, [], expect.anything()); |
| 3040 |
}); |
| 3041 |
|
| 3042 |
it("resets {isFileDialogActive} state", async () => { |
| 3043 |
const onDropSpy = jest.fn(); |
| 3044 |
const activeRef = createRef(); |
| 3045 |
const active = <span ref={activeRef}>I am active</span>; |
| 3046 |
|
| 3047 |
const { container } = render( |
| 3048 |
<Dropzone onDrop={onDropSpy}> |
| 3049 |
{({ getRootProps, getInputProps, isFileDialogActive }) => ( |
| 3050 |
<div {...getRootProps()}> |
| 3051 |
<input {...getInputProps()} /> |
| 3052 |
{isFileDialogActive && active} |
| 3053 |
</div> |
| 3054 |
)} |
| 3055 |
</Dropzone> |
| 3056 |
); |
| 3057 |
|
| 3058 |
const dropzone = container.querySelector("div"); |
| 3059 |
|
| 3060 |
fireEvent.click(dropzone); |
| 3061 |
|
| 3062 |
expect(activeRef.current).not.toBeNull(); |
| 3063 |
expect(dropzone).toContainElement(activeRef.current); |
| 3064 |
|
| 3065 |
await act(() => fireEvent.drop(dropzone, createDtWithFiles(files))); |
| 3066 |
|
| 3067 |
expect(activeRef.current).toBeNull(); |
| 3068 |
expect(dropzone).not.toContainElement(activeRef.current); |
| 3069 |
}); |
| 3070 |
|
| 3071 |
it("gets invoked with both accepted/rejected files", async () => { |
| 3072 |
const onDropSpy = jest.fn(); |
| 3073 |
|
| 3074 |
const { container } = render( |
| 3075 |
<Dropzone |
| 3076 |
accept={{ |
| 3077 |
"image/*": [], |
| 3078 |
}} |
| 3079 |
onDrop={onDropSpy} |
| 3080 |
> |
| 3081 |
{({ getRootProps, getInputProps }) => ( |
| 3082 |
<div {...getRootProps()}> |
| 3083 |
<input {...getInputProps()} /> |
| 3084 |
</div> |
| 3085 |
)} |
| 3086 |
</Dropzone> |
| 3087 |
); |
| 3088 |
const dropzone = container.querySelector("div"); |
| 3089 |
|
| 3090 |
await act(() => fireEvent.drop(dropzone, createDtWithFiles(files))); |
| 3091 |
|
| 3092 |
expect(onDropSpy).toHaveBeenCalledWith( |
| 3093 |
[], |
| 3094 |
[ |
| 3095 |
{ |
| 3096 |
file: files[0], |
| 3097 |
errors: [ |
| 3098 |
{ |
| 3099 |
code: "file-invalid-type", |
| 3100 |
message: "File type must be image/*", |
| 3101 |
}, |
| 3102 |
], |
| 3103 |
}, |
| 3104 |
], |
| 3105 |
expect.anything() |
| 3106 |
); |
| 3107 |
onDropSpy.mockClear(); |
| 3108 |
|
| 3109 |
await act(() => fireEvent.drop(dropzone, createDtWithFiles(images))); |
| 3110 |
|
| 3111 |
expect(onDropSpy).toHaveBeenCalledWith(images, [], expect.anything()); |
| 3112 |
onDropSpy.mockClear(); |
| 3113 |
|
| 3114 |
await act(() => |
| 3115 |
fireEvent.drop(dropzone, createDtWithFiles([...files, ...images])) |
| 3116 |
); |
| 3117 |
|
| 3118 |
expect(onDropSpy).toHaveBeenCalledWith( |
| 3119 |
images, |
| 3120 |
[ |
| 3121 |
{ |
| 3122 |
file: files[0], |
| 3123 |
errors: [ |
| 3124 |
{ |
| 3125 |
code: "file-invalid-type", |
| 3126 |
message: "File type must be image/*", |
| 3127 |
}, |
| 3128 |
], |
| 3129 |
}, |
| 3130 |
], |
| 3131 |
expect.anything() |
| 3132 |
); |
| 3133 |
}); |
| 3134 |
|
| 3135 |
test("onDropAccepted callback is invoked if some files are accepted", async () => { |
| 3136 |
const onDropAcceptedSpy = jest.fn(); |
| 3137 |
|
| 3138 |
const { container } = render( |
| 3139 |
<Dropzone |
| 3140 |
accept={{ |
| 3141 |
"image/*": [], |
| 3142 |
}} |
| 3143 |
onDropAccepted={onDropAcceptedSpy} |
| 3144 |
> |
| 3145 |
{({ getRootProps, getInputProps }) => ( |
| 3146 |
<div {...getRootProps()}> |
| 3147 |
<input {...getInputProps()} /> |
| 3148 |
</div> |
| 3149 |
)} |
| 3150 |
</Dropzone> |
| 3151 |
); |
| 3152 |
const dropzone = container.querySelector("div"); |
| 3153 |
|
| 3154 |
await act(() => fireEvent.drop(dropzone, createDtWithFiles(files))); |
| 3155 |
expect(onDropAcceptedSpy).not.toHaveBeenCalled(); |
| 3156 |
onDropAcceptedSpy.mockClear(); |
| 3157 |
|
| 3158 |
await act(() => fireEvent.drop(dropzone, createDtWithFiles(images))); |
| 3159 |
|
| 3160 |
expect(onDropAcceptedSpy).toHaveBeenCalledWith(images, expect.anything()); |
| 3161 |
onDropAcceptedSpy.mockClear(); |
| 3162 |
|
| 3163 |
await act(() => |
| 3164 |
fireEvent.drop(dropzone, createDtWithFiles([...files, ...images])) |
| 3165 |
); |
| 3166 |
|
| 3167 |
expect(onDropAcceptedSpy).toHaveBeenCalledWith(images, expect.anything()); |
| 3168 |
}); |
| 3169 |
|
| 3170 |
test("onDropRejected callback is invoked if some files are rejected", async () => { |
| 3171 |
const onDropRejectedSpy = jest.fn(); |
| 3172 |
|
| 3173 |
const { container } = render( |
| 3174 |
<Dropzone |
| 3175 |
accept={{ |
| 3176 |
"image/*": [], |
| 3177 |
}} |
| 3178 |
onDropRejected={onDropRejectedSpy} |
| 3179 |
> |
| 3180 |
{({ getRootProps, getInputProps }) => ( |
| 3181 |
<div {...getRootProps()}> |
| 3182 |
<input {...getInputProps()} /> |
| 3183 |
</div> |
| 3184 |
)} |
| 3185 |
</Dropzone> |
| 3186 |
); |
| 3187 |
const dropzone = container.querySelector("div"); |
| 3188 |
|
| 3189 |
await act(() => fireEvent.drop(dropzone, createDtWithFiles(files))); |
| 3190 |
|
| 3191 |
expect(onDropRejectedSpy).toHaveBeenCalledWith( |
| 3192 |
[ |
| 3193 |
{ |
| 3194 |
file: files[0], |
| 3195 |
errors: [ |
| 3196 |
{ |
| 3197 |
code: "file-invalid-type", |
| 3198 |
message: "File type must be image/*", |
| 3199 |
}, |
| 3200 |
], |
| 3201 |
}, |
| 3202 |
], |
| 3203 |
expect.anything() |
| 3204 |
); |
| 3205 |
onDropRejectedSpy.mockClear(); |
| 3206 |
|
| 3207 |
await act(() => fireEvent.drop(dropzone, createDtWithFiles(images))); |
| 3208 |
|
| 3209 |
expect(onDropRejectedSpy).not.toHaveBeenCalled(); |
| 3210 |
onDropRejectedSpy.mockClear(); |
| 3211 |
|
| 3212 |
await act(() => |
| 3213 |
fireEvent.drop(dropzone, createDtWithFiles([...files, ...images])) |
| 3214 |
); |
| 3215 |
|
| 3216 |
expect(onDropRejectedSpy).toHaveBeenCalledWith( |
| 3217 |
[ |
| 3218 |
{ |
| 3219 |
file: files[0], |
| 3220 |
errors: [ |
| 3221 |
{ |
| 3222 |
code: "file-invalid-type", |
| 3223 |
message: "File type must be image/*", |
| 3224 |
}, |
| 3225 |
], |
| 3226 |
}, |
| 3227 |
], |
| 3228 |
expect.anything() |
| 3229 |
); |
| 3230 |
}); |
| 3231 |
|
| 3232 |
it("accepts a dropped image when Firefox provides a bogus file type", async () => { |
| 3233 |
const onDropSpy = jest.fn(); |
| 3234 |
|
| 3235 |
const { container } = render( |
| 3236 |
<Dropzone |
| 3237 |
accept={{ |
| 3238 |
"image/*": [], |
| 3239 |
}} |
| 3240 |
onDrop={onDropSpy} |
| 3241 |
> |
| 3242 |
{({ getRootProps, getInputProps }) => ( |
| 3243 |
<div {...getRootProps()}> |
| 3244 |
<input {...getInputProps()} /> |
| 3245 |
</div> |
| 3246 |
)} |
| 3247 |
</Dropzone> |
| 3248 |
); |
| 3249 |
|
| 3250 |
const images = [createFile("bogus.gif", 1234, "application/x-moz-file")]; |
| 3251 |
await act(() => |
| 3252 |
fireEvent.drop( |
| 3253 |
container.querySelector("div"), |
| 3254 |
createDtWithFiles(images) |
| 3255 |
) |
| 3256 |
); |
| 3257 |
|
| 3258 |
expect(onDropSpy).toHaveBeenCalledWith(images, [], expect.anything()); |
| 3259 |
}); |
| 3260 |
|
| 3261 |
it("filters files according to {maxSize}", async () => { |
| 3262 |
const onDropSpy = jest.fn(); |
| 3263 |
|
| 3264 |
const { container } = render( |
| 3265 |
<Dropzone onDrop={onDropSpy} maxSize={1111}> |
| 3266 |
{({ getRootProps, getInputProps }) => ( |
| 3267 |
<div {...getRootProps()}> |
| 3268 |
<input {...getInputProps()} /> |
| 3269 |
</div> |
| 3270 |
)} |
| 3271 |
</Dropzone> |
| 3272 |
); |
| 3273 |
|
| 3274 |
await act(() => |
| 3275 |
fireEvent.drop( |
| 3276 |
container.querySelector("div"), |
| 3277 |
createDtWithFiles(images) |
| 3278 |
) |
| 3279 |
); |
| 3280 |
|
| 3281 |
expect(onDropSpy).toHaveBeenCalledWith( |
| 3282 |
[], |
| 3283 |
[ |
| 3284 |
{ |
| 3285 |
file: images[0], |
| 3286 |
errors: [ |
| 3287 |
{ |
| 3288 |
code: "file-too-large", |
| 3289 |
message: "File is larger than 1111 bytes", |
| 3290 |
}, |
| 3291 |
], |
| 3292 |
}, |
| 3293 |
{ |
| 3294 |
file: images[1], |
| 3295 |
errors: [ |
| 3296 |
{ |
| 3297 |
code: "file-too-large", |
| 3298 |
message: "File is larger than 1111 bytes", |
| 3299 |
}, |
| 3300 |
], |
| 3301 |
}, |
| 3302 |
], |
| 3303 |
expect.anything() |
| 3304 |
); |
| 3305 |
}); |
| 3306 |
|
| 3307 |
it("filters files according to {minSize}", async () => { |
| 3308 |
const onDropSpy = jest.fn(); |
| 3309 |
|
| 3310 |
const { container } = render( |
| 3311 |
<Dropzone onDrop={onDropSpy} minSize={1112}> |
| 3312 |
{({ getRootProps, getInputProps }) => ( |
| 3313 |
<div {...getRootProps()}> |
| 3314 |
<input {...getInputProps()} /> |
| 3315 |
</div> |
| 3316 |
)} |
| 3317 |
</Dropzone> |
| 3318 |
); |
| 3319 |
const dropzone = container.querySelector("div"); |
| 3320 |
|
| 3321 |
await act(() => fireEvent.drop(dropzone, createDtWithFiles(files))); |
| 3322 |
|
| 3323 |
expect(onDropSpy).toHaveBeenCalledWith( |
| 3324 |
[], |
| 3325 |
[ |
| 3326 |
{ |
| 3327 |
file: files[0], |
| 3328 |
errors: [ |
| 3329 |
{ |
| 3330 |
code: "file-too-small", |
| 3331 |
message: "File is smaller than 1112 bytes", |
| 3332 |
}, |
| 3333 |
], |
| 3334 |
}, |
| 3335 |
], |
| 3336 |
expect.anything() |
| 3337 |
); |
| 3338 |
}); |
| 3339 |
}); |
| 3340 |
|
| 3341 |
describe("onFileDialogCancel", () => { |
| 3342 |
beforeEach(() => { |
| 3343 |
jest.useFakeTimers(); |
| 3344 |
}); |
| 3345 |
|
| 3346 |
afterEach(() => { |
| 3347 |
jest.useRealTimers(); |
| 3348 |
}); |
| 3349 |
|
| 3350 |
it("is not invoked every time window receives focus", () => { |
| 3351 |
const onFileDialogCancelSpy = jest.fn(); |
| 3352 |
|
| 3353 |
render( |
| 3354 |
<Dropzone onFileDialogCancel={onFileDialogCancelSpy}> |
| 3355 |
{({ getRootProps, getInputProps }) => ( |
| 3356 |
<div {...getRootProps()}> |
| 3357 |
<input {...getInputProps()} /> |
| 3358 |
</div> |
| 3359 |
)} |
| 3360 |
</Dropzone> |
| 3361 |
); |
| 3362 |
|
| 3363 |
focusWindow(); |
| 3364 |
drainPendingTimers(); |
| 3365 |
|
| 3366 |
expect(onFileDialogCancelSpy).not.toHaveBeenCalled(); |
| 3367 |
}); |
| 3368 |
|
| 3369 |
it("resets {isFileDialogActive}", () => { |
| 3370 |
const activeRef = createRef(); |
| 3371 |
const active = <span ref={activeRef}>I am active</span>; |
| 3372 |
const onFileDialogCancelSpy = jest.fn(); |
| 3373 |
|
| 3374 |
const { container } = render( |
| 3375 |
<Dropzone onFileDialogCancel={onFileDialogCancelSpy}> |
| 3376 |
{({ getRootProps, getInputProps, isFileDialogActive }) => ( |
| 3377 |
<div {...getRootProps()}> |
| 3378 |
<input {...getInputProps()} /> |
| 3379 |
{isFileDialogActive && active} |
| 3380 |
</div> |
| 3381 |
)} |
| 3382 |
</Dropzone> |
| 3383 |
); |
| 3384 |
|
| 3385 |
const dropzone = container.querySelector("div"); |
| 3386 |
|
| 3387 |
fireEvent.click(dropzone); |
| 3388 |
|
| 3389 |
expect(activeRef.current).not.toBeNull(); |
| 3390 |
expect(dropzone).toContainElement(activeRef.current); |
| 3391 |
|
| 3392 |
focusWindow(); |
| 3393 |
drainPendingTimers(); |
| 3394 |
|
| 3395 |
expect(onFileDialogCancelSpy).toHaveBeenCalled(); |
| 3396 |
expect(dropzone).not.toContainElement(activeRef.current); |
| 3397 |
}); |
| 3398 |
|
| 3399 |
it("is not invoked if <input> is not rendered", () => { |
| 3400 |
const onFileDialogCancelSpy = jest.fn(); |
| 3401 |
const { container, rerender } = render( |
| 3402 |
<Dropzone onFileDialogCancel={onFileDialogCancelSpy}> |
| 3403 |
{({ getRootProps, getInputProps }) => ( |
| 3404 |
<div {...getRootProps()}> |
| 3405 |
<input {...getInputProps()} /> |
| 3406 |
</div> |
| 3407 |
)} |
| 3408 |
</Dropzone> |
| 3409 |
); |
| 3410 |
|
| 3411 |
fireEvent.click(container.querySelector("div")); |
| 3412 |
|
| 3413 |
// Remove the input, then on window focus nothing should happen because we check if the input ref is set |
| 3414 |
rerender( |
| 3415 |
<Dropzone onFileDialogCancel={onFileDialogCancelSpy}> |
| 3416 |
{({ getRootProps }) => <div {...getRootProps()} />} |
| 3417 |
</Dropzone> |
| 3418 |
); |
| 3419 |
|
| 3420 |
focusWindow(); |
| 3421 |
drainPendingTimers(); |
| 3422 |
|
| 3423 |
expect(onFileDialogCancelSpy).not.toHaveBeenCalled(); |
| 3424 |
}); |
| 3425 |
|
| 3426 |
it("is not invoked if files were selected", async () => { |
| 3427 |
const onFileDialogCancelSpy = jest.fn(); |
| 3428 |
|
| 3429 |
const { container } = render( |
| 3430 |
<Dropzone onFileDialogCancel={onFileDialogCancelSpy}> |
| 3431 |
{({ getRootProps, getInputProps }) => ( |
| 3432 |
<div {...getRootProps()}> |
| 3433 |
<input {...getInputProps()} /> |
| 3434 |
</div> |
| 3435 |
)} |
| 3436 |
</Dropzone> |
| 3437 |
); |
| 3438 |
|
| 3439 |
await act(async () => |
| 3440 |
fireEvent.change(container.querySelector("input"), { |
| 3441 |
target: { files }, |
| 3442 |
}) |
| 3443 |
); |
| 3444 |
fireEvent.click(container.querySelector("div")); |
| 3445 |
|
| 3446 |
focusWindow(); |
| 3447 |
drainPendingTimers(); |
| 3448 |
|
| 3449 |
expect(onFileDialogCancelSpy).not.toHaveBeenCalled(); |
| 3450 |
}); |
| 3451 |
|
| 3452 |
it("does not throw if callback is not provided", () => { |
| 3453 |
const { container } = render( |
| 3454 |
<Dropzone onFileDialogCancel={null}> |
| 3455 |
{({ getRootProps, getInputProps }) => ( |
| 3456 |
<div {...getRootProps()}> |
| 3457 |
<input {...getInputProps()} /> |
| 3458 |
</div> |
| 3459 |
)} |
| 3460 |
</Dropzone> |
| 3461 |
); |
| 3462 |
|
| 3463 |
fireEvent.click(container.querySelector("div")); |
| 3464 |
|
| 3465 |
const fn = () => { |
| 3466 |
focusWindow(); |
| 3467 |
drainPendingTimers(); |
| 3468 |
}; |
| 3469 |
expect(fn).not.toThrow(); |
| 3470 |
}); |
| 3471 |
}); |
| 3472 |
|
| 3473 |
describe("onFileDialogOpen", () => { |
| 3474 |
it("is invoked when opening the file dialog", () => { |
| 3475 |
const onFileDialogOpenSpy = jest.fn(); |
| 3476 |
const { container } = render( |
| 3477 |
<Dropzone onFileDialogOpen={onFileDialogOpenSpy}> |
| 3478 |
{({ getRootProps, getInputProps }) => ( |
| 3479 |
<div {...getRootProps()}> |
| 3480 |
<input {...getInputProps()} /> |
| 3481 |
</div> |
| 3482 |
)} |
| 3483 |
</Dropzone> |
| 3484 |
); |
| 3485 |
|
| 3486 |
fireEvent.click(container.querySelector("div")); |
| 3487 |
|
| 3488 |
expect(onFileDialogOpenSpy).toHaveBeenCalled(); |
| 3489 |
}); |
| 3490 |
|
| 3491 |
it("is invoked when opening the file dialog programmatically", () => { |
| 3492 |
const onFileDialogOpenSpy = jest.fn(); |
| 3493 |
const { container } = render( |
| 3494 |
<Dropzone onFileDialogOpen={onFileDialogOpenSpy}> |
| 3495 |
{({ getRootProps, getInputProps, open }) => ( |
| 3496 |
<div {...getRootProps()}> |
| 3497 |
<input {...getInputProps()} /> |
| 3498 |
<button type="button" onClick={open}> |
| 3499 |
Open |
| 3500 |
</button> |
| 3501 |
</div> |
| 3502 |
)} |
| 3503 |
</Dropzone> |
| 3504 |
); |
| 3505 |
|
| 3506 |
fireEvent.click(container.querySelector("button")); |
| 3507 |
|
| 3508 |
expect(onFileDialogOpenSpy).toHaveBeenCalled(); |
| 3509 |
}); |
| 3510 |
}); |
| 3511 |
|
| 3512 |
describe("{open}", () => { |
| 3513 |
it("can open file dialog programmatically", () => { |
| 3514 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 3515 |
const { container } = render( |
| 3516 |
<Dropzone> |
| 3517 |
{({ getRootProps, getInputProps, open }) => ( |
| 3518 |
<div {...getRootProps()}> |
| 3519 |
<input {...getInputProps()} /> |
| 3520 |
<button type="button" onClick={open}> |
| 3521 |
Open |
| 3522 |
</button> |
| 3523 |
</div> |
| 3524 |
)} |
| 3525 |
</Dropzone> |
| 3526 |
); |
| 3527 |
|
| 3528 |
fireEvent.click(container.querySelector("button")); |
| 3529 |
|
| 3530 |
expect(onClickSpy).toHaveBeenCalled(); |
| 3531 |
}); |
| 3532 |
|
| 3533 |
it("sets {isFileDialogActive} state", () => { |
| 3534 |
const activeRef = createRef(); |
| 3535 |
const active = <span ref={activeRef}>I am active</span>; |
| 3536 |
const { container } = render( |
| 3537 |
<Dropzone> |
| 3538 |
{({ getRootProps, getInputProps, isFileDialogActive, open }) => ( |
| 3539 |
<div {...getRootProps()}> |
| 3540 |
<input {...getInputProps()} /> |
| 3541 |
{isFileDialogActive && active} |
| 3542 |
<button type="button" onClick={open}> |
| 3543 |
Open |
| 3544 |
</button> |
| 3545 |
</div> |
| 3546 |
)} |
| 3547 |
</Dropzone> |
| 3548 |
); |
| 3549 |
|
| 3550 |
fireEvent.click(container.querySelector("button")); |
| 3551 |
|
| 3552 |
expect(activeRef.current).not.toBeNull(); |
| 3553 |
expect(container.querySelector("div")).toContainElement( |
| 3554 |
activeRef.current |
| 3555 |
); |
| 3556 |
}); |
| 3557 |
|
| 3558 |
it("does nothing if {disabled} is true", () => { |
| 3559 |
const onClickSpy = jest.spyOn(HTMLInputElement.prototype, "click"); |
| 3560 |
const { container } = render( |
| 3561 |
<Dropzone disabled> |
| 3562 |
{({ getRootProps, getInputProps, open }) => ( |
| 3563 |
<div {...getRootProps()}> |
| 3564 |
<input {...getInputProps()} /> |
| 3565 |
<button type="button" onClick={open}> |
| 3566 |
Open |
| 3567 |
</button> |
| 3568 |
</div> |
| 3569 |
)} |
| 3570 |
</Dropzone> |
| 3571 |
); |
| 3572 |
|
| 3573 |
fireEvent.click(container.querySelector("button")); |
| 3574 |
|
| 3575 |
expect(onClickSpy).not.toHaveBeenCalled(); |
| 3576 |
}); |
| 3577 |
|
| 3578 |
it("does not throw if <input> is missing", () => { |
| 3579 |
const { container } = render( |
| 3580 |
<Dropzone> |
| 3581 |
{({ getRootProps, open }) => ( |
| 3582 |
<div {...getRootProps()}> |
| 3583 |
<button type="button" onClick={open}> |
| 3584 |
Open |
| 3585 |
</button> |
| 3586 |
</div> |
| 3587 |
)} |
| 3588 |
</Dropzone> |
| 3589 |
); |
| 3590 |
|
| 3591 |
const fn = () => fireEvent.click(container.querySelector("button")); |
| 3592 |
expect(fn).not.toThrow(); |
| 3593 |
}); |
| 3594 |
}); |
| 3595 |
|
| 3596 |
describe("validator", () => { |
| 3597 |
it("rejects with custom error", async () => { |
| 3598 |
const validator = (file) => { |
| 3599 |
if (/dogs/i.test(file.name)) |
| 3600 |
return { code: "dogs-not-allowed", message: "Dogs not allowed" }; |
| 3601 |
|
| 3602 |
return null; |
| 3603 |
}; |
| 3604 |
|
| 3605 |
const onDropSpy = jest.fn(); |
| 3606 |
|
| 3607 |
const { container } = render( |
| 3608 |
<Dropzone validator={validator} onDrop={onDropSpy} multiple={true}> |
| 3609 |
{({ getRootProps }) => <div {...getRootProps()} />} |
| 3610 |
</Dropzone> |
| 3611 |
); |
| 3612 |
|
| 3613 |
await act(() => |
| 3614 |
fireEvent.drop( |
| 3615 |
container.querySelector("div"), |
| 3616 |
createDtWithFiles(images) |
| 3617 |
) |
| 3618 |
); |
| 3619 |
|
| 3620 |
expect(onDropSpy).toHaveBeenCalledWith( |
| 3621 |
[images[0]], |
| 3622 |
[ |
| 3623 |
{ |
| 3624 |
file: images[1], |
| 3625 |
errors: [ |
| 3626 |
{ |
| 3627 |
code: "dogs-not-allowed", |
| 3628 |
message: "Dogs not allowed", |
| 3629 |
}, |
| 3630 |
], |
| 3631 |
}, |
| 3632 |
], |
| 3633 |
expect.anything() |
| 3634 |
); |
| 3635 |
}); |
| 3636 |
|
| 3637 |
it("sets {isDragAccept, isDragReject}", async () => { |
| 3638 |
const data = createDtWithFiles(images); |
| 3639 |
const validator = () => ({ |
| 3640 |
code: "not-allowed", |
| 3641 |
message: "Cannot do this!", |
| 3642 |
}); |
| 3643 |
|
| 3644 |
const ui = ( |
| 3645 |
<Dropzone validator={validator} multiple={true}> |
| 3646 |
{({ getRootProps, getInputProps, isDragAccept, isDragReject }) => ( |
| 3647 |
<div {...getRootProps()}> |
| 3648 |
<input {...getInputProps()} /> |
| 3649 |
{isDragAccept && "dragAccept"} |
| 3650 |
{isDragReject && "dragReject"} |
| 3651 |
</div> |
| 3652 |
)} |
| 3653 |
</Dropzone> |
| 3654 |
); |
| 3655 |
|
| 3656 |
const { container } = render(ui); |
| 3657 |
const dropzone = container.querySelector("div"); |
| 3658 |
|
| 3659 |
await act(() => fireEvent.dragEnter(dropzone, data)); |
| 3660 |
|
| 3661 |
expect(dropzone).not.toHaveTextContent("dragAccept"); |
| 3662 |
expect(dropzone).toHaveTextContent("dragReject"); |
| 3663 |
}); |
| 3664 |
}); |
| 3665 |
|
| 3666 |
describe("accessibility", () => { |
| 3667 |
it("sets the role attribute to button by default on the root", () => { |
| 3668 |
const { container } = render( |
| 3669 |
<Dropzone> |
| 3670 |
{({ getRootProps }) => <div id="root" {...getRootProps()} />} |
| 3671 |
</Dropzone> |
| 3672 |
); |
| 3673 |
|
| 3674 |
expect(container.querySelector("#root")).toHaveAttribute( |
| 3675 |
"role", |
| 3676 |
"presentation" |
| 3677 |
); |
| 3678 |
}); |
| 3679 |
|
| 3680 |
test("users can override the default role attribute on the root", () => { |
| 3681 |
const { container } = render( |
| 3682 |
<Dropzone> |
| 3683 |
{({ getRootProps }) => ( |
| 3684 |
<div id="root" {...getRootProps({ role: "generic" })} /> |
| 3685 |
)} |
| 3686 |
</Dropzone> |
| 3687 |
); |
| 3688 |
|
| 3689 |
expect(container.querySelector("#root")).toHaveAttribute( |
| 3690 |
"role", |
| 3691 |
"generic" |
| 3692 |
); |
| 3693 |
}); |
| 3694 |
}); |
| 3695 |
}); |
| 3696 |
|
| 3697 |
/** |
| 3698 |
* drainPendingTimers just runs pending timers wrapped in act() to avoid |
| 3699 |
* getting warnings from react about side effects that happen async. |
| 3700 |
* |
| 3701 |
* This can be used whenever a setTimeout(), setInterval() or async operation is used |
| 3702 |
* which triggers a state update. |
| 3703 |
*/ |
| 3704 |
function drainPendingTimers() { |
| 3705 |
return act(() => jest.runOnlyPendingTimers()); |
| 3706 |
} |
| 3707 |
|
| 3708 |
/** |
| 3709 |
* focusWindow triggers focus on the window |
| 3710 |
*/ |
| 3711 |
function focusWindow() { |
| 3712 |
return fireEvent.focus(document.body, { bubbles: true }); |
| 3713 |
} |
| 3714 |
|
| 3715 |
/** |
| 3716 |
* createDtWithFiles creates a mock data transfer object that can be used for drop events |
| 3717 |
* @param {File[]} files |
| 3718 |
* @param {object} options |
| 3719 |
* @param {boolean} options.emptyTypes - If true, sets item types to empty string (simulates Chrome drag behavior) |
| 3720 |
*/ |
| 3721 |
function createDtWithFiles(files = [], options = {}) { |
| 3722 |
const { emptyTypes = false } = options; |
| 3723 |
return { |
| 3724 |
dataTransfer: { |
| 3725 |
files, |
| 3726 |
items: files.map((file) => ({ |
| 3727 |
kind: "file", |
| 3728 |
size: file.size, |
| 3729 |
type: emptyTypes ? "" : file.type, |
| 3730 |
getAsFile: () => file, |
| 3731 |
})), |
| 3732 |
types: ["Files"], |
| 3733 |
}, |
| 3734 |
}; |
| 3735 |
} |
| 3736 |
|
| 3737 |
/** |
| 3738 |
* createFileSystemFileHandle creates a mock [FileSystemFileHandle](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle) |
| 3739 |
* @param {File} file |
| 3740 |
*/ |
| 3741 |
function createFileSystemFileHandle(file) { |
| 3742 |
return { getFile: () => Promise.resolve(file) }; |
| 3743 |
} |
| 3744 |
|
| 3745 |
/** |
| 3746 |
* createFile creates a mock File object |
| 3747 |
* @param {string} name |
| 3748 |
* @param {number} size |
| 3749 |
* @param {string} type |
| 3750 |
*/ |
| 3751 |
function createFile(name, size, type) { |
| 3752 |
const file = new File([], name, { type }); |
| 3753 |
Object.defineProperty(file, "size", { |
| 3754 |
get() { |
| 3755 |
return size; |
| 3756 |
}, |
| 3757 |
}); |
| 3758 |
return file; |
| 3759 |
} |
| 3760 |
|
| 3761 |
/** |
| 3762 |
* createThenable creates a Promise that can be controlled from outside its inner scope |
| 3763 |
*/ |
| 3764 |
function createThenable() { |
| 3765 |
let done, cancel; |
| 3766 |
|
| 3767 |
const promise = new Promise((resolve, reject) => { |
| 3768 |
done = resolve; |
| 3769 |
cancel = reject; |
| 3770 |
}); |
| 3771 |
|
| 3772 |
return { |
| 3773 |
promise, |
| 3774 |
done, |
| 3775 |
cancel, |
| 3776 |
}; |
| 3777 |
} |