Skip to content

Commit

Permalink
BREAKING CHANGE: inputValidator and preConfirm should always resolve (#…
Browse files Browse the repository at this point in the history
…1383)

BREAKING CHANGE: inputValidator and preConfirm should always resolve (#1383)
  • Loading branch information
limonte committed Jan 18, 2019
1 parent b8c4c33 commit fc70cf9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 47 deletions.
8 changes: 2 additions & 6 deletions src/SweetAlert.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@ function SweetAlert (...args) {
}

// `catch` cannot be the name of a module export, so we define our thenable methods here instead
SweetAlert.prototype.then = function (onFulfilled, onRejected) {
SweetAlert.prototype.then = function (onFulfilled) {
const promise = privateProps.promise.get(this)
return promise.then(onFulfilled, onRejected)
}
SweetAlert.prototype.catch = function (onRejected) {
const promise = privateProps.promise.get(this)
return promise.catch(onRejected)
return promise.then(onFulfilled)
}
SweetAlert.prototype.finally = function (onFinally) {
const promise = privateProps.promise.get(this)
Expand Down
14 changes: 4 additions & 10 deletions src/instanceMethods/_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export function _main (userParams) {

const constructor = this.constructor

return new Promise((resolve, reject) => {
// functions to handle all resolving/rejecting/settling
return new Promise((resolve) => {
// functions to handle all closings/dismissals
const succeedWith = (value) => {
constructor.closePopup(innerParams.onClose, innerParams.onAfterClose) // TODO: make closePopup an *instance* method
resolve({ value })
Expand All @@ -50,10 +50,6 @@ export function _main (userParams) {
constructor.closePopup(innerParams.onClose, innerParams.onAfterClose)
resolve({ dismiss })
}
const errorWith = (error) => {
constructor.closePopup(innerParams.onClose, innerParams.onAfterClose)
reject(error)
}

// Close on timer
if (innerParams.timer) {
Expand Down Expand Up @@ -106,8 +102,7 @@ export function _main (userParams) {
} else {
succeedWith(preConfirmValue || value)
}
},
(error) => errorWith(error)
}
)
} else {
succeedWith(value)
Expand Down Expand Up @@ -141,8 +136,7 @@ export function _main (userParams) {
} else {
confirm(inputValue)
}
},
error => errorWith(error)
}
)
} else if (!this.getInput().checkValidity()) {
this.enableButtons()
Expand Down
31 changes: 0 additions & 31 deletions test/qunit/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,37 +666,6 @@ QUnit.test('confirm button', (assert) => {
Swal.clickConfirm()
})

QUnit.test('on errors in *async* user-defined functions, cleans up and propagates the error', (assert) => {
const done = assert.async()

const expectedError = new Error('my bad')
const erroringFunction = () => {
return Promise.reject(expectedError)
}

// inputValidator
const rejectedPromise = SwalWithoutAnimation.fire({ input: 'text', inputValidator: erroringFunction })
Swal.clickConfirm()
rejectedPromise.catch((error) => {
assert.equal(error, expectedError) // error is bubbled up back to user code
setTimeout(() => {
assert.notOk(Swal.isVisible()) // display is cleaned up

// preConfirm
const rejectedPromise = SwalWithoutAnimation.fire({ preConfirm: erroringFunction })
Swal.clickConfirm()
rejectedPromise.catch((error) => {
assert.equal(error, expectedError) // error is bubbled up back to user code
setTimeout(() => {
assert.notOk(Swal.isVisible()) // display is cleaned up

done()
})
})
})
})
})

QUnit.test('params validation', (assert) => {
assert.ok(Swal.isValidParameter('title'))
assert.notOk(Swal.isValidParameter('foobar'))
Expand Down

0 comments on commit fc70cf9

Please sign in to comment.