forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbindable-tests.ts
More file actions
544 lines (425 loc) · 22.2 KB
/
bindable-tests.ts
File metadata and controls
544 lines (425 loc) · 22.2 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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
import observable = require("data/observable");
import bindable = require("ui/core/bindable");
import dependencyObservableModule = require("ui/core/dependency-observable");
import TKUnit = require("../TKUnit");
import types = require("utils/types");
import helper = require("../ui/helper");
import viewModule = require("ui/core/view");
import buttonModule = require("ui/button");
import utils = require("utils/utils");
import pageModule = require("ui/page");
import stackLayoutModule = require("ui/layouts/stack-layout");
import bindingBuilder = require("ui/builder/binding-builder");
import labelModule = require("ui/label");
import textFieldModule = require("ui/text-field");
import fs = require("file-system");
import appModule = require("application");
// <snippet module="ui/core/bindable" title="bindable">
// For information and examples how to use bindings please refer to special [**Data binding**](../../../../bindings.md) topic.
// </snippet>
// <snippet module="ui/core/weak-event-listener" title="weak-event-listener">
// For information and example how to use weak event listeners please refer to special [**Events**](../../../../events.md) topic which has a dedicated part about weak event listeners.
// </snippet>
export var test_Bindable_Members = function () {
var obj = new bindable.Bindable();
TKUnit.assert(types.isDefined(obj.bind), "Bindable.bind not defined");
TKUnit.assert(types.isDefined(obj.unbind), "Bindable.unbind not defined");
TKUnit.assert(types.isDefined(obj._updateTwoWayBinding), "Bindable.updateTwoWayBinding not defined");
}
export var test_Bindable_Bind_ToTarget_OneWay = function () {
var model = new observable.Observable();
model.set("name", "John");
var options: bindable.BindingOptions = {
sourceProperty: "name",
targetProperty: "test"
}
var obj = new bindable.Bindable();
obj.bind(options, model);
TKUnit.assert(obj.get("test") === "John", "Expected result after binding is [test value] === 'John'");
model.set("name", "Changed");
TKUnit.assert(obj.get("test") === "Changed", "Expected result after binding is [test value] === 'Changed'");
}
export var test_Bindable_Bind_ToTarget_TwoWay = function () {
var model = new observable.Observable();
model.set("name", "John");
var options: bindable.BindingOptions = {
sourceProperty: "name",
targetProperty: "test",
twoWay: true
}
var obj = new bindable.Bindable();
obj.bind(options, model);
obj.set("test", "Changed");
TKUnit.assert(model.get("name") === "Changed", "Two-way binding not updating the source when target is changed.");
model.set("name", "John");
TKUnit.assert(obj.get("test") === "John", "Two-way binding not updating the target when source is changed.");
}
export var test_Bindable_Bind_ToBindingContext_OneWay = function () {
var model = new observable.Observable();
model.set("name", "John");
var options: bindable.BindingOptions = {
sourceProperty: "name",
targetProperty: "test"
}
var obj = new bindable.Bindable();
obj.bind(options);
obj.set("test", "local");
obj.bindingContext = model;
TKUnit.assert(obj.get("test") === "John", "Binding to a context does not update the target property.");
}
export var test_Bindable_Bind_ToBindingContext_TwoWay = function () {
var model = new observable.Observable();
model.set("name", "John");
var options: bindable.BindingOptions = {
sourceProperty: "name",
targetProperty: "test",
twoWay: true
}
var obj = new bindable.Bindable();
obj.bind(options);
obj.set("test", "local");
obj.bindingContext = model;
TKUnit.assert(obj.get("test") === "John", "Binding to a context does not update the target property.");
obj.set("test", "local");
TKUnit.assert(model.get("name") === "local", "Two-way binding to a context does not update the source property.");
}
export var test_Bindable_Unbind = function () {
var model = new observable.Observable();
var options: bindable.BindingOptions = {
sourceProperty: "name",
targetProperty: "test"
}
var obj = new bindable.Bindable();
obj.bind(options, model);
model.set("name", "John");
TKUnit.assert(obj.get("test") === "John", "Binding does not updates target property.");
obj.unbind("test");
model.set("name", "Chaged");
TKUnit.assert(obj.get("test") === "John", "Unbind does not remove binding.");
}
export var test_bind_NoSource_WillUse_BindingContext = function () {
var model = new observable.Observable();
model.set("testProperty", "testValue");
var test = function (views: Array<viewModule.View>) {
views[0].bindingContext = model;
views[1].bind({
sourceProperty: "testProperty",
targetProperty: "text"
});
var button = <buttonModule.Button>views[1];
TKUnit.assert(button.text === model.get("testProperty"), "Bind method not working when no source is passed but a valid bindingContext is present.");
}
helper.do_PageTest_WithButton(test);
}
export var test_bindingContext_ValueSource_IsInherited = function () {
var test = function (views: Array<viewModule.View>) {
var context = {};
views[0].bindingContext = context;
TKUnit.assert(views[1].bindingContext === context, "bindingContext not inherited.");
TKUnit.assert(views[1]._getValueSource(bindable.Bindable.bindingContextProperty) === dependencyObservableModule.ValueSource.Inherited, "bindingContext should be propagated as Inherited.");
}
helper.do_PageTest_WithButton(test);
}
export var test_bindingContext_Change_IsReflected_Properly = function () {
var model = new observable.Observable();
model.set("testProperty", "testValue");
var test = function (views: Array<viewModule.View>) {
views[1].bind({
sourceProperty: "testProperty",
targetProperty: "text"
});
var button = <buttonModule.Button>views[1];
TKUnit.assert(button.text === "", "Bind should do nothing when no source and binding context are available.");
views[0].bindingContext = model;
TKUnit.assert(button.text === "testValue", "Binding not updated properly when a valid bindingContext is provided.");
views[0].bindingContext = undefined;
model.set("testProperty", "updatedValue");
TKUnit.assert(button.text === "testValue", "Binding not properly detached when bindingContext is cleared.");
}
helper.do_PageTest_WithButton(test);
}
export var test_WhenBindingIsSetToAnElement_AndElementIsRemoved_ShouldBeCollectedByGC = function (done) {
var testFinished = false;
var pageFactory = function () {
var page = new pageModule.Page();
var stack = new stackLayoutModule.StackLayout();
var expectedValue = "testValue";
var sourcePropertyName = "testProperty";
var targetPropertyName = "text";
page.on(viewModule.View.loadedEvent, () => {
var model = new observable.Observable();
model.set(sourcePropertyName, expectedValue);
function createButton(bindContext) {
var button = new buttonModule.Button();
button.bind({
sourceProperty: sourcePropertyName,
targetProperty: targetPropertyName
}, bindContext);
return new WeakRef(button);
}
var weakRef = createButton(model);
try {
stack.addChild(weakRef.get());
TKUnit.assert(weakRef.get().text === expectedValue, "Binding is not working properly!");
stack.removeChild(weakRef.get());
TKUnit.waitUntilReady(() => { return !weakRef.get().isLoaded });
utils.GC();
TKUnit.assert(!weakRef.get(), "UIElement is still alive!");
testFinished = true;
}
catch (e) {
done(e);
}
});
page.content = stack;
return page;
};
helper.navigate(pageFactory);
TKUnit.waitUntilReady(() => { return testFinished });
helper.goBack();
done(null);
}
export var test_OneBindableToBindMoreThanOneProperty_ToSameSource = function () {
var model = new observable.Observable();
var firstPropertyOptions: bindable.BindingOptions = {
sourceProperty: "name",
targetProperty: "test"
}
var secondPropertyOptions: bindable.BindingOptions = {
sourceProperty: "sourceProperty",
targetProperty: "targetProperty"
}
var obj = new bindable.Bindable();
obj.bind(firstPropertyOptions, model);
obj.bind(secondPropertyOptions, model);
model.set("name", "John");
model.set("sourceProperty", "testValue");
TKUnit.assert(obj.get("test") === "John", "Binding does not updates target property.");
TKUnit.assert(obj.get("targetProperty") === "testValue", "Binding does not updates target property1.");
}
export var test_MoreThanOneBindables_BindToASameSourceAndProperty = function () {
var model = new observable.Observable();
var bindingOptions: bindable.BindingOptions = {
sourceProperty: "sourceProperty",
targetProperty: "targetProperty"
};
var obj1 = new bindable.Bindable();
obj1.bind(bindingOptions, model);
var obj2 = new bindable.Bindable();
obj2.bind(bindingOptions, model);
model.set("sourceProperty", "testValue");
TKUnit.assert(obj1.get("targetProperty") === "testValue", "Binding does not updates target property for first object.");
TKUnit.assert(obj2.get("targetProperty") === "testValue", "Binding does not updates target property for second object.");
}
class TestClass extends bindable.Bindable {
private _value: string;
get value(): string {
return this._value
}
set value(v: string) {
if (v === "invalid") {
throw new Error("Trying to set invalid value.");
}
this._value = v;
}
};
export function test_WhenBindingSetsInvalidValue_NoExptionIsThrown() {
var model = new observable.Observable();
var options: bindable.BindingOptions = {
sourceProperty: "value",
targetProperty: "value"
}
var obj = new TestClass();
obj.bind(options, model);
model.set("value", "valid");
TKUnit.assert(obj.get("value") === "valid");
// Try set invalid value - no exception should be thrown and the value should remain the same.
model.set("value", "invalid");
TKUnit.assert(obj.get("value") === "valid");
}
export var test_binding_bindingContext_setRootContextFirst = function () {
var test = function (views: Array<viewModule.View>) {
var rootContext = new observable.Observable();
rootContext.set("selectedItem", "item 1");
views[0].bindingContext = rootContext;
var stack = <stackLayoutModule.StackLayout>views[1];
var options: bindable.BindingOptions = {
sourceProperty: "selectedItem",
targetProperty: "bindingContext"
}
stack.bind(options);
TKUnit.assertEqual(stack.bindingContext, "item 1", "Initial binding value");
TKUnit.assertEqual(views[2].bindingContext, "item 1", "Initial binding value");
rootContext.set("selectedItem", "item 2");
TKUnit.assertEqual(stack.bindingContext, "item 2", "Changed binding value");
TKUnit.assertEqual(views[2].bindingContext, "item 2", "Changed binding value");
}
helper.do_PageTest_WithStackLayout_AndButton(test);
}
export var test_binding_bindingContext_setBindingFirst = function () {
var test = function (views: Array<viewModule.View>) {
var rootContext = new observable.Observable();
rootContext.set("selectedItem", "item 1");
var stack = <stackLayoutModule.StackLayout>views[1];
var options: bindable.BindingOptions = {
twoWay: true,
sourceProperty: "selectedItem",
targetProperty: "bindingContext"
}
stack.bind(options);
views[0].bindingContext = rootContext;
TKUnit.assertEqual(stack.bindingContext, "item 1", "Initial binding value");
TKUnit.assertEqual(views[2].bindingContext, "item 1", "Initial binding value");
rootContext.set("selectedItem", "item 2");
TKUnit.assertEqual(stack.bindingContext, "item 2", "Changed binding value");
TKUnit.assertEqual(views[2].bindingContext, "item 2", "Changed binding value");
}
helper.do_PageTest_WithStackLayout_AndButton(test);
}
export var test_Bindable_BindingContext_Number_DoesNotThrow = function () {
var obj = new bindable.Bindable();
obj.bindingContext = 42;
}
export var test_Bindable_BindingContext_Boolean_DoesNotThrow = function () {
var obj = new bindable.Bindable();
obj.bindingContext = true;
}
export var test_Bindable_BindingContext_String_DoesNotThrow = function () {
var options: bindable.BindingOptions = {
sourceProperty: "length",
targetProperty: "test"
}
var obj = new bindable.Bindable();
obj.bind(options);
obj.set("test", "local");
obj.bindingContext = "string";
TKUnit.assert(obj.get("test") === 6, "Expected: 6; Actual: " + obj.get("test"));
}
export var test_getBindableOptionsFromStringFullFormat = function () {
var bindingExpression = "bindProperty, bindProperty * 2, false";
var bindOptions = bindingBuilder.getBindingOptions("targetBindProperty", bindingExpression);
TKUnit.assert(bindOptions.sourceProperty === "bindProperty", "Expected: bindProperty, Actual: " + bindOptions.sourceProperty);
TKUnit.assert(bindOptions.targetProperty === "targetBindProperty", "Expected: targetBindProperty, Actual: " + bindOptions.targetProperty);
TKUnit.assert(bindOptions.expression === "bindProperty * 2", "Expected: bindProperty * 2, Actual:" + bindOptions.expression);
TKUnit.assert(bindOptions.twoWay === false, "Expected: false, Actual: " + bindOptions.twoWay);
}
export var test_getBindableOptionsFromStringShortFormatExpression = function () {
var bindingExpression = "bindProperty * 2";
var bindOptions = bindingBuilder.getBindingOptions("targetBindProperty", bindingExpression);
TKUnit.assert(bindOptions.sourceProperty === "bindProperty", "Expected: bindProperty, Actual: " + bindOptions.sourceProperty);
TKUnit.assert(bindOptions.targetProperty === "targetBindProperty", "Expected: targetBindProperty, Actual: " + bindOptions.targetProperty);
TKUnit.assert(bindOptions.expression === "bindProperty * 2", "Expected: bindProperty * 2, Actual: " + bindOptions.expression);
TKUnit.assert(bindOptions.twoWay === true, "Expected: true, Actual: " + bindOptions.twoWay);
}
export var test_getBindableOptionsFromStringShortFormatProperty = function () {
var bindingExpression = "bindProperty";
var bindOptions = bindingBuilder.getBindingOptions("targetBindProperty", bindingExpression);
TKUnit.assert(bindOptions.sourceProperty === "bindProperty", "Expected: bindProperty, Actual: " + bindOptions.sourceProperty);
TKUnit.assert(bindOptions.targetProperty === "targetBindProperty", "Expected: targetBindProperty, Actual: " + bindOptions.targetProperty);
TKUnit.assert(bindOptions.expression === undefined, "Expected: null, Actual: " + bindOptions.expression);
TKUnit.assert(bindOptions.twoWay === true, "Expected: true, Actual: " + bindOptions.twoWay);
}
export var test_getBindableOptionsFromStringTwoParamsFormat = function () {
var bindingExpression = "bindProperty, bindProperty * 2";
var bindOptions = bindingBuilder.getBindingOptions("targetBindProperty", bindingExpression);
TKUnit.assert(bindOptions.sourceProperty === "bindProperty", "Expected: bindProperty, Actual: " + bindOptions.sourceProperty);
TKUnit.assert(bindOptions.targetProperty === "targetBindProperty", "Expected: targetBindProperty, Actual: " + bindOptions.targetProperty);
TKUnit.assert(bindOptions.expression === "bindProperty * 2", "Expected: bindProperty * 2, Actual:" + bindOptions.expression);
TKUnit.assert(bindOptions.twoWay === true, "Expected: true, Actual: " + bindOptions.twoWay);
}
export var test_getBindableOptionsFromStringFullNamedFormat = function () {
var bindingExpression = "bindProperty, expression = bindProperty * 2, twoWay = false";
var bindOptions = bindingBuilder.getBindingOptions("targetBindProperty", bindingExpression);
TKUnit.assert(bindOptions.sourceProperty === "bindProperty", "Expected: bindProperty, Actual: " + bindOptions.sourceProperty);
TKUnit.assert(bindOptions.targetProperty === "targetBindProperty", "Expected: targetBindProperty, Actual: " + bindOptions.targetProperty);
TKUnit.assert(bindOptions.expression === "bindProperty * 2", "Expected: bindProperty * 2, Actual:" + bindOptions.expression);
TKUnit.assert(bindOptions.twoWay === false, "Expected: false, Actual: " + bindOptions.twoWay);
}
export var test_getBindableOptionsFromStringShortNamedFormatExpression = function () {
var bindingExpression = "sourceProperty = bindProperty, expression = bindProperty * 2";
var bindOptions = bindingBuilder.getBindingOptions("targetBindProperty", bindingExpression);
TKUnit.assert(bindOptions.sourceProperty === "bindProperty", "Expected: bindProperty, Actual: " + bindOptions.sourceProperty);
TKUnit.assert(bindOptions.targetProperty === "targetBindProperty", "Expected: targetBindProperty, Actual: " + bindOptions.targetProperty);
TKUnit.assert(bindOptions.expression === "bindProperty * 2", "Expected: bindProperty * 2, Actual: " + bindOptions.expression);
TKUnit.assert(bindOptions.twoWay === true, "Expected: true, Actual: " + bindOptions.twoWay);
}
export var test_getBindableOptionsFromStringShortNamedFormatProperty = function () {
var bindingExpression = "sourceProperty = bindProperty";
var bindOptions = bindingBuilder.getBindingOptions("targetBindProperty", bindingExpression);
TKUnit.assert(bindOptions.sourceProperty === "bindProperty", "Expected: bindProperty, Actual: " + bindOptions.sourceProperty);
TKUnit.assert(bindOptions.targetProperty === "targetBindProperty", "Expected: targetBindProperty, Actual: " + bindOptions.targetProperty);
TKUnit.assert(bindOptions.expression === undefined, "Expected: null, Actual: " + bindOptions.expression);
TKUnit.assert(bindOptions.twoWay === true, "Expected: true, Actual: " + bindOptions.twoWay);
}
export var test_getBindableOptionsFromStringTwoParamsNamedFormat = function () {
var bindingExpression = "bindProperty, expression = bindProperty * 2";
var bindOptions = bindingBuilder.getBindingOptions("targetBindProperty", bindingExpression);
TKUnit.assert(bindOptions.sourceProperty === "bindProperty", "Expected: bindProperty, Actual: " + bindOptions.sourceProperty);
TKUnit.assert(bindOptions.targetProperty === "targetBindProperty", "Expected: targetBindProperty, Actual: " + bindOptions.targetProperty);
TKUnit.assert(bindOptions.expression === "bindProperty * 2", "Expected: bindProperty * 2, Actual:" + bindOptions.expression);
TKUnit.assert(bindOptions.twoWay === true, "Expected: true, Actual: " + bindOptions.twoWay);
}
export var test_TwoElementsBindingToSameBindingContext = function () {
var testFunc = function (page: pageModule.Page) {
var upperStackLabel = <labelModule.Label>(page.getViewById("upperStackLabel"));
var label1 = <labelModule.Label>(page.getViewById("label1"));
var label2 = <labelModule.Label>(page.getViewById("label2"));
TKUnit.assertEqual(upperStackLabel.text, label1.text);
TKUnit.assertEqual(upperStackLabel.text, label2.text);
}
var moduleName = __dirname.substr(fs.knownFolders.currentApp().path.length);
helper.navigateToModuleAndRunTest(("." + moduleName + "/bindingContext_testPage"), null, testFunc);
}
export var test_BindingContext_NavigatingForwardAndBack = function () {
var expectedValue = "Tralala";
var testFunc = function (page: pageModule.Page) {
var innerTestFunc = function (childPage: pageModule.Page) {
var testTextField: textFieldModule.TextField = <textFieldModule.TextField>(childPage.getViewById("testTextField"));
testTextField.text = expectedValue;
};
var moduleName = __dirname.substr(fs.knownFolders.currentApp().path.length);
helper.navigateToModuleAndRunTest(("." + moduleName + "/bindingContext_testPage2"), page.bindingContext, innerTestFunc);
var testLabel: labelModule.Label = <labelModule.Label>(page.getViewById("testLabel"));
TKUnit.assertEqual(testLabel.text, expectedValue);
}
var moduleName = __dirname.substr(fs.knownFolders.currentApp().path.length);
helper.navigateToModuleAndRunTest(("." + moduleName + "/bindingContext_testPage1"), null, testFunc);
}
export var test_BindingToSource_FailsAfterBindingContextChange = function () {
var createLabel = function () {
var label = new labelModule.Label();
return label;
}
var labelViewModel = new observable.Observable();
var expectedValue = "testValue";
labelViewModel.set("testProperty", expectedValue);
var testFunc = function (views: Array<viewModule.View>) {
var testLabel = <labelModule.Label>(views[0]);
testLabel.bind({ sourceProperty: "testProperty", targetProperty: "text" }, labelViewModel);
var page = <pageModule.Page>(views[1]);
page.bindingContext = new observable.Observable;
TKUnit.assertEqual(testLabel.text, expectedValue);
}
helper.buildUIAndRunTest(createLabel(), testFunc);
}
export function test_BindingToDictionaryAtAppLevel() {
var createLabel = function () {
var label = new labelModule.Label();
return label;
}
var pageViewModel = new observable.Observable();
var testPropertyName = "testValue";
var expectedValue = "expectedValue";
pageViewModel.set("testProperty", testPropertyName);
var dict = {};
dict[testPropertyName] = expectedValue;
appModule.resources["dict"] = dict;
var testFunc = function (views: Array<viewModule.View>) {
var testLabel = <labelModule.Label>(views[0]);
testLabel.bind({ sourceProperty: "testProperty", targetProperty: "text", expression: "dict[testProperty]" });
var page = <pageModule.Page>(views[1]);
page.bindingContext = pageViewModel;
TKUnit.assertEqual(testLabel.text, expectedValue);
}
helper.buildUIAndRunTest(createLabel(), testFunc);
}