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

feat: fire event when restriction-failed #1436

Merged
merged 2 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/@uppy/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ class Uppy {
try {
this._checkRestrictions(newFile)
} catch (err) {
this.emit('restriction-failed', newFile, err)
onError(err)
}

Expand Down
24 changes: 24 additions & 0 deletions packages/@uppy/core/src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs')
const path = require('path')
const prettyBytes = require('prettier-bytes')
const Core = require('./index')
const Plugin = require('./Plugin')
const AcquirerPlugin1 = require('../../../../test/mocks/acquirerPlugin1')
Expand Down Expand Up @@ -1270,6 +1271,29 @@ describe('src/Core', () => {
expect(core.getState().info.message).toEqual('This file exceeds maximum allowed size of 1.2 KB')
}
})

it('should emit `restriction-failed` event when some rule is violated', () => {
const maxFileSize = 100
const core = new Core({
restrictions: {
maxFileSize
}
})
const restrictionsViolatedEventMock = jest.fn()
const file = {
name: 'test.jpg',
data: new Blob([Buffer.alloc(2 * maxFileSize)])
}
const errorMessage = `${core.i18n('exceedsSize')} ${prettyBytes(maxFileSize)}`
try {
core.on('restriction-failed', restrictionsViolatedEventMock)
core.addFile(file)
} catch (err) {}

expect(restrictionsViolatedEventMock.mock.calls.length).toEqual(1)
expect(restrictionsViolatedEventMock.mock.calls[0][0].name).toEqual(file.name)
expect(restrictionsViolatedEventMock.mock.calls[0][1].message).toEqual(errorMessage)
})
})

describe('actions', () => {
Expand Down
10 changes: 10 additions & 0 deletions website/src/docs/uppy.md
Original file line number Diff line number Diff line change
Expand Up @@ -642,3 +642,13 @@ Fired when “info” message should be hidden in the UI. See [`info-visible`](#
### `cancel-all`

Fired when [`uppy.cancelAll()`]() is called, all uploads are canceled, files removed and progress is reset.

### `restriction-failed`

Fired when a file violates certain restrictions when added. This event is just providing another choice for those who want to customize the behavior of file upload restrictions.

```javascript
uppy.on('restriction-failed', (file, error) => {
// do some customized logic like showing system notice to users
})
```