|
1 | 1 | /* eslint-disable class-methods-use-this */ |
2 | 2 | import { spyElementPrototype } from '../src/test/domHook'; |
3 | | -import { getFocusNodeList } from '../src/Dom/focus'; |
| 3 | +import { getFocusNodeList, triggerFocus } from '../src/Dom/focus'; |
4 | 4 |
|
5 | 5 | describe('focus', () => { |
6 | 6 | beforeAll(() => { |
@@ -31,4 +31,29 @@ describe('focus', () => { |
31 | 31 | const tabFocusList = getFocusNodeList(div, true); |
32 | 32 | expect(tabFocusList).toHaveLength(5); |
33 | 33 | }); |
| 34 | + |
| 35 | + it('triggerFocus should set cursor position for textarea', () => { |
| 36 | + const textarea = document.createElement('textarea'); |
| 37 | + textarea.value = 'test content'; |
| 38 | + |
| 39 | + const focusSpy = jest.spyOn(textarea, 'focus'); |
| 40 | + const setSelectionRangeSpy = jest.spyOn(textarea, 'setSelectionRange'); |
| 41 | + |
| 42 | + // Test cursor: 'start' |
| 43 | + triggerFocus(textarea, { cursor: 'start' }); |
| 44 | + expect(setSelectionRangeSpy).toHaveBeenCalledWith(0, 0); |
| 45 | + |
| 46 | + // Test cursor: 'end' |
| 47 | + triggerFocus(textarea, { cursor: 'end' }); |
| 48 | + expect(setSelectionRangeSpy).toHaveBeenCalledWith(12, 12); // 'test content'.length = 12 |
| 49 | + |
| 50 | + // Test cursor: 'all' |
| 51 | + triggerFocus(textarea, { cursor: 'all' }); |
| 52 | + expect(setSelectionRangeSpy).toHaveBeenCalledWith(0, 12); // select all text |
| 53 | + |
| 54 | + expect(focusSpy).toHaveBeenCalledTimes(3); |
| 55 | + |
| 56 | + focusSpy.mockRestore(); |
| 57 | + setSelectionRangeSpy.mockRestore(); |
| 58 | + }); |
34 | 59 | }); |
0 commit comments