observablemap.spec.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { ObservableMap } from '@jupyterlab/observables';
  4. describe('@jupyterlab/observables', () => {
  5. describe('ObservableMap', () => {
  6. describe('#constructor()', () => {
  7. it('should accept no arguments', () => {
  8. const value = new ObservableMap<number>();
  9. expect(value instanceof ObservableMap).toBe(true);
  10. });
  11. });
  12. describe('#type', () => {
  13. it('should return `Map`', () => {
  14. const value = new ObservableMap<number>();
  15. expect(value.type).toBe('Map');
  16. });
  17. });
  18. describe('#size', () => {
  19. it('should return the number of entries in the map', () => {
  20. const value = new ObservableMap<number>();
  21. value.set('one', 1);
  22. value.set('two', 2);
  23. expect(value.size).toBe(2);
  24. });
  25. });
  26. describe('#changed', () => {
  27. it('should be emitted when the map changes state', () => {
  28. let called = false;
  29. const value = new ObservableMap<number>();
  30. value.changed.connect(() => {
  31. called = true;
  32. });
  33. value.set('entry', 1);
  34. expect(called).toBe(true);
  35. });
  36. it('should have value changed args', () => {
  37. let called = false;
  38. const value = new ObservableMap<number>();
  39. value.changed.connect((sender, args) => {
  40. expect(sender).toBe(value);
  41. expect(args.type).toBe('add');
  42. expect(args.newValue).toBe(0);
  43. expect(args.oldValue).toBeUndefined();
  44. expect(args.key).toBe('entry');
  45. called = true;
  46. });
  47. value.set('entry', 0);
  48. expect(called).toBe(true);
  49. });
  50. });
  51. describe('#isDisposed', () => {
  52. it('should test whether the map is disposed', () => {
  53. const value = new ObservableMap<number>();
  54. expect(value.isDisposed).toBe(false);
  55. value.dispose();
  56. expect(value.isDisposed).toBe(true);
  57. });
  58. });
  59. describe('#dispose()', () => {
  60. it('should dispose of the resources held by the map', () => {
  61. const value = new ObservableMap<number>();
  62. value.set('one', 1);
  63. value.set('two', 2);
  64. value.dispose();
  65. expect(value.isDisposed).toBe(true);
  66. });
  67. });
  68. describe('#set()', () => {
  69. it('should set the item at a specific key', () => {
  70. const value = new ObservableMap<number>();
  71. value.set('one', 1);
  72. expect(value.get('one')).toBe(1);
  73. });
  74. it('should return the old value for that key', () => {
  75. const value = new ObservableMap<number>();
  76. value.set('one', 1);
  77. const x = value.set('one', 1.01);
  78. expect(x).toBe(1);
  79. });
  80. it('should trigger a changed signal', () => {
  81. let called = false;
  82. const value = new ObservableMap<number>();
  83. value.changed.connect((sender, args) => {
  84. expect(sender).toBe(value);
  85. expect(args.type).toBe('add');
  86. expect(args.newValue).toBe(1);
  87. expect(args.oldValue).toBeUndefined();
  88. expect(args.key).toBe('one');
  89. called = true;
  90. });
  91. value.set('one', 1);
  92. expect(called).toBe(true);
  93. });
  94. });
  95. describe('#get()', () => {
  96. it('should get the value for a key', () => {
  97. const value = new ObservableMap<number>();
  98. value.set('one', 1);
  99. expect(value.get('one')).toBe(1);
  100. });
  101. it('should return undefined if the key does not exist', () => {
  102. const value = new ObservableMap<number>();
  103. value.set('one', 1);
  104. expect(value.get('two')).toBeUndefined();
  105. });
  106. });
  107. describe('#has()', () => {
  108. it('should whether the key exists in a map', () => {
  109. const value = new ObservableMap<number>();
  110. value.set('one', 1);
  111. expect(value.has('one')).toBe(true);
  112. expect(value.has('two')).toBe(false);
  113. });
  114. });
  115. describe('#keys()', () => {
  116. it('should return a list of the keys in the map', () => {
  117. const value = new ObservableMap<number>();
  118. value.set('one', 1);
  119. value.set('two', 2);
  120. value.set('three', 3);
  121. const keys = value.keys();
  122. expect(keys).toEqual(['one', 'two', 'three']);
  123. });
  124. });
  125. describe('#values()', () => {
  126. it('should return a list of the values in the map', () => {
  127. const value = new ObservableMap<number>();
  128. value.set('one', 1);
  129. value.set('two', 2);
  130. value.set('three', 3);
  131. const keys = value.values();
  132. expect(keys).toEqual([1, 2, 3]);
  133. });
  134. });
  135. describe('#delete()', () => {
  136. it('should remove an item from the map', () => {
  137. const value = new ObservableMap<number>();
  138. value.set('one', 1);
  139. value.set('two', 2);
  140. value.set('three', 3);
  141. expect(value.get('two')).toBe(2);
  142. value.delete('two');
  143. expect(value.get('two')).toBeUndefined();
  144. });
  145. it('should return the value of the key it removed', () => {
  146. const value = new ObservableMap<number>();
  147. value.set('one', 1);
  148. expect(value.delete('one')).toBe(1);
  149. expect(value.delete('one')).toBeUndefined();
  150. });
  151. it('should trigger a changed signal if actually removed', () => {
  152. const value = new ObservableMap<number>();
  153. value.set('one', 1);
  154. value.set('two', 2);
  155. value.set('three', 3);
  156. let called = false;
  157. value.changed.connect((sender, args) => {
  158. expect(sender).toBe(value);
  159. expect(args.type).toBe('remove');
  160. expect(args.key).toBe('two');
  161. expect(args.oldValue).toBe(2);
  162. expect(args.newValue).toBeUndefined();
  163. called = true;
  164. });
  165. value.delete('two');
  166. expect(called).toBe(true);
  167. });
  168. it('should not trigger a changed signal if not actually removed', () => {
  169. const value = new ObservableMap<number>();
  170. value.set('one', 1);
  171. value.set('three', 3);
  172. let called = false;
  173. value.changed.connect((sender, args) => {
  174. expect(sender).toBe(value);
  175. expect(args.type).toBe('remove');
  176. expect(args.key).toBe('two');
  177. expect(args.oldValue).toBe(2);
  178. expect(args.newValue).toBeUndefined();
  179. called = true;
  180. });
  181. // 'two' is not in the map
  182. value.delete('two');
  183. expect(called).toBe(false);
  184. });
  185. });
  186. describe('#clear()', () => {
  187. it('should remove all items from the map', () => {
  188. const value = new ObservableMap<number>();
  189. value.set('one', 1);
  190. value.set('two', 2);
  191. value.set('three', 3);
  192. value.clear();
  193. expect(value.size).toBe(0);
  194. value.clear();
  195. expect(value.size).toBe(0);
  196. });
  197. it('should trigger a changed signal', () => {
  198. const value = new ObservableMap<number>();
  199. value.set('one', 1);
  200. let called = false;
  201. value.changed.connect((sender, args) => {
  202. expect(sender).toBe(value);
  203. expect(args.type).toBe('remove');
  204. expect(args.key).toBe('one');
  205. expect(args.oldValue).toBe(1);
  206. expect(args.newValue).toBeUndefined();
  207. called = true;
  208. });
  209. value.clear();
  210. expect(called).toBe(true);
  211. });
  212. });
  213. });
  214. });