import { observable } from '@trpc/server/observable'; import { splitLink } from '../'; import { OperationLink, TRPCLink } from '../'; import { AnyRouter } from '../../../server/src'; import { createChain } from '../links/internals/createChain'; test('splitLink', () => { const wsLinkSpy = vi.fn(); const wsLink: TRPCLink = () => () => observable(() => { wsLinkSpy(); }); const httpLinkSpy = vi.fn(); const httpLink: TRPCLink = () => () => observable(() => { httpLinkSpy(); }); const links: OperationLink[] = [ // "dedupe link" splitLink({ condition(op) { return op.type === 'subscription'; }, true: wsLink, false: [httpLink], })(null as any), ]; createChain({ links, op: { type: 'query', input: null, path: '.', id: 0, context: {}, }, }).subscribe({}); expect(httpLinkSpy).toHaveBeenCalledTimes(1); expect(wsLinkSpy).toHaveBeenCalledTimes(0); vi.resetAllMocks(); createChain({ links, op: { type: 'subscription', input: null, path: '.', id: 0, context: {}, }, }).subscribe({}); expect(httpLinkSpy).toHaveBeenCalledTimes(0); expect(wsLinkSpy).toHaveBeenCalledTimes(1); });