forked from kentcdodds/dom-testing-library-with-anything
-
Notifications
You must be signed in to change notification settings - Fork 0
/
from-html.test.js
54 lines (48 loc) · 1.18 KB
/
from-html.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
import '@testing-library/jest-dom/extend-expect'
import fromHTML from 'from-html/lib/from-html'
import {getQueriesForElement} from '@testing-library/dom'
import userEvent from '@testing-library/user-event'
class Counter {
constructor() {
fromHTML(
`
<div ref="container">
<button ref="counter" on="click">0</button>
</div>
`,
this,
'refs',
)
}
mount(target) {
target.append(this.refs.container)
}
handleEvent({type}) {
if (type === 'click') {
const {counter} = this.refs
counter.textContent = Number(counter.textContent) + 1
}
}
}
// from-html-testing-library
function render(FromHtmlClass) {
const instance = new FromHtmlClass()
const container = document.createElement('div')
instance.mount(container)
return {
container,
instance,
...getQueriesForElement(container),
}
}
// export * from '@testing-library/dom'
// export {render}
// tests:
test('counter increments', () => {
const {getByText} = render(Counter)
const counter = getByText('0')
userEvent.click(counter)
expect(counter).toHaveTextContent('1')
userEvent.click(counter)
expect(counter).toHaveTextContent('2')
})