-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathBJB-tests.js
381 lines (342 loc) · 12.1 KB
/
BJB-tests.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
var expect = chai.expect
describe('bacon-dom', function() {
describe('textField.value', function() {
var field
beforeEach(function() {
$('#bacon-dom').html('<input type="text" id="text" value="defaultVal">')
field = $('#bacon-dom #text')
})
describe('with initVal', function() {
it('sets value to DOM', function() {
var model = Bacon.$.textField.value(field, 'initVal')
expect(field.val()).to.equal('initVal')
})
it('sets the initVal as the initial value of the model', function() {
var model = Bacon.$.textField.value(field, 'initVal')
specifyValue(model, 'initVal')
})
})
describe('without initVal', function() {
it('leaves DOM unaffected', function() {
var model = Bacon.$.textField.value(field)
expect(field.val()).to.equal('defaultVal')
})
it('uses value from DOM as initial value of the model', function() {
var model = Bacon.$.textField.value(field)
specifyValue(model, 'defaultVal')
})
describe('with empty default value', function() {
it('waits for browser to autofill textfield', function(done) {
field.val('')
var model = Bacon.$.textField.value(field)
model.filter(function (v) {
return v
}).onValue(function() {
specifyValue(model, 'newVal')
done()
})
field.val("newVal")
})
})
})
describe('when setting value of model', function() {
it('sets value to DOM', function() {
Bacon.$.textField.value(field).set('newVal')
expect(field.val()).to.equal('newVal')
})
})
describe('when DOM value changes', function() {
it('updates value of model', function() {
var model = Bacon.$.textField.value(field)
field.val("newVal")
field.trigger("keyup")
specifyValue(model, "newVal")
})
it('ignores duplicates', function() {
var model = Bacon.$.textField.value(field)
field.val("newVal")
field.trigger("keyup")
specifyValue(model, "newVal")
var values = collectValues(model)
field.trigger("keyup")
field.trigger("keyup")
expect(values).to.deep.equal(["newVal"])
})
})
describe('when element is not found', function() {
it('returns empty string as value', function() {
var model = Bacon.$.textField.value($('.asdfqwer'))
specifyValue(model, '')
})
})
})
describe('checkBox.value', function() {
var field
beforeEach(function() {
$('#bacon-dom').html('<input type="checkbox" id="checkbox">')
field = $('#bacon-dom #checkbox')
})
describe('with initVal', function() {
it('sets value to DOM', function() {
var model = Bacon.$.checkBox.value(field, true)
expect(field.prop("checked")).to.equal(true)
})
it('sets the initVal as the initial value of the model', function() {
var model = Bacon.$.checkBox.value(field, true)
specifyValue(model, true)
})
})
describe('without initVal', function() {
it('leaves DOM unaffected', function() {
var model = Bacon.$.checkBox.value(field)
expect(field.prop("checked")).to.equal(false)
})
it('uses value from DOM as initial value of the model', function() {
var model = Bacon.$.checkBox.value(field)
specifyValue(model, false)
})
})
describe('when setting value of model', function() {
it('sets value to DOM', function() {
Bacon.$.checkBox.value(field).set(true)
expect(field.prop("checked")).to.equal(true)
})
it('leaves defaultChecked property as is', function() {
Bacon.$.checkBox.value(field).set(true)
expect(field.prop("defaultChecked")).to.equal(false)
})
})
describe('when DOM value changes', function() {
it('updates value of model', function() {
var model = Bacon.$.checkBox.value(field)
field.trigger("click")
specifyValue(model, true)
})
})
describe('when element is not found', function() {
it('returns false as value', function() {
var model = Bacon.$.checkBox.value($('.asdfqwer'))
specifyValue(model, false)
})
})
})
describe('select.value', function() {
var field
beforeEach(function() {
$('#bacon-dom').html('<select id="select"><option value="a">A</option><option value="b" selected>B</option></select>')
field = $('#bacon-dom #select')
})
describe('with initVal', function() {
it('sets value to DOM', function() {
var model = Bacon.$.select.value(field, 'a')
expect(field.val()).to.equal('a')
})
it('sets the initVal as the initial value of the model', function() {
var model = Bacon.$.select.value(field, 'a')
specifyValue(model, 'a')
})
})
describe('without initVal', function() {
it('leaves DOM unaffected', function() {
var model = Bacon.$.select.value(field)
expect(field.val()).to.equal('b')
})
it('uses value from DOM as initial value of the model', function() {
var model = Bacon.$.select.value(field)
specifyValue(model, 'b')
})
})
describe('when setting value of model', function() {
it('sets value to DOM', function() {
Bacon.$.select.value(field).set('a')
expect(field.val()).to.equal('a')
})
})
describe('when DOM value changes', function() {
it('updates value of model', function() {
var model = Bacon.$.select.value(field)
field.val("a")
field.trigger("change")
specifyValue(model, "a")
})
})
})
describe('select.value without any options', function() {
it('sets `null` as initial value of the model', function() {
$('#bacon-dom').html('<select id="select"></select>')
var model = Bacon.$.select.value($('#bacon-dom #select'))
specifyValue(model, null)
})
})
describe('select.value when element is not found', function() {
it('sets `undefined` as initial value of the model', function() {
var model = Bacon.$.select.value($('.asdfqwer'))
specifyValue(model, undefined)
})
})
describe('radioGroup.value', function() {
testradioGroup.valueModel(Bacon.$.radioGroup.value, "a", "b")
})
describe('radioGroup.intValue', function() {
testradioGroup.valueModel(Bacon.$.radioGroup.intValue, 1, 2)
testradioGroup.valueModel(Bacon.$.radioGroup.intValue, 0, 23)
})
testEventHelper('click')
testEventHelper('keyup')
testEventHelper('keydown')
testEventHelper('mouseup')
$.mockjax({
url: "/test",
responseTime: 0,
responseText: "good"
})
describe("Observable.prototype.toDeferred", function(){
it("Converts EventStream into jQuery Deferred", function() {
testDeferred(Bacon.fromArray([1,2,3]), [1], [])
})
it("Converts Property into jQuery Deferred", function() {
testDeferred(Bacon.fromArray([1,2,3]).toProperty(), [1], [])
})
it("Respects Property initial value", function() {
testDeferred(Bacon.fromArray([1,2,3]).toProperty(0), [0], [])
})
it("Converts Errors", function() {
testDeferred(Bacon.fromArray([new Bacon.Error("err1"), new Bacon.Error("err2")]), [], ["err1"])
})
function testDeferred(observable, expectedValues, expectedErrors) {
var values = [], errors = []
observable.toDeferred().done(function(value) {
values.push(value)
}).fail(function(error) {
errors.push(error)
})
expect(values).to.deep.equal(expectedValues)
expect(errors).to.deep.equal(expectedErrors)
}
})
describe("AJAX", function() {
describe("Converts EventStream of requests into EventStream of responses", function() {
expectStreamValues(Bacon.once({url:"/test"}).ajax(), ["good"])
})
describe("Converts Property of requests into EventStream of responses", function() {
expectStreamValues(Bacon.once({url:"/test"}).toProperty().ajax(), ["good"])
})
})
})
function testRadioGroupValueModel(modelProvider, value1, value2) {
var firstRadio = asTestValueObject(value1)
var secondRadio = asTestValueObject(value2)
describe('with initVal', function() {
doTest('sets value to DOM', function(fields) {
var model = modelProvider(fields, firstRadio.value)
expect($(firstRadio.selector).prop("checked")).to.equal(true)
expect($(secondRadio.selector).prop("checked")).to.equal(false)
})
doTest('sets the initVal as the initial value of the model', function(fields) {
var model = modelProvider(fields, firstRadio.value)
specifyValue(model, firstRadio.value)
})
})
describe('without initVal', function() {
doTest('leaves DOM unaffected', function(fields) {
var model = modelProvider(fields)
expect($(secondRadio.selector).prop("checked")).to.equal(true)
expect($(firstRadio.selector).prop("checked")).to.equal(false)
})
doTest('uses value from DOM as initial value of the model', function(fields) {
var model = modelProvider(fields)
specifyValue(model, secondRadio.value)
})
})
describe('when setting value of model', function() {
doTest('sets value to DOM', function(fields) {
modelProvider(fields).set(firstRadio.value)
expect($(firstRadio.selector).prop("checked")).to.equal(true)
expect($(secondRadio.selector).prop("checked")).to.equal(false)
})
doTest('leaves defaultChecked property as is', function(fields) {
modelProvider(fields).set(firstRadio.value)
expect($(firstRadio.selector).prop("defaultChecked")).to.equal(false)
expect($(secondRadio.selector).prop("defaultChecked")).to.equal(true)
})
})
describe('when DOM value changes', function() {
doTest('updates value of model', function(fields) {
var model = modelProvider(fields)
$(secondRadio.selector).click()
$(firstRadio.selector).click()
specifyValue(model, firstRadio.value)
})
})
describe('when elements are not found', function() {
it('returns undefined as value', function() {
var model = modelProvider($(".asdfqwer"))
specifyValue(model, undefined)
})
})
function doTest(name, f) {
function setup() {
var elements = $(firstRadio.html + secondRadio.html)
elements.filter("input:last").attr("checked", "checked")
$('#bacon-dom').html(elements)
}
describe("with single jQuery object", function() {
before(setup)
it(name, function() { f($(firstRadio.selector + "," + secondRadio.selector)) })
})
describe("with array of jQuery objects", function() {
before(setup)
it(name, function() { f([$(firstRadio.selector), $(secondRadio.selector)]) })
})
}
function asTestValueObject(val) {
return {
value: val,
selector: "#" + val.toString(),
html: '<label for=":val">:val</label><input type="radio" id=":val" value=":val"><br>'.replace(/\:val/g, val.toString())
}
}
}
function expectStreamValues(stream, expectedValues) {
var values = []
before(function(done) {
stream.onValue(function(value) { values.push(value) })
stream.onEnd(done)
})
it("is an EventStream", function() {
expect(stream instanceof Bacon.EventStream).to.be.ok()
})
it("contains expected values", function() {
expect(values).to.deep.equal(expectedValues)
})
}
function testEventHelper(eventName) {
var methodName = eventName + "E"
describe(methodName, function() {
it("captures DOM events as EventStream", function() {
$('#bacon-dom').html('<input type="text" id="text">')
var el = $('#bacon-dom #text')
var stream = el[methodName]()
var values = collectValues(stream)
el[eventName]()
expect(values.length).to.equal(1)
})
})
}
function specifyValue(obs, expected) {
var gotIt = false
var value
obs.onValue(function(v) {
gotIt = true
value = v
})
expect(gotIt).to.equal(true)
expect(value).to.deep.equal(expected)
}
function collectValues(observable) {
var values = [];
observable.onValue(function(value) {
return values.push(value);
});
return values;
}