|
2 | 2 | import React, { useState } from 'react';
|
3 | 3 | import { mount } from 'enzyme';
|
4 | 4 | import TreeSelect, { TreeNode } from '../src';
|
| 5 | +import KeyCode from 'rc-util/lib/KeyCode'; |
5 | 6 |
|
6 | 7 | describe('TreeSelect.SearchInput', () => {
|
7 | 8 | it('select item will clean searchInput', () => {
|
@@ -198,4 +199,98 @@ describe('TreeSelect.SearchInput', () => {
|
198 | 199 | nodes.first().simulate('click');
|
199 | 200 | expect(called).toBe(1);
|
200 | 201 | });
|
| 202 | + |
| 203 | + describe('keyboard events', () => { |
| 204 | + it('should select first matched node when press enter', () => { |
| 205 | + const onSelect = jest.fn(); |
| 206 | + const wrapper = mount( |
| 207 | + <TreeSelect |
| 208 | + showSearch |
| 209 | + onSelect={onSelect} |
| 210 | + open |
| 211 | + treeData={[ |
| 212 | + { value: '1', label: '1' }, |
| 213 | + { value: '2', label: '2', disabled: true }, |
| 214 | + { value: '3', label: '3' }, |
| 215 | + ]} |
| 216 | + />, |
| 217 | + ); |
| 218 | + |
| 219 | + // 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 }); |
| 222 | + expect(onSelect).toHaveBeenCalledWith('1', expect.anything()); |
| 223 | + |
| 224 | + onSelect.mockReset(); |
| 225 | + |
| 226 | + // Search disabled node and press enter, should not select |
| 227 | + wrapper.search('2'); |
| 228 | + wrapper.find('input').first().simulate('keyDown', { which: KeyCode.ENTER }); |
| 229 | + expect(onSelect).not.toHaveBeenCalled(); |
| 230 | + }); |
| 231 | + |
| 232 | + it('should not select any node when no search value and press enter', () => { |
| 233 | + const onSelect = jest.fn(); |
| 234 | + const wrapper = mount( |
| 235 | + <TreeSelect |
| 236 | + showSearch |
| 237 | + onSelect={onSelect} |
| 238 | + open |
| 239 | + treeData={[ |
| 240 | + { value: '1', label: '1' }, |
| 241 | + { value: '2', label: '2' }, |
| 242 | + ]} |
| 243 | + />, |
| 244 | + ); |
| 245 | + |
| 246 | + // Press enter without search value, should not select any node |
| 247 | + wrapper.find('input').first().simulate('keyDown', { which: KeyCode.ENTER }); |
| 248 | + expect(onSelect).not.toHaveBeenCalled(); |
| 249 | + |
| 250 | + // Search and press enter, should select first matched node |
| 251 | + wrapper.search('1'); |
| 252 | + wrapper.find('input').first().simulate('keyDown', { which: KeyCode.ENTER }); |
| 253 | + expect(onSelect).toHaveBeenCalledWith('1', expect.anything()); |
| 254 | + }); |
| 255 | + |
| 256 | + it('should not select node when no matches found', () => { |
| 257 | + const onSelect = jest.fn(); |
| 258 | + const wrapper = mount( |
| 259 | + <TreeSelect |
| 260 | + showSearch |
| 261 | + onSelect={onSelect} |
| 262 | + open |
| 263 | + treeData={[ |
| 264 | + { value: '1', label: '1' }, |
| 265 | + { value: '2', label: '2' }, |
| 266 | + ]} |
| 267 | + />, |
| 268 | + ); |
| 269 | + |
| 270 | + // Search non-existent value and press enter, should not select any node |
| 271 | + wrapper.search('not-exist'); |
| 272 | + wrapper.find('input').first().simulate('keyDown', { which: KeyCode.ENTER }); |
| 273 | + expect(onSelect).not.toHaveBeenCalled(); |
| 274 | + }); |
| 275 | + |
| 276 | + it('should ignore enter press when all matched nodes are disabled', () => { |
| 277 | + const onSelect = jest.fn(); |
| 278 | + const wrapper = mount( |
| 279 | + <TreeSelect |
| 280 | + showSearch |
| 281 | + onSelect={onSelect} |
| 282 | + open |
| 283 | + treeData={[ |
| 284 | + { value: '1', label: '1', disabled: true }, |
| 285 | + { value: '2', label: '2', disabled: true }, |
| 286 | + ]} |
| 287 | + />, |
| 288 | + ); |
| 289 | + |
| 290 | + // When all matched nodes are disabled, press enter should not select any node |
| 291 | + wrapper.search('1'); |
| 292 | + wrapper.find('input').first().simulate('keyDown', { which: KeyCode.ENTER }); |
| 293 | + expect(onSelect).not.toHaveBeenCalled(); |
| 294 | + }); |
| 295 | + }); |
201 | 296 | });
|
0 commit comments