activitymonitor.spec.ts 2.9 KB

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