forked from kentcdodds/dom-testing-library-with-anything
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stimulus.test.js
45 lines (36 loc) · 1.08 KB
/
stimulus.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
import 'jest-dom/extend-expect'
import {Application, Controller} from 'stimulus'
import {getQueriesForElement, fireEvent} from 'dom-testing-library'
class Counter extends Controller {
static targets = ['output']
initialize() {
this.count = 0
}
increment() {
this.count = this.count + 1
this.outputTarget.textContent = this.count
}
}
async function render(identifier, controller, html) {
const container = document.createElement('div')
container.innerHTML = html
const application = await Application.start(container)
application.register(identifier, controller)
return {
container,
...getQueriesForElement(container),
}
}
test('counter increments', async () => {
const html = `
<div data-controller="counter">
<button data-action="counter#increment" data-target="counter.output">0</button>
</div>
`
const {getByText} = await render('counter', Counter, html)
const counter = getByText('0')
fireEvent.click(counter)
expect(counter).toHaveTextContent('1')
fireEvent.click(counter)
expect(counter).toHaveTextContent('2')
})