toEmitValues.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { interval, Observable, of, throwError } from 'rxjs';
  2. import { map, mergeMap, take } from 'rxjs/operators';
  3. import { OBSERVABLE_TEST_TIMEOUT_IN_MS } from './types';
  4. describe('toEmitValues matcher', () => {
  5. describe('failing tests', () => {
  6. describe('passing null in expect', () => {
  7. it('should fail', async () => {
  8. const observable = null as unknown as Observable<number>;
  9. const rejects = expect(() => expect(observable).toEmitValues([1, 2, 3])).rejects;
  10. await rejects.toThrow();
  11. });
  12. });
  13. describe('passing undefined in expect', () => {
  14. it('should fail', async () => {
  15. const observable = undefined as unknown as Observable<number>;
  16. const rejects = expect(() => expect(observable).toEmitValues([1, 2, 3])).rejects;
  17. await rejects.toThrow();
  18. });
  19. });
  20. describe('passing number instead of Observable in expect', () => {
  21. it('should fail', async () => {
  22. const observable = 1 as unknown as Observable<number>;
  23. const rejects = expect(() => expect(observable).toEmitValues([1, 2, 3])).rejects;
  24. await rejects.toThrow();
  25. });
  26. });
  27. describe('wrong number of emitted values', () => {
  28. it('should fail', async () => {
  29. const observable = interval(10).pipe(take(3));
  30. const rejects = expect(() => expect(observable).toEmitValues([0, 1])).rejects;
  31. await rejects.toThrow();
  32. });
  33. });
  34. describe('wrong emitted values', () => {
  35. it('should fail', async () => {
  36. const observable = interval(10).pipe(take(3));
  37. const rejects = expect(() => expect(observable).toEmitValues([1, 2, 3])).rejects;
  38. await rejects.toThrow();
  39. });
  40. });
  41. describe('wrong emitted value types', () => {
  42. it('should fail', async () => {
  43. const observable = interval(10).pipe(take(3)) as unknown as Observable<string>;
  44. const rejects = expect(() => expect(observable).toEmitValues(['0', '1', '2'])).rejects;
  45. await rejects.toThrow();
  46. });
  47. });
  48. describe(`observable that does not complete within ${OBSERVABLE_TEST_TIMEOUT_IN_MS}ms`, () => {
  49. it('should fail', async () => {
  50. const observable = interval(600);
  51. const rejects = expect(() => expect(observable).toEmitValues([0])).rejects;
  52. await rejects.toThrow();
  53. });
  54. });
  55. });
  56. describe('passing tests', () => {
  57. describe('correct emitted values', () => {
  58. it('should pass with correct message', async () => {
  59. const observable = interval(10).pipe(take(3));
  60. await expect(observable).toEmitValues([0, 1, 2]);
  61. });
  62. });
  63. describe('using nested arrays', () => {
  64. it('should pass with correct message', async () => {
  65. const observable = interval(10).pipe(
  66. map((interval) => [{ text: interval.toString(), value: interval }]),
  67. take(3)
  68. );
  69. await expect(observable).toEmitValues([
  70. [{ text: '0', value: 0 }],
  71. [{ text: '1', value: 1 }],
  72. [{ text: '2', value: 2 }],
  73. ]);
  74. });
  75. });
  76. describe('using nested objects', () => {
  77. it('should pass with correct message', async () => {
  78. const observable = interval(10).pipe(
  79. map((interval) => ({ inner: { text: interval.toString(), value: interval } })),
  80. take(3)
  81. );
  82. await expect(observable).toEmitValues([
  83. { inner: { text: '0', value: 0 } },
  84. { inner: { text: '1', value: 1 } },
  85. { inner: { text: '2', value: 2 } },
  86. ]);
  87. });
  88. });
  89. describe('correct emitted values with throw', () => {
  90. it('should pass with correct message', async () => {
  91. const observable = interval(10).pipe(
  92. map((interval) => {
  93. if (interval > 1) {
  94. throw 'an error';
  95. }
  96. return interval;
  97. })
  98. ) as unknown as Observable<string | number>;
  99. await expect(observable).toEmitValues([0, 1, 'an error']);
  100. });
  101. });
  102. describe('correct emitted values with throwError', () => {
  103. it('should pass with correct message', async () => {
  104. const observable = interval(10).pipe(
  105. mergeMap((interval) => {
  106. if (interval === 1) {
  107. return throwError('an error');
  108. }
  109. return of(interval);
  110. })
  111. ) as unknown as Observable<string | number>;
  112. await expect(observable).toEmitValues([0, 'an error']);
  113. });
  114. });
  115. });
  116. });