-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathidentifierBehaviors.ts
More file actions
202 lines (176 loc) Β· 6.63 KB
/
identifierBehaviors.ts
File metadata and controls
202 lines (176 loc) Β· 6.63 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { options, triggerEvent } from '@tko/utils'
import { DataBindProvider } from '@tko/provider.databind'
import { bindings as coreBindings } from '@tko/binding.core'
import { observable } from '@tko/observable'
import { computed } from '@tko/computed'
import { applyBindings, dataFor, bindingContext } from '@tko/bind'
import { Identifier, Arguments } from '../dist'
import { assert } from 'chai'
describe('Identifier', function () {
function testLookup(identifier, $data) {
const ctx = new bindingContext($data)
return new Identifier(null, identifier).get_value(undefined, ctx, {})
}
function testWrite(identifier, $data, newValue) {
const ctx = new bindingContext($data)
return new Identifier(null, identifier).set_value(newValue, ctx, {})
}
const c = 'Z',
f = function () {
return 'Fv'
}
it('looks up values on the parser context', function () {
const context = { c: c, f: f }
assert.equal(testLookup('c', context), 'Z')
assert.equal(testLookup('f', context), f)
})
it('looks up values on no-prototype $data', function () {
const $data = Object.create(null)
$data.c = c
assert.equal(testLookup('c', $data), 'Z')
})
it('returns null as expected', function () {
assert.equal(testLookup('$data', null), null)
})
it('returns undefined as expected', function () {
assert.equal(testLookup('$data', undefined), undefined)
})
it('sets plain values on $data', () => {
const $data = { c: c }
assert.equal($data.c, 'Z')
testWrite('c', $data, 'X')
assert.equal($data.c, 'X')
})
it('sets observable values on $data', () => {
const $data = { c: observable(c) }
assert.equal($data.c(), 'Z')
testWrite('c', $data, 'X')
assert.equal($data.c(), 'X')
})
it('sets plain values on no-prototype $data', () => {
const $data = Object.create(null)
$data.c = c
testWrite('c', $data, 'X')
assert.equal($data.c, 'X')
})
it('dereferences values on the parser', function () {
const context = new bindingContext({ f: f })
const fake_args = new Arguments(null, [])
const derefs = [fake_args]
const identifier = new Identifier(null, 'f', derefs)
assert.equal(identifier.get_value(undefined, context), 'Fv')
})
describe('the dereference function', function () {
it('does nothing with no references', function () {
let refs = Array(),
ident = new Identifier({}, 'x', refs)
assert.equal(ident.dereference('1', {}), 1)
})
it('does nothing with empty array references', function () {
const refs = new Array(),
ident = new Identifier({}, 'x', refs)
assert.equal(ident.dereference('1', {}), 1)
})
it('applies the functions of the refs to the value', function () {
const fake_args = new Arguments(null, []),
refs = [fake_args, fake_args],
ident = new Identifier({}, 'x', refs),
g = function () {
return '42'
},
f = function () {
return g
}
assert.equal(ident.dereference(f, {}), 42)
})
it('sets `this` to the parent member', function () {
const div = document.createElement('div'),
context = {
fn: function () {
assert.isObject(this)
assert.equal(this, context)
return 'ahab'
},
moby: 'dick'
}
div.setAttribute('data-bind', 'text: $data.fn()')
options.bindingProviderInstance = new DataBindProvider()
options.bindingProviderInstance.bindingHandlers.set(coreBindings)
applyBindings(context, div)
assert.equal(div.textContent || div.innerText, 'ahab')
})
it('sets `this` of a called function', function () {
const div = document.createElement('div'),
P = function () {},
thisIs = observable(),
context = { p: new P() }
P.prototype.fn = function p_fn() {
thisIs(this)
}
div.setAttribute('data-bind', 'click: p.fn')
options.bindingProviderInstance = new DataBindProvider()
options.bindingProviderInstance.bindingHandlers.set(coreBindings)
applyBindings(context, div)
assert.equal(thisIs(), undefined)
triggerEvent(div, 'click')
assert.strictEqual(thisIs(), context.p)
})
it('uses `$data` as explicit `this` reference', function () {
const div = document.createElement('div'),
obs = observable(),
context = { fn: obs }
div.setAttribute('data-bind', 'click: => fn(this)')
options.bindingProviderInstance = new DataBindProvider()
options.bindingProviderInstance.bindingHandlers.set(coreBindings)
applyBindings(context, div)
assert.equal(obs(), undefined)
triggerEvent(div, 'click')
assert.strictEqual(obs(), context)
})
it('does not break `this`/prototype of observable/others', function () {
const div = document.createElement('div'),
comp = computed(function () {
return 'rrr'
}),
Fn = function ffn() {
this.comp = comp
},
context = { instance: new Fn() }
div.setAttribute('data-bind', 'check: instance.comp')
options.bindingProviderInstance = new DataBindProvider()
options.bindingProviderInstance.bindingHandlers.set({
check: function (params) {
assert.equal(this.value.peek(), 'rrr')
}
})
applyBindings(context, div)
triggerEvent(div, 'click')
})
it('sets `this` of a top-level item to $data', function () {
// `restoreAfter` is a global cleanup-stack helper from
// builds/knockout/helpers/mocha-test-helpers.js. Vitest runs
// each spec file in isolated module state so this mutation is
// invisible to later specs; in the browser /tests runner all
// specs share one module graph, so without this restore the
// mutated `bindingGlobals` leaks into every subsequent spec
// that resolves `$parent` / `$customProp` / β¦ against the
// global lookup, breaking 31 unrelated tests.
// @ts-ignore β global helper from mocha-test-helpers.js
restoreAfter(options, 'bindingGlobals')
options.bindingGlobals = Object.create({ Ramanujan: '1729' })
const div = document.createElement('div'),
context = {
fn: function () {
assert.isObject(this)
assert.strictEqual(dataFor(div), this, '$data')
return 'sigtext'
}
}
div.setAttribute('data-bind', 'text: fn()')
options.bindingProviderInstance = new DataBindProvider()
options.bindingProviderInstance.bindingHandlers.set(coreBindings)
applyBindings(context, div)
assert.equal(div.textContent || div.innerText, 'sigtext')
})
})
})