|
1 | 1 | import userEvent from '#src'
|
| 2 | +import {setSelection} from '#src/utils' |
2 | 3 | import {setup} from '#testHelpers/utils'
|
3 | 4 |
|
4 |
| -test('collapse selection to the left', async () => { |
5 |
| - const {element} = setup<HTMLInputElement>(`<input value="foobar"/>`) |
6 |
| - element.focus() |
7 |
| - element.setSelectionRange(2, 4) |
| 5 | +describe('in text input', () => { |
| 6 | + test('collapse selection to the left', async () => { |
| 7 | + const {element} = setup<HTMLInputElement>(`<input value="foobar"/>`) |
| 8 | + element.focus() |
| 9 | + element.setSelectionRange(2, 4) |
8 | 10 |
|
9 |
| - await userEvent.keyboard('[ArrowLeft]') |
| 11 | + await userEvent.keyboard('[ArrowLeft]') |
10 | 12 |
|
11 |
| - expect(element.selectionStart).toBe(2) |
12 |
| - expect(element.selectionEnd).toBe(2) |
13 |
| -}) |
| 13 | + expect(element.selectionStart).toBe(2) |
| 14 | + expect(element.selectionEnd).toBe(2) |
| 15 | + }) |
14 | 16 |
|
15 |
| -test('collapse selection to the right', async () => { |
16 |
| - const {element} = setup<HTMLInputElement>(`<input value="foobar"/>`) |
17 |
| - element.focus() |
18 |
| - element.setSelectionRange(2, 4) |
| 17 | + test('collapse selection to the right', async () => { |
| 18 | + const {element} = setup<HTMLInputElement>(`<input value="foobar"/>`) |
| 19 | + element.focus() |
| 20 | + element.setSelectionRange(2, 4) |
19 | 21 |
|
20 |
| - await userEvent.keyboard('[ArrowRight]') |
| 22 | + await userEvent.keyboard('[ArrowRight]') |
21 | 23 |
|
22 |
| - expect(element.selectionStart).toBe(4) |
23 |
| - expect(element.selectionEnd).toBe(4) |
24 |
| -}) |
| 24 | + expect(element.selectionStart).toBe(4) |
| 25 | + expect(element.selectionEnd).toBe(4) |
| 26 | + }) |
| 27 | + |
| 28 | + test('move cursor left', async () => { |
| 29 | + const {element} = setup<HTMLInputElement>(`<input value="foobar"/>`) |
| 30 | + element.focus() |
| 31 | + element.setSelectionRange(2, 2) |
| 32 | + |
| 33 | + await userEvent.keyboard('[ArrowLeft]') |
| 34 | + |
| 35 | + expect(element.selectionStart).toBe(1) |
| 36 | + expect(element.selectionEnd).toBe(1) |
| 37 | + }) |
25 | 38 |
|
26 |
| -test('move cursor left', async () => { |
27 |
| - const {element} = setup<HTMLInputElement>(`<input value="foobar"/>`) |
28 |
| - element.focus() |
29 |
| - element.setSelectionRange(2, 2) |
| 39 | + test('move cursor right', async () => { |
| 40 | + const {element} = setup<HTMLInputElement>(`<input value="foobar"/>`) |
| 41 | + element.focus() |
| 42 | + element.setSelectionRange(2, 2) |
30 | 43 |
|
31 |
| - await userEvent.keyboard('[ArrowLeft]') |
| 44 | + await userEvent.keyboard('[ArrowRight]') |
32 | 45 |
|
33 |
| - expect(element.selectionStart).toBe(1) |
34 |
| - expect(element.selectionEnd).toBe(1) |
| 46 | + expect(element.selectionStart).toBe(3) |
| 47 | + expect(element.selectionEnd).toBe(3) |
| 48 | + }) |
35 | 49 | })
|
36 | 50 |
|
37 |
| -test('move cursor right', async () => { |
38 |
| - const {element} = setup<HTMLInputElement>(`<input value="foobar"/>`) |
39 |
| - element.focus() |
40 |
| - element.setSelectionRange(2, 2) |
| 51 | +describe('in contenteditable', () => { |
| 52 | + test('collapse selection to the left', async () => { |
| 53 | + const {element} = setup( |
| 54 | + `<div contenteditable><span>foo</span><span>bar</span></div>`, |
| 55 | + ) |
| 56 | + setSelection({ |
| 57 | + anchorNode: element.firstChild?.firstChild as Text, |
| 58 | + anchorOffset: 2, |
| 59 | + focusNode: element.lastChild?.firstChild as Text, |
| 60 | + focusOffset: 1, |
| 61 | + }) |
| 62 | + |
| 63 | + await userEvent.keyboard('[ArrowLeft]') |
| 64 | + |
| 65 | + expect(element.ownerDocument.getSelection()).toHaveProperty( |
| 66 | + 'focusNode', |
| 67 | + element.firstChild?.firstChild, |
| 68 | + ) |
| 69 | + expect(element.ownerDocument.getSelection()).toHaveProperty( |
| 70 | + 'focusOffset', |
| 71 | + 2, |
| 72 | + ) |
| 73 | + }) |
| 74 | + |
| 75 | + test('collapse selection to the right', async () => { |
| 76 | + const {element} = setup( |
| 77 | + `<div contenteditable><span>foo</span><span>bar</span></div>`, |
| 78 | + ) |
| 79 | + setSelection({ |
| 80 | + anchorNode: element.firstChild?.firstChild as Text, |
| 81 | + anchorOffset: 2, |
| 82 | + focusNode: element.lastChild?.firstChild as Text, |
| 83 | + focusOffset: 1, |
| 84 | + }) |
| 85 | + |
| 86 | + await userEvent.keyboard('[ArrowRight]') |
| 87 | + |
| 88 | + expect(element.ownerDocument.getSelection()).toHaveProperty( |
| 89 | + 'focusNode', |
| 90 | + element.lastChild?.firstChild, |
| 91 | + ) |
| 92 | + expect(element.ownerDocument.getSelection()).toHaveProperty( |
| 93 | + 'focusOffset', |
| 94 | + 1, |
| 95 | + ) |
| 96 | + }) |
| 97 | + |
| 98 | + test('move cursor to the left', async () => { |
| 99 | + const { |
| 100 | + elements: [, div], |
| 101 | + } = setup( |
| 102 | + `<span>abc</span><div contenteditable><span>foo</span><span>bar</span></div><span>def</span>`, |
| 103 | + ) |
| 104 | + setSelection({ |
| 105 | + focusNode: div.lastChild?.firstChild as Text, |
| 106 | + focusOffset: 1, |
| 107 | + }) |
| 108 | + |
| 109 | + await userEvent.keyboard('[ArrowLeft][ArrowLeft]') |
| 110 | + |
| 111 | + expect(div.ownerDocument.getSelection()).toHaveProperty( |
| 112 | + 'focusNode', |
| 113 | + div.firstChild?.firstChild, |
| 114 | + ) |
| 115 | + expect(div.ownerDocument.getSelection()).toHaveProperty('focusOffset', 2) |
| 116 | + |
| 117 | + await userEvent.keyboard('[ArrowLeft][ArrowLeft][ArrowLeft][ArrowLeft]') |
| 118 | + |
| 119 | + expect(div.ownerDocument.getSelection()).toHaveProperty( |
| 120 | + 'focusNode', |
| 121 | + div.firstChild?.firstChild, |
| 122 | + ) |
| 123 | + expect(div.ownerDocument.getSelection()).toHaveProperty('focusOffset', 0) |
| 124 | + }) |
| 125 | + |
| 126 | + test('move cursor to the right', async () => { |
| 127 | + const { |
| 128 | + elements: [, div], |
| 129 | + } = setup( |
| 130 | + `<span>abc</span><div contenteditable><span>foo</span><span>bar</span></div><span>def</span>`, |
| 131 | + ) |
| 132 | + setSelection({ |
| 133 | + focusNode: div.firstChild?.firstChild as Text, |
| 134 | + focusOffset: 2, |
| 135 | + }) |
| 136 | + |
| 137 | + await userEvent.keyboard('[ArrowRight][ArrowRight]') |
| 138 | + |
| 139 | + expect(div.ownerDocument.getSelection()).toHaveProperty( |
| 140 | + 'focusNode', |
| 141 | + div.lastChild?.firstChild, |
| 142 | + ) |
| 143 | + expect(div.ownerDocument.getSelection()).toHaveProperty('focusOffset', 1) |
41 | 144 |
|
42 |
| - await userEvent.keyboard('[ArrowRight]') |
| 145 | + await userEvent.keyboard('[ArrowRight][ArrowRight][ArrowRight][ArrowRight]') |
43 | 146 |
|
44 |
| - expect(element.selectionStart).toBe(3) |
45 |
| - expect(element.selectionEnd).toBe(3) |
| 147 | + expect(div.ownerDocument.getSelection()).toHaveProperty( |
| 148 | + 'focusNode', |
| 149 | + div.lastChild?.firstChild, |
| 150 | + ) |
| 151 | + expect(div.ownerDocument.getSelection()).toHaveProperty('focusOffset', 3) |
| 152 | + }) |
46 | 153 | })
|
0 commit comments