-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(steps): fix some steps bugs and add test cases (#2066)
- Loading branch information
Showing
9 changed files
with
426 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { CheckOutlined } from '@gio-design/icons'; | ||
import { usePrefixCls } from '@gio-design/utils'; | ||
import classnames from 'classnames'; | ||
import React, { useMemo } from 'react'; | ||
import { StepProps } from './interface'; | ||
|
||
const Step = (props: StepProps & { stepIndex?: number; active?: number }) => { | ||
const { title, status, prefix, onClick, disabled = false, className, stepIndex = 1, active } = props; | ||
const prefixCls = usePrefixCls('steps-item'); | ||
const cls = classnames(`${prefixCls}`, className, `${prefixCls}-${status}`, { | ||
[`${prefixCls}-active`]: active, | ||
[`${prefixCls}-disabled`]: disabled, | ||
}); | ||
|
||
const stepIcon = useMemo(() => { | ||
if (!React.isValidElement(prefix) && status === 'finish') { | ||
return <CheckOutlined className={`${prefixCls}-finish-icon`} />; | ||
} | ||
return prefix; | ||
}, [prefix, status, prefixCls]); | ||
return ( | ||
<button | ||
className={cls} | ||
disabled={disabled} | ||
type="button" | ||
data-testid={`step-${stepIndex}`} | ||
value={stepIndex} | ||
tabIndex={onClick && !disabled ? 0 : undefined} | ||
onClick={() => onClick?.(stepIndex)} | ||
> | ||
<span | ||
className={classnames(`${prefixCls}-prefix`, { | ||
[`${prefixCls}-prefix-none`]: !stepIcon, | ||
[`${prefixCls}-prefix-only`]: !title, | ||
})} | ||
> | ||
{stepIcon} | ||
</span> | ||
{title} | ||
</button> | ||
); | ||
}; | ||
export default Step; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import React from 'react'; | ||
import Steps from '..'; | ||
|
||
describe('Test Steps', () => { | ||
it('should render correctly', () => { | ||
const { container } = render( | ||
<Steps> | ||
<Steps.Step title="Step 1" /> | ||
<Steps.Step title="Step 2" /> | ||
</Steps> | ||
); | ||
expect(container.querySelector('.gio-steps')).toBeInTheDocument(); | ||
}); | ||
it('should render correctly with current below zaro ', () => { | ||
const { container } = render( | ||
<Steps current={-1}> | ||
<Steps.Step title="Step 1" /> | ||
<Steps.Step title="Step 2" /> | ||
</Steps> | ||
); | ||
expect(container.querySelector('.gio-steps-item-pending')).toHaveTextContent('Step 1'); | ||
}); | ||
it('renders with prop size', () => { | ||
const { container } = render( | ||
<Steps size="small"> | ||
<Steps.Step title="Step 1" /> | ||
<Steps.Step title="Step 2" /> | ||
</Steps> | ||
); | ||
expect(container.querySelector('.gio-steps-small')).toBeInTheDocument(); | ||
}); | ||
it('can click step', () => { | ||
const stepClick = jest.fn(); | ||
const { container } = render( | ||
<Steps defaultCurrent={2}> | ||
<Steps.Step title="Step 1" onClick={stepClick} /> | ||
<Steps.Step title="Step 2" /> | ||
</Steps> | ||
); | ||
fireEvent.click(screen.getByText('Step 1')); | ||
expect(container.querySelector('.gio-steps-item-active')).toHaveTextContent('Step 1'); | ||
expect(stepClick).toHaveBeenCalledTimes(1); | ||
}); | ||
it('should change current', () => { | ||
const change = jest.fn(); | ||
const { container } = render( | ||
<Steps defaultCurrent={2} onChange={change}> | ||
<Steps.Step title="Step 1" /> | ||
<Steps.Step title="Step 2" /> | ||
</Steps> | ||
); | ||
expect(container.querySelector('.gio-steps-item-active')).toHaveTextContent('Step 2'); | ||
fireEvent.click(screen.getByText('Step 1')); | ||
expect(change).toHaveBeenCalledWith(1); | ||
expect(change).toHaveBeenCalledTimes(1); | ||
expect(container.querySelector('.gio-steps-item-active')).toHaveTextContent('Step 1'); | ||
}); | ||
|
||
it('render disabled Step', () => { | ||
const { container } = render( | ||
<Steps> | ||
<Steps.Step title="Step 1" disabled /> | ||
<Steps.Step title="Step 2" /> | ||
</Steps> | ||
); | ||
expect(container.querySelector('.gio-steps-item-disabled')).toBeInTheDocument(); | ||
expect(container.querySelector('.gio-steps-item-disabled')).not.toHaveAttribute('tabIndex'); | ||
}); | ||
|
||
it('render Step with icon', () => { | ||
const { container } = render(<Steps.Step title="Step 1" prefix={<span>1</span>} />); | ||
expect(container.querySelector('.gio-steps-item-prefix').firstChild).toHaveTextContent('1'); | ||
fireEvent.click(screen.getByText('Step 1')); | ||
}); | ||
}); |
Oops, something went wrong.
610eb51
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
gio-design – ./
gio-design-growingio.vercel.app
gio-design-git-master-growingio.vercel.app
gio-design.vercel.app