DataBindingTest.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.createSimpleDataBinding(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.createSimpleDataBinding(BABYLON.CustomerViewModel.cityProperty, "mainAddress.city");
  29. // Setting a dataSource should update the targets
  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.createSimpleDataBinding(BABYLON.CustomerViewModel.firstNameProperty, "firstName");
  57. // Setting a dataSource should update the targets
  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. it("binding Format", () => {
  72. let c = new BABYLON.Customer();
  73. c.firstName = "Loic Baumann";
  74. c.age = 40;
  75. // Create a View Model and a binding with an indirection
  76. let vm = new BABYLON.CustomerViewModel();
  77. // Setting a dataSource should setup vm.age with the binding source value
  78. vm.dataSource = c;
  79. // Create the binding and set it up
  80. let b = new BABYLON.Binding();
  81. b.propertyPathName = "firstName";
  82. b.stringFormat = v => `My Name is ${v}`;
  83. vm.createDataBinding(BABYLON.CustomerViewModel.firstNameProperty, b);
  84. // Check it's ok
  85. expect(vm.firstName).toBe("My Name is Loic Baumann", "binding string format doesn't work");
  86. // Bind age to city with "Age: $value" format
  87. b = new BABYLON.Binding();
  88. b.propertyPathName = "age";
  89. b.stringFormat = v => `Age: ${v}`;
  90. vm.createDataBinding(BABYLON.CustomerViewModel.cityProperty, b);
  91. // Check it's ok
  92. expect(vm.city).toBe("Age: 40", "binding string format doesn't work on non string source type");
  93. });
  94. it("binding custom source", () => {
  95. let c1 = new BABYLON.Customer();
  96. c1.firstName = "Loic Baumann";
  97. c1.age = 40;
  98. let c2 = new BABYLON.Customer();
  99. c2.firstName = "John Doe";
  100. c2.age = 20;
  101. // Create a View Model and a binding with an indirection
  102. let vm = new BABYLON.CustomerViewModel();
  103. // Setting a dataSource should setup vm.age with the binding source value
  104. vm.dataSource = c1;
  105. // Create the binding and set it up
  106. let b = new BABYLON.Binding();
  107. b.propertyPathName = "firstName";
  108. vm.createDataBinding(BABYLON.CustomerViewModel.firstNameProperty, b);
  109. // Bind age with a custom source
  110. b = new BABYLON.Binding();
  111. b.propertyPathName = "age";
  112. b.dataSource = c2;
  113. vm.createDataBinding(BABYLON.CustomerViewModel.ageProperty, b);
  114. // Check it's ok
  115. expect(vm.firstName).toBe("Loic Baumann", "binding string format doesn't work");
  116. // Check it's ok
  117. expect(vm.age).toBe(20, "binding string format doesn't work on non string source type");
  118. });
  119. });