1 | import { act, renderHook } from '@testing-library/react-hooks';
|
2 | import gql from 'graphql-tag';
|
3 | import { ProviderPyramid } from 'ProviderPyramid';
|
4 | import React from 'react';
|
5 | import { CartIdProvider } from '../components/Cart/CartIdContext';
|
6 | import throwErrorInDev from '../helpers/throwErrorInDev';
|
7 | import { useAddMultipleToCart } from './useAddMultipleToCart';
|
8 |
|
9 | jest.mock('../helpers/throwErrorInDev', () => jest.fn());
|
10 |
|
11 | const MOCK_QUERY = gql`
|
12 | query CartQuery {
|
13 | cart {
|
14 | id
|
15 | totalQuantity
|
16 | }
|
17 | }
|
18 | `;
|
19 |
|
20 | const MOCK_MUTATION = gql`
|
21 | mutation addMultipleToCart($items: [AddMultipleToCartInput]!) {
|
22 | addMultipleToCart(items: $items) {
|
23 | cart {
|
24 | id
|
25 | totalQuantity
|
26 | }
|
27 | }
|
28 | }
|
29 | `;
|
30 |
|
31 | const MOCK_PRODUCTS = [
|
32 | {
|
33 | articleNumber: 'boogie',
|
34 | quantity: 2
|
35 | },
|
36 | {
|
37 | articleNumber: 'woogie',
|
38 | quantity: 1
|
39 | }
|
40 | ];
|
41 |
|
42 | describe('useAddMultipleToCart', () => {
|
43 | it('throws an error when no products or queries are passed in', () => {
|
44 | renderHook(() => useAddMultipleToCart(), {
|
45 | wrapper: ProviderPyramid
|
46 | });
|
47 |
|
48 | expect(throwErrorInDev).toHaveBeenCalledTimes(1);
|
49 |
|
50 | jest.resetAllMocks();
|
51 |
|
52 |
|
53 | renderHook(() => useAddMultipleToCart(MOCK_PRODUCTS), {
|
54 | wrapper: ProviderPyramid
|
55 | });
|
56 | expect(throwErrorInDev).toHaveBeenCalledTimes(0);
|
57 | });
|
58 |
|
59 | it('fires a mutation and updates the returned status booleans', async () => {
|
60 | const mocks = [
|
61 | {
|
62 | request: {
|
63 | query: MOCK_MUTATION,
|
64 | variables: {
|
65 | cartId: null,
|
66 | items: MOCK_PRODUCTS
|
67 | }
|
68 | },
|
69 | result: {
|
70 | data: {
|
71 | addMultipleToCart: { cart: { id: null, totalQuantity: 5 } }
|
72 | }
|
73 | }
|
74 | }
|
75 | ];
|
76 | const Providers = ({ children }) => (
|
77 | <ProviderPyramid apolloMocks={mocks}>{children}</ProviderPyramid>
|
78 | );
|
79 |
|
80 | const { result, waitForNextUpdate } = renderHook(
|
81 | () =>
|
82 | useAddMultipleToCart(MOCK_PRODUCTS, {
|
83 | addMultipleToCartMutation: MOCK_MUTATION,
|
84 | cartQuery: MOCK_QUERY
|
85 | }),
|
86 | { wrapper: Providers }
|
87 | );
|
88 |
|
89 | const [mutate, { called, loading }] = result.current;
|
90 |
|
91 |
|
92 | expect(called).toBe(false);
|
93 | expect(loading).toBe(false);
|
94 |
|
95 | act(() => mutate()).then(() => {
|
96 |
|
97 | const [, { called, loading, error }] = result.current;
|
98 | expect(called).toBe(true);
|
99 | expect(loading).toBe(false);
|
100 | expect(error).toBe(false);
|
101 | });
|
102 |
|
103 | {
|
104 |
|
105 | const [, { called, loading }] = result.current;
|
106 |
|
107 | expect(called).toBe(true);
|
108 | expect(loading).toBe(true);
|
109 | }
|
110 |
|
111 | await waitForNextUpdate();
|
112 | });
|
113 |
|
114 | it('handles partial failure', async () => {
|
115 | const FAIL_INDEX = 0;
|
116 |
|
117 | const mocks = [
|
118 | {
|
119 | request: {
|
120 | query: MOCK_MUTATION,
|
121 | variables: {
|
122 | cartId: null,
|
123 | items: MOCK_PRODUCTS
|
124 | }
|
125 | },
|
126 | result: {
|
127 | data: {
|
128 | addMultipleToCart: { cart: { id: 'dfgsdfg', totalQuantity: 5 } }
|
129 | },
|
130 | errors: [
|
131 | {
|
132 | extensions: {
|
133 | code: 'AddToCartFailed',
|
134 | data: { index: FAIL_INDEX }
|
135 | }
|
136 | }
|
137 | ]
|
138 | }
|
139 | },
|
140 | {
|
141 | request: {
|
142 | query: MOCK_QUERY,
|
143 | variables: { cartId: null }
|
144 | },
|
145 | result: {
|
146 | data: {
|
147 | cart: {
|
148 | id: null,
|
149 | totalQuantity: 1
|
150 | }
|
151 | }
|
152 | }
|
153 | }
|
154 | ];
|
155 |
|
156 | const Providers = ({ children }) => (
|
157 | <ProviderPyramid apolloMocks={mocks}>
|
158 | <CartIdProvider>{children}</CartIdProvider>
|
159 | </ProviderPyramid>
|
160 | );
|
161 |
|
162 | const { result } = renderHook(
|
163 | () =>
|
164 | useAddMultipleToCart(MOCK_PRODUCTS, {
|
165 | addMultipleToCartMutation: MOCK_MUTATION,
|
166 | cartQuery: MOCK_QUERY
|
167 | }),
|
168 | { wrapper: Providers }
|
169 | );
|
170 |
|
171 | const [mutate] = result.current;
|
172 |
|
173 | await act(() => mutate());
|
174 |
|
175 | const [, { failedItems, error }] = result.current;
|
176 |
|
177 | expect(failedItems.has(MOCK_PRODUCTS[FAIL_INDEX])).toBe(true);
|
178 | expect(failedItems.get(MOCK_PRODUCTS[FAIL_INDEX])).toBe('AddToCartFailed');
|
179 | expect(error).toBe(false);
|
180 | });
|
181 | });
|