|
| 1 | +import {setup} from '#testHelpers/utils' |
| 2 | +import {selectAll} from '#src/utils/focus/selectAll' |
| 3 | +import {getUISelection} from '#src/document' |
| 4 | + |
| 5 | +test('select all in input', () => { |
| 6 | + const {element} = setup<HTMLInputElement>(`<input value="foo bar baz"/>`) |
| 7 | + |
| 8 | + selectAll(element) |
| 9 | + |
| 10 | + expect(getUISelection(element)).toHaveProperty('startOffset', 0) |
| 11 | + expect(getUISelection(element)).toHaveProperty('endOffset', 11) |
| 12 | + expect(element).toHaveProperty('selectionStart', 0) |
| 13 | + expect(element).toHaveProperty('selectionEnd', 11) |
| 14 | +}) |
| 15 | + |
| 16 | +test('select all in textarea', () => { |
| 17 | + const {element} = setup<HTMLTextAreaElement>( |
| 18 | + `<textarea>foo\nbar\nbaz</textarea>`, |
| 19 | + ) |
| 20 | + |
| 21 | + selectAll(element) |
| 22 | + |
| 23 | + expect(getUISelection(element)).toHaveProperty('startOffset', 0) |
| 24 | + expect(getUISelection(element)).toHaveProperty('endOffset', 11) |
| 25 | + expect(element).toHaveProperty('selectionStart', 0) |
| 26 | + expect(element).toHaveProperty('selectionEnd', 11) |
| 27 | +}) |
| 28 | + |
| 29 | +test('select all in contenteditable', () => { |
| 30 | + const {element} = setup(` |
| 31 | + <div contenteditable><div>foo</div><div>bar</div></div> |
| 32 | + <div>baz</div> |
| 33 | + `) |
| 34 | + |
| 35 | + selectAll(element) |
| 36 | + |
| 37 | + const selection = document.getSelection() |
| 38 | + expect(selection).toHaveProperty('anchorNode', element) |
| 39 | + expect(selection).toHaveProperty('anchorOffset', 0) |
| 40 | + expect(selection).toHaveProperty('focusNode', element) |
| 41 | + expect(selection).toHaveProperty('focusOffset', 2) |
| 42 | +}) |
| 43 | + |
| 44 | +test('select all outside of editable', () => { |
| 45 | + const {element} = setup(` |
| 46 | + <input type="checkbox"/> |
| 47 | + <div>foo</div> |
| 48 | + `) |
| 49 | + |
| 50 | + selectAll(element) |
| 51 | + |
| 52 | + const selection = document.getSelection() |
| 53 | + expect(selection).toHaveProperty('anchorNode', element.ownerDocument.body) |
| 54 | + expect(selection).toHaveProperty('anchorOffset', 0) |
| 55 | + expect(selection).toHaveProperty('focusNode', element.ownerDocument.body) |
| 56 | + expect(selection).toHaveProperty( |
| 57 | + 'focusOffset', |
| 58 | + element.ownerDocument.body.childNodes.length, |
| 59 | + ) |
| 60 | +}) |
0 commit comments