Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

apollo-client-mock

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apollo-client-mock

Easily mock the Apollo client for your integration tests

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
143
decreased by-24.74%
Maintainers
1
Weekly downloads
 
Created
Source

Apollo Client Mock

Easily mock your apollo client for testing. It uses Apollo Link Schema under the surface

Setup

To setup Apollo mock client you need to import your schema in as well as your mock resolvers. Then you can setup your client with these two arguments which will then return a createClient function which you can create a mockedClient for each test. createClient can optionally take a new set of resolvers that can overwrite your defaults

import typeDefs from '../link/to/schema'
import setupClient from 'apollo-mock-client'

const defaultMocks = {
  Query: () => ({
    nodes: () => []
    //...other queries
  }),
  Mutation: () => ({
    getDomainState: (_, { name }, context) => {
      return {
        name,
        state: 'Open'
      }
    }
    //...other mutations
  })
}

const createClient = setupClient(defaultMocks, typeDefs)

export default createClient
//Test file

import React from 'react'
import {
  render
} from 'react-testing-library'

import { ApolloProvider } from 'react-apollo'
import createClient from '../testing-utils/mockedClient'

import CheckAvailabilityContainer from '../CheckAvailability'

afterEach(cleanup)

test('should call resolver without blowing up', () => {
  const { getByText, container } = render(
    <ApolloProvider client={createClient()}>
      <CheckAvailabilityContainer />
    </ApolloProvider>
  )

  //the rest of your test
}

The following example shows you can overwrite each resolver per test

test('should call resolver without blowing up', () => {

  const overwriteResolvers = {
    Mutation: () => ({
      getDomainState: (_, { name }, context) => {
        return {
          name,
          state: 'Closed'
        }
      }
    })
  }

  render(
    <ApolloProvider client={createClient(overwriteResolvers)}>
      <CheckAvailabilityContainer />
    </ApolloProvider>
  )

  //the rest of your test
}

Keywords

FAQs

Package last updated on 10 May 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc