Skip to content

Commit eff2d53

Browse files
Jessica JiangMina Smart
Jessica Jiang
authored and
Mina Smart
committed
Add query checking to tests
1 parent bec4260 commit eff2d53

File tree

4 files changed

+117
-6
lines changed

4 files changed

+117
-6
lines changed

fixtures/query-collection-fixture.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export default {
2+
"data": {
3+
"shop": {
4+
"collections": {
5+
"pageInfo": {
6+
"hasNextPage": false,
7+
"hasPreviousPage": false
8+
},
9+
"edges": [
10+
{
11+
"cursor": "eyJsYXN0X2lkIjozNjkzMTI1ODQsImxhc3RfdmFsdWUiOiIzNjkzMTI1ODQifQ==",
12+
"node": {
13+
"id": "gid://shopify/Collection/369312584",
14+
"title": "Cat Collection",
15+
"updatedAt": "2017-02-09T19:06:44Z"
16+
}
17+
}
18+
]
19+
}
20+
}
21+
}
22+
};

fixtures/query-product-fixture.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export default {
2+
"data": {
3+
"shop": {
4+
"products": {
5+
"pageInfo": {
6+
"hasNextPage": false,
7+
"hasPreviousPage": false
8+
},
9+
"edges": [
10+
{
11+
"cursor": "eyJsYXN0X2lkIjo3ODU3OTg5Mzg0LCJsYXN0X3ZhbHVlIjoiNzg1Nzk4OTM4NCJ9",
12+
"node": {
13+
"id": "gid://shopify/Product/7857989384",
14+
"updatedAt": "2017-02-17T19:56:02Z",
15+
"createdAt": "2016-09-25T21:31:33Z",
16+
"productType": "dog",
17+
"title": "Cat"
18+
}
19+
}
20+
]
21+
}
22+
}
23+
}
24+
};

test/.eslintrc.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"no-unused-vars": ["error", { "varsIgnorePattern": "^_" }]
4+
}
5+
}

test/client-test.js

+66-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import productWithPaginatedImagesFixture from '../fixtures/product-with-paginate
1414
import {secondPageImagesFixture, thirdPageImagesFixture} from '../fixtures/paginated-images-fixtures';
1515
import productWithPaginatedVariantsFixture from '../fixtures/product-with-paginated-variants-fixture';
1616
import {secondPageVariantsFixture, thirdPageVariantsFixture} from '../fixtures/paginated-variants-fixtures';
17+
import queryProductFixture from '../fixtures/query-product-fixture';
18+
import queryCollectionFixture from '../fixtures/query-collection-fixture';
1719
import fetchMock from './isomorphic-fetch-mock'; // eslint-disable-line import/no-unresolved
1820
import productQuery from '../src/product-query';
1921
import imageQuery from '../src/image-query';
@@ -31,6 +33,12 @@ import shopInfoFixture from '../fixtures/shop-info-fixture';
3133
import shopPoliciesFixture from '../fixtures/shop-policies-fixture';
3234

3335
suite('client-test', () => {
36+
const querySplitter = /[\s,]+/;
37+
38+
function tokens(query) {
39+
return query.split(querySplitter).filter((token) => Boolean(token));
40+
}
41+
3442
teardown(() => {
3543
fetchMock.restore();
3644
});
@@ -283,11 +291,39 @@ suite('client-test', () => {
283291

284292
const client = new Client(config);
285293

286-
fetchMock.postOnce('https://query-products.myshopify.com/api/graphql', {data: {shop: {products: {edges: [{node: {title: 'Cat'}}], pageInfo: {hasNextPage: false, hasPreviousPage: false}}}}});
294+
fetchMock.postOnce('https://query-products.myshopify.com/api/graphql', queryProductFixture);
295+
296+
const query = {title: 'Cat', updatedAtMin: '2016-09-25T21:31:33', createdAtMin: '2016-09-25T21:31:33', productType: 'dog', limit: 10};
297+
298+
return client.fetchQueryProducts(query, productConnectionQuery(['updatedAt', 'createdAt', 'productType', 'title'])).then((products) => {
299+
const [_arg, {body}] = fetchMock.lastCall('https://query-products.myshopify.com/api/graphql');
300+
const queryString = `query {
301+
shop {
302+
products (first: 10 query: "title:'Cat' updated_at:>='2016-09-25T21:31:33' created_at:>='2016-09-25T21:31:33' product_type:'dog'") {
303+
pageInfo {
304+
hasNextPage,
305+
hasPreviousPage
306+
},
307+
edges {
308+
cursor,
309+
node {
310+
id,
311+
updatedAt,
312+
createdAt,
313+
productType,
314+
title
315+
}
316+
}
317+
}
318+
}
319+
}`;
287320

288-
return client.fetchQueryProducts({title: 'Cat', limit: 10}, productConnectionQuery(['title'])).then((products) => {
321+
assert.deepEqual(tokens(JSON.parse(body).query), tokens(queryString));
289322
assert.equal(products.length, 1);
290-
assert.equal(products[0].title, 'Cat');
323+
assert.equal(products[0].title, queryProductFixture.data.shop.products.edges[0].node.title);
324+
assert.equal(products[0].createdAt, queryProductFixture.data.shop.products.edges[0].node.createdAt);
325+
assert.equal(products[0].updatedAt, queryProductFixture.data.shop.products.edges[0].node.updatedAt);
326+
assert.equal(products[0].productType, queryProductFixture.data.shop.products.edges[0].node.productType);
291327
assert.ok(fetchMock.done());
292328
});
293329
});
@@ -300,11 +336,35 @@ suite('client-test', () => {
300336

301337
const client = new Client(config);
302338

303-
fetchMock.postOnce('https://query-collections.myshopify.com/api/graphql', {data: {shop: {collections: {edges: [{node: {title: 'Cat Collection'}}], pageInfo: {hasNextPage: false, hasPreviousPage: false}}}}});
339+
fetchMock.postOnce('https://query-collections.myshopify.com/api/graphql', queryCollectionFixture);
340+
341+
const query = {title: 'Cat Collection', updatedAtMin: '2016-09-25T21:31:33', limit: 10};
342+
343+
return client.fetchQueryCollections(query, collectionConnectionQuery(['title', 'updatedAt'])).then((collections) => {
344+
const [_arg, {body}] = fetchMock.lastCall('https://query-collections.myshopify.com/api/graphql');
345+
const queryString = `query {
346+
shop {
347+
collections (first: 10 query: "title:'Cat Collection' updated_at:>='2016-09-25T21:31:33'") {
348+
pageInfo {
349+
hasNextPage,
350+
hasPreviousPage
351+
},
352+
edges {
353+
cursor,
354+
node {
355+
id,
356+
title,
357+
updatedAt
358+
}
359+
}
360+
}
361+
}
362+
}`;
304363

305-
return client.fetchQueryCollections({title: 'Cat Collection', limit: 10}, collectionConnectionQuery(['title'])).then((collections) => {
364+
assert.deepEqual(tokens(JSON.parse(body).query), tokens(queryString));
306365
assert.equal(collections.length, 1);
307-
assert.equal(collections[0].title, 'Cat Collection');
366+
assert.equal(collections[0].title, queryCollectionFixture.data.shop.collections.edges[0].node.title);
367+
assert.equal(collections[0].updatedAt, queryCollectionFixture.data.shop.collections.edges[0].node.updatedAt);
308368
assert.ok(fetchMock.done());
309369
});
310370
});

0 commit comments

Comments
 (0)