1import { makeSubject, map, pipe, publish, Source, Subject } from 'wonka';
2import { vi, expect, it, beforeEach, describe, afterEach } from 'vitest';
3
4import { Client } from '../client';
5import { queryOperation, queryResponse } from '../test-utils';
6import { Operation } from '../types';
7import { debugExchange } from './debug';
8
9let exchangeArgs;
10let forwardedOperations: Operation[];
11let input: Subject<Operation>;
12
13beforeEach(() => {
14 forwardedOperations = [];
15 input = makeSubject<Operation>();
16
17 // Collect all forwarded operations
18 const forward = (s: Source<Operation>) => {
19 return pipe(
20 s,
21 map(op => {
22 forwardedOperations.push(op);
23 return queryResponse;
24 })
25 );
26 };
27
28 exchangeArgs = { forward, subject: {} as Client };
29});
30
31it('forwards query operations correctly', async () => {
32 vi.spyOn(globalThis.console, 'debug').mockImplementation(() => {
33 /** Do NOthing */
34 });
35 const { source: ops$, next, complete } = input;
36 const exchange = debugExchange(exchangeArgs)(ops$);
37
38 publish(exchange);
39 next(queryOperation);
40 complete();
41 // eslint-disable-next-line no-console
42 expect(console.debug).toBeCalled();
43 // eslint-disable-next-line no-console
44 expect(console.debug).toBeCalledTimes(2);
45});
46
47describe('production', () => {
48 beforeEach(() => {
49 process.env.NODE_ENV = 'production';
50 });
51
52 afterEach(() => {
53 process.env.NODE_ENV = 'test';
54 });
55
56 it('is a noop in production', () => {
57 const { source: ops$ } = input;
58
59 debugExchange({
60 forward: ops => {
61 expect(ops).toBe(ops$);
62 },
63 } as any)(ops$);
64 });
65});