Skip to content

Commit

Permalink
fix: update internal state when maxFiles prop changes and close #1025
Browse files Browse the repository at this point in the history
  • Loading branch information
ath92 committed Oct 30, 2020
1 parent b4a1ac6 commit bb42b94
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.js
Expand Up @@ -657,6 +657,7 @@ export function useDropzone(options = {}) {
accept,
minSize,
maxSize,
maxFiles,
getFilesFromEvent,
onDrop,
onDropAccepted,
Expand Down
30 changes: 30 additions & 0 deletions src/index.spec.js
Expand Up @@ -2137,6 +2137,36 @@ describe('useDropzone() hook', () => {
], expect.anything())
})

it('rejects all files if {multiple} is true and maxFiles has been updated so that it is less than files', async () => {
const onDropSpy = jest.fn()
const onDropRejectedSpy = jest.fn()
const ui = (maxFiles) => (
<Dropzone accept="image/*" onDrop={onDropSpy} multiple={true} maxFiles={maxFiles} onDropRejected={onDropRejectedSpy}>
{({ getRootProps, getInputProps }) => (
<div {...getRootProps()}>
<input {...getInputProps()} />
</div>
)}
</Dropzone>
)
const { container, rerender } = render(ui(3))
const dropzone = container.querySelector('div')

fireDrop(dropzone, createDtWithFiles(images))
await flushPromises(rerender, ui(3))
expect(onDropRejectedSpy).not.toHaveBeenCalled()
expect(onDropSpy).toHaveBeenCalledWith(images, [], expect.anything())

rerender(ui(1));

fireDrop(dropzone, createDtWithFiles(images))
await flushPromises(rerender, ui(1))
expect(onDropRejectedSpy).toHaveBeenCalledWith(
expect.arrayContaining(images.map((image) => expect.objectContaining({ errors: expect.any(Array), file: image }))),
expect.anything()
)
})

it('accepts multiple files if {multiple} is true and {accept} criteria is met', async () => {
const onDropSpy = jest.fn()
const onDropRejectedSpy = jest.fn()
Expand Down

0 comments on commit bb42b94

Please sign in to comment.