|
4 | 4 | setSelection,
|
5 | 5 | setSelectionRange,
|
6 | 6 | modifySelection,
|
| 7 | + moveSelection, |
7 | 8 | } from '#src/utils'
|
8 | 9 | import {setup} from '#testHelpers'
|
9 | 10 |
|
@@ -183,3 +184,78 @@ describe('update selection when moving focus into element with own selection imp
|
183 | 184 | expect(document.getSelection()).toHaveProperty('focusOffset', 0)
|
184 | 185 | })
|
185 | 186 | })
|
| 187 | + |
| 188 | +describe('move selection', () => { |
| 189 | + test('do nothing without a selection range', () => { |
| 190 | + const {element} = setup(`<div tabindex="0"></div>`) |
| 191 | + document.getSelection()?.removeAllRanges() |
| 192 | + |
| 193 | + moveSelection(element, 1) |
| 194 | + |
| 195 | + expect(document.getSelection()).toHaveProperty('rangeCount', 0) |
| 196 | + }) |
| 197 | + |
| 198 | + test('move to next cursor position', () => { |
| 199 | + const {element} = setup(`<div tabindex="0">foo</div>`, { |
| 200 | + selection: {focusNode: 'div/text()', focusOffset: 1}, |
| 201 | + }) |
| 202 | + |
| 203 | + moveSelection(element, 1) |
| 204 | + |
| 205 | + expect(document.getSelection()).toHaveProperty( |
| 206 | + 'focusNode', |
| 207 | + element.firstChild, |
| 208 | + ) |
| 209 | + expect(document.getSelection()).toHaveProperty('focusOffset', 2) |
| 210 | + }) |
| 211 | + |
| 212 | + test('move to next cursor position', () => { |
| 213 | + const {element} = setup(`<div tabindex="0">foo</div>`, { |
| 214 | + selection: {focusNode: 'div/text()', focusOffset: 1}, |
| 215 | + }) |
| 216 | + |
| 217 | + moveSelection(element, 1) |
| 218 | + |
| 219 | + expect(document.getSelection()).toHaveProperty( |
| 220 | + 'focusNode', |
| 221 | + element.firstChild, |
| 222 | + ) |
| 223 | + expect(document.getSelection()).toHaveProperty('focusOffset', 2) |
| 224 | + expect(document.getSelection()).toHaveProperty('isCollapsed', true) |
| 225 | + }) |
| 226 | + |
| 227 | + test('collapse range', () => { |
| 228 | + const {element} = setup(`<div tabindex="0">foo</div>`, { |
| 229 | + selection: {focusNode: 'div/text()', anchorOffset: 1, focusOffset: 2}, |
| 230 | + }) |
| 231 | + |
| 232 | + moveSelection(element, 1) |
| 233 | + |
| 234 | + expect(document.getSelection()).toHaveProperty( |
| 235 | + 'focusNode', |
| 236 | + element.firstChild, |
| 237 | + ) |
| 238 | + expect(document.getSelection()).toHaveProperty('focusOffset', 2) |
| 239 | + expect(document.getSelection()).toHaveProperty('isCollapsed', true) |
| 240 | + }) |
| 241 | + |
| 242 | + test('move cursor in input', () => { |
| 243 | + const {element} = setup(`<input value="foo"/>`) |
| 244 | + |
| 245 | + moveSelection(element, 1) |
| 246 | + |
| 247 | + expect(element).toHaveProperty('selectionStart', 1) |
| 248 | + expect(element).toHaveProperty('selectionEnd', 1) |
| 249 | + }) |
| 250 | + |
| 251 | + test('collapse range in input', () => { |
| 252 | + const {element} = setup(`<input value="foo"/>`, { |
| 253 | + selection: {anchorOffset: 1, focusOffset: 2}, |
| 254 | + }) |
| 255 | + |
| 256 | + moveSelection(element, 1) |
| 257 | + |
| 258 | + expect(element).toHaveProperty('selectionStart', 2) |
| 259 | + expect(element).toHaveProperty('selectionEnd', 2) |
| 260 | + }) |
| 261 | +}) |
0 commit comments