-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcreate-default-events.spec.js
72 lines (60 loc) · 1.93 KB
/
create-default-events.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import {expect} from 'chai'
import {createRange} from '../src/util/dom.js'
import Cursor from '../src/cursor.js'
import {Editable} from '../src/core.js'
describe('Default Events', function () {
// create a Cursor object and set the selection to it
function createCursor (elem, range) {
const cursor = new Cursor(elem, range)
cursor.setVisibleSelection()
return cursor
}
function createRangeAtEnd (node) {
const range = createRange()
range.selectNodeContents(node)
range.collapse(false)
return range
}
// register one listener per test
function on (editable, eventName, func) {
// off() // make sure the last listener is unregistered
const obj = {calls: 0}
function proxy () {
obj.calls += 1
func.apply(this, arguments)
}
editable.on(eventName, proxy)
return obj
}
describe('for editable', function () {
describe('on focus', function () {
beforeEach(function () {
this.focus = new Event('focus')
this.blur = new Event('blur')
this.elem = document.createElement('div')
document.body.appendChild(this.elem)
this.editable = new Editable()
this.editable.add(this.elem)
this.elem.focus()
})
afterEach(function () {
this.editable.unload()
this.elem.remove()
})
it('always dispatches with virtual and native ranges in sync.', function () {
// <div>foo\</div>
this.elem.innerHTML = 'foo'
createCursor(this.elem, createRangeAtEnd(this.elem))
const onFocus = on(this.editable, 'focus', (element, selection) => {
if (!selection) return
expect(element).to.equal(this.elem)
expect(selection.range).to.be.an.instanceOf(Range)
})
this.elem.dispatchEvent(this.focus)
this.elem.dispatchEvent(this.blur)
this.elem.dispatchEvent(this.focus)
expect(onFocus.calls).to.equal(2)
})
})
})
})