|
1 | 1 | import assert from 'assert';
|
2 | 2 | import Client from '../src/client';
|
3 |
| -import fetchMock from './isomorphic-fetch-mock'; // eslint-disable-line import/no-unresolved |
4 |
| -import fetchMockPostOnce from './fetch-mock-helper'; |
5 |
| - |
6 |
| -// fixtures |
7 |
| -import shopWithProductsFixture from '../fixtures/query-products-fixture'; |
8 |
| -import singleProductFixture from '../fixtures/product-fixture'; |
9 |
| -import multipleProductsFixture from '../fixtures/multiple-products-fixture'; |
10 |
| -import shopWithCollectionsFixture from '../fixtures/query-collections-fixture'; |
11 |
| -import singleCollectionFixture from '../fixtures/collection-fixture'; |
12 |
| -import productWithPaginatedImagesFixture from '../fixtures/product-with-paginated-images-fixture'; |
13 |
| -import {secondPageImagesFixture, thirdPageImagesFixture, fourthPageImagesFixture, fifthPageImagesFixture} from '../fixtures/paginated-images-fixtures'; |
14 |
| -import productWithPaginatedVariantsFixture from '../fixtures/product-with-paginated-variants-fixture'; |
15 |
| -import {secondPageVariantsFixture, thirdPageVariantsFixture} from '../fixtures/paginated-variants-fixtures'; |
16 |
| -import productByHandleFixture from '../fixtures/product-by-handle-fixture'; |
17 |
| -import productRecommendationsFixture from '../fixtures/product-recommendations-fixture'; |
18 |
| -import collectionByHandleFixture from '../fixtures/collection-by-handle-fixture'; |
19 |
| -import collectionWithProductsFixture from '../fixtures/collection-with-products-fixture'; |
20 |
| -import shopWithCollectionsWithPaginationFixture from '../fixtures/query-collections-with-pagination-fixture'; |
21 |
| -import shopWithCollectionsWithProductsFixture from '../fixtures/query-collections-with-products-fixture'; |
22 |
| -import shopInfoFixture from '../fixtures/shop-info-fixture'; |
23 |
| -import shopPoliciesFixture from '../fixtures/shop-policies-fixture'; |
| 3 | + |
| 4 | +const PRODUCT_IDS = [ |
| 5 | + 'gid://shopify/Product/9895276099', |
| 6 | + 'gid://shopify/Product/9895279043', |
| 7 | + 'gid://shopify/Product/9895281475', |
| 8 | + 'gid://shopify/Product/9895284291', |
| 9 | + 'gid://shopify/Product/9895287619', |
| 10 | + 'gid://shopify/Product/9895288515', |
| 11 | + 'gid://shopify/Product/9895289667', |
| 12 | + 'gid://shopify/Product/9895290755', |
| 13 | + 'gid://shopify/Product/9895292227', |
| 14 | + 'gid://shopify/Product/9895293571', |
| 15 | + 'gid://shopify/Product/9895295235', |
| 16 | + 'gid://shopify/Product/9895296451', |
| 17 | + 'gid://shopify/Product/9895297923', |
| 18 | + 'gid://shopify/Product/9895299331', |
| 19 | + 'gid://shopify/Product/9895300995', |
| 20 | + 'gid://shopify/Product/9895302467', |
| 21 | + 'gid://shopify/Product/9895303555', |
| 22 | + 'gid://shopify/Product/9895304515', |
| 23 | + 'gid://shopify/Product/9895305219', |
| 24 | + 'gid://shopify/Product/9895306115' |
| 25 | +]; |
| 26 | + |
| 27 | +const COLLECTIONS = [ |
| 28 | + {id: 'gid://shopify/Collection/243638019', handle: 'frontpage'}, |
| 29 | + { |
| 30 | + id: 'gid://shopify/Collection/257697667', |
| 31 | + handle: 'antiperistaltic-gold-socks' |
| 32 | + }, |
| 33 | + { |
| 34 | + id: 'gid://shopify/Collection/257697731', |
| 35 | + handle: 'blistered-aluminum-boat' |
| 36 | + }, |
| 37 | + {id: 'gid://shopify/Collection/389242179', handle: 'latest-stuff'}, |
| 38 | + {id: 'gid://shopify/Collection/389242499', handle: 'casual-things'}, |
| 39 | + { |
| 40 | + id: 'gid://shopify/Collection/389242819', |
| 41 | + handle: 'summer-collection' |
| 42 | + }, |
| 43 | + {id: 'gid://shopify/Collection/425800451', handle: 'services'}, |
| 44 | + {id: 'gid://shopify/Collection/67984719928', handle: 'gifts'}, |
| 45 | + {id: 'gid://shopify/Collection/627010207800', handle: 'jsbuysdk'} |
| 46 | +]; |
24 | 47 |
|
25 | 48 | suite('client-integration-test', () => {
|
26 |
| - const domain = 'client-integration-tests.myshopify.io'; |
| 49 | + const domain = 'graphql.myshopify.com'; |
27 | 50 | const config = {
|
28 |
| - storefrontAccessToken: 'abc123', |
| 51 | + storefrontAccessToken: '595005d0c565f6969eece280de85edb5', |
29 | 52 | domain
|
30 | 53 | };
|
31 | 54 | let client;
|
32 |
| - let apiUrl; |
33 | 55 |
|
34 | 56 | setup(() => {
|
35 | 57 | client = Client.buildClient(config);
|
36 |
| - apiUrl = `https://${domain}/api/${client.config.apiVersion}/graphql`; |
37 |
| - fetchMock.reset(); |
38 | 58 | });
|
39 | 59 |
|
40 | 60 | teardown(() => {
|
41 | 61 | client = null;
|
42 |
| - fetchMock.restore(); |
43 | 62 | });
|
44 | 63 |
|
45 | 64 | test('it resolves with an array of products on Client.product#fetchAllProducts', () => {
|
46 |
| - fetchMockPostOnce(fetchMock, apiUrl, shopWithProductsFixture); |
47 | 65 |
|
48 | 66 | return client.product.fetchAll().then((products) => {
|
49 | 67 | assert.ok(Array.isArray(products), 'products is an array');
|
50 |
| - assert.equal(products.length, 2, 'there are two products'); |
51 |
| - |
52 |
| - assert.equal(products[0].id, shopWithProductsFixture.data.products.edges[0].node.id); |
53 |
| - assert.equal(products[1].id, shopWithProductsFixture.data.products.edges[1].node.id); |
54 |
| - assert.ok(fetchMock.done()); |
| 68 | + assert.equal(products[0].id, PRODUCT_IDS[0]); |
55 | 69 | });
|
56 | 70 | });
|
57 | 71 |
|
58 | 72 | test('it resolves with a single product on Client.product#fetch', () => {
|
59 |
| - fetchMockPostOnce(fetchMock, apiUrl, singleProductFixture); |
60 |
| - |
61 |
| - const id = singleProductFixture.data.node.id; |
62 | 73 |
|
63 |
| - return client.product.fetch(id).then((product) => { |
| 74 | + return client.product.fetch(PRODUCT_IDS[0]).then((product) => { |
64 | 75 | assert.ok(!Array.isArray(product), 'product is not an array');
|
65 |
| - assert.equal(product.id, id); |
66 |
| - assert.ok(fetchMock.done()); |
| 76 | + assert.equal(product.id, PRODUCT_IDS[0]); |
67 | 77 | });
|
68 | 78 | });
|
69 | 79 |
|
70 | 80 | test('it resolves recommended products on Client.product#fetchRecommendations', () => {
|
71 |
| - fetchMockPostOnce(fetchMock, apiUrl, productRecommendationsFixture); |
72 | 81 |
|
73 |
| - const productId = productRecommendationsFixture.data.productRecommendations[0].id; |
74 |
| - |
75 |
| - return client.product.fetchRecommendations(productId).then((products) => { |
| 82 | + return client.product.fetchRecommendations(PRODUCT_IDS[0]).then((products) => { |
76 | 83 | assert.ok(Array.isArray(products), 'recommended products are an array');
|
77 |
| - assert.ok(fetchMock.done()); |
78 | 84 | });
|
79 | 85 | });
|
80 | 86 |
|
81 | 87 | test('it resolves with an array of products on Client.product#fetchMultiple', () => {
|
82 |
| - fetchMockPostOnce(fetchMock, apiUrl, multipleProductsFixture); |
83 |
| - |
84 |
| - const id1 = multipleProductsFixture.data.nodes[0].id; |
85 |
| - const id2 = multipleProductsFixture.data.nodes[1].id; |
86 |
| - |
87 |
| - return client.product.fetchMultiple([id1, id2]).then((products) => { |
| 88 | + return client.product.fetchMultiple([PRODUCT_IDS[0], PRODUCT_IDS[1]]).then((products) => { |
88 | 89 | assert.ok(Array.isArray(products), 'products are an array');
|
89 |
| - assert.equal(products[0].id, id1); |
90 |
| - assert.equal(products[1].id, id2); |
91 |
| - assert.ok(fetchMock.done()); |
| 90 | + assert.equal(products[0].id, PRODUCT_IDS[0]); |
| 91 | + assert.equal(products[1].id, PRODUCT_IDS[1]); |
92 | 92 | });
|
93 | 93 | });
|
94 | 94 |
|
95 | 95 | test('it fetches all images on products on Client.product#fetchAll', () => {
|
96 |
| - fetchMockPostOnce(fetchMock, apiUrl, productWithPaginatedImagesFixture); |
97 |
| - fetchMockPostOnce(fetchMock, apiUrl, secondPageImagesFixture); |
98 |
| - fetchMockPostOnce(fetchMock, apiUrl, thirdPageImagesFixture); |
99 |
| - |
100 | 96 | return client.product.fetchAll().then((products) => {
|
101 | 97 | const images = products[0].images;
|
102 | 98 |
|
103 | 99 | assert.ok(Array.isArray(images), 'images is an array');
|
104 |
| - // Each image page fixture only contains 1 image rather than 20 for simplicity |
105 |
| - assert.equal(images.length, 3, 'all three pages of images are returned'); |
106 |
| - assert.equal(images[0].id, productWithPaginatedImagesFixture.data.products.edges[0].node.images.edges[0].node.id); |
107 |
| - assert.equal(images[1].id, secondPageImagesFixture.data.node.images.edges[0].node.id); |
108 |
| - assert.equal(images[2].id, thirdPageImagesFixture.data.node.images.edges[0].node.id); |
109 |
| - assert.ok(fetchMock.done()); |
| 100 | + assert.equal(images.length, 4, 'all three pages of images are returned'); |
110 | 101 | });
|
111 | 102 | });
|
112 | 103 |
|
113 | 104 | test('it fetches all variants on Client.product#fetch', () => {
|
114 |
| - fetchMockPostOnce(fetchMock, apiUrl, productWithPaginatedVariantsFixture); |
115 |
| - fetchMockPostOnce(fetchMock, apiUrl, secondPageVariantsFixture); |
116 |
| - fetchMockPostOnce(fetchMock, apiUrl, thirdPageVariantsFixture); |
117 | 105 |
|
118 |
| - return client.product.fetch(productWithPaginatedVariantsFixture.data.node.id).then((product) => { |
| 106 | + return client.product.fetch(PRODUCT_IDS[0]).then((product) => { |
119 | 107 | const variants = product.variants;
|
120 | 108 |
|
121 | 109 | assert.ok(Array.isArray(variants), 'variants is an array');
|
122 |
| - // Each variant page fixture only contains 1 variant rather than 20 for simplicity |
123 |
| - assert.equal(variants.length, 3, 'all three pages of variants are returned'); |
124 |
| - assert.equal(variants[0].id, productWithPaginatedVariantsFixture.data.node.variants.edges[0].node.id); |
125 |
| - assert.equal(variants[1].id, secondPageVariantsFixture.data.node.variants.edges[0].node.id); |
126 |
| - assert.equal(variants[2].id, thirdPageVariantsFixture.data.node.variants.edges[0].node.id); |
127 |
| - assert.ok(fetchMock.done()); |
128 |
| - }); |
129 |
| - }); |
130 |
| - |
131 |
| - test('it does not fetch paginated images if the images query result was empty on Client.product#fetch', () => { |
132 |
| - fetchMockPostOnce(fetchMock, apiUrl, { |
133 |
| - data: { |
134 |
| - node: { |
135 |
| - images: { |
136 |
| - edges: [], |
137 |
| - pageInfo: { |
138 |
| - hasNextPage: false, |
139 |
| - hasPreviousPage: false |
140 |
| - } |
141 |
| - } |
142 |
| - } |
143 |
| - } |
144 |
| - }); |
145 |
| - |
146 |
| - return client.product.fetch('an-id').then((product) => { |
147 |
| - assert.equal(product.images.length, 0); |
148 |
| - assert.ok(fetchMock.done()); |
| 110 | + assert.equal(variants.length, 10, 'all three pages of variants are returned'); |
149 | 111 | });
|
150 | 112 | });
|
151 | 113 |
|
152 | 114 | test('it can fetch a product by handle through Client.product#fetchByHandle', () => {
|
153 |
| - fetchMockPostOnce(fetchMock, apiUrl, productByHandleFixture); |
154 |
| - |
155 |
| - const handle = productByHandleFixture.data.productByHandle.handle; |
| 115 | + const handle = 'snare-boot'; |
156 | 116 |
|
157 | 117 | return client.product.fetchByHandle(handle).then((product) => {
|
158 |
| - assert.equal(product.id, productByHandleFixture.data.productByHandle.id); |
| 118 | + assert.equal(product.id, PRODUCT_IDS[0]); |
159 | 119 | assert.equal(product.handle, handle);
|
160 |
| - assert.ok(fetchMock.done()); |
161 | 120 | });
|
162 | 121 | });
|
163 | 122 |
|
164 | 123 | test('it resolves products with Client.product#fetchQuery', () => {
|
165 |
| - fetchMockPostOnce(fetchMock, apiUrl, shopWithProductsFixture); |
166 | 124 |
|
167 | 125 | return client.product.fetchQuery({}).then((products) => {
|
168 |
| - assert.equal(products.length, 2); |
169 |
| - assert.equal(products[0].id, shopWithProductsFixture.data.products.edges[0].node.id); |
170 |
| - assert.equal(products[1].id, shopWithProductsFixture.data.products.edges[1].node.id); |
171 |
| - assert.ok(fetchMock.done()); |
| 126 | + assert.ok(products.length > 10, 'products are returned'); |
172 | 127 | });
|
173 | 128 | });
|
174 | 129 |
|
175 | 130 | test('it resolves with an array of collections on Client.collection#fetchAll', () => {
|
176 |
| - fetchMockPostOnce(fetchMock, apiUrl, shopWithCollectionsFixture); |
177 |
| - |
178 | 131 | return client.collection.fetchAll().then((collections) => {
|
179 | 132 | assert.ok(Array.isArray(collections), 'collections is an array');
|
180 |
| - assert.equal(collections.length, 2, 'there are two collections'); |
181 |
| - |
182 |
| - assert.equal(collections[0].id, shopWithCollectionsFixture.data.collections.edges[0].node.id); |
183 |
| - assert.equal(collections[1].id, shopWithCollectionsFixture.data.collections.edges[1].node.id); |
184 |
| - assert.ok(fetchMock.done()); |
| 133 | + assert.ok(collections.length > 8, 'there are more than 9 collections'); |
185 | 134 | });
|
186 | 135 | });
|
187 | 136 |
|
188 | 137 | test('it resolves with a single collection on Client.collection#fetch', () => {
|
189 |
| - fetchMockPostOnce(fetchMock, apiUrl, singleCollectionFixture); |
190 |
| - |
191 |
| - const id = singleCollectionFixture.data.node.id; |
192 | 138 |
|
193 |
| - return client.collection.fetch(id).then((collection) => { |
| 139 | + return client.collection.fetch(COLLECTIONS[0].id).then((collection) => { |
194 | 140 | assert.ok(Array.isArray(collection) === false, 'collection is not an array');
|
195 |
| - assert.equal(collection.id, id); |
196 |
| - assert.ok(fetchMock.done()); |
| 141 | + assert.equal(collection.id, collection.id); |
197 | 142 | });
|
198 | 143 | });
|
199 | 144 |
|
200 | 145 | test('it can fetch a collection by handle through Client.collection#fetchByHandle', () => {
|
201 |
| - fetchMockPostOnce(fetchMock, apiUrl, collectionByHandleFixture); |
202 | 146 |
|
203 |
| - const handle = collectionByHandleFixture.data.collectionByHandle.handle; |
204 |
| - |
205 |
| - return client.collection.fetchByHandle(handle).then((collection) => { |
206 |
| - assert.equal(collection.id, collectionByHandleFixture.data.collectionByHandle.id); |
207 |
| - assert.equal(collection.handle, handle); |
208 |
| - assert.ok(fetchMock.done()); |
| 147 | + return client.collection.fetchByHandle(COLLECTIONS[0].handle).then((collection) => { |
| 148 | + assert.equal(collection.id, COLLECTIONS[0].id); |
| 149 | + assert.equal(collection.handle, COLLECTIONS[0].handle); |
209 | 150 | });
|
210 | 151 | });
|
211 | 152 |
|
212 | 153 | test('it can fetch collections with the query arg on Client.collection#fetchQuery', () => {
|
213 |
| - fetchMockPostOnce(fetchMock, apiUrl, shopWithCollectionsFixture); |
214 | 154 |
|
215 | 155 | return client.collection.fetchQuery({}).then((collections) => {
|
216 |
| - assert.equal(collections.length, 2); |
217 |
| - assert.equal(collections[0].id, shopWithCollectionsFixture.data.collections.edges[0].node.id); |
218 |
| - assert.equal(collections[1].id, shopWithCollectionsFixture.data.collections.edges[1].node.id); |
219 |
| - assert.ok(fetchMock.done()); |
| 156 | + assert.ok(collections.length > 8, 'collections are returned'); |
220 | 157 | });
|
221 | 158 | });
|
222 | 159 |
|
223 | 160 | test('it can fetch all collections with products on Client.collection#fetchAllWithProducts', () => {
|
224 |
| - fetchMockPostOnce(fetchMock, apiUrl, shopWithCollectionsWithProductsFixture); |
225 | 161 |
|
226 | 162 | return client.collection.fetchAllWithProducts().then((collections) => {
|
227 |
| - assert.ok(Array.isArray(collections), 'collections is an array'); |
228 |
| - assert.equal(collections.length, 2, 'there are two collections'); |
229 |
| - assert.equal(collections[0].id, shopWithCollectionsWithProductsFixture.data.collections.edges[0].node.id); |
230 |
| - assert.equal(collections[1].id, shopWithCollectionsWithProductsFixture.data.collections.edges[1].node.id); |
231 |
| - assert.ok(fetchMock.done()); |
| 163 | + const collectionProducts = collections[3].products; |
| 164 | + |
| 165 | + assert.ok(collectionProducts.length > 0, 'collection returned with products'); |
232 | 166 | });
|
233 | 167 | });
|
234 | 168 |
|
235 | 169 | test('it can fetch a single collection with products on Client.collection#fetchWithProducts', () => {
|
236 |
| - fetchMockPostOnce(fetchMock, apiUrl, collectionWithProductsFixture); |
237 | 170 |
|
238 |
| - const id = collectionWithProductsFixture.data.node.id; |
239 |
| - |
240 |
| - return client.collection.fetchWithProducts(id).then((collection) => { |
| 171 | + return client.collection.fetchWithProducts(COLLECTIONS[3].id).then((collection) => { |
241 | 172 | assert.equal(Array.isArray(collection), false, 'collection is not an array');
|
242 |
| - assert.equal(collection.id, id); |
243 |
| - assert.equal(collection.products.length, 2); |
244 |
| - assert.ok(fetchMock.done()); |
| 173 | + assert.equal(collection.id, COLLECTIONS[3].id); |
| 174 | + assert.ok(collection.products.length > 2); |
245 | 175 | });
|
246 | 176 | });
|
247 | 177 |
|
248 | 178 | test('it paginates on images on products within collections on Client.collection#fetchAllWithProducts', () => {
|
249 |
| - fetchMockPostOnce(fetchMock, apiUrl, shopWithCollectionsWithPaginationFixture); |
250 |
| - fetchMockPostOnce(fetchMock, apiUrl, thirdPageImagesFixture); |
251 |
| - fetchMockPostOnce(fetchMock, apiUrl, fourthPageImagesFixture); |
252 |
| - fetchMockPostOnce(fetchMock, apiUrl, fifthPageImagesFixture); |
253 | 179 |
|
254 | 180 | return client.collection.fetchAllWithProducts().then((collections) => {
|
255 |
| - assert.equal(collections.length, 2); |
256 |
| - // Verify that all images are added |
257 |
| - assert.equal(collections[0].products[0].images.length, 2); |
258 |
| - assert.equal(collections[0].products[1].images.length, 2); |
259 |
| - assert.equal(collections[1].products[0].images.length, 2); |
260 |
| - assert.ok(fetchMock.done()); |
| 181 | + assert.ok(collections[3].products[0].images.length > 0); |
| 182 | + assert.ok(collections[3].products[1].images.length > 0); |
| 183 | + assert.ok(collections[4].products[0].images.length > 0); |
261 | 184 | });
|
262 | 185 | });
|
263 | 186 |
|
264 | 187 | test('it resolves with `shop` on Client.shop#fetchInfo', () => {
|
265 |
| - fetchMockPostOnce(fetchMock, apiUrl, shopInfoFixture); |
266 |
| - |
267 | 188 | return client.shop.fetchInfo().then((shop) => {
|
268 |
| - assert.equal(shop.name, shopInfoFixture.data.shop.name); |
269 |
| - assert.equal(shop.description, shopInfoFixture.data.shop.description); |
270 |
| - assert.ok(fetchMock.done()); |
| 189 | + assert.equal(shop.name, 'graphql'); |
| 190 | + assert.equal(shop.description, 'An example shop with GraphQL.'); |
271 | 191 | });
|
272 | 192 | });
|
273 | 193 |
|
274 | 194 | test('it resolves with `shop` on Client.shop#fetchPolicies', () => {
|
275 |
| - fetchMockPostOnce(fetchMock, apiUrl, shopPoliciesFixture); |
276 | 195 |
|
277 | 196 | return client.shop.fetchPolicies().then((shop) => {
|
278 |
| - assert.equal(shop.privacyPolicy.id, shopPoliciesFixture.data.shop.privacyPolicy.id); |
279 |
| - assert.equal(shop.termsOfService.id, shopPoliciesFixture.data.shop.termsOfService.id); |
280 |
| - assert.equal(shop.refundPolicy.id, shopPoliciesFixture.data.shop.refundPolicy.id); |
281 |
| - assert.ok(fetchMock.done()); |
| 197 | + assert.equal(shop.privacyPolicy.id, 'gid://shopify/ShopPolicy/30401283'); |
| 198 | + assert.equal(shop.termsOfService.id, 'gid://shopify/ShopPolicy/30401347'); |
| 199 | + assert.equal(shop.refundPolicy.id, 'gid://shopify/ShopPolicy/30401219'); |
282 | 200 | });
|
283 | 201 | });
|
284 | 202 | });
|
0 commit comments