DataBindingTest.ts 6.0 KB

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