Skip to content

Commit 6f5a84c

Browse files
committed
test: use testing-library
1 parent 9c7ad27 commit 6f5a84c

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

tests/Select.SearchInput.spec.js

+25-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable no-undef */
22
import React, { useState } from 'react';
33
import { mount } from 'enzyme';
4+
import { render, fireEvent } from '@testing-library/react';
45
import TreeSelect, { TreeNode } from '../src';
56
import KeyCode from 'rc-util/lib/KeyCode';
67

@@ -203,7 +204,7 @@ describe('TreeSelect.SearchInput', () => {
203204
describe('keyboard events', () => {
204205
it('should select first matched node when press enter', () => {
205206
const onSelect = jest.fn();
206-
const wrapper = mount(
207+
const { getByRole } = render(
207208
<TreeSelect
208209
showSearch
209210
open
@@ -217,26 +218,26 @@ describe('TreeSelect.SearchInput', () => {
217218
);
218219

219220
// Search and press enter, should select first matched non-disabled node
220-
wrapper.search('1');
221-
wrapper.find('input').first().simulate('keyDown', { which: KeyCode.ENTER });
221+
const input = getByRole('combobox');
222+
fireEvent.change(input, { target: { value: '1' } });
223+
fireEvent.keyDown(input, { keyCode: KeyCode.ENTER });
222224
expect(onSelect).toHaveBeenCalledWith('1', expect.anything());
223225
onSelect.mockReset();
224226

225227
// Search disabled node and press enter, should not select
226-
wrapper.search('2');
227-
wrapper.find('input').first().simulate('keyDown', { which: KeyCode.ENTER });
228+
fireEvent.change(input, { target: { value: '2' } });
229+
fireEvent.keyDown(input, { keyCode: KeyCode.ENTER });
228230
expect(onSelect).not.toHaveBeenCalled();
229231
onSelect.mockReset();
230232

231-
wrapper.search('3');
232-
wrapper.find('input').first().simulate('keyDown', { which: KeyCode.ENTER });
233+
fireEvent.change(input, { target: { value: '3' } });
234+
fireEvent.keyDown(input, { keyCode: KeyCode.ENTER });
233235
expect(onSelect).toHaveBeenCalledWith('3', expect.anything());
234-
onSelect.mockReset();
235236
});
236237

237238
it('should not select node when no matches found', () => {
238239
const onSelect = jest.fn();
239-
const wrapper = mount(
240+
const { getByRole } = render(
240241
<TreeSelect
241242
showSearch
242243
onSelect={onSelect}
@@ -249,14 +250,15 @@ describe('TreeSelect.SearchInput', () => {
249250
);
250251

251252
// Search non-existent value and press enter, should not select any node
252-
wrapper.search('not-exist');
253-
wrapper.find('input').first().simulate('keyDown', { which: KeyCode.ENTER });
253+
const input = getByRole('combobox');
254+
fireEvent.change(input, { target: { value: 'not-exist' } });
255+
fireEvent.keyDown(input, { keyCode: KeyCode.ENTER });
254256
expect(onSelect).not.toHaveBeenCalled();
255257
});
256258

257259
it('should ignore enter press when all matched nodes are disabled', () => {
258260
const onSelect = jest.fn();
259-
const wrapper = mount(
261+
const { getByRole } = render(
260262
<TreeSelect
261263
showSearch
262264
onSelect={onSelect}
@@ -269,13 +271,14 @@ describe('TreeSelect.SearchInput', () => {
269271
);
270272

271273
// When all matched nodes are disabled, press enter should not select any node
272-
wrapper.search('1');
273-
wrapper.find('input').first().simulate('keyDown', { which: KeyCode.ENTER });
274+
const input = getByRole('combobox');
275+
fireEvent.change(input, { target: { value: '1' } });
276+
fireEvent.keyDown(input, { keyCode: KeyCode.ENTER });
274277
expect(onSelect).not.toHaveBeenCalled();
275278
});
276279

277280
it('should activate first matched node when searching', () => {
278-
const wrapper = mount(
281+
const { getByRole, container } = render(
279282
<TreeSelect
280283
showSearch
281284
open
@@ -288,12 +291,15 @@ describe('TreeSelect.SearchInput', () => {
288291
);
289292

290293
// When searching, first matched non-disabled node should be activated
291-
wrapper.search('1');
292-
expect(wrapper.find('.rc-tree-select-tree-treenode-active').text()).toBe('1');
294+
const input = getByRole('combobox');
295+
fireEvent.change(input, { target: { value: '1' } });
296+
expect(container.querySelector('.rc-tree-select-tree-treenode-active')).toHaveTextContent(
297+
'1',
298+
);
293299

294300
// Should skip disabled nodes
295-
wrapper.search('2');
296-
expect(wrapper.find('.rc-tree-select-tree-treenode-active').length).toBe(0);
301+
fireEvent.change(input, { target: { value: '2' } });
302+
expect(container.querySelectorAll('.rc-tree-select-tree-treenode-active')).toHaveLength(0);
297303
});
298304
});
299305
});

0 commit comments

Comments
 (0)