Skip to content

Commit 176d631

Browse files
juanpprietokdaviduik
authored andcommitted
Improve tests in client-integration-test
Before these tests weren't actually testing anything useful - the API response was mocked, and then the test ensured that the mock returned the return value we mocked. Now these tests execute actual storefront API calls, and are now useful integration tests that we can trust.
1 parent bf81996 commit 176d631

File tree

1 file changed

+80
-162
lines changed

1 file changed

+80
-162
lines changed

test/client-integration-test.js

+80-162
Original file line numberDiff line numberDiff line change
@@ -1,284 +1,202 @@
11
import assert from 'assert';
22
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+
];
2447

2548
suite('client-integration-test', () => {
26-
const domain = 'client-integration-tests.myshopify.io';
49+
const domain = 'graphql.myshopify.com';
2750
const config = {
28-
storefrontAccessToken: 'abc123',
51+
storefrontAccessToken: '595005d0c565f6969eece280de85edb5',
2952
domain
3053
};
3154
let client;
32-
let apiUrl;
3355

3456
setup(() => {
3557
client = Client.buildClient(config);
36-
apiUrl = `https://${domain}/api/${client.config.apiVersion}/graphql`;
37-
fetchMock.reset();
3858
});
3959

4060
teardown(() => {
4161
client = null;
42-
fetchMock.restore();
4362
});
4463

4564
test('it resolves with an array of products on Client.product#fetchAllProducts', () => {
46-
fetchMockPostOnce(fetchMock, apiUrl, shopWithProductsFixture);
4765

4866
return client.product.fetchAll().then((products) => {
4967
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]);
5569
});
5670
});
5771

5872
test('it resolves with a single product on Client.product#fetch', () => {
59-
fetchMockPostOnce(fetchMock, apiUrl, singleProductFixture);
60-
61-
const id = singleProductFixture.data.node.id;
6273

63-
return client.product.fetch(id).then((product) => {
74+
return client.product.fetch(PRODUCT_IDS[0]).then((product) => {
6475
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]);
6777
});
6878
});
6979

7080
test('it resolves recommended products on Client.product#fetchRecommendations', () => {
71-
fetchMockPostOnce(fetchMock, apiUrl, productRecommendationsFixture);
7281

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) => {
7683
assert.ok(Array.isArray(products), 'recommended products are an array');
77-
assert.ok(fetchMock.done());
7884
});
7985
});
8086

8187
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) => {
8889
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]);
9292
});
9393
});
9494

9595
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-
10096
return client.product.fetchAll().then((products) => {
10197
const images = products[0].images;
10298

10399
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');
110101
});
111102
});
112103

113104
test('it fetches all variants on Client.product#fetch', () => {
114-
fetchMockPostOnce(fetchMock, apiUrl, productWithPaginatedVariantsFixture);
115-
fetchMockPostOnce(fetchMock, apiUrl, secondPageVariantsFixture);
116-
fetchMockPostOnce(fetchMock, apiUrl, thirdPageVariantsFixture);
117105

118-
return client.product.fetch(productWithPaginatedVariantsFixture.data.node.id).then((product) => {
106+
return client.product.fetch(PRODUCT_IDS[0]).then((product) => {
119107
const variants = product.variants;
120108

121109
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');
149111
});
150112
});
151113

152114
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';
156116

157117
return client.product.fetchByHandle(handle).then((product) => {
158-
assert.equal(product.id, productByHandleFixture.data.productByHandle.id);
118+
assert.equal(product.id, PRODUCT_IDS[0]);
159119
assert.equal(product.handle, handle);
160-
assert.ok(fetchMock.done());
161120
});
162121
});
163122

164123
test('it resolves products with Client.product#fetchQuery', () => {
165-
fetchMockPostOnce(fetchMock, apiUrl, shopWithProductsFixture);
166124

167125
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');
172127
});
173128
});
174129

175130
test('it resolves with an array of collections on Client.collection#fetchAll', () => {
176-
fetchMockPostOnce(fetchMock, apiUrl, shopWithCollectionsFixture);
177-
178131
return client.collection.fetchAll().then((collections) => {
179132
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');
185134
});
186135
});
187136

188137
test('it resolves with a single collection on Client.collection#fetch', () => {
189-
fetchMockPostOnce(fetchMock, apiUrl, singleCollectionFixture);
190-
191-
const id = singleCollectionFixture.data.node.id;
192138

193-
return client.collection.fetch(id).then((collection) => {
139+
return client.collection.fetch(COLLECTIONS[0].id).then((collection) => {
194140
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);
197142
});
198143
});
199144

200145
test('it can fetch a collection by handle through Client.collection#fetchByHandle', () => {
201-
fetchMockPostOnce(fetchMock, apiUrl, collectionByHandleFixture);
202146

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);
209150
});
210151
});
211152

212153
test('it can fetch collections with the query arg on Client.collection#fetchQuery', () => {
213-
fetchMockPostOnce(fetchMock, apiUrl, shopWithCollectionsFixture);
214154

215155
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');
220157
});
221158
});
222159

223160
test('it can fetch all collections with products on Client.collection#fetchAllWithProducts', () => {
224-
fetchMockPostOnce(fetchMock, apiUrl, shopWithCollectionsWithProductsFixture);
225161

226162
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');
232166
});
233167
});
234168

235169
test('it can fetch a single collection with products on Client.collection#fetchWithProducts', () => {
236-
fetchMockPostOnce(fetchMock, apiUrl, collectionWithProductsFixture);
237170

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) => {
241172
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);
245175
});
246176
});
247177

248178
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);
253179

254180
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);
261184
});
262185
});
263186

264187
test('it resolves with `shop` on Client.shop#fetchInfo', () => {
265-
fetchMockPostOnce(fetchMock, apiUrl, shopInfoFixture);
266-
267188
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.');
271191
});
272192
});
273193

274194
test('it resolves with `shop` on Client.shop#fetchPolicies', () => {
275-
fetchMockPostOnce(fetchMock, apiUrl, shopPoliciesFixture);
276195

277196
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');
282200
});
283201
});
284202
});

0 commit comments

Comments
 (0)