forked from yuanqing/charming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
124 lines (114 loc) · 4.5 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const test = require('tape')
const charming = require('./')
function createElement (innerHTML) {
const element = document.createElement('div')
element.innerHTML = innerHTML || ''
document.body.appendChild(element)
return element
}
test('should not inject spans when `element` has no child nodes', function (t) {
t.plan(2)
const element = createElement()
charming(element)
t.equal(element.innerHTML, '')
t.equal(element.getAttribute('aria-label'), null)
})
test('should not inject spans when `element` has no child text nodes', function (t) {
t.plan(2)
const innerHTML = '<span></span>'
const element = createElement(innerHTML)
charming(element)
t.equal(element.innerHTML, innerHTML)
t.equal(element.getAttribute('aria-label'), null)
})
test('should inject spans when `element` has a single child text node', function (t) {
t.plan(2)
const element = createElement('foo')
charming(element)
t.equal(element.getAttribute('aria-label'), 'foo')
t.equal(
element.innerHTML,
'<span class="char1" aria-hidden="true">f</span><span class="char2" aria-hidden="true">o</span><span class="char3" aria-hidden="true">o</span>'
)
})
test('should inject spans when `element` has multiple child text nodes', function (t) {
t.plan(2)
const element = createElement(
'<span>foo</span> <span>bar <span>baz</span></span>'
)
charming(element)
t.equal(element.getAttribute('aria-label'), null)
t.equal(
element.innerHTML,
'<span aria-label="foo"><span class="char1" aria-hidden="true">f</span><span class="char2" aria-hidden="true">o</span><span class="char3" aria-hidden="true">o</span></span><span class="char4" aria-hidden="true"> </span><span aria-label="bar "><span class="char5" aria-hidden="true">b</span><span class="char6" aria-hidden="true">a</span><span class="char7" aria-hidden="true">r</span><span class="char8" aria-hidden="true"> </span><span aria-label="baz"><span class="char9" aria-hidden="true">b</span><span class="char10" aria-hidden="true">a</span><span class="char11" aria-hidden="true">z</span></span></span>'
)
})
test('should correctly set `aria-label` when `element` contains adjacent child text nodes', function (t) {
t.plan(2)
const element = createElement()
element.appendChild(document.createTextNode('foo'))
element.appendChild(document.createTextNode('bar'))
charming(element)
t.equal(element.getAttribute('aria-label'), 'foobar')
t.equal(
element.innerHTML,
'<span class="char1" aria-hidden="true">f</span><span class="char2" aria-hidden="true">o</span><span class="char3" aria-hidden="true">o</span><span class="char4" aria-hidden="true">b</span><span class="char5" aria-hidden="true">a</span><span class="char6" aria-hidden="true">r</span>'
)
})
test('can inject custom tags', function (t) {
t.plan(2)
const element = createElement('foo')
charming(element, {
tagName: 'b'
})
t.equal(element.getAttribute('aria-label'), 'foo')
t.equal(
element.innerHTML,
'<b class="char1" aria-hidden="true">f</b><b class="char2" aria-hidden="true">o</b><b class="char3" aria-hidden="true">o</b>'
)
})
test('can inject spans without classes', function (t) {
t.plan(2)
const element = createElement('foo')
charming(element, {
setClassName: function () {
return null
}
})
t.equal(element.getAttribute('aria-label'), 'foo')
t.equal(
element.innerHTML,
'<span aria-hidden="true">f</span><span aria-hidden="true">o</span><span aria-hidden="true">o</span>'
)
})
test('can inject spans with a custom class', function (t) {
t.plan(2)
const element = createElement('foo')
charming(element, {
setClassName: function (index, letter) {
return `index-${index} letter-${letter}`
}
})
t.equal(element.getAttribute('aria-label'), 'foo')
t.equal(
element.innerHTML,
'<span class="index-1 letter-f" aria-hidden="true">f</span><span class="index-2 letter-o" aria-hidden="true">o</span><span class="index-3 letter-o" aria-hidden="true">o</span>'
)
})
test('supports passing in a function for splitting the `element` contents', function (t) {
t.plan(2)
const element = createElement('foo bar')
charming(element, {
split: function (string) {
return string.split(/(\s+)/)
},
setClassName: function (index) {
return `word-${index}`
}
})
t.equal(element.getAttribute('aria-label'), 'foo bar')
t.equal(
element.innerHTML,
'<span class="word-1" aria-hidden="true">foo</span><span class="word-2" aria-hidden="true"> </span><span class="word-3" aria-hidden="true">bar</span>'
)
})