activitymonitor.spec.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import { Signal } from '@lumino/signaling';
  5. import { ActivityMonitor } from '@jupyterlab/coreutils';
  6. import { sleep } from '@jupyterlab/testutils';
  7. class TestObject {
  8. one = new Signal<TestObject, number>(this);
  9. two = new Signal<TestObject, string[]>(this);
  10. }
  11. describe('@jupyterlab/coreutils', () => {
  12. describe('ActivityMonitor()', () => {
  13. let testObj: TestObject;
  14. let signal: Signal<TestObject, number>;
  15. beforeEach(() => {
  16. testObj = new TestObject();
  17. signal = testObj.one;
  18. });
  19. describe('#constructor()', () => {
  20. it('should accept a signal', () => {
  21. const monitor = new ActivityMonitor<TestObject, number>({ signal });
  22. expect(monitor).toBeInstanceOf(ActivityMonitor);
  23. });
  24. it('should accept a timeout', () => {
  25. const monitor = new ActivityMonitor<TestObject, string[]>({
  26. signal: testObj.two,
  27. timeout: 100
  28. });
  29. expect(monitor).toBeInstanceOf(ActivityMonitor);
  30. });
  31. });
  32. describe('#activityStopped', () => {
  33. it('should be emitted after the signal has fired and a timeout', async () => {
  34. let called = false;
  35. const monitor = new ActivityMonitor({ signal, timeout: 100 });
  36. monitor.activityStopped.connect((sender, args) => {
  37. expect(sender).toBe(monitor);
  38. expect(args.sender).toBe(testObj);
  39. expect(args.args).toBe(10);
  40. called = true;
  41. });
  42. signal.emit(10);
  43. expect(called).toBe(false);
  44. await sleep(100);
  45. expect(called).toBe(true);
  46. });
  47. });
  48. describe('#timeout', () => {
  49. it('should default to `1000`', () => {
  50. const monitor = new ActivityMonitor<TestObject, number>({ signal });
  51. expect(monitor.timeout).toBe(1000);
  52. });
  53. it('should be set-able', () => {
  54. const monitor = new ActivityMonitor<TestObject, number>({ signal });
  55. monitor.timeout = 200;
  56. expect(monitor.timeout).toBe(200);
  57. });
  58. });
  59. describe('#isDisposed', () => {
  60. it('should test whether the monitor is disposed', () => {
  61. const monitor = new ActivityMonitor<TestObject, number>({ signal });
  62. expect(monitor.isDisposed).toBe(false);
  63. monitor.dispose();
  64. expect(monitor.isDisposed).toBe(true);
  65. });
  66. });
  67. describe('#dispose()', () => {
  68. it('should dispose of the resources used by the monitor', () => {
  69. const monitor = new ActivityMonitor<TestObject, number>({ signal });
  70. monitor.dispose();
  71. expect(monitor.isDisposed).toBe(true);
  72. });
  73. it('should be a no-op if called more than once', () => {
  74. const monitor = new ActivityMonitor<TestObject, number>({ signal });
  75. monitor.dispose();
  76. monitor.dispose();
  77. expect(monitor.isDisposed).toBe(true);
  78. });
  79. });
  80. });
  81. });