utils.spec.ts 2.5 KB

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