DataBindingTest.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /// <reference path="../../../src/canvas2d/babylon.smartpropertyprim.ts" />
  2. /// <reference path="testclasses.ts" />
  3. describe("GUI - Data Binding", function () {
  4. it("target update, no indirection", function () {
  5. // Create a customer, set its age
  6. var c = new BABYLON.Customer();
  7. c.age = 18;
  8. // Create a View Model and a binding
  9. var vm = new BABYLON.CustomerViewModel();
  10. vm.createSimpleDataBinding(BABYLON.CustomerViewModel.ageProperty, "age");
  11. // Setting a dataSource should setup vm.age with the binding source value
  12. vm.dataSource = c;
  13. // Check it's ok
  14. expect(vm.age).toBe(18);
  15. // Change the source value, check the target is updated
  16. c.age = 19;
  17. expect(vm.age).toBe(19);
  18. });
  19. it("target update, with indirection", function () {
  20. // Create a customer, set its city
  21. var c = new BABYLON.Customer();
  22. c.mainAddress.city = "Pontault Combault";
  23. // Create a View Model and a binding with an indirection
  24. var vm = new BABYLON.CustomerViewModel();
  25. vm.createSimpleDataBinding(BABYLON.CustomerViewModel.cityProperty, "mainAddress.city");
  26. // Setting a dataSource should update the targets
  27. vm.dataSource = c;
  28. // Check it's ok
  29. expect(vm.city).toBe("Pontault Combault", "setting a new dataSource didn't immediately update the target");
  30. // Change the source value, check the target is updated
  31. c.mainAddress.city = "Paris";
  32. expect(vm.city).toBe("Paris", "changing source property didn't update the target property");
  33. // Change the address object, target should be updated
  34. var address = new BABYLON.Address();
  35. address.city = "Seattle";
  36. var oldAddress = c.mainAddress;
  37. c.mainAddress = address;
  38. expect(vm.city).toBe("Seattle", "changing intermediate object (the address) didn't update the target");
  39. // Check that if we change again inside Address, it still works
  40. c.mainAddress.city = "Redmond";
  41. expect(vm.city).toBe("Redmond", "changing final source property didn't change the target");
  42. // Now checks that changing the oldAddress city doesn't change the target
  43. oldAddress.city = "Berlin";
  44. expect(vm.city).not.toBe("Berlin", "Changed old address changed the target, which should not");
  45. });
  46. it("target, one time update", function () {
  47. var c = new BABYLON.Customer();
  48. c.firstName = "Loic Baumann";
  49. // Create a View Model and a binding with an indirection
  50. var vm = new BABYLON.CustomerViewModel();
  51. vm.createSimpleDataBinding(BABYLON.CustomerViewModel.firstNameProperty, "firstName");
  52. // Setting a dataSource should update the targets
  53. vm.dataSource = c;
  54. // Check it's ok
  55. expect(vm.firstName).toBe("Loic Baumann", "setting a new dataSource didn't immediately update the target with one time binding");
  56. // A change of the source shouldn't update the target
  57. c.firstName = "Nockawa";
  58. expect(vm.firstName).not.toBe("Nockawa", "Changing source property of a One Time binding updated the target, which should not");
  59. // A change of dataSource should update the target
  60. var c2 = new BABYLON.Customer();
  61. c2.firstName = "John";
  62. vm.dataSource = c2;
  63. expect(vm.firstName).toBe("John", "setting a new dataSource again didn't immediately update the target with one time binding");
  64. });
  65. it("binding Format", function () {
  66. var c = new BABYLON.Customer();
  67. c.firstName = "Loic Baumann";
  68. c.age = 40;
  69. // Create a View Model and a binding with an indirection
  70. var vm = new BABYLON.CustomerViewModel();
  71. // Setting a dataSource should setup vm.age with the binding source value
  72. vm.dataSource = c;
  73. // Create the binding and set it up
  74. var b = new BABYLON.Binding();
  75. b.propertyPathName = "firstName";
  76. b.stringFormat = function (v) { return ("My Name is " + v); };
  77. vm.createDataBinding(BABYLON.CustomerViewModel.firstNameProperty, b);
  78. // Check it's ok
  79. expect(vm.firstName).toBe("My Name is Loic Baumann", "binding string format doesn't work");
  80. // Bind age to city with "Age: $value" format
  81. b = new BABYLON.Binding();
  82. b.propertyPathName = "age";
  83. b.stringFormat = function (v) { return ("Age: " + v); };
  84. vm.createDataBinding(BABYLON.CustomerViewModel.cityProperty, b);
  85. // Check it's ok
  86. expect(vm.city).toBe("Age: 40", "binding string format doesn't work on non string source type");
  87. });
  88. it("binding custom source", function () {
  89. var c1 = new BABYLON.Customer();
  90. c1.firstName = "Loic Baumann";
  91. c1.age = 40;
  92. var c2 = new BABYLON.Customer();
  93. c2.firstName = "John Doe";
  94. c2.age = 20;
  95. // Create a View Model and a binding with an indirection
  96. var vm = new BABYLON.CustomerViewModel();
  97. // Setting a dataSource should setup vm.age with the binding source value
  98. vm.dataSource = c1;
  99. // Create the binding and set it up
  100. var b = new BABYLON.Binding();
  101. b.propertyPathName = "firstName";
  102. vm.createDataBinding(BABYLON.CustomerViewModel.firstNameProperty, b);
  103. // Bind age with a custom source
  104. b = new BABYLON.Binding();
  105. b.propertyPathName = "age";
  106. b.dataSource = c2;
  107. vm.createDataBinding(BABYLON.CustomerViewModel.ageProperty, b);
  108. // Check it's ok
  109. expect(vm.firstName).toBe("Loic Baumann", "binding string format doesn't work");
  110. // Check it's ok
  111. expect(vm.age).toBe(20, "binding string format doesn't work on non string source type");
  112. });
  113. });
  114. //# sourceMappingURL=DataBindingTest.js.map