observablemap.spec.ts 7.3 KB

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