import { makeSubject, map, pipe, publish, Source, Subject } from 'wonka'; import { vi, expect, it, beforeEach, describe, afterEach } from 'vitest'; import { Client } from '../client'; import { queryOperation, queryResponse } from '../test-utils'; import { Operation } from '../types'; import { debugExchange } from './debug'; let exchangeArgs; let forwardedOperations: Operation[]; let input: Subject; beforeEach(() => { forwardedOperations = []; input = makeSubject(); // Collect all forwarded operations const forward = (s: Source) => { return pipe( s, map(op => { forwardedOperations.push(op); return queryResponse; }) ); }; exchangeArgs = { forward, subject: {} as Client }; }); it('forwards query operations correctly', async () => { vi.spyOn(globalThis.console, 'debug').mockImplementation(() => { /** Do NOthing */ }); const { source: ops$, next, complete } = input; const exchange = debugExchange(exchangeArgs)(ops$); publish(exchange); next(queryOperation); complete(); // eslint-disable-next-line no-console expect(console.debug).toBeCalled(); // eslint-disable-next-line no-console expect(console.debug).toBeCalledTimes(2); }); describe('production', () => { beforeEach(() => { process.env.NODE_ENV = 'production'; }); afterEach(() => { process.env.NODE_ENV = 'test'; }); it('is a noop in production', () => { const { source: ops$ } = input; debugExchange({ forward: ops => { expect(ops).toBe(ops$); }, } as any)(ops$); }); });