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