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: ensure totype always return stringified null when null passed #30383

Merged
merged 3 commits into from Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 7 additions & 1 deletion js/src/util/index.js
Expand Up @@ -10,7 +10,13 @@ const MILLISECONDS_MULTIPLIER = 1000
const TRANSITION_END = 'transitionend'

// Shoutout AngusCroll (https://goo.gl/pxwQGp)
const toType = obj => ({}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase())
const toType = obj => {
if (obj === null || obj === undefined) {
return `${obj}`
}

return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase()
}

/**
* --------------------------------------------------------------------------
Expand Down
31 changes: 30 additions & 1 deletion js/tests/unit/util/index.spec.js
Expand Up @@ -198,8 +198,9 @@ describe('Util', () => {
})

describe('typeCheckConfig', () => {
const namePlugin = 'collapse'

it('should check type of the config object', () => {
const namePlugin = 'collapse'
const defaultType = {
toggle: 'boolean',
parent: '(string|element)'
Expand All @@ -213,6 +214,34 @@ describe('Util', () => {
Util.typeCheckConfig(namePlugin, config, defaultType)
}).toThrow(new Error('COLLAPSE: Option "parent" provided type "number" but expected type "(string|element)".'))
})

it('should return null stringified when null is passed', () => {
const defaultType = {
toggle: 'boolean',
parent: '(null|element)'
}
const config = {
toggle: true,
parent: null
}

Util.typeCheckConfig(namePlugin, config, defaultType)
expect().nothing()
})

it('should return undefined stringified when undefined is passed', () => {
const defaultType = {
toggle: 'boolean',
parent: '(undefined|element)'
}
const config = {
toggle: true,
parent: undefined
}

Util.typeCheckConfig(namePlugin, config, defaultType)
expect().nothing()
})
})

describe('makeArray', () => {
Expand Down