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.
2.7 kB · 113 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114// @vitest-environment jsdom
import { testEffect } from '@solidjs/testing-library';import { expect, it, describe, vi } from 'vitest';import { CreateMutationState, createMutation } from './createMutation';import { OperationResult, OperationResultSource, createClient, gql,} from '@urql/core';import { makeSubject } from 'wonka';import { createEffect } from 'solid-js';
const QUERY = gql` mutation { test }`;
const client = createClient({ url: '/graphql', exchanges: [], suspense: false,});
vi.mock('./context', () => { const useClient = () => { return client!; };
return { useClient };});
// Given that it is not possible to directly listen to all store changes it is necessary// to access all relevant parts on which `createEffect` should listen onconst markStateDependencies = (state: CreateMutationState<any, any>) => { state.data; state.error; state.extensions; state.fetching; state.operation; state.stale;};
describe('createMutation', () => { it('should have expected state before and after finish', () => { const subject = makeSubject<any>(); const clientMutation = vi .spyOn(client, 'executeMutation') .mockImplementation( () => subject.source as OperationResultSource<OperationResult> );
return testEffect(done => { const [state, execute] = createMutation< { test: boolean }, { variable: number } >(QUERY);
createEffect((run: number = 0) => { markStateDependencies(state);
switch (run) { case 0: { expect(state).toMatchObject({ data: undefined, stale: false, fetching: false, error: undefined, extensions: undefined, operation: undefined, });
execute({ variable: 1 }); break; }
case 1: { expect(state).toMatchObject({ data: undefined, stale: false, fetching: true, error: undefined, extensions: undefined, operation: undefined, });
expect(clientMutation).toHaveBeenCalledTimes(1); subject.next({ data: { test: true }, stale: false });
break; }
case 2: { expect(state).toMatchObject({ data: { test: true }, stale: false, fetching: false, error: undefined, extensions: undefined, });
done(); break; } }
return run + 1; }); }); });});