Skip to content

Commit f5ed7f3

Browse files
committed
test: fix all
1 parent e4ba16d commit f5ed7f3

File tree

3 files changed

+63
-27
lines changed

3 files changed

+63
-27
lines changed

tests/Select.multiple.spec.js

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
/* eslint-disable no-undef */
2-
import { render, fireEvent, within } from '@testing-library/react';
2+
import { render, fireEvent, within, screen } from '@testing-library/react';
33
import { mount } from 'enzyme';
44
import KeyCode from '@rc-component/util/lib/KeyCode';
55
import React from 'react';
66
import TreeSelect, { TreeNode } from '../src';
77
import focusTest from './shared/focusTest';
8+
import { selectNode, clearSelection, search, expectOpen, triggerOpen } from './util';
89

910
describe('TreeSelect.multiple', () => {
11+
beforeEach(() => {
12+
jest.useFakeTimers();
13+
});
14+
15+
afterEach(() => {
16+
jest.clearAllTimers();
17+
jest.useRealTimers();
18+
});
19+
1020
focusTest(true);
1121

1222
const treeData = [
@@ -129,14 +139,29 @@ describe('TreeSelect.multiple', () => {
129139
});
130140

131141
it('do not open tree when close button click', () => {
132-
const wrapper = mount(createSelect());
133-
wrapper.openSelect();
134-
wrapper.selectNode(0);
135-
wrapper.selectNode(1);
136-
wrapper.openSelect();
137-
wrapper.clearSelection(0);
138-
expect(wrapper.isOpen()).toBeFalsy();
139-
expect(wrapper.getSelection()).toHaveLength(1);
142+
const { container } = render(createSelect());
143+
144+
// Open the select dropdown
145+
triggerOpen(container);
146+
147+
// Select two nodes
148+
selectNode(0);
149+
selectNode(1);
150+
151+
// Check selections exist
152+
expect(container.querySelectorAll('.rc-tree-select-selection-item')).toHaveLength(2);
153+
154+
// Open again to ensure dropdown is open
155+
triggerOpen(container);
156+
157+
// Clear one selection - this should NOT open the dropdown
158+
clearSelection(container, 0);
159+
160+
// Check that only one selection remains
161+
expect(container.querySelectorAll('.rc-tree-select-selection-item')).toHaveLength(1);
162+
163+
// Check that dropdown is closed after clearing
164+
expectOpen(container, false);
140165
});
141166

142167
describe('maxTagCount', () => {
@@ -239,8 +264,8 @@ describe('TreeSelect.multiple', () => {
239264
// https://github.com/ant-design/ant-design/issues/12315
240265
it('select searched node', () => {
241266
const onChange = jest.fn();
242-
const wrapper = mount(
243-
<TreeSelect value={['leaf1']} multiple onChange={onChange}>
267+
const { container } = render(
268+
<TreeSelect value={['leaf1']} multiple onChange={onChange} open>
244269
<TreeNode value="parent 1" title="parent 1" key="0-1">
245270
<TreeNode value="parent 1-0" title="parent 1-0" key="0-1-1">
246271
<TreeNode value="leaf1" title="my leaf" key="random" />
@@ -252,8 +277,12 @@ describe('TreeSelect.multiple', () => {
252277
</TreeSelect>,
253278
);
254279

255-
wrapper.search('sss');
256-
wrapper.selectNode(2);
280+
// Search for 'sss'
281+
search(container, 'sss');
282+
283+
// Find and click on the searched node - use selectNode from util with correct index
284+
selectNode(2); // The sss node should be at index 2 after search filtering
285+
257286
expect(onChange).toHaveBeenCalledWith(['leaf1', 'sss'], expect.anything(), expect.anything());
258287
});
259288

tests/Select.tree.spec.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
/* eslint-disable no-undef, react/no-multi-comp, no-console */
22
import React from 'react';
3-
import { mount } from 'enzyme';
3+
import { render, fireEvent, act } from '@testing-library/react';
44
import { resetWarned } from '@rc-component/util/lib/warning';
55
import TreeSelect, { TreeNode as SelectNode } from '../src';
6+
import { selectNode, triggerOpen, expectOpen } from './util';
7+
import { mount } from 'enzyme';
68

79
describe('TreeSelect.tree', () => {
810
const createSelect = props => (
@@ -71,7 +73,7 @@ describe('TreeSelect.tree', () => {
7173
it('warning if node key are not same as value', () => {
7274
resetWarned();
7375
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
74-
mount(<TreeSelect treeData={[{ title: 'little', value: 'ttt', key: 'little' }]} />);
76+
render(<TreeSelect treeData={[{ title: 'little', value: 'ttt', key: 'little' }]} />);
7577
expect(spy).toHaveBeenCalledWith(
7678
'Warning: `key` or `value` with TreeNode must be the same or you can remove one of them. key: little, value: ttt.',
7779
);
@@ -81,15 +83,15 @@ describe('TreeSelect.tree', () => {
8183
it('warning if node undefined value', () => {
8284
resetWarned();
8385
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
84-
mount(<TreeSelect treeData={[{ title: 'little' }]} />);
86+
render(<TreeSelect treeData={[{ title: 'little' }]} />);
8587
expect(spy).toHaveBeenCalledWith('Warning: TreeNode `value` is invalidate: undefined');
8688
spy.mockRestore();
8789
});
8890

8991
it('warning if node has same value', () => {
9092
resetWarned();
9193
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
92-
mount(
94+
render(
9395
<TreeSelect
9496
treeData={[
9597
{ title: 'little', value: 'ttt' },
@@ -103,13 +105,14 @@ describe('TreeSelect.tree', () => {
103105

104106
// https://github.com/ant-design/ant-design/issues/14597
105107
it('empty string is also a value', () => {
106-
const wrapper = mount(
108+
const { container } = render(
107109
<TreeSelect placeholder="Please select" value="">
108110
<SelectNode key="" value="" title="empty string" />
109111
</TreeSelect>,
110112
);
111113

112-
expect(wrapper.getSelection(0).text()).toEqual('empty string');
114+
const selectionContent = container.querySelector('.rc-tree-select-content-value');
115+
expect(selectionContent?.textContent).toEqual('empty string');
113116
});
114117

115118
describe('treeNodeLabelProp', () => {
@@ -121,7 +124,7 @@ describe('TreeSelect.tree', () => {
121124
},
122125
].forEach(({ name, ...restProps }) => {
123126
it(name, () => {
124-
const wrapper = mount(
127+
const { container } = render(
125128
<TreeSelect
126129
open
127130
treeDefaultExpandAll
@@ -131,31 +134,35 @@ describe('TreeSelect.tree', () => {
131134
/>,
132135
);
133136

134-
expect(wrapper.find('.rc-tree-select-tree-title').text()).toEqual('a light');
135-
expect(wrapper.find('.rc-tree-select-selection-item').text()).toEqual('Light');
137+
expect(container.querySelector('.rc-tree-select-tree-title')?.textContent).toEqual(
138+
'a light',
139+
);
140+
expect(container.querySelector('.rc-tree-select-content-value')?.textContent).toEqual(
141+
'Light',
142+
);
136143
});
137144
});
138145
});
139146

140147
it('Node icon', () => {
141-
const wrapper = mount(
148+
const { container } = render(
142149
<TreeSelect open>
143150
<SelectNode value="little" title="Little" icon={<span className="bamboo-light" />} />
144151
</TreeSelect>,
145152
);
146153

147-
expect(wrapper.exists('.bamboo-light')).toBeTruthy();
154+
expect(container.querySelector('.bamboo-light')).toBeTruthy();
148155
});
149156

150157
it('dynamic with filter should not show expand icon', () => {
151-
const wrapper = mount(
158+
const { container } = render(
152159
<TreeSelect
153160
open
154161
treeData={[{ label: 'Bamboo', value: 'bamboo', isLeaf: false }]}
155162
searchValue="boo"
156163
/>,
157164
);
158165

159-
expect(wrapper.exists('.rc-tree-select-tree-icon__open')).toBeFalsy();
166+
expect(container.querySelector('.rc-tree-select-tree-icon__open')).toBeFalsy();
160167
});
161168
});

tests/setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Object.assign(Enzyme.ReactWrapper.prototype, {
4040
return this.find('.rc-tree-select-clear').first().simulate('mouseDown');
4141
},
4242
search(text) {
43-
this.find('input.rc-tree-select-selection-search-input').simulate('change', {
43+
this.find('input.rc-tree-select-input').simulate('change', {
4444
target: { value: text },
4545
});
4646
},

0 commit comments

Comments
 (0)