observer-error.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import assert from 'assert';
  2. import { testMethodProperty } from './properties.js';
  3. describe('observer.error', () => {
  4. function getObserver(inner) {
  5. let observer;
  6. new Observable(x => { observer = x }).subscribe(inner);
  7. return observer;
  8. }
  9. it('is a method of SubscriptionObserver', () => {
  10. let observer = getObserver();
  11. testMethodProperty(Object.getPrototypeOf(observer), 'error', {
  12. configurable: true,
  13. writable: true,
  14. length: 1,
  15. });
  16. });
  17. it('forwards the argument', () => {
  18. let args;
  19. let observer = getObserver({ error(...a) { args = a } });
  20. observer.error(1);
  21. assert.deepEqual(args, [1]);
  22. });
  23. it('does not return a value', () => {
  24. let observer = getObserver({ error() { return 1 } });
  25. assert.equal(observer.error(), undefined);
  26. });
  27. it('does not throw when the subscription is complete', () => {
  28. let observer = getObserver({ error() {} });
  29. observer.complete();
  30. observer.error('error');
  31. });
  32. it('does not throw when the subscription is cancelled', () => {
  33. let observer;
  34. let subscription = new Observable(x => { observer = x }).subscribe({
  35. error() {},
  36. });
  37. subscription.unsubscribe();
  38. observer.error(1);
  39. assert.ok(!hostError);
  40. });
  41. it('queues if the subscription is not initialized', async () => {
  42. let error;
  43. new Observable(x => { x.error({}) }).subscribe({
  44. error(err) { error = err },
  45. });
  46. assert.equal(error, undefined);
  47. await null;
  48. assert.ok(error);
  49. });
  50. it('queues if the observer is running', async () => {
  51. let observer;
  52. let error;
  53. new Observable(x => { observer = x }).subscribe({
  54. next() { observer.error({}) },
  55. error(e) { error = e },
  56. });
  57. observer.next();
  58. assert.ok(!error);
  59. await null;
  60. assert.ok(error);
  61. });
  62. it('closes the subscription before invoking inner observer', () => {
  63. let closed;
  64. let observer = getObserver({
  65. error() { closed = observer.closed },
  66. });
  67. observer.error(1);
  68. assert.equal(closed, true);
  69. });
  70. it('reports an error if "error" is not a method', () => {
  71. let observer = getObserver({ error: 1 });
  72. observer.error(1);
  73. assert.ok(hostError);
  74. });
  75. it('reports an error if "error" is undefined', () => {
  76. let error = {};
  77. let observer = getObserver({ error: undefined });
  78. observer.error(error);
  79. assert.equal(hostError, error);
  80. });
  81. it('reports an error if "error" is null', () => {
  82. let error = {};
  83. let observer = getObserver({ error: null });
  84. observer.error(error);
  85. assert.equal(hostError, error);
  86. });
  87. it('reports error if "error" throws', () => {
  88. let error = {};
  89. let observer = getObserver({ error() { throw error } });
  90. observer.error(1);
  91. assert.equal(hostError, error);
  92. });
  93. it('calls the cleanup method after "error"', () => {
  94. let calls = [];
  95. let observer;
  96. new Observable(x => {
  97. observer = x;
  98. return () => { calls.push('cleanup') };
  99. }).subscribe({
  100. error() { calls.push('error') },
  101. });
  102. observer.error();
  103. assert.deepEqual(calls, ['error', 'cleanup']);
  104. });
  105. it('calls the cleanup method if there is no "error"', () => {
  106. let calls = [];
  107. let observer;
  108. new Observable(x => {
  109. observer = x;
  110. return () => { calls.push('cleanup') };
  111. }).subscribe({});
  112. try {
  113. observer.error();
  114. } catch (err) {}
  115. assert.deepEqual(calls, ['cleanup']);
  116. });
  117. it('reports error if the cleanup function throws', () => {
  118. let error = {};
  119. let observer;
  120. new Observable(x => {
  121. observer = x;
  122. return () => { throw error };
  123. }).subscribe();
  124. observer.error(1);
  125. assert.equal(hostError, error);
  126. });
  127. });