ObservableDictionaryTest.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. module JasmineTest {
  2. import ObservableStringDictionary = BABYLON.ObservableStringDictionary;
  3. import PropertyChangedBase = BABYLON.PropertyChangedBase;
  4. import DictionaryChanged = BABYLON.DictionaryChanged;
  5. class Customer extends PropertyChangedBase {
  6. constructor(firstName: string, lastName: string) {
  7. super();
  8. this._firstName = firstName;
  9. this._lastName = lastName;
  10. }
  11. public get firstName(): string {
  12. return this._firstName;
  13. }
  14. public set firstName(value: string) {
  15. if (this._firstName === value) {
  16. return;
  17. }
  18. let old = this._firstName;
  19. let oldDN = this.displayName;
  20. this._firstName = value;
  21. this.onPropertyChanged("firstName", old, value);
  22. this.onPropertyChanged("displayName", oldDN, this.displayName);
  23. }
  24. public get lastName(): string {
  25. return this._lastName;
  26. }
  27. public set lastName(value: string) {
  28. if (this._lastName === value) {
  29. return;
  30. }
  31. let old = this._lastName;
  32. let oldDN = this.displayName;
  33. this._lastName = value;
  34. this.onPropertyChanged("lastName", old, value);
  35. this.onPropertyChanged("displayName", oldDN, this.displayName);
  36. }
  37. public get displayName(): string {
  38. return this.firstName + " " + this.lastName;
  39. }
  40. private _firstName: string;
  41. private _lastName: string;
  42. }
  43. describe("Tools - ObservableDictionary",
  44. () => {
  45. it("Add",
  46. () => {
  47. let oa = new ObservableStringDictionary<Customer>(true);
  48. oa.add("cust0", new Customer("loic", "baumann"));
  49. oa.propertyChanged.add((e, c) => {
  50. expect(e.oldValue).toBe(1, "PropChanged length is bad");
  51. expect(e.newValue).toBe(2, "PropChanged length is bad");
  52. });
  53. oa.dictionaryChanged.add((e, c) => {
  54. expect(e.action).toEqual(DictionaryChanged.newItemAction);
  55. let item = e.newItem;
  56. expect(item.key).toEqual("cust1");
  57. expect(item.value.firstName).toEqual("david");
  58. expect(item.value.lastName).toEqual("catuhe");
  59. });
  60. oa.add("cust1", new Customer("david", "catuhe"));
  61. expect(oa.count).toBe(2);
  62. let cust = oa.get("cust1");
  63. expect(cust).toBeDefined();
  64. expect(cust.firstName).toEqual("david");
  65. expect(cust.lastName).toEqual("catuhe");
  66. }
  67. );
  68. it("Remove",
  69. () => {
  70. let oa = new ObservableStringDictionary<Customer>(true);
  71. let cust0 = new Customer("loic", "baumann");
  72. let cust1 = new Customer("david", "catuhe");
  73. oa.add("cust0", cust0);
  74. oa.add("cust1", cust1);
  75. oa.propertyChanged.add((e, c) => {
  76. expect(e.oldValue).toBe(2, "PropChanged length is bad");
  77. expect(e.newValue).toBe(1, "PropChanged length is bad");
  78. });
  79. oa.dictionaryChanged.add((e, c) => {
  80. expect(e.action).toEqual(DictionaryChanged.removedItemAction);
  81. let key = e.removedKey;
  82. expect(key).toEqual("cust0");
  83. });
  84. oa.watchedObjectChanged.add((e, c) => {
  85. fail("watchedObject shouldn't be called as only a removed object had a property changed");
  86. });
  87. expect(oa.count).toBe(2);
  88. let cust = oa.get("cust1");
  89. expect(cust).toBeDefined();
  90. expect(cust.firstName).toEqual("david");
  91. expect(cust.lastName).toEqual("catuhe");
  92. oa.remove("cust0");
  93. cust = oa.get("cust0");
  94. expect(cust).toBeUndefined();
  95. cust0.firstName = "nockawa";
  96. }
  97. );
  98. it("Watched Element",
  99. () => {
  100. let oa = new ObservableStringDictionary<Customer>(true);
  101. oa.add("cust0", new Customer("loic", "baumann"));
  102. oa.add("cust1", new Customer("david", "catuhe"));
  103. let triggerCounter = 0;
  104. oa.watchedObjectChanged.add((e, c) => {
  105. if (triggerCounter === 0) {
  106. expect(e.key).toBe("cust1");
  107. expect(e.propertyChanged.propertyName).toBe("firstName");
  108. expect(e.propertyChanged.oldValue).toBe("david");
  109. expect(e.propertyChanged.newValue).toBe("delta");
  110. ++triggerCounter;
  111. } else {
  112. expect(e.key).toBe("cust1");
  113. expect(e.propertyChanged.propertyName).toBe("displayName");
  114. expect(e.propertyChanged.oldValue).toBe("david catuhe");
  115. expect(e.propertyChanged.newValue).toBe("delta catuhe");
  116. ++triggerCounter;
  117. }
  118. });
  119. let cust = oa.get("cust1");
  120. cust.firstName = "delta";
  121. }
  122. );
  123. }
  124. );
  125. }