-
Notifications
You must be signed in to change notification settings - Fork 3
/
getComponentParams.spec.js
153 lines (128 loc) · 4.11 KB
/
getComponentParams.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict'
const ko = require('knockout')
const { renderHtml } = require('../src')
const { expect } = require('chai')
describe('getComponentParams' , () => {
afterEach(() => {
ko.components.unregister('foo')
})
it('can access/update component params with a function viewModel', () => {
ko.components.register('foo', {
template: '<div data-bind="text: baz"></div>',
viewModel(params) { this.baz = params.baz }
})
const $el = renderHtml({
template: `
<div>
<foo params="bar: bar, baz: baz"></foo>
</div>
`,
viewModel: {
bar: 'bar',
baz: ko.observable('baz')
}
})
expect($el.find('foo')).to.exist
expect($el.find('foo').getComponentParams().bar).to.equal('bar')
expect($el.find('foo').getComponentParams().baz()).to.equal('baz')
$el.find('foo').getComponentParams().baz('qux')
expect($el.find('foo').getComponentParams().baz()).to.equal('qux')
expect($el.find('foo').html()).contains('qux')
ko.components.unregister('foo')
})
it('can access/update component params with a class viewModel', () => {
ko.components.register('foo', {
template: '<div data-bind="text: baz"></div>',
viewModel: class ViewModel { constructor(params) { this.baz = params.baz } }
})
const $el = renderHtml({
template: `
<div>
<foo params="bar: bar, baz: baz"></foo>
</div>
`,
viewModel: {
bar: 'bar',
baz: ko.observable('baz')
}
})
expect($el.find('foo')).to.exist
expect($el.find('foo').getComponentParams().bar).to.equal('bar')
expect($el.find('foo').getComponentParams().baz()).to.equal('baz')
$el.find('foo').getComponentParams().baz('qux')
expect($el.find('foo').getComponentParams().baz()).to.equal('qux')
expect($el.find('foo').html()).contains('qux')
ko.components.unregister('foo')
})
it('can access/update component params with a factory viewModel', () => {
ko.components.register('foo', {
template: '<div data-bind="text: baz"></div>',
viewModel: {
createViewModel(params) {
class ViewModel { constructor(p) { this.baz = p.baz } }
return new ViewModel(params)
}
}
})
const $el = renderHtml({
template: `
<div>
<foo params="bar: bar, baz: baz"></foo>
</div>
`,
viewModel: {
bar: 'bar',
baz: ko.observable('baz')
}
})
expect($el.find('foo')).to.exist
expect($el.find('foo').getComponentParams().bar).to.equal('bar')
expect($el.find('foo').getComponentParams().baz()).to.equal('baz')
$el.find('foo').getComponentParams().baz('qux')
expect($el.find('foo').getComponentParams().baz()).to.equal('qux')
expect($el.find('foo').html()).contains('qux')
ko.components.unregister('foo')
})
it('can access/update component params with no viewModel', () => {
ko.components.register('foo', {
template: '<div data-bind="text: baz"></div>'
})
const $el = renderHtml({
template: `
<div>
<foo params="bar: bar, baz: baz"></foo>
</div>
`,
viewModel: {
bar: 'bar',
baz: ko.observable('baz')
}
})
expect($el.find('foo')).to.exist
expect($el.find('foo').getComponentParams().bar).to.equal('bar')
expect($el.find('foo').getComponentParams().baz()).to.equal('baz')
$el.find('foo').getComponentParams().baz('qux')
expect($el.find('foo').getComponentParams().baz()).to.equal('qux')
expect($el.find('foo').html()).contains('qux')
ko.components.unregister('foo')
})
it('can\'t access/update component params with a shared instance', () => {
ko.components.register('foo', {
template: '<div></div>',
viewModel: { instance: {} }
})
const $el = renderHtml({
template: `
<div>
<foo params="bar: bar, baz: baz"></foo>
</div>
`,
viewModel: {
bar: 'bar',
baz: ko.observable('baz')
}
})
expect($el.find('foo')).to.exist
expect($el.find('foo').getComponentParams()).to.be.undefined
})
})