From 85086f14b6616ba5afc069dc5720fabfdf9a0e66 Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Fri, 27 Jan 2017 17:08:05 +0800 Subject: [PATCH] Using a special static property for type detecting (#4673) Related issue: #4655 --- .eslintrc.js | 2 +- components/breadcrumb/Breadcrumb.tsx | 4 ++-- components/breadcrumb/BreadcrumbItem.tsx | 2 ++ .../breadcrumb/__tests__/Breadcrumb.test.js | 19 +++++++++++++++++++ 4 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 components/breadcrumb/__tests__/Breadcrumb.test.js diff --git a/.eslintrc.js b/.eslintrc.js index ae73dba075..5a7680725d 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -3,7 +3,7 @@ const eslintrc = { env: { browser: true, node: true, - mocha: true, + jasmine: true, jest: true, es6: true, }, diff --git a/components/breadcrumb/Breadcrumb.tsx b/components/breadcrumb/Breadcrumb.tsx index ef1e5a64dc..e19e72feca 100755 --- a/components/breadcrumb/Breadcrumb.tsx +++ b/components/breadcrumb/Breadcrumb.tsx @@ -89,8 +89,8 @@ export default class Breadcrumb extends React.Component { } else if (children) { crumbs = React.Children.map(children, (element: any, index) => { warning( - element.type === BreadcrumbItem, - ' `BreadcrumbItem` is required as the wrapper of children element. ' + element && element.type.__ANT_BREADCRUMB_ITEM, + 'Breadcrumb only accetps Breadcrumb.Item as it\'s children' ); return cloneElement(element, { separator, diff --git a/components/breadcrumb/BreadcrumbItem.tsx b/components/breadcrumb/BreadcrumbItem.tsx index 02b4c4f0df..b1ec47ebd8 100644 --- a/components/breadcrumb/BreadcrumbItem.tsx +++ b/components/breadcrumb/BreadcrumbItem.tsx @@ -8,6 +8,8 @@ export interface BreadcrumbItemProps { } export default class BreadcrumbItem extends React.Component { + static __ANT_BREADCRUMB_ITEM = true; + static defaultProps = { prefixCls: 'ant-breadcrumb', separator: '/', diff --git a/components/breadcrumb/__tests__/Breadcrumb.test.js b/components/breadcrumb/__tests__/Breadcrumb.test.js new file mode 100644 index 0000000000..a1e48d54ce --- /dev/null +++ b/components/breadcrumb/__tests__/Breadcrumb.test.js @@ -0,0 +1,19 @@ +import React from 'react'; +import { mount } from 'enzyme'; +import Breadcrumb from '../Breadcrumb'; + +describe('Breadcrumb', () => { + it('warns on non-Breadcrumb.Item children', () => { + const MyCom = () =>
foo
; + spyOn(console, 'error'); + mount( + + + + ); + expect(console.error.calls.count()).toBe(1); + expect(console.error.calls.argsFor(0)[0]).toContain( + 'Breadcrumb only accetps Breadcrumb.Item as it\'s children' + ); + }); +});