Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJs - Jasmine marbles forkJoin operator test

Here is forkJoin operator jasmine marble test:

it('test1', () => {

 const a = cold('---a|', { a: 1 });
 const b = cold('---b|', { b: 2 });

 const observable = forkJoin(
   a,
   b
 );

 const expected = cold('---21');
 expect(observable).toBeObservable(expected);
});

The test produces the following errors:

Expected $[0].frame = 40 to equal 30.
Expected $[0].notification.value = [ 1, 2 ] to equal '2'.
Expected $[1].frame = 40 to equal 50.
Expected $[1].notification.kind = 'C' to equal 'N'.
Expected $[1].notification.value = undefined to equal '1'.
Expected $[1].notification.hasValue = false to equal true.

Could anyone tell me what I am doing wrong?

like image 948
tsiro Avatar asked May 27 '19 14:05

tsiro


1 Answers

First ---21 will wait three frames, then emit '2' and then emit '1. Fork join is not a merge it will emit once, and it will emit [1, 2].

Alphanumeric marbles advance one frame when they emit. So both a and b will complete at frame 4. The forkJoin will then resolve emit. The forkJoin will then immediately complete (also at frame 4).

So the errors you are getting:

Expected $[0].frame = 40 to equal 30. The first item emitted from the fork-join is at time 4 (because a and b complete at time 4) not at time 3.

Expected $[0].notification.value = [ 1, 2 ] to equal '2'. The value emitted is [1, 2] and not '2' as described above.

Expected $[1].frame = 40 to equal 50.
Expected $[1].notification.kind = 'C' to equal 'N'.
Expected $[1].notification.value = undefined to equal '1'.
Expected $[1].notification.hasValue = false to equal true.

You are expecting a '1' at time 5. It is getting a complete at time 4.

So you probably want...

const expected = cold('----(a|)', { a: [1, 2] });
expect(observable).toBeObservable(expected);
like image 132
Pace Avatar answered Nov 05 '22 16:11

Pace