From 1f9da2870d4c151d2108ba56bc8f6c5cdde0afb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B9=90=E4=BB=AA?= Date: Sat, 1 Apr 2017 16:51:01 +0800 Subject: [PATCH 1/2] fix Select[multipie] with OptGroup will cause error. --- src/util.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/util.js b/src/util.js index e1f98b20a..e5116c29c 100644 --- a/src/util.js +++ b/src/util.js @@ -8,6 +8,9 @@ export function getValuePropValue(child) { if (child.key) { return child.key; } + if (child.type && child.type.isSelectOptGroup && props.label) { + return props.label; + } throw new Error(`no key or value for ${child}`); } From cd045464d9bb3bdfa8bf4a0be20499219aadc9e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B9=90=E4=BB=AA?= Date: Thu, 6 Apr 2017 00:31:34 +0800 Subject: [PATCH 2/2] add test --- src/util.js | 2 +- tests/Select.multiple.spec.js | 18 ++++++++++++++++++ tests/util.spec.js | 26 +++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/util.js b/src/util.js index e5116c29c..449938d1d 100644 --- a/src/util.js +++ b/src/util.js @@ -11,7 +11,7 @@ export function getValuePropValue(child) { if (child.type && child.type.isSelectOptGroup && props.label) { return props.label; } - throw new Error(`no key or value for ${child}`); + throw new Error(`no key or value or label(only for OptGroup) for ${child}`); } export function getPropValue(child, prop) { diff --git a/tests/Select.multiple.spec.js b/tests/Select.multiple.spec.js index c665bf109..1641ab3a9 100644 --- a/tests/Select.multiple.spec.js +++ b/tests/Select.multiple.spec.js @@ -67,4 +67,22 @@ describe('Select.multiple', () => { expect(handleFocus).toBeCalled(); }); }); + + it('OptGroup without key', () => { + expect(() => { + mount( + , + ); + }).not.toThrow(); + }); }); diff --git a/tests/util.spec.js b/tests/util.spec.js index 1c432a36c..5518141d0 100644 --- a/tests/util.spec.js +++ b/tests/util.spec.js @@ -1,5 +1,5 @@ /* eslint-disable no-undef */ -import { includesSeparators, splitBySeparators } from '../src/util'; +import { includesSeparators, splitBySeparators, getValuePropValue } from '../src/util'; describe('includesSeparators', () => { const separators = [' ', ',']; @@ -38,3 +38,27 @@ describe('splitBySeparators', () => { expect(splitBySeparators(string, separators)).toEqual([]); }); }); + +describe('getValuePropValue', () => { + const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + + afterEach(() => { + errorSpy.mockReset(); + }); + + afterAll(() => { + errorSpy.mockRestore(); + }); + + it('return label when type is isSelectOptGroup', () => { + expect(getValuePropValue({ + props: { + label: 'Manager', + }, + type: { + isSelectOptGroup: true, + }, + })).toBe('Manager'); + expect(errorSpy).not.toHaveBeenCalled(); + }); +});