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

@shopify/graphql-testing

Package Overview
Dependencies
Maintainers
13
Versions
135
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@shopify/graphql-testing

Utilities to create mock graphql factory.

  • 3.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
44K
increased by2.78%
Maintainers
13
Weekly downloads
 
Created
Source

@shopify/graphql-testing

Build Status License: MIT npm version

This package provides utilities to help in the following testing scenarios:

  1. Testing a graphQL operation with mock data.
  2. Testing the state of your application before/after all the graphQL operations resolve.

Installation

$ yarn add @shopify/graphql-testing

Usage

The default utility exported by this library is createGraphQLFactory.

const createGraphQL = createGraphQLFactory();

This factory function can then be use to creating a mock ApolloClient that you will pass into your test setup with the desire mock data.

const graphQL = createGraphQL(mockData);
const apolloClient = graphQL.client;

By default, this mock client will hold all the graphQL operations triggered by your application in a pending state.

To resolve all pending graphQL operations:

await graphQL.resolveAll();

You can also access all the graphQL operations triggered by your application (pending and non-pending) using:

graphQL.operations.all();

and only those graphQL operations with specific name using:

graphQL.operations.all({operationName: 'Pet'});

Examples

Below is an example of how to use createGraphQLFactory in a React component test.

Note: In a typical application you will want to generalized some of this implementation (ie. mouting of ApolloProvider) for repeated use.

import {mount} from 'enzyme';
import {ApolloProvider} from 'react-apollo';
import {createGraphQLFactory} from '@shopify/graphql-testing';

export const createGraphQL = createGraphQLFactory();

it('loads mock data from GraphQL', async () => {
  const mockCustomerData = {firstName: 'Jane', lastName: 'Doe'};
  const graphQL = createGraphQL({
    CustomerDetails: {
      customer: mockCustomerData,
    },
  });

  const customerDetails = mount(
    <ApolloProvider client={graphQL.client}>
      <CustomerDetails id="123" />
    </ApolloProvider>,
  );

  expect(customerDetails.find(TextField)).toHaveProp('value', '');

  await graphQL.resolveAll();
  customerDetails.update();

  expect(customerDetails.find(TextField)).toHaveProp(
    'value',
    mockCustomerData.firstName,
  );
});

Below is an example of how to assert that a graphQL request was triggered.

import {mount} from 'enzyme';
import {ApolloProvider} from 'react-apollo';
import {trigger} from '@shopify/enzyme-utilities';
import {createGraphQLFactory} from '@shopify/graphql-testing';

export const createGraphQL = createGraphQLFactory();

it('triggers a graphQL request when the load data button is clicked', async () => {
  const mockCustomerData = {firstName: 'Jane', lastName: 'Doe'};
  const graphQL = createGraphQL({
    CustomerDetails: {
      customer: mockCustomerData,
    },
  });

  const customerDetails = mount(
    <ApolloProvider client={graphQL.client}>
      <CustomerDetails id="123" />
    </ApolloProvider>,
  );

  expect(graphQL.operations.all()).toHaveLength(0);

  trigger(customerDetails.find(LoadDataButton), 'onClick');

  expect(graphQL.operations.all()).toHaveLength(1);
  expect(graphQL.operations.last().operationName).toEqual('CustomerDetails');
});

FAQs

Package last updated on 18 Apr 2019

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