Skip to content

Commit

Permalink
test: add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Apr 27, 2023
1 parent 4db9ad1 commit 52f7978
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 36 deletions.
36 changes: 9 additions & 27 deletions components/breadcrumb/Breadcrumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ import useStyle from './style';
import useItemRender from './useItemRender';
import useItems from './useItems';

/** @deprecated New of Breadcrumb using `items` instead of `routes` */
export interface Route {
path: string;
breadcrumbName: string;
children?: Omit<Route, 'children'>[];
}

export interface BreadcrumbItemType {
key?: React.Key;
/**
Expand All @@ -30,7 +23,9 @@ export interface BreadcrumbItemType {
* Different with `href`. It will concat all prev `path` to the current one.
*/
path?: string;
title: React.ReactNode;
title?: React.ReactNode;
/* @deprecated Please use `title` instead */
breadcrumbName?: string;
menu?: BreadcrumbItemProps['menu'];
/** @deprecated Please use `menu` instead */
overlay?: React.ReactNode;
Expand All @@ -42,29 +37,23 @@ export interface BreadcrumbSeparatorType {
separator?: React.ReactNode;
}

export type ItemType = BreadcrumbItemType | BreadcrumbSeparatorType;
export type ItemType = Partial<BreadcrumbItemType & BreadcrumbSeparatorType>;

export type InternalRouteType = Partial<BreadcrumbItemType & BreadcrumbSeparatorType>;

export interface BaseBreadcrumbProps {
export interface BreadcrumbProps {
prefixCls?: string;
params?: any;
separator?: React.ReactNode;
style?: React.CSSProperties;
className?: string;
rootClassName?: string;
children?: React.ReactNode;
}

export interface LegacyBreadcrumbProps extends BaseBreadcrumbProps {
/** @deprecated Please use `items` instead */
routes: Route[];

itemRender?: (route: Route, params: any, routes: Route[], paths: string[]) => React.ReactNode;
}
routes?: ItemType[];

export interface NewBreadcrumbProps extends BaseBreadcrumbProps {
items: ItemType[];
items?: ItemType[];

itemRender?: (
route: ItemType,
Expand All @@ -74,8 +63,6 @@ export interface NewBreadcrumbProps extends BaseBreadcrumbProps {
) => React.ReactNode;
}

export type BreadcrumbProps = BaseBreadcrumbProps | LegacyBreadcrumbProps | NewBreadcrumbProps;

const getPath = (params: any, path?: string) => {
if (path === undefined) {
return path;
Expand All @@ -88,12 +75,7 @@ const getPath = (params: any, path?: string) => {
return mergedPath;
};

type CompoundedComponent = React.FC<BreadcrumbProps> & {
Item: typeof BreadcrumbItem;
Separator: typeof BreadcrumbSeparator;
};

const Breadcrumb: CompoundedComponent = (props) => {
const Breadcrumb = (props: BreadcrumbProps) => {
const {
prefixCls: customizePrefixCls,
separator = '/',
Expand All @@ -106,7 +88,7 @@ const Breadcrumb: CompoundedComponent = (props) => {
itemRender,
params = {},
...restProps
} = props as LegacyBreadcrumbProps & NewBreadcrumbProps;
} = props;

const { getPrefixCls, direction } = React.useContext(ConfigContext);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Breadcrumb.ItemRender render as expect 1`] = `
<div>
<nav
class="ant-breadcrumb"
>
<ol>
<li>
<a
class="my-link"
data-path="/"
>
Home
</a>
</li>
<li
aria-hidden="true"
class="ant-breadcrumb-separator"
>
/
</li>
<li>
<a
class="my-link"
data-path="/bamboo"
>
Bamboo
</a>
</li>
</ol>
</nav>
</div>
`;
29 changes: 29 additions & 0 deletions components/breadcrumb/__tests__/itemRender.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import { render } from '../../../tests/utils';
import Breadcrumb from '../index';

describe('Breadcrumb.ItemRender', () => {
it('render as expect', () => {
const { container } = render(
<Breadcrumb
items={[
{
path: '/',
title: 'Home',
},
{
path: '/bamboo',
title: 'Bamboo',
},
]}
itemRender={(route) => (
<a className="my-link" data-path={route.path}>
{route.title}
</a>
)}
/>,
);

expect(container).toMatchSnapshot();
});
});
12 changes: 3 additions & 9 deletions components/breadcrumb/useItemRender.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import classNames from 'classnames';
import omit from 'rc-util/lib/omit';
import * as React from 'react';
import type {
BreadcrumbItemType,
BreadcrumbSeparatorType,
InternalRouteType,
ItemType,
NewBreadcrumbProps,
} from './Breadcrumb';
import type { BreadcrumbProps, InternalRouteType, ItemType } from './Breadcrumb';

type AddParameters<TFunction extends (...args: any) => any, TParameters extends [...args: any]> = (
...args: [...Parameters<TFunction>, ...TParameters]
) => ReturnType<TFunction>;

type ItemRender = NonNullable<NewBreadcrumbProps['itemRender']>;
type ItemRender = NonNullable<BreadcrumbProps['itemRender']>;
type InternalItemRenderParams = AddParameters<ItemRender, [href?: string]>;

function getBreadcrumbName(route: InternalRouteType, params: any) {
Expand All @@ -39,7 +33,7 @@ export function renderItem(
return null;
}

const { className, ...passedProps } = omit(item as BreadcrumbItemType & BreadcrumbSeparatorType, [
const { className, ...passedProps } = omit(item, [
'title',
'type',
'separator',
Expand Down

0 comments on commit 52f7978

Please sign in to comment.