-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwaitForBinding.spec.js
89 lines (77 loc) · 2.11 KB
/
waitForBinding.spec.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'use strict'
const ko = require('knockout')
const { renderComponent } = require('../src')
const { expect } = require('chai')
describe('waitForBinding' , () => {
let $el
before(() => {
ko.bindingHandlers.asyncText = {
init(el, valueAccessor) {
setTimeout(() => {
ko.applyBindingsToNode(el, {
text: valueAccessor()
})
}, 200)
return { controlsDescendantBindings: true }
}
}
ko.bindingHandlers.asyncVisible = {
init(el, valueAccessor) {
setTimeout(() => {
ko.applyBindingsToNode(el, {
visible: valueAccessor()
})
}, 200)
return { controlsDescendantBindings: true }
}
}
})
after(() => {
ko.options.deferUpdates = false
ko.bindingHandlers.asyncText = (void 0)
})
afterEach(() => {
$el.dispose()
})
it('should throw error if binding is not defined', (done) => {
$el = renderComponent({
template: '<span class="test-me" data-bind="notdefined: \'\'"></span>',
viewModel() { }
})
const $$el = $el.find('.test-me')
try {
$$el.waitForBinding('notdefined')
}
catch (e) {
expect(e.message).to.contain('binding does not exist')
}
done()
})
it('works with bindings that have init funcs', (done) => {
$el = renderComponent({
template: `
<span class="ignore-me" data-bind="asyncText: greeting"></span>
<span class="test-me" data-bind="asyncText: greeting"></span>
`,
viewModel() { this.greeting = 'Hello Component' }
})
const $$el = $el.find('.test-me')
$$el.waitForBinding('text').then(() => {
expect($$el.html()).to.contain('Hello Component')
done()
})
})
it('works with bindings that DON\'T have init funcs', (done) => {
$el = renderComponent({
template: `
<span class="test-me" data-bind="asyncVisible: visible"></span>
`,
viewModel() { this.visible = false }
})
const $$el = $el.find('.test-me')
$$el.waitForBinding('visible').then(() => {
expect($$el).to.not.be.visible
done()
})
})
})