import { Observable, fromObject, fromObjectRecursive } from "@nativescript/core/data/observable"; import { ViewBase } from "@nativescript/core/ui/core/view-base"; import { BindingOptions } from "@nativescript/core/ui/core/bindable"; import * as TKUnit from "../../../tk-unit"; import * as types from "@nativescript/core/utils/types"; import * as helper from "../../../ui-helper"; import * as utils from "@nativescript/core/utils/utils"; import * as bindingBuilder from "@nativescript/core/ui/builder/binding-builder"; import * as appModule from "@nativescript/core/application"; import * as trace from "@nativescript/core/trace"; import { View } from "@nativescript/core/ui/core/view"; import { Button } from "@nativescript/core/ui/button"; import { Page } from "@nativescript/core/ui/page"; import { StackLayout } from "@nativescript/core/ui/layouts/stack-layout"; import { Label } from "@nativescript/core/ui/label"; import { TextField } from "@nativescript/core/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 Label(); TKUnit.assert(types.isDefined(obj.bind), "Bindable.bind not defined"); TKUnit.assert(types.isDefined(obj.unbind), "Bindable.unbind not defined"); } export function test_Binding_to_bindingContext_of_View() { const target = new Button(); const source = new Button(); target.bind({ targetProperty: "bindingContext", sourceProperty: "text" }, source); source.text = "a"; TKUnit.assertEqual(target.bindingContext, "a"); } export function test_Bindable_Bind_ToTarget_OneWay() { const model = new Observable(); model.set("name", "John"); const options: BindingOptions = { sourceProperty: "name", targetProperty: "text" }; const obj = new Label(); obj.bind(options, model); TKUnit.assert(obj.get("text") === "John", "Expected result after binding is [test value] === 'John'"); model.set("name", "Changed"); TKUnit.assert(obj.get("text") === "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: "text", twoWay: true }; const obj = new Label(); obj.bind(options, model); obj.set("text", "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("text"), "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: "text" }; const obj = new Label(); obj.bind(options); obj.set("text", "local"); obj.bindingContext = model; TKUnit.assert(obj.get("text") === "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: "text", twoWay: true }; const obj = new Label(); obj.bind(options); obj.set("text", "local"); obj.bindingContext = model; TKUnit.assertEqual(obj.get("text"), "John", "Binding to a context does not update the target property."); obj.set("text", "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: "text" }; const obj = new Label(); obj.bind(options, model); model.set("name", "John"); TKUnit.assert(obj.get("text") === "John", "Binding does not updates target property."); obj.unbind("text"); model.set("name", "Changed"); TKUnit.assert(obj.get("text") === "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 =