utils.spec.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import { PromiseDelegate } from '@lumino/coreutils';
  5. import { Signal } from '@lumino/signaling';
  6. import {
  7. expectFailure,
  8. isFulfilled,
  9. testEmission
  10. } from '@jupyterlab/testutils';
  11. describe('test/utils', () => {
  12. describe('testEmission', () => {
  13. it('should resolve to the given value', async () => {
  14. const owner = {};
  15. const x = new Signal<{}, number>(owner);
  16. const emission = testEmission(x, {
  17. value: 'done'
  18. });
  19. x.emit(0);
  20. expect(await emission).toBe('done');
  21. });
  22. it('should find the given emission', async () => {
  23. const owner = {};
  24. const x = new Signal<{}, number>(owner);
  25. const emission = testEmission(x, {
  26. find: (a, b) => b === 1,
  27. value: 'done'
  28. });
  29. x.emit(0);
  30. expect(await isFulfilled(emission)).toBe(false);
  31. x.emit(1);
  32. expect(await emission).toBe('done');
  33. });
  34. it('should reject if the test throws an error', async () => {
  35. const owner = {};
  36. const x = new Signal<{}, number>(owner);
  37. const emission = testEmission(x, {
  38. find: (a, b) => b === 1,
  39. test: (a, b) => {
  40. throw new Error('my error');
  41. },
  42. value: 'done'
  43. });
  44. x.emit(0);
  45. expect(await isFulfilled(emission)).toBe(false);
  46. x.emit(1);
  47. await expectFailure(emission, 'my error');
  48. });
  49. it('should resolve if the test succeeds', async () => {
  50. const owner = {};
  51. const x = new Signal<{}, number>(owner);
  52. const emission = testEmission(x, {
  53. find: (a, b) => b === 1,
  54. test: (a, b) => {
  55. expect(b).toBe(1);
  56. },
  57. value: 'done'
  58. });
  59. x.emit(0);
  60. expect(await isFulfilled(emission)).toBe(false);
  61. x.emit(1);
  62. expect(await emission).toBe('done');
  63. });
  64. });
  65. describe('isFulfilled', () => {
  66. it('should resolve to true only after a promise is fulfilled', async () => {
  67. const p = new PromiseDelegate<number>();
  68. expect(await isFulfilled(p.promise)).toBe(false);
  69. p.resolve(10);
  70. expect(await isFulfilled(p.promise)).toBe(true);
  71. });
  72. it('should resolve to true even if the promise is rejected', async () => {
  73. const p = new PromiseDelegate<number>();
  74. expect(await isFulfilled(p.promise)).toBe(false);
  75. p.reject(new Error('my error'));
  76. expect(await isFulfilled(p.promise)).toBe(true);
  77. });
  78. });
  79. });