forked from kentcdodds/dom-testing-library-with-anything
-
Notifications
You must be signed in to change notification settings - Fork 0
/
knockout.test.js
34 lines (30 loc) · 861 Bytes
/
knockout.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
import {getQueriesForElement} from '@testing-library/dom'
import '@testing-library/jest-dom/extend-expect'
import userEvent from '@testing-library/user-event'
import $ from 'jquery'
import ko from 'knockout'
var viewModel = {
counter: ko.observable(0),
increment: function () {
this.counter(this.counter() + 1)
},
}
$.fn.countify = function countify() {
this.html(`
<div>
<button data-bind='text: counter, click: increment'></button>
</div>
`)
ko.applyBindings(viewModel, this.find('button')[0])
}
// tests:
test('counter increments', () => {
const div = document.createElement('div')
$(div).countify()
const {getByText} = getQueriesForElement(div)
const counter = getByText('0')
userEvent.click(counter)
expect(counter).toHaveTextContent('1')
userEvent.click(counter)
expect(counter).toHaveTextContent('2')
})