operation-name-mock-link

Mock Apollo queries based on operation name (instead of query and variables)


Keywords
apollo
License
MIT
Install
npm install operation-name-mock-link@0.0.6

Documentation

Operation Name Mock Link

Differences with default Apollo MockedProvider

MockedProvider has a very strict matching behaviour: your query variables and the mock variables must match EXACTLY, including keys with the value undefined. It makes tests very bristle and leads to the dreaded No more mocked responses for the query: error.

This package uses a lighter approach, mocks are matched only based on the operationName.

Limitation: this might be problematic if a component is triggering multiple queries with the same operationName. But it is fine in most scenarios.

Usage

import {
  OperationNameMockedResponse,
  OperationNameMockLink
} from "operation-name-mock-link";

interface GetSomeDataResult {
  getSomeData: {
    id: string;
  }
}

const mock: OperationNameMockedResponse<GetSomeDataResult> = {
  request: {
    operationName: "myQuery",
    query: gql`
      query myQuery {
        getSomeData {
          id
        }
      }`
  },
  result: {
    data: {
      getSomeData: {
          id: "42"
      }
    }
  }
};

const wrapper = ({ children }) => (
  <MockedProvider
    // We replace MockedProvider default link with our custom MockLink
    link={new OperationNameMockLink([mock], false)}
  >
    {children}
  </MockedProvider>
);