1 | import { InMemoryCache } from 'apollo-cache-inmemory';
|
2 | import { ApolloClient } from 'apollo-client';
|
3 | import { MockLink } from 'apollo-link-mock';
|
4 | import gql from 'graphql-tag';
|
5 | import * as React from 'react';
|
6 | import { ApolloProvider } from 'react-apollo';
|
7 | import { render, wait, cleanup } from '@testing-library/react';
|
8 | import useCustomerOrders from './useCustomerOrdersQuery';
|
9 | import throwErrorInDev from '../helpers/throwErrorInDev';
|
10 |
|
11 | jest.mock('../helpers/throwErrorInDev');
|
12 |
|
13 | afterEach(cleanup);
|
14 |
|
15 | const helloQuery = gql`
|
16 | query HelloQuery {
|
17 | customer {
|
18 | orderHeaders {
|
19 | result {
|
20 | id
|
21 | }
|
22 | totalResults
|
23 | }
|
24 | }
|
25 | }
|
26 | `;
|
27 |
|
28 | const notACustomerQuery = gql`
|
29 | query NotACustomerQuery {
|
30 | something
|
31 | }
|
32 | `;
|
33 |
|
34 | const HELLO_MOCKS = [
|
35 | {
|
36 | request: {
|
37 | query: helloQuery,
|
38 | variables: {}
|
39 | },
|
40 | result: {
|
41 | data: {
|
42 | customer: {
|
43 | orderHeaders: {
|
44 | totalResults: 2,
|
45 | result: [
|
46 | {
|
47 | id: 'an order'
|
48 | },
|
49 | {
|
50 | id: 'another order'
|
51 | }
|
52 | ]
|
53 | }
|
54 | }
|
55 | }
|
56 | }
|
57 | },
|
58 | {
|
59 | request: {
|
60 | query: notACustomerQuery,
|
61 | variables: {}
|
62 | },
|
63 | result: {
|
64 | data: {
|
65 | something: 'hello'
|
66 | }
|
67 | }
|
68 | }
|
69 | ];
|
70 |
|
71 | function createClient(mocks) {
|
72 | return new ApolloClient({
|
73 | cache: new InMemoryCache({ addTypename: false }),
|
74 | link: new MockLink(mocks)
|
75 | });
|
76 | }
|
77 |
|
78 | describe('useCustomerOrders', () => {
|
79 | function setup({ collect, query }) {
|
80 | const client = createClient(HELLO_MOCKS);
|
81 |
|
82 | const utils = render(
|
83 | <ApolloProvider client={client}>
|
84 | <TestHarness query={query}>
|
85 | {resp => {
|
86 | collect && collect(resp);
|
87 | return null;
|
88 | }}
|
89 | </TestHarness>
|
90 | </ApolloProvider>
|
91 | );
|
92 |
|
93 | return utils;
|
94 | }
|
95 |
|
96 | it('passes through loading state from query', async () => {
|
97 | let loading;
|
98 |
|
99 | setup({
|
100 | collect: resp => {
|
101 | loading = resp.loading;
|
102 | }
|
103 | });
|
104 |
|
105 | expect(loading).toBeTruthy();
|
106 |
|
107 | await wait();
|
108 |
|
109 | expect(loading).toBeFalsy();
|
110 | });
|
111 |
|
112 | it('returns orders', async () => {
|
113 | let orders;
|
114 |
|
115 | setup({
|
116 | collect: resp => {
|
117 | orders = resp.orders;
|
118 | }
|
119 | });
|
120 |
|
121 | expect(orders).toMatchSnapshot('loading orders');
|
122 | expect(orders).toHaveLength(0);
|
123 |
|
124 | await wait();
|
125 |
|
126 | expect(orders).toMatchSnapshot('loaded orders');
|
127 | expect(orders).toHaveLength(2);
|
128 | });
|
129 |
|
130 | it('returns totalResult', async () => {
|
131 | let total;
|
132 |
|
133 | setup({
|
134 | collect: resp => {
|
135 | total = resp.totalOrders;
|
136 | }
|
137 | });
|
138 |
|
139 | expect(total).toBe(undefined);
|
140 |
|
141 | await wait();
|
142 |
|
143 | expect(total).toBe(2);
|
144 | });
|
145 |
|
146 | it('returns the entire query response', async () => {
|
147 | let response;
|
148 |
|
149 | setup({
|
150 | collect: resp => {
|
151 | response = resp.result;
|
152 | }
|
153 | });
|
154 |
|
155 | await wait();
|
156 |
|
157 | expect(response).toMatchSnapshot();
|
158 | });
|
159 |
|
160 | it('throws an error when the query is not a valid customer order query', async () => {
|
161 | setup({ query: notACustomerQuery });
|
162 |
|
163 | await wait();
|
164 |
|
165 | expect(throwErrorInDev).toHaveBeenLastCalledWith(true, expect.any(String));
|
166 | });
|
167 | });
|
168 |
|
169 | function TestHarness({ children, query }) {
|
170 | const resp = useCustomerOrders({
|
171 | query: query || helloQuery,
|
172 | variables: {}
|
173 | });
|
174 |
|
175 | return children(resp);
|
176 | }
|