Something went wrong. Try again.
Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123import { pipe, map, makeSubject, publish, tap } from 'wonka';import { vi, expect, it, beforeEach } from 'vitest';
import { gql, createClient, Operation, OperationResult, ExchangeIO,} from '@urql/core';
import { queryResponse } from '../../../packages/core/src/test-utils';import { contextExchange } from './context';
const queryOne = gql` { author { id name } }`;
const queryOneData = { __typename: 'Query', author: { __typename: 'Author', id: '123', name: 'Author', },};
const dispatchDebug = vi.fn();let client, op, ops$, next;beforeEach(() => { client = createClient({ url: 'http://0.0.0.0', exchanges: [], }); op = client.createRequestOperation('query', { key: 1, query: queryOne, });
({ source: ops$, next } = makeSubject<Operation>());});
it(`calls getContext`, () => { const response = vi.fn((forwardOp: Operation): OperationResult => { return { ...queryResponse, operation: forwardOp, data: queryOneData, }; });
const result = vi.fn(); const forward: ExchangeIO = ops$ => { return pipe(ops$, map(response)); };
const headers = { hello: 'world' }; pipe( contextExchange({ getContext: op => ({ ...op.context, headers }), })({ forward, client, dispatchDebug, })(ops$), tap(result), publish );
next(op);
expect(response).toHaveBeenCalledTimes(1); expect(response.mock.calls[0][0].context.headers).toEqual(headers); expect(result).toHaveBeenCalledTimes(1);});
it(`calls getContext async`, async () => { const response = vi.fn((forwardOp: Operation): OperationResult => { return { ...queryResponse, operation: forwardOp, data: queryOneData, }; });
const result = vi.fn(); const forward: ExchangeIO = ops$ => { return pipe(ops$, map(response)); };
const headers = { hello: 'world' }; pipe( contextExchange({ getContext: async op => { await Promise.resolve(); return { ...op.context, headers }; }, })({ forward, client, dispatchDebug, })(ops$), tap(result), publish );
next(op);
await new Promise(res => { setTimeout(() => { expect(response).toHaveBeenCalledTimes(1); expect(response.mock.calls[0][0].context.headers).toEqual(headers); expect(result).toHaveBeenCalledTimes(1); res(null); }, 10); });});