Skip to content

Commit 2304164

Browse files
committed
chore(dispatcher): add selectToBoundary tests
1 parent 2cb2d02 commit 2304164

File tree

5 files changed

+84
-9
lines changed

5 files changed

+84
-9
lines changed

spec/dispatcher.spec.js

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,24 +140,27 @@ describe('Dispatcher', function () {
140140
expect(insert.calls).toEqual(1)
141141
})
142142

143-
it('fires merge if cursor is in the middle', function () {
144-
// <div>fo|o</div>
145-
elem.innerHTML = 'foo'
143+
it('fires "split" if cursor is in the middle', function () {
144+
console.log('HERE................')
145+
// <div>ba|r</div>
146+
elem.innerHTML = 'bar'
146147
const range = rangy.createRange()
147148
range.setStart(elem.firstChild, 2)
148149
range.setEnd(elem.firstChild, 2)
150+
range.collapse()
149151
createCursor(range)
150152

151153
const insert = on('split', (element, before, after, cursor) => {
152154
expect(element).toEqual(elem)
153-
expect(content.getInnerHtmlOfFragment(before)).toEqual('fo')
154-
expect(content.getInnerHtmlOfFragment(after)).toEqual('o')
155+
expect(content.getInnerHtmlOfFragment(before)).toEqual('ba')
156+
expect(content.getInnerHtmlOfFragment(after)).toEqual('r')
155157
expect(cursor.isCursor).toEqual(true)
156158
})
157159

158160
const evt = new KeyboardEvent('keydown', {keyCode: key.enter})
159161
elem.dispatchEvent(evt)
160162
expect(insert.calls).toEqual(1)
163+
console.log('END...........')
161164
})
162165
})
163166

@@ -304,5 +307,61 @@ describe('Dispatcher', function () {
304307
elem.dispatchEvent(evt)
305308
})
306309
})
310+
311+
describe('selectToBoundary event:', function () {
312+
313+
it('fires "both" if all is selected', function () {
314+
elem.innerHTML = 'People Make The World Go Round'
315+
// select all
316+
const range = rangy.createRange()
317+
range.selectNodeContents(elem)
318+
createCursor(range)
319+
// listen for event
320+
let position
321+
editable.selectToBoundary(function (element, evt, pos) {
322+
position = pos
323+
})
324+
// trigger selectionchange event
325+
const selectionEvent = new Event('selectionchange', {bubbles: true})
326+
elem.dispatchEvent(selectionEvent)
327+
expect(position).toEqual('both')
328+
})
329+
330+
it('fires "start" if selection is at beginning but not end', function () {
331+
elem.innerHTML = 'People Make The World Go Round'
332+
// select "People"
333+
const range = rangy.createRange()
334+
range.setStart(elem.firstChild, 0)
335+
range.setEnd(elem.firstChild, 5)
336+
createCursor(range)
337+
// listen for event
338+
let position
339+
editable.selectToBoundary(function (element, evt, pos) {
340+
position = pos
341+
})
342+
// trigger selectionchange event
343+
const selectionEvent = new Event('selectionchange', {bubbles: true})
344+
elem.dispatchEvent(selectionEvent)
345+
expect(position).toEqual('start')
346+
})
347+
348+
it('fires "end" if selection is at end but not beginning', function () {
349+
elem.innerHTML = 'People Make The World Go Round'
350+
// select "Round"
351+
const range = rangy.createRange()
352+
range.setStart(elem.firstChild, 25)
353+
range.setEnd(elem.firstChild, 30)
354+
createCursor(range)
355+
// listen for event
356+
let position
357+
editable.selectToBoundary(function (element, evt, pos) {
358+
position = pos
359+
})
360+
// trigger selectionchange event
361+
const selectionEvent = new Event('selectionchange', {bubbles: true})
362+
elem.dispatchEvent(selectionEvent)
363+
expect(position).toEqual('end')
364+
})
365+
})
307366
})
308367
})

spec/highlighting.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sinon from 'sinon'
12
import rangy from 'rangy'
23
import {Editable} from '../src/core'
34
import Highlighting from '../src/highlighting'
@@ -422,6 +423,17 @@ Make The <br> W<span class="highlight-spellcheck" data-word-id="spellcheckId" da
422423
expect(this.extract('comment')).toEqual(expectedRanges)
423424
expect(startIndex).toEqual(3)
424425
})
426+
427+
it('normalizes a simple text node after removing a highlight', function () {
428+
setupHighlightEnv(self, 'People Make The World Go Round')
429+
self.highlightRange('myId', 3, 7)
430+
const normalizeSpy = sinon.spy(self.div, 'normalize')
431+
self.removeHighlight('myId')
432+
// There is no way to see the actual error in a test since it only happens in (non-headless)
433+
// Chome environments. We just check if the normalize method has been called here.
434+
expect(normalizeSpy.callCount).toEqual(1)
435+
normalizeSpy.restore()
436+
})
425437
})
426438

427439
describe('highlight support with special characters', function () {

spec/selection.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe('Selection', function () {
100100
expect(this.selection.isAllSelected()).toEqual(true)
101101
})
102102

103-
it('returns true if all is selected', function () {
103+
it('returns false if not all is selected', function () {
104104
const textNode = this.oneWord.firstChild
105105
let range = rangy.createRange()
106106
range.setStartBefore(textNode)

src/dispatcher.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,13 @@ export default class Dispatcher {
276276
const cursor = range.forceCursor()
277277

278278
if (cursor.isAtTextEnd()) {
279+
console.log('at end', event.target)
279280
self.notify('insert', this, 'after', cursor)
280281
} else if (cursor.isAtBeginning()) {
282+
console.log('at beginning', event.target)
281283
self.notify('insert', this, 'before', cursor)
282284
} else {
285+
console.log('in split', event.target)
283286
self.notify('split', this, cursor.before(), cursor.after(), cursor)
284287
}
285288
})
@@ -330,11 +333,11 @@ export default class Dispatcher {
330333
const cursor = this.selectionWatcher.getFreshSelection()
331334

332335
if (cursor && cursor.isSelection && cursor.isAtBeginning() && cursor.isAtEnd()) {
333-
this.notify('selectToBoundary', cursor.host, evt, 'both', cursor)
336+
this.notify('selectToBoundary', cursor.host, evt, 'both')
334337
} else if (cursor && cursor.isSelection && cursor.isAtBeginning()) {
335-
this.notify('selectToBoundary', cursor.host, evt, 'start', cursor)
338+
this.notify('selectToBoundary', cursor.host, evt, 'start')
336339
} else if (cursor && cursor.isSelection && cursor.isAtEnd()) {
337-
this.notify('selectToBoundary', cursor.host, evt, 'end', cursor)
340+
this.notify('selectToBoundary', cursor.host, evt, 'end')
338341
}
339342

340343
if (suppressSelectionChanges) {

src/highlight-support.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const highlightSupport = {
6969
const elems = editableHost.querySelectorAll(`[data-word-id="${highlightId}"]`)
7070
for (const elem of elems) {
7171
content.unwrap(elem)
72+
// in Chrome browsers the unwrap method leaves the host node split into 2 (lastChild !== firstChild)
7273
editableHost.normalize()
7374
if (dispatcher) dispatcher.notify('change', editableHost)
7475
}

0 commit comments

Comments
 (0)