123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- import { interval, Observable, of, throwError } from 'rxjs';
- import { map, mergeMap, take } from 'rxjs/operators';
- import { OBSERVABLE_TEST_TIMEOUT_IN_MS } from './types';
- describe('toEmitValuesWith matcher', () => {
- describe('failing tests', () => {
- describe('passing null in expect', () => {
- it('should fail with correct message', async () => {
- const observable = null as unknown as Observable<number>;
- const rejects = expect(() =>
- expect(observable).toEmitValuesWith((received) => {
- expect(received).toEqual([1, 2, 3]);
- })
- ).rejects;
- await rejects.toThrow();
- });
- });
- describe('passing undefined in expect', () => {
- it('should fail with correct message', async () => {
- const observable = undefined as unknown as Observable<number>;
- const rejects = expect(() =>
- expect(observable).toEmitValuesWith((received) => {
- expect(received).toEqual([1, 2, 3]);
- })
- ).rejects;
- await rejects.toThrow();
- });
- });
- describe('passing number instead of Observable in expect', () => {
- it('should fail with correct message', async () => {
- const observable = 1 as unknown as Observable<number>;
- const rejects = expect(() =>
- expect(observable).toEmitValuesWith((received) => {
- expect(received).toEqual([1, 2, 3]);
- })
- ).rejects;
- await rejects.toThrow();
- });
- });
- describe('wrong number of emitted values', () => {
- it('should fail with correct message', async () => {
- const observable = interval(10).pipe(take(3));
- const rejects = expect(() =>
- expect(observable).toEmitValuesWith((received) => {
- expect(received).toEqual([0, 1]);
- })
- ).rejects;
- await rejects.toThrow();
- });
- });
- describe('wrong emitted values', () => {
- it('should fail with correct message', async () => {
- const observable = interval(10).pipe(take(3));
- const rejects = expect(() =>
- expect(observable).toEmitValuesWith((received) => {
- expect(received).toEqual([1, 2, 3]);
- })
- ).rejects;
- await rejects.toThrow();
- });
- });
- describe('wrong emitted value types', () => {
- it('should fail with correct message', async () => {
- const observable = interval(10).pipe(take(3)) as unknown as Observable<string>;
- const rejects = expect(() =>
- expect(observable).toEmitValuesWith((received) => {
- expect(received).toEqual(['0', '1', '2']);
- })
- ).rejects;
- await rejects.toThrow();
- });
- });
- describe(`observable that does not complete within ${OBSERVABLE_TEST_TIMEOUT_IN_MS}ms`, () => {
- it('should fail with correct message', async () => {
- const observable = interval(600);
- const rejects = expect(() =>
- expect(observable).toEmitValuesWith((received) => {
- expect(received).toEqual([0]);
- })
- ).rejects;
- await rejects.toThrow();
- });
- });
- });
- describe('passing tests', () => {
- describe('correct emitted values', () => {
- it('should pass with correct message', async () => {
- const observable = interval(10).pipe(take(3));
- await expect(observable).toEmitValuesWith((received) => {
- expect(received).toEqual([0, 1, 2]);
- });
- });
- });
- describe('correct emitted values with throw', () => {
- it('should pass with correct message', async () => {
- const observable = interval(10).pipe(
- map((interval) => {
- if (interval > 1) {
- throw 'an error';
- }
- return interval;
- })
- );
- await expect(observable).toEmitValuesWith((received) => {
- expect(received).toEqual([0, 1, 'an error']);
- });
- });
- });
- describe('correct emitted values with throwError', () => {
- it('should pass with correct message', async () => {
- const observable = interval(10).pipe(
- mergeMap((interval) => {
- if (interval === 1) {
- return throwError('an error');
- }
- return of(interval);
- })
- );
- await expect(observable).toEmitValuesWith((received) => {
- expect(received).toEqual([0, 'an error']);
- });
- });
- });
- });
- });
|