Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix: make sure changes to maxFiles prop are reflected in behaviour #1026

Merged
merged 2 commits into from Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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