-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample.js
57 lines (45 loc) · 1.99 KB
/
example.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
const vhs = require('.')
const { Simple, Timer } = require('./components')
vhs('A simple mounting of some html async/await', async t => {
const exampleComponent = new Simple('This should be loaded')
t.element.appendChild(exampleComponent.element)
await t.onload(exampleComponent.element)
// t.click takes a query selector rooted from the test element
await t.click('button')
t.equal(exampleComponent.element.querySelector('.counter').innerText, 'Counter: 1')
// t.click also takes an element
await t.click(t.element.querySelector('button'))
t.equal(exampleComponent.element.querySelector('.counter').innerText, 'Counter: 2')
// you can also directly interact with elements but you may need to await t.raf()
// to wait for updates
t.element.querySelector('button').click()
await t.raf()
t.equal(exampleComponent.element.querySelector('.counter').innerText, 'Counter: 3')
})
vhs('A simple mounting of some html', t => {
const exampleComponent = new Simple('This should be loaded')
t.element.appendChild(exampleComponent.element)
setTimeout(() => {
exampleComponent.element.querySelector('button').click()
t.end()
}, 500)
})
vhs.slow('A simple component mounted with a "slow" helper', async t => {
// mount component and wait for loading
const timerComponent = new Timer()
t.element.appendChild(timerComponent.element)
await t.onload(timerComponent.element)
// start increment by one each second
await t.click('button') // => count should be equal to 0
await t.click('button') // => count should be equal to 5
t.equal(timerComponent.element.querySelector('#counter').innerText, 'Count: 5')
})
vhs('A simple appended div in a simple html component', async t => {
const component = new Simple('Test of t.appendChild()')
await t.appendChild(component.element)
// append a new div
const newDiv = document.createElement('div')
newDiv.innerText = 'New div to append'
await t.appendChild(newDiv)
await t.removeChild(newDiv, 'Removed child from test element')
})