-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest.js
More file actions
128 lines (113 loc) · 4.32 KB
/
Copy pathtest.js
File metadata and controls
128 lines (113 loc) · 4.32 KB
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
125
126
127
128
import {assert} from '@open-wc/testing'
import {FilterInputElement} from '../src/index.ts'
describe('filter-input', function () {
describe('element creation', function () {
it('creates from document.createElement', function () {
const el = document.createElement('filter-input')
assert.equal('FILTER-INPUT', el.nodeName)
})
it('creates from constructor', function () {
const el = new FilterInputElement()
assert.equal('FILTER-INPUT', el.nodeName)
})
})
describe('after tree insertion', function () {
let filterInput
let input
let list
let emptyState
let newItem
beforeEach(function () {
document.body.innerHTML = `
<filter-input aria-owns="robots">
<label>
Filter robots
<input type="text" autofocus autocomplete="off">
</label>
</filter-input>
<div id="robots">
<ul data-filter-list>
<li>Bender</li>
<li>Hubot</li>
<li>Wall-E</li>
<li>BB-8</li>
</ul>
<p data-filter-empty-state hidden>0 robots found.</p>
<form data-filter-new-item hidden>
<button data-filter-new-item-value>Create "<span data-filter-new-item-text></span>"</button>
</form>
</div>
`
filterInput = document.querySelector('filter-input')
input = filterInput.querySelector('input')
list = document.querySelector('[data-filter-list]')
emptyState = document.querySelector('[data-filter-empty-state]')
newItem = document.querySelector('[data-filter-new-item]')
})
afterEach(function () {
document.body.innerHTML = ''
})
it('filters and toggles new item form', async function () {
const listener = once('filter-input-updated')
changeValue(input, 'hu')
const customEvent = await listener
assert.equal(customEvent.detail.count, 1)
assert.equal(customEvent.detail.total, 4)
changeValue(input, 'BB-8 robot')
assert.notOk(newItem.hidden, 'New item form should be shown')
assert.equal(newItem.querySelector('[data-filter-new-item-value]').value, 'BB-8 robot')
assert.equal(newItem.querySelector('[data-filter-new-item-text]').textContent, 'BB-8 robot')
})
it('filters and toggles blankslate', async function () {
// Remove new item form, which is prioritized over blankslate
newItem.remove()
const listener = once('filter-input-updated')
changeValue(input, 'hu')
const customEvent = await listener
const results = Array.from(list.children).filter(el => !el.hidden)
assert.equal(results.length, 1)
assert.equal(results[0].textContent, 'Hubot')
assert.equal(customEvent.detail.count, 1)
assert.equal(customEvent.detail.total, 4)
changeValue(input, 'boom')
assert.notOk(emptyState.hidden, 'Empty state should be shown')
})
it('filters with custom filter', async function () {
filterInput.filter = (_item, itemText) => {
return {match: itemText.indexOf('-') >= 0}
}
const listener = once('filter-input-updated')
changeValue(input, ':)')
const customEvent = await listener
const results = Array.from(list.children).filter(el => !el.hidden)
assert.equal(results.length, 2)
assert.equal(results[0].textContent, 'Wall-E')
assert.equal(customEvent.detail.count, 2)
assert.equal(customEvent.detail.total, 4)
})
it('filters again with the same value when a change event is fired', async function () {
const listener = once('filter-input-updated')
changeValue(input, '-')
const customEvent = await listener
assert.equal(customEvent.detail.count, 2)
assert.equal(customEvent.detail.total, 4)
const newRobot = document.createElement('li')
newRobot.textContent = 'R2-D2'
list.append(newRobot)
const listener2 = once('filter-input-updated')
changeValue(input, '-')
const customEvent2 = await listener2
assert.equal(customEvent2.detail.count, 3)
assert.equal(customEvent2.detail.total, 5)
})
})
})
function changeValue(input, value) {
input.value = value
input.dispatchEvent(new Event('change', {bubbles: true}))
}
function once(eventName) {
return new Promise(resolve => {
document.addEventListener(eventName, resolve, {once: true})
})
}