From c5f2fa91cb1bf338a6329c1a61a9ca52abd833dd Mon Sep 17 00:00:00 2001 From: Johann-S Date: Wed, 11 Mar 2020 20:55:20 +0100 Subject: [PATCH] fix: ensure totype always return stringified null when null passed --- js/src/util/index.js | 8 +++++++- js/tests/unit/util/index.spec.js | 17 ++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/js/src/util/index.js b/js/src/util/index.js index 8a5ae21566a4..c0219b9d0a9c 100644 --- a/js/src/util/index.js +++ b/js/src/util/index.js @@ -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) { + return `${obj}` + } + + return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase() +} /** * -------------------------------------------------------------------------- diff --git a/js/tests/unit/util/index.spec.js b/js/tests/unit/util/index.spec.js index 42c273f060bd..d2b3a4578fb4 100644 --- a/js/tests/unit/util/index.spec.js +++ b/js/tests/unit/util/index.spec.js @@ -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)' @@ -213,6 +214,20 @@ 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 to string when null passed', () => { + const defaultType = { + toggle: 'boolean', + parent: '(null|element)' + } + const config = { + toggle: true, + parent: null + } + + Util.typeCheckConfig(namePlugin, config, defaultType) + expect().nothing() + }) }) describe('makeArray', () => {