import { Observable, fromObjectRecursive } from "data/observable"; import { ViewBase } from "ui/core/view-base"; import { BindingOptions } from "ui/core/bindable"; import * as TKUnit from "../TKUnit"; import * as types from "utils/types"; import * as helper from "../ui/helper"; import * as utils from "utils/utils"; import * as bindingBuilder from "ui/builder/binding-builder"; import * as fs from "file-system"; import * as appModule from "application"; import * as trace from "trace"; import { View } from "ui/core/view"; import { Button } from "ui/button"; import { Page } from "ui/page"; import { StackLayout } from "ui/layouts/stack-layout"; import { Label } from "ui/label"; import { TextField } from "ui/text-field"; // // For information and examples how to use bindings please refer to special [**Data binding**](../../../../bindings.md) topic. // // // 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. // export function test_Bindable_Members() { const obj = new ViewBase(); TKUnit.assert(types.isDefined(obj.bind), "Bindable.bind not defined"); TKUnit.assert(types.isDefined(obj.unbind), "Bindable.unbind not defined"); }; export function test_Bindable_Bind_ToTarget_OneWay() { const model = new Observable(); model.set("name", "John"); const options: BindingOptions = { sourceProperty: "name", targetProperty: "test" }; const obj = new ViewBase(); 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 function test_Bindable_Bind_ToTarget_TwoWay() { const model = new Observable(); model.set("name", "John"); const options: BindingOptions = { sourceProperty: "name", targetProperty: "test", twoWay: true }; const obj = new ViewBase(); obj.bind(options, model); obj.set("test", "Changed"); TKUnit.assertEqual(model.get("name"), "Changed", "Two-way binding not updating the source when target is changed."); model.set("name", "John"); TKUnit.assertEqual(obj.get("test"), "John", "Two-way binding not updating the target when source is changed."); }; export function test_Bindable_Bind_ToBindingContext_OneWay() { const model = new Observable(); model.set("name", "John"); const options: BindingOptions = { sourceProperty: "name", targetProperty: "test" }; const obj = new ViewBase(); 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 function test_Bindable_Bind_ToBindingContext_TwoWay() { const model = new Observable(); model.set("name", "John"); const options: BindingOptions = { sourceProperty: "name", targetProperty: "test", twoWay: true }; const obj = new ViewBase(); obj.bind(options); obj.set("test", "local"); obj.bindingContext = model; TKUnit.assertEqual(obj.get("test"), "John", "Binding to a context does not update the target property."); obj.set("test", "local"); TKUnit.assertEqual(model.get("name"), "local", "Two-way binding to a context does not update the source property."); }; export function test_Bindable_Unbind() { const model = new Observable(); const options: BindingOptions = { sourceProperty: "name", targetProperty: "test" }; const obj = new ViewBase(); 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", "Changed"); TKUnit.assert(obj.get("test") === "John", "Unbind does not remove binding."); }; export function test_bind_NoSource_WillUse_BindingContext() { const model = new Observable(); model.set("testProperty", "testValue"); const test = function (views: Array) { views[0].bindingContext = model; views[1].bind({ sourceProperty: "testProperty", targetProperty: "text" }); const button =