Skip to content
Permalink
Browse files

fix: pass isToast to removePopupAndResetState() (#1585)

It's too late to run dom.isToast inside removePopupAndResetState()
  • Loading branch information...
limonte committed May 11, 2019
1 parent a43ba27 commit 53f1047d6dd20712b563a1cc67f0bca332bc7cc6
Showing with 31 additions and 35 deletions.
  1. +8 −8 src/instanceMethods/close.js
  2. +23 −27 test/qunit/toast.js
@@ -11,13 +11,13 @@ import privateMethods from '../privateMethods.js'
* Instance method to close sweetAlert
*/

function removePopupAndResetState (container, onAfterClose) {
if (!dom.isToast()) {
function removePopupAndResetState (container, isToast, onAfterClose) {
if (isToast) {
triggerOnAfterClose(onAfterClose)
} else {
restoreActiveElement().then(() => triggerOnAfterClose(onAfterClose))
globalState.keydownTarget.removeEventListener('keydown', globalState.keydownHandler, { capture: globalState.keydownListenerCapture })
globalState.keydownHandlerAdded = false
} else {
triggerOnAfterClose(onAfterClose)
}

// Unset globalState props so GC will dispose globalState (#1569)
@@ -50,10 +50,10 @@ function removeBodyClasses () {
)
}

function swalCloseEventFinished (popup, container, onAfterClose) {
function swalCloseEventFinished (popup, container, isToast, onAfterClose) {
popup.removeEventListener(dom.animationEndEvent, swalCloseEventFinished)
if (dom.hasClass(popup, swalClasses.hide)) {
removePopupAndResetState(container, onAfterClose)
removePopupAndResetState(container, isToast, onAfterClose)
}

// Unset WeakMaps so GC will be able to dispose them (#1569)
@@ -78,10 +78,10 @@ export function close (resolveValue) {

// If animation is supported, animate
if (dom.animationEndEvent && dom.hasCssAnimation(popup)) {
popup.addEventListener(dom.animationEndEvent, swalCloseEventFinished.bind(null, popup, container, onAfterClose))
popup.addEventListener(dom.animationEndEvent, swalCloseEventFinished.bind(null, popup, container, dom.isToast(), onAfterClose))
} else {
// Otherwise, remove immediately
removePopupAndResetState(container, onAfterClose)
removePopupAndResetState(container, dom.isToast(), onAfterClose)
}

if (onClose !== null && typeof onClose === 'function') {
@@ -1,11 +1,12 @@
const { $, Swal, SwalWithoutAnimation } = require('./helpers')
const { Swal } = require('./helpers')
const sinon = require('sinon/pkg/sinon')

const Toast = Swal.mixin({ toast: true })
QUnit.test('.swal2-toast-shown', (assert) => {
Swal.fire({ toast: true })
Toast.fire()
assert.ok(document.body.classList.contains('swal2-toast-shown'))
assert.ok(document.documentElement.classList.contains('swal2-toast-shown'))
Swal.fire({ toast: false })
Swal.fire()
assert.notOk(document.body.classList.contains('swal2-toast-shown'))
assert.notOk(document.documentElement.classList.contains('swal2-toast-shown'))
})
@@ -14,9 +15,8 @@ QUnit.test('should throw console warnings for incompatible parameters', (assert)
const _consoleWarn = console.warn
const spy = sinon.spy(console, 'warn')

SwalWithoutAnimation.fire({
allowOutsideClick: true,
toast: true
Toast.fire({
allowOutsideClick: true
})
assert.ok(spy.calledWith('SweetAlert2: The parameter "allowOutsideClick" is incompatible with toasts'))

@@ -26,78 +26,74 @@ QUnit.test('should throw console warnings for incompatible parameters', (assert)
QUnit.test('.swal2-toast-column if input', (assert) => {
const inputs = ['text', 'email', 'password', 'number', 'tel', 'range', 'textarea', 'select', 'radio', 'checkbox', 'file', 'url']
inputs.forEach((input) => {
SwalWithoutAnimation.fire({ toast: true, input: input })
Toast.fire({ input: input })
assert.ok(document.body.classList.contains('swal2-toast-column'))

SwalWithoutAnimation.fire({ input: input })
Swal.fire({ input: input })
assert.notOk(document.body.classList.contains('swal2-toast-column'))
})
})

QUnit.test('.swal2-toast-column if footer', (assert) => {
SwalWithoutAnimation.fire({ toast: true, footer: 'footer' })
Toast.fire({ footer: 'footer' })
assert.ok(document.body.classList.contains('swal2-toast-column'))

SwalWithoutAnimation.fire({ footer: 'footer' })
Swal.fire({ footer: 'footer' })
assert.notOk(document.body.classList.contains('swal2-toast-column'))
})

QUnit.test('.swal2-toast-column if close button', (assert) => {
SwalWithoutAnimation.fire({ toast: true, showCloseButton: true })
Toast.fire({ showCloseButton: true })
assert.ok(document.body.classList.contains('swal2-toast-column'))

SwalWithoutAnimation.fire({ showCloseButton: true })
Swal.fire({ showCloseButton: true })
assert.notOk(document.body.classList.contains('swal2-toast-column'))
})

QUnit.test('toast click closes when no buttons or input are specified', (assert) => {
const done = assert.async()

SwalWithoutAnimation.fire({
title: 'toast click close',
toast: true,
Toast.fire({
showConfirmButton: false
}).then((result) => {
assert.deepEqual(result, { dismiss: Swal.DismissReason.close })
assert.deepEqual(result, { dismiss: Toast.DismissReason.close })
done()
})

$('.swal2-popup').click()
Toast.getPopup().click()
})

QUnit.test('toast click does not close if cancel button is present', (assert) => {
const done = assert.async()

SwalWithoutAnimation.fire({
title: 'toast no close with button present',
toast: true,
Toast.fire({
animation: false,
showConfirmButton: false,
showCancelButton: true
})

$('.swal2-popup').click()
Toast.getPopup().click()

setTimeout(() => {
assert.ok(Swal.isVisible())
assert.ok(Toast.isVisible())
done()
})
})

QUnit.test('toast click does not close if input option is specified', (assert) => {
const done = assert.async()

SwalWithoutAnimation.fire({
title: 'toast no close with input present',
toast: true,
Toast.fire({
animation: false,
showConfirmButton: false,
showCancelButton: false,
input: 'text'
})

$('.swal2-popup').click()
Toast.getPopup().click()

setTimeout(() => {
assert.ok(Swal.isVisible())
assert.ok(Toast.isVisible())
done()
})
})

0 comments on commit 53f1047

Please sign in to comment.
You can’t perform that action at this time.