1import { Source } from '../types';
2import { fromValue, makeSubject } from '../sources';
3import { forEach } from '../sinks';
4
5import {
6 passesPassivePull,
7 passesActivePush,
8 passesSinkClose,
9 passesSourceEnd,
10 passesSingleStart,
11 passesStrictEnd,
12} from './compliance';
13
14import { combine, zip } from '../combine';
15
16beforeEach(() => {
17 jest.useFakeTimers();
18});
19
20describe('zip', () => {
21 const noop = (source: Source<any>) => zip([fromValue(0), source]);
22
23 passesPassivePull(noop, [0, 0]);
24 passesActivePush(noop, [0, 0]);
25 passesSinkClose(noop);
26 passesSourceEnd(noop, [0, 0]);
27 passesSingleStart(noop);
28 passesStrictEnd(noop);
29
30 it('emits the zipped values of two sources', () => {
31 const { source: sourceA, next: nextA } = makeSubject<number>();
32 const { source: sourceB, next: nextB } = makeSubject<number>();
33 const fn = jest.fn();
34
35 const combined = combine(sourceA, sourceB);
36 forEach(fn)(combined);
37
38 nextA(1);
39 expect(fn).not.toHaveBeenCalled();
40 nextB(2);
41 expect(fn).toHaveBeenCalledWith([1, 2]);
42 });
43
44 it('emits the zipped values of three sources', () => {
45 const { source: sourceA, next: nextA } = makeSubject<number>();
46 const { source: sourceB, next: nextB } = makeSubject<number>();
47 const { source: sourceC, next: nextC } = makeSubject<number>();
48 const fn = jest.fn();
49
50 const combined = zip([sourceA, sourceB, sourceC]);
51 forEach(fn)(combined);
52
53 nextA(1);
54 expect(fn).not.toHaveBeenCalled();
55 nextB(2);
56 expect(fn).not.toHaveBeenCalled();
57 nextC(3);
58 expect(fn).toHaveBeenCalledWith([1, 2, 3]);
59 });
60
61 it('emits the zipped values of a dictionary of two sources', () => {
62 const { source: sourceA, next: nextA } = makeSubject<number>();
63 const { source: sourceB, next: nextB } = makeSubject<number>();
64 const fn = jest.fn();
65
66 const combined = zip({ a: sourceA, b: sourceB });
67 forEach(fn)(combined);
68
69 nextA(1);
70 expect(fn).not.toHaveBeenCalled();
71 nextB(2);
72 expect(fn).toHaveBeenCalledWith({ a: 1, b: 2 });
73 });
74});