restorablepool.spec.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import { RestorablePool } from '@jupyterlab/statedb';
  5. import { signalToPromise } from '@jupyterlab/testutils';
  6. import { IObservableDisposable } from '@lumino/disposable';
  7. import { ISignal, Signal } from '@lumino/signaling';
  8. const namespace = 'restorable-pool-test';
  9. class ObservableDisposable implements IObservableDisposable {
  10. constructor(public id: string = '') {
  11. // no-op
  12. }
  13. get disposed(): ISignal<this, void> {
  14. return this._disposed;
  15. }
  16. get isDisposed(): boolean {
  17. return this._isDisposed;
  18. }
  19. dispose(): void {
  20. if (this.isDisposed) {
  21. return;
  22. }
  23. this._isDisposed = true;
  24. this._disposed.emit(undefined);
  25. Signal.clearData(this);
  26. }
  27. private _disposed = new Signal<this, void>(this);
  28. private _isDisposed = false;
  29. }
  30. describe('@jupyterlab/coreutils', () => {
  31. describe('RestorablePool', () => {
  32. let pool: RestorablePool<ObservableDisposable>;
  33. beforeEach(() => {
  34. pool = new RestorablePool({ namespace });
  35. });
  36. afterEach(() => {
  37. pool.dispose();
  38. });
  39. describe('#constructor()', () => {
  40. it('should create a RestorablePool', () => {
  41. expect(pool).toBeInstanceOf(RestorablePool);
  42. });
  43. });
  44. describe('#added', () => {
  45. it('should emit when an instance has been added', async () => {
  46. const instance = new ObservableDisposable();
  47. const promise = signalToPromise(pool.added);
  48. await pool.add(instance);
  49. const [sender, args] = await promise;
  50. expect(sender).toBe(pool);
  51. expect(args).toBe(instance);
  52. instance.dispose();
  53. });
  54. });
  55. describe('#current', () => {
  56. it('should default to null', () => {
  57. expect(pool.current).toBeNull();
  58. });
  59. it('should be settable by client code', async () => {
  60. const instance = new ObservableDisposable();
  61. void pool.add(instance);
  62. expect(pool.current).toBe(null);
  63. pool.current = instance;
  64. expect(pool.current).toBe(instance);
  65. instance.dispose();
  66. });
  67. it('should be a no-op if set to an untracked instance', async () => {
  68. const instance = new ObservableDisposable();
  69. expect(pool.current).toBe(null);
  70. pool.current = instance;
  71. expect(pool.current).toBe(null);
  72. instance.dispose();
  73. });
  74. });
  75. describe('#currentChanged', () => {
  76. it('should emit when the current object has been updated', async () => {
  77. const instance = new ObservableDisposable();
  78. const promise = signalToPromise(pool.currentChanged);
  79. void pool.add(instance);
  80. pool.current = instance;
  81. await promise;
  82. instance.dispose();
  83. });
  84. });
  85. describe('#isDisposed', () => {
  86. it('should test whether the pool is disposed', () => {
  87. expect(pool.isDisposed).toBe(false);
  88. pool.dispose();
  89. expect(pool.isDisposed).toBe(true);
  90. });
  91. });
  92. describe('#add()', () => {
  93. it('should add an instance to the pool', async () => {
  94. const instance = new ObservableDisposable();
  95. expect(pool.has(instance)).toBe(false);
  96. await pool.add(instance);
  97. expect(pool.has(instance)).toBe(true);
  98. });
  99. it('should reject an instance that already exists', async () => {
  100. const instance = new ObservableDisposable();
  101. let failed = false;
  102. expect(pool.has(instance)).toBe(false);
  103. await pool.add(instance);
  104. expect(pool.has(instance)).toBe(true);
  105. try {
  106. await pool.add(instance);
  107. } catch (error) {
  108. failed = true;
  109. }
  110. expect(failed).toBe(true);
  111. });
  112. it('should reject an instance that is disposed', async () => {
  113. const instance = new ObservableDisposable();
  114. let failed = false;
  115. expect(pool.has(instance)).toBe(false);
  116. instance.dispose();
  117. try {
  118. await pool.add(instance);
  119. } catch (error) {
  120. failed = true;
  121. }
  122. expect(failed).toBe(true);
  123. });
  124. it('should remove an added instance if it is disposed', async () => {
  125. const instance = new ObservableDisposable();
  126. await pool.add(instance);
  127. expect(pool.has(instance)).toBe(true);
  128. instance.dispose();
  129. expect(pool.has(instance)).toBe(false);
  130. });
  131. });
  132. describe('#dispose()', () => {
  133. it('should dispose of the resources used by the pool', () => {
  134. expect(pool.isDisposed).toBe(false);
  135. pool.dispose();
  136. expect(pool.isDisposed).toBe(true);
  137. });
  138. it('should be safe to call multiple times', () => {
  139. expect(pool.isDisposed).toBe(false);
  140. pool.dispose();
  141. pool.dispose();
  142. expect(pool.isDisposed).toBe(true);
  143. });
  144. });
  145. describe('#find()', () => {
  146. it('should find a tracked item that matches a filter function', () => {
  147. const instanceA = new ObservableDisposable('A');
  148. const instanceB = new ObservableDisposable('B');
  149. const instanceC = new ObservableDisposable('C');
  150. void pool.add(instanceA);
  151. void pool.add(instanceB);
  152. void pool.add(instanceC);
  153. expect(pool.find(obj => obj.id === 'B')).toBe(instanceB);
  154. instanceA.dispose();
  155. instanceB.dispose();
  156. instanceC.dispose();
  157. });
  158. it('should return a void if no item is found', () => {
  159. const instanceA = new ObservableDisposable('A');
  160. const instanceB = new ObservableDisposable('B');
  161. const instanceC = new ObservableDisposable('C');
  162. void pool.add(instanceA);
  163. void pool.add(instanceB);
  164. void pool.add(instanceC);
  165. expect(pool.find(widget => widget.id === 'D')).toBeFalsy();
  166. instanceA.dispose();
  167. instanceB.dispose();
  168. instanceC.dispose();
  169. });
  170. });
  171. describe('#filter()', () => {
  172. it('should filter according to a predicate function', () => {
  173. const instanceA = new ObservableDisposable('include-A');
  174. const instanceB = new ObservableDisposable('include-B');
  175. const instanceC = new ObservableDisposable('exclude-C');
  176. void pool.add(instanceA);
  177. void pool.add(instanceB);
  178. void pool.add(instanceC);
  179. const list = pool.filter(obj => obj.id.indexOf('include') !== -1);
  180. expect(list.length).toBe(2);
  181. expect(list[0]).toBe(instanceA);
  182. expect(list[1]).toBe(instanceB);
  183. instanceA.dispose();
  184. instanceB.dispose();
  185. instanceC.dispose();
  186. });
  187. it('should return an empty array if no item is found', () => {
  188. const instanceA = new ObservableDisposable('A');
  189. const instanceB = new ObservableDisposable('B');
  190. const instanceC = new ObservableDisposable('C');
  191. void pool.add(instanceA);
  192. void pool.add(instanceB);
  193. void pool.add(instanceC);
  194. expect(pool.filter(widget => widget.id === 'D').length).toBe(0);
  195. instanceA.dispose();
  196. instanceB.dispose();
  197. instanceC.dispose();
  198. });
  199. });
  200. describe('#forEach()', () => {
  201. it('should iterate through all the tracked items', () => {
  202. const instanceA = new ObservableDisposable('A');
  203. const instanceB = new ObservableDisposable('B');
  204. const instanceC = new ObservableDisposable('C');
  205. let visited = '';
  206. void pool.add(instanceA);
  207. void pool.add(instanceB);
  208. void pool.add(instanceC);
  209. pool.forEach(obj => {
  210. visited += obj.id;
  211. });
  212. expect(visited).toBe('ABC');
  213. });
  214. });
  215. describe('#has()', () => {
  216. it('should return `true` if an item exists in the pool', () => {
  217. const instance = new ObservableDisposable();
  218. expect(pool.has(instance)).toBe(false);
  219. void pool.add(instance);
  220. expect(pool.has(instance)).toBe(true);
  221. });
  222. });
  223. });
  224. });