|
1 | 1 | import React, { useState } from 'react' |
2 | | -import { fireEvent, render, testA11y, waitFor } from 'testing' |
3 | | -import Cascader from '../' |
| 2 | +import { fireEvent, render, screen, testA11y, waitFor } from 'testing' |
| 3 | +import Cascader, { CascaderValue } from '../' |
4 | 4 | import { options } from '../demos/data' |
5 | 5 |
|
6 | 6 | describe('Cascader', () => { |
@@ -52,6 +52,95 @@ describe('Cascader', () => { |
52 | 52 | expect(onConfirm.mock.calls[0][0]).toEqual(['浙江', '杭州']) |
53 | 53 | }) |
54 | 54 |
|
| 55 | + test('should sync value when visible and external value changes', async () => { |
| 56 | + const App = () => { |
| 57 | + const [visible, setVisible] = useState(false) |
| 58 | + const [value, setValue] = useState<CascaderValue[]>([]) |
| 59 | + |
| 60 | + return ( |
| 61 | + <> |
| 62 | + <button onClick={() => setVisible(true)}>Open</button> |
| 63 | + <button onClick={() => setValue(['安徽', '合肥'])}>Set Value</button> |
| 64 | + <Cascader |
| 65 | + options={options} |
| 66 | + visible={visible} |
| 67 | + value={value} |
| 68 | + onConfirm={val => { |
| 69 | + setValue(val) |
| 70 | + setVisible(false) |
| 71 | + }} |
| 72 | + onClose={() => setVisible(false)} |
| 73 | + onCancel={() => setVisible(false)} |
| 74 | + /> |
| 75 | + </> |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + render(<App />) |
| 80 | + |
| 81 | + fireEvent.click(screen.getByText('Open')) |
| 82 | + await waitFor(() => { |
| 83 | + expect(screen.getByText('浙江')).toBeInTheDocument() |
| 84 | + }) |
| 85 | + |
| 86 | + // While the popup is open, change the external value |
| 87 | + fireEvent.click(screen.getByText('Set Value')) |
| 88 | + |
| 89 | + // The cascader should reflect the new value (合肥 should appear in both tab and list) |
| 90 | + await waitFor(() => { |
| 91 | + const matches = screen.getAllByText('合肥') |
| 92 | + expect(matches.length).toBeGreaterThanOrEqual(1) |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + test('should preserve draft selection when parent re-renders with same value', async () => { |
| 97 | + const App = () => { |
| 98 | + const [visible, setVisible] = useState(false) |
| 99 | + const [value, setValue] = useState<CascaderValue[]>([]) |
| 100 | + const [, setTick] = useState(0) |
| 101 | + |
| 102 | + return ( |
| 103 | + <> |
| 104 | + <button onClick={() => setVisible(true)}>Open</button> |
| 105 | + {/* Trigger a parent re-render without changing value content */} |
| 106 | + <button onClick={() => setTick(t => t + 1)}>Rerender</button> |
| 107 | + <Cascader |
| 108 | + options={options} |
| 109 | + visible={visible} |
| 110 | + value={value} |
| 111 | + onConfirm={val => { |
| 112 | + setValue(val) |
| 113 | + setVisible(false) |
| 114 | + }} |
| 115 | + onClose={() => setVisible(false)} |
| 116 | + onCancel={() => setVisible(false)} |
| 117 | + /> |
| 118 | + </> |
| 119 | + ) |
| 120 | + } |
| 121 | + |
| 122 | + render(<App />) |
| 123 | + |
| 124 | + fireEvent.click(screen.getByText('Open')) |
| 125 | + await waitFor(() => { |
| 126 | + expect(screen.getByText('浙江')).toBeInTheDocument() |
| 127 | + }) |
| 128 | + |
| 129 | + // User selects 浙江 in the cascader (draft, not confirmed) |
| 130 | + fireEvent.click(screen.getByText('浙江')) |
| 131 | + |
| 132 | + // Wait for the selection to take effect |
| 133 | + await waitFor(() => { |
| 134 | + expect(screen.getAllByText('浙江').length).toBeGreaterThanOrEqual(2) |
| 135 | + }) |
| 136 | + |
| 137 | + // Parent re-renders with the same value content ([]) |
| 138 | + fireEvent.click(screen.getByText('Rerender')) |
| 139 | + |
| 140 | + // The draft selection should be preserved — 浙江 should still appear as a selected tab |
| 141 | + expect(screen.getAllByText('浙江').length).toBeGreaterThanOrEqual(2) |
| 142 | + }) |
| 143 | + |
55 | 144 | test('use in an imperative way', async () => { |
56 | 145 | const fn = jest.fn() |
57 | 146 | const onClick = async () => { |
|
0 commit comments