observer-next.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import assert from 'assert';
  2. import { testMethodProperty } from './properties.js';
  3. describe('observer.next', () => {
  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), 'next', {
  12. configurable: true,
  13. writable: true,
  14. length: 1,
  15. });
  16. });
  17. it('forwards the first argument', () => {
  18. let args;
  19. let observer = getObserver({ next(...a) { args = a } });
  20. observer.next(1, 2);
  21. assert.deepEqual(args, [1]);
  22. });
  23. it('does not return a value', () => {
  24. let observer = getObserver({ next() { return 1 } });
  25. assert.equal(observer.next(), undefined);
  26. });
  27. it('does not forward when the subscription is complete', () => {
  28. let count = 0;
  29. let observer = getObserver({ next() { count++ } });
  30. observer.complete();
  31. observer.next();
  32. assert.equal(count, 0);
  33. });
  34. it('does not forward when the subscription is cancelled', () => {
  35. let count = 0;
  36. let observer;
  37. let subscription = new Observable(x => { observer = x }).subscribe({
  38. next() { count++ },
  39. });
  40. subscription.unsubscribe();
  41. observer.next();
  42. assert.equal(count, 0);
  43. });
  44. it('remains closed if the subscription is cancelled from "next"', () => {
  45. let observer;
  46. let subscription = new Observable(x => { observer = x }).subscribe({
  47. next() { subscription.unsubscribe() },
  48. });
  49. observer.next();
  50. assert.equal(observer.closed, true);
  51. });
  52. it('queues if the subscription is not initialized', async () => {
  53. let values = [];
  54. let observer;
  55. new Observable(x => { observer = x, x.next(1) }).subscribe({
  56. next(val) {
  57. values.push(val);
  58. if (val === 1) {
  59. observer.next(3);
  60. }
  61. },
  62. });
  63. observer.next(2);
  64. assert.deepEqual(values, []);
  65. await null;
  66. assert.deepEqual(values, [1, 2]);
  67. await null;
  68. assert.deepEqual(values, [1, 2, 3]);
  69. });
  70. it('drops queue if subscription is closed', async () => {
  71. let values = [];
  72. let subscription = new Observable(x => { x.next(1) }).subscribe({
  73. next(val) { values.push(val) },
  74. });
  75. assert.deepEqual(values, []);
  76. subscription.unsubscribe();
  77. await null;
  78. assert.deepEqual(values, []);
  79. });
  80. it('queues if the observer is running', async () => {
  81. let observer;
  82. let values = [];
  83. new Observable(x => { observer = x }).subscribe({
  84. next(val) {
  85. values.push(val);
  86. if (val === 1) observer.next(2);
  87. },
  88. });
  89. observer.next(1);
  90. assert.deepEqual(values, [1]);
  91. await null;
  92. assert.deepEqual(values, [1, 2]);
  93. });
  94. it('reports error if "next" is not a method', () => {
  95. let observer = getObserver({ next: 1 });
  96. observer.next();
  97. assert.ok(hostError);
  98. });
  99. it('does not report error if "next" is undefined', () => {
  100. let observer = getObserver({ next: undefined });
  101. observer.next();
  102. assert.ok(!hostError);
  103. });
  104. it('does not report error if "next" is null', () => {
  105. let observer = getObserver({ next: null });
  106. observer.next();
  107. assert.ok(!hostError);
  108. });
  109. it('reports error if "next" throws', () => {
  110. let error = {};
  111. let observer = getObserver({ next() { throw error } });
  112. observer.next();
  113. assert.equal(hostError, error);
  114. });
  115. it('does not close the subscription on error', () => {
  116. let observer = getObserver({ next() { throw {} } });
  117. observer.next();
  118. assert.equal(observer.closed, false);
  119. });
  120. });