DataBindingTest.ts 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /// <reference path="../../../src/canvas2d/babylon.smartpropertyprim.ts" />
  2. /// <reference path="testclasses.ts" />
  3. describe("GUI - Data Binding", () => {
  4. it("target update, no indirection",
  5. () => {
  6. // Create a customer, set its age
  7. let c = new BABYLON.Customer();
  8. c.age = 18;
  9. // Create a View Model and a binding
  10. let vm = new BABYLON.CustomerViewModel();
  11. vm.registerSimpleDataBinding(BABYLON.CustomerViewModel.ageProperty, "age");
  12. // Setting a dataSource should setup vm.age with the binding source value
  13. vm.dataSource = c;
  14. // Check it's ok
  15. expect(vm.age).toBe(18);
  16. // Change the source value, check the target is updated
  17. c.age = 19;
  18. expect(vm.age).toBe(19);
  19. }
  20. );
  21. it("target update, with indirection",
  22. () => {
  23. // Create a customer, set its city
  24. let c = new BABYLON.Customer();
  25. c.mainAddress.city = "Pontault Combault";
  26. // Create a View Model and a binding with an indirection
  27. let vm = new BABYLON.CustomerViewModel();
  28. vm.registerSimpleDataBinding(BABYLON.CustomerViewModel.cityProperty, "mainAddress.city");
  29. // Setting a dataSource should setup vm.age with the binding source value
  30. vm.dataSource = c;
  31. // Check it's ok
  32. expect(vm.city).toBe("Pontault Combault", "setting a new dataSource didn't immediately update the target");
  33. // Change the source value, check the target is updated
  34. c.mainAddress.city = "Paris";
  35. expect(vm.city).toBe("Paris", "changing source property didn't update the target property");
  36. // Change the address object, target should be updated
  37. let address = new BABYLON.Address();
  38. address.city = "Seattle";
  39. let oldAddress = c.mainAddress;
  40. c.mainAddress = address;
  41. expect(vm.city).toBe("Seattle", "changing intermediate object (the address) didn't update the target");
  42. // Check that if we change again inside Address, it still works
  43. c.mainAddress.city = "Redmond";
  44. expect(vm.city).toBe("Redmond", "changing final source property didn't change the target");
  45. // Now checks that changing the oldAddress city doesn't change the target
  46. oldAddress.city = "Berlin";
  47. expect(vm.city).not.toBe("Berlin", "Changed old address changed the target, which should not");
  48. }
  49. );
  50. it("target, one time update",
  51. () => {
  52. let c = new BABYLON.Customer();
  53. c.firstName = "Loic Baumann";
  54. // Create a View Model and a binding with an indirection
  55. let vm = new BABYLON.CustomerViewModel();
  56. vm.registerSimpleDataBinding(BABYLON.CustomerViewModel.firstNameProperty, "firstName");
  57. // Setting a dataSource should setup vm.age with the binding source value
  58. vm.dataSource = c;
  59. // Check it's ok
  60. expect(vm.firstName).toBe("Loic Baumann", "setting a new dataSource didn't immediately update the target with one time binding");
  61. // A change of the source shouldn't update the target
  62. c.firstName = "Nockawa";
  63. expect(vm.firstName).not.toBe("Nockawa", "Changing source property of a One Time binding updated the target, which should not");
  64. // A change of dataSource should update the target
  65. let c2 = new BABYLON.Customer();
  66. c2.firstName = "John";
  67. vm.dataSource = c2;
  68. expect(vm.firstName).toBe("John", "setting a new dataSource again didn't immediately update the target with one time binding");
  69. }
  70. );
  71. });