-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
75 lines (67 loc) · 2.79 KB
/
test.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
73
74
75
import test from 'tapava';
import getSelectionRangeFromElm from './lib/index';
const setNewSelectionRange = (startElm, startPosition, endElm, endPosition) => {
const selection = window.getSelection();
selection.removeAllRanges();
const range = document.createRange();
range.setStart(startElm, startPosition);
range.setEnd(endElm, endPosition);
selection.addRange(range);
};
test('no selection', t => {
const elm = document.body.appendChild(document.createElement('div'));
const actual = getSelectionRangeFromElm(elm);
const expected = null;
t.is(actual, expected);
});
test('selection, not within elm', t => {
const elm1 = document.body.appendChild(document.createElement('div'));
const elm2 = document.body.appendChild(document.createElement('div'));
setNewSelectionRange(elm2, 0, elm2, 0);
const actual = getSelectionRangeFromElm(elm1);
const expected = null;
t.is(actual, expected);
});
test('selection, within elm & no size', t => {
const elm = document.body.appendChild(document.createElement('div'));
setNewSelectionRange(elm, 0, elm, 0);
const actual = getSelectionRangeFromElm(elm);
const expectedContainer = elm;
const expectedStartOffset = 0;
const expectedEndOffset = 0;
t.is(actual.startContainer, expectedContainer);
t.is(actual.endContainer, expectedContainer);
t.is(actual.startOffset, expectedStartOffset);
t.is(actual.endOffset, expectedEndOffset);
});
test('selection, within elm & with size', t => {
const elm = document.body.appendChild(document.createTextNode('beep boop'));
setNewSelectionRange(elm, 0, elm, 3);
const actual = getSelectionRangeFromElm(elm);
const expectedContainer = elm;
const expectedStartOffset = 0;
const expectedEndOffset = 3;
t.is(actual.startContainer, expectedContainer);
t.is(actual.endContainer, expectedContainer);
t.is(actual.startOffset, expectedStartOffset);
t.is(actual.endOffset, expectedEndOffset);
});
test('selection, startContainer within elm but not endContainer', t => {
const elm = document.body.appendChild(document.createElement('div'));
const startChild = elm.appendChild(document.createElement('div'));
const endChild = elm.appendChild(document.createElement('div'));
setNewSelectionRange(startChild, 0, endChild, 0);
const actual = getSelectionRangeFromElm(startChild);
const expected = null;
t.is(actual, expected);
});
test('selection, endContainer within elm but not startContainer', t => {
const elm = document.body.appendChild(document.createElement('div'));
const startChild = elm.appendChild(document.createElement('div'));
const endChild = elm.appendChild(document.createElement('div'));
setNewSelectionRange(startChild, 0, endChild, 0);
const actual = getSelectionRangeFromElm(endChild);
const expected = null;
t.is(actual, expected);
});
test.onFinish(window.close);