From 45376f08ea5e7599736806062e639fe9c58c6013 Mon Sep 17 00:00:00 2001 From: Jan Siegel Date: Fri, 5 Apr 2019 17:52:30 +0200 Subject: [PATCH] fix(core): fix casting symbol values to strings in the prop type check warning - fix the TypeError being thrown instead of a prop type check warning in case of passing a symbol as the the prop's default value - add prop type check warning test cases for symbols --- src/core/util/props.js | 13 ++++++---- test/unit/features/options/props.spec.js | 30 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/core/util/props.js b/src/core/util/props.js index 1b57f77cdb5..5255710d69a 100644 --- a/src/core/util/props.js +++ b/src/core/util/props.js @@ -210,7 +210,8 @@ function getInvalidTypeMessage (name, value, expectedTypes) { // check if we need to specify expected value if (expectedTypes.length === 1 && isExplicable(expectedType) && - !isBoolean(expectedType, receivedType)) { + !isBoolean(expectedType, receivedType) && + !isSymbol(expectedType, receivedType)) { message += ` with value ${expectedValue}` } message += `, got ${receivedType} ` @@ -223,11 +224,11 @@ function getInvalidTypeMessage (name, value, expectedTypes) { function styleValue (value, type) { if (type === 'String') { - return `"${value}"` + return `"${String(value)}"` } else if (type === 'Number') { - return `${Number(value)}` + return `${Number(String(value))}` } else { - return `${value}` + return String(value) } } @@ -239,3 +240,7 @@ function isExplicable (value) { function isBoolean (...args) { return args.some(elem => elem.toLowerCase() === 'boolean') } + +function isSymbol (...args) { + return args.some(elem => elem.toLowerCase() === 'symbol') +} diff --git a/test/unit/features/options/props.spec.js b/test/unit/features/options/props.spec.js index 5adedeb2e6e..bd9f4af5861 100644 --- a/test/unit/features/options/props.spec.js +++ b/test/unit/features/options/props.spec.js @@ -241,6 +241,36 @@ describe('Options props', () => { makeInstance({}, Symbol) expect('Expected Symbol, got Object').toHaveBeenWarned() }) + + it('string & symbol', () => { + makeInstance(Symbol('foo'), String) + expect('Expected String, got Symbol').toHaveBeenWarned() + }) + + it('number & symbol', () => { + makeInstance(Symbol('foo'), Number) + expect('Expected Number, got Symbol').toHaveBeenWarned() + }) + + it('boolean & symbol', () => { + makeInstance(Symbol('foo'), Boolean) + expect('Expected Boolean, got Symbol').toHaveBeenWarned() + }) + + it('function & symbol', () => { + makeInstance(Symbol('foo'), Function) + expect('Expected Function, got Symbol').toHaveBeenWarned() + }) + + it('object & symbol', () => { + makeInstance(Symbol('foo'), Object) + expect('Expected Object, got Symbol').toHaveBeenWarned() + }) + + it('array & symbol', () => { + makeInstance(Symbol('foo'), Array) + expect('Expected Array, got Symbol').toHaveBeenWarned() + }) } it('custom constructor', () => {