forked from kentcdodds/dom-testing-library-with-anything
-
Notifications
You must be signed in to change notification settings - Fork 0
/
from-html.test.js
53 lines (47 loc) · 1.12 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
import 'jest-dom/extend-expect'
import fromHTML from 'from-html/lib/from-html'
import {getQueriesForElement, fireEvent} from 'dom-testing-library'
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 'dom-testing-library'
// export {render}
// tests:
test('counter increments', () => {
const {getByText} = render(Counter)
const counter = getByText('0')
fireEvent.click(counter)
expect(counter).toHaveTextContent('1')
fireEvent.click(counter)
expect(counter).toHaveTextContent('2')
})