Skip to content

Commit

Permalink
fix(inputValue): warn about invalid inputValue
Browse files Browse the repository at this point in the history
  • Loading branch information
limonte committed Sep 23, 2018
1 parent 45d31b9 commit f3e067c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
8 changes: 6 additions & 2 deletions src/instanceMethods/_main.js
Expand Up @@ -2,7 +2,7 @@ import defaultParams, { showWarningsForParams } from '../utils/params'
import * as dom from '../utils/dom/index'
import { swalClasses } from '../utils/classes'
import Timer from '../utils/Timer'
import { formatInputOptions, error, callIfFunction, isThenable } from '../utils/utils'
import { formatInputOptions, error, warn, callIfFunction, isThenable } from '../utils/utils'
import setParameters from '../utils/setParameters'
import globalState from '../globalState'
import { openPopup } from '../utils/openPopup'
Expand Down Expand Up @@ -417,7 +417,11 @@ export function _main (userParams) {
case 'tel':
case 'url': {
input = dom.getChildByClass(domCache.content, swalClasses.input)
input.value = innerParams.inputValue
if (typeof innerParams.inputValue === 'string' || typeof innerParams.inputValue === 'number') {
input.value = innerParams.inputValue
} else {
warn(`Unexpected type of inputValue! Expected "string" or "number", got "${typeof innerParams.inputValue}"`)
}
input.placeholder = innerParams.inputPlaceholder
input.type = innerParams.input
dom.show(input)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/utils.js
Expand Up @@ -79,4 +79,4 @@ export const warnOnce = (message) => {
*/
export const callIfFunction = (arg) => typeof arg === 'function' ? arg() : arg

export const isThenable = (arg) => typeof arg === 'object' && typeof arg.then === 'function'
export const isThenable = (arg) => arg && typeof arg === 'object' && typeof arg.then === 'function'
37 changes: 37 additions & 0 deletions test/qunit/params/inputValue.js
@@ -0,0 +1,37 @@
const { Swal, SwalWithoutAnimation, TIMEOUT } = require('../helpers')
const sinon = require('sinon/pkg/sinon')

QUnit.test('inputValue number', (assert) => {
Swal({ input: 'text', inputValue: 333 })
assert.ok(Swal.getInput().value, '333')
})

QUnit.test('inputValue as a Promise', (assert) => {
const inputTypes = ['text', 'email', 'number', 'tel', 'textarea']
const done = assert.async(inputTypes.length)
const value = '1.1 input value'
const inputValue = new Promise((resolve) => {
resolve('1.1 input value')
})
inputTypes.forEach(input => {
SwalWithoutAnimation({
input,
inputValue,
onOpen: (modal) => {
setTimeout(() => {
const inputEl = input === 'textarea' ? modal.querySelector('.swal2-textarea') : modal.querySelector('.swal2-input')
assert.equal(inputEl.value, input === 'number' ? parseFloat(value) : value)
done()
}, TIMEOUT)
}
})
})
})

QUnit.test('should throw console warning about unexpected type of inputValue', (assert) => {
const _consoleWarn = console.warn
const spy = sinon.spy(console, 'warn')
Swal({ input: 'text', inputValue: undefined })
console.warn = _consoleWarn
assert.ok(spy.calledWith('SweetAlert2: Unexpected type of inputValue! Expected "string" or "number", got "undefined"'))
})
22 changes: 0 additions & 22 deletions test/qunit/tests.js
Expand Up @@ -790,25 +790,3 @@ QUnit.test('Custom content', (assert) => {
done()
})
})

QUnit.test('inputValue as a Promise', (assert) => {
const inputTypes = ['text', 'email', 'number', 'tel', 'textarea']
const done = assert.async(inputTypes.length)
const value = '1.1 input value'
const inputValue = new Promise((resolve) => {
resolve('1.1 input value')
})
inputTypes.forEach(input => {
SwalWithoutAnimation({
input,
inputValue,
onOpen: (modal) => {
setTimeout(() => {
const inputEl = input === 'textarea' ? modal.querySelector('.swal2-textarea') : modal.querySelector('.swal2-input')
assert.equal(inputEl.value, input === 'number' ? parseFloat(value) : value)
done()
}, TIMEOUT)
}
})
})
})

0 comments on commit f3e067c

Please sign in to comment.