From 335f821aeccc92df14225f784bed9aa28ccf9527 Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Fri, 28 Sep 2018 19:31:17 +0800 Subject: [PATCH] fix: warning on conflicting theme of Icon fix #12441 --- components/icon/__tests__/index.test.js | 23 +++++++++++++++++++++++ components/icon/index.tsx | 8 ++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/components/icon/__tests__/index.test.js b/components/icon/__tests__/index.test.js index 13d872337d..5e677205cb 100644 --- a/components/icon/__tests__/index.test.js +++ b/components/icon/__tests__/index.test.js @@ -102,6 +102,29 @@ describe('Icon', () => { }).not.toThrow(); }); + describe('warning on conflicting theme', () => { + let errorSpy; + beforeEach(() => { + errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + }); + + afterEach(() => { + errorSpy.mockRestore(); + }); + + it('does not warn', () => { + mount(); + expect(errorSpy).not.toBeCalled(); + }); + + it('warns', () => { + mount(); + expect(errorSpy).toBeCalledWith( + "Warning: The icon name 'clock-circle-o' already specify a theme 'outlined', the 'theme' prop 'filled' will be ignored." + ); + }); + }); + describe('`component` prop', () => { it('can access to svg defs if has children', () => { const wrapper = render( diff --git a/components/icon/index.tsx b/components/icon/index.tsx index 09c41664c2..b773619366 100755 --- a/components/icon/index.tsx +++ b/components/icon/index.tsx @@ -121,10 +121,10 @@ const Icon: React.SFC = (props) => { if (typeof type === 'string') { let computedType = type; if (theme) { - const alreadyHaveTheme = getThemeFromTypeName(type); - warning(!alreadyHaveTheme, - `This icon already has a theme '${alreadyHaveTheme}'.` + - ` The prop 'theme' ${theme} will be ignored.`); + const themeInName = getThemeFromTypeName(type); + warning(!themeInName || theme === themeInName, + `The icon name '${type}' already specify a theme '${themeInName}',` + + ` the 'theme' prop '${theme}' will be ignored.`); } computedType = withThemeSuffix( removeTypeTheme(type),