|
| 1 | +import React from 'react'; |
| 2 | +import { render, waitFor, fireEvent, vi } from '@test/utils'; |
| 3 | +import Steps from '../Steps'; |
| 4 | +import StepItem from '../StepItem'; |
| 5 | +import StepsContext from '../StepsContext'; |
| 6 | + |
| 7 | +const TEST_ROOT_ID = 'step-test-root-extra'; |
| 8 | + |
| 9 | +const defaultOptions = [ |
| 10 | + { title: '0', content: '这里', value: 0 }, |
| 11 | + { title: '1', content: '这里', value: 1 }, |
| 12 | + { title: '2', content: '这里', value: 2 }, |
| 13 | +]; |
| 14 | + |
| 15 | +describe('Steps extra branches', () => { |
| 16 | + test("current='FINISH' -> all items finish (options)", async () => { |
| 17 | + const { getByTestId } = render( |
| 18 | + <div data-testid={TEST_ROOT_ID}> |
| 19 | + <Steps current={'FINISH'} options={defaultOptions} /> |
| 20 | + </div>, |
| 21 | + ); |
| 22 | + const root = await waitFor(() => getByTestId(TEST_ROOT_ID)); |
| 23 | + const finishes = root.querySelectorAll('.t-steps-item--finish'); |
| 24 | + expect(finishes.length).toBe(defaultOptions.length); |
| 25 | + }); |
| 26 | + |
| 27 | + test('item.status override (error) renders error class', async () => { |
| 28 | + const opts = [ |
| 29 | + { title: 'a', content: 'a', value: 0, status: 'error' as any }, |
| 30 | + { title: 'b', content: 'b', value: 1 }, |
| 31 | + ]; |
| 32 | + const { getByTestId } = render( |
| 33 | + <div data-testid={TEST_ROOT_ID}> |
| 34 | + <Steps current={0} options={opts} /> |
| 35 | + </div>, |
| 36 | + ); |
| 37 | + const root = await waitFor(() => getByTestId(TEST_ROOT_ID)); |
| 38 | + const errorItem = root.querySelector('.t-steps-item--error'); |
| 39 | + expect(errorItem).not.toBeNull(); |
| 40 | + }); |
| 41 | + |
| 42 | + test('current value not exist triggers console.warn and defaults to wait', async () => { |
| 43 | + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined); |
| 44 | + const opts = [ |
| 45 | + { title: 'a', content: 'a', value: 'a' }, |
| 46 | + { title: 'b', content: 'b', value: 'b' }, |
| 47 | + ]; |
| 48 | + const { getByTestId } = render( |
| 49 | + <div data-testid={TEST_ROOT_ID}> |
| 50 | + <Steps current={'not-exist'} options={opts as any} /> |
| 51 | + </div>, |
| 52 | + ); |
| 53 | + const root = await waitFor(() => getByTestId(TEST_ROOT_ID)); |
| 54 | + expect(warnSpy).toHaveBeenCalledWith('TDesign Steps Warn: The current `value` is not exist.'); |
| 55 | + const waitItems = root.querySelectorAll('.t-steps-item--wait'); |
| 56 | + // both should be in default/wait state |
| 57 | + expect(waitItems.length).toBe(opts.length); |
| 58 | + warnSpy.mockRestore(); |
| 59 | + }); |
| 60 | + |
| 61 | + test('status=process prevents click triggering onChange', async () => { |
| 62 | + const onChange = vi.fn(); |
| 63 | + const { getByTestId } = render( |
| 64 | + <div data-testid={TEST_ROOT_ID}> |
| 65 | + <StepsContext.Provider value={{ current: 0, theme: 'default', readonly: false, onChange }}> |
| 66 | + <StepItem title="t" index={0} status={'process'} /> |
| 67 | + </StepsContext.Provider> |
| 68 | + </div>, |
| 69 | + ); |
| 70 | + const root = await waitFor(() => getByTestId(TEST_ROOT_ID)); |
| 71 | + const inner = root.querySelector('.t-steps-item__inner'); |
| 72 | + fireEvent.click(inner); |
| 73 | + expect(onChange).not.toHaveBeenCalled(); |
| 74 | + }); |
| 75 | + |
| 76 | + test('icon=false results in no icon content', async () => { |
| 77 | + const { getByTestId } = render( |
| 78 | + <div data-testid={TEST_ROOT_ID}> |
| 79 | + <StepsContext.Provider value={{ current: 0, theme: 'default', readonly: false, onChange: null }}> |
| 80 | + <StepItem title="t" index={0} status={'default'} icon={false as any} /> |
| 81 | + </StepsContext.Provider> |
| 82 | + </div>, |
| 83 | + ); |
| 84 | + const root = await waitFor(() => getByTestId(TEST_ROOT_ID)); |
| 85 | + const icon = root.querySelector('.t-steps-item__icon'); |
| 86 | + // icon container exists but should have no child nodes when icon=false |
| 87 | + expect(icon).not.toBeNull(); |
| 88 | + expect(icon.childNodes.length).toBe(0); |
| 89 | + }); |
| 90 | +}); |
0 commit comments