@@ -14,6 +14,8 @@ import productWithPaginatedImagesFixture from '../fixtures/product-with-paginate
14
14
import { secondPageImagesFixture , thirdPageImagesFixture } from '../fixtures/paginated-images-fixtures' ;
15
15
import productWithPaginatedVariantsFixture from '../fixtures/product-with-paginated-variants-fixture' ;
16
16
import { secondPageVariantsFixture , thirdPageVariantsFixture } from '../fixtures/paginated-variants-fixtures' ;
17
+ import queryProductFixture from '../fixtures/query-product-fixture' ;
18
+ import queryCollectionFixture from '../fixtures/query-collection-fixture' ;
17
19
import fetchMock from './isomorphic-fetch-mock' ; // eslint-disable-line import/no-unresolved
18
20
import productQuery from '../src/product-query' ;
19
21
import imageQuery from '../src/image-query' ;
@@ -31,6 +33,12 @@ import shopInfoFixture from '../fixtures/shop-info-fixture';
31
33
import shopPoliciesFixture from '../fixtures/shop-policies-fixture' ;
32
34
33
35
suite ( 'client-test' , ( ) => {
36
+ const querySplitter = / [ \s , ] + / ;
37
+
38
+ function tokens ( query ) {
39
+ return query . split ( querySplitter ) . filter ( ( token ) => Boolean ( token ) ) ;
40
+ }
41
+
34
42
teardown ( ( ) => {
35
43
fetchMock . restore ( ) ;
36
44
} ) ;
@@ -283,11 +291,39 @@ suite('client-test', () => {
283
291
284
292
const client = new Client ( config ) ;
285
293
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
+ }` ;
287
320
288
- return client . fetchQueryProducts ( { title : 'Cat' , limit : 10 } , productConnectionQuery ( [ 'title' ] ) ) . then ( ( products ) => {
321
+ assert . deepEqual ( tokens ( JSON . parse ( body ) . query ) , tokens ( queryString ) ) ;
289
322
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 ) ;
291
327
assert . ok ( fetchMock . done ( ) ) ;
292
328
} ) ;
293
329
} ) ;
@@ -300,11 +336,35 @@ suite('client-test', () => {
300
336
301
337
const client = new Client ( config ) ;
302
338
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
+ }` ;
304
363
305
- return client . fetchQueryCollections ( { title : 'Cat Collection' , limit : 10 } , collectionConnectionQuery ( [ 'title' ] ) ) . then ( ( collections ) => {
364
+ assert . deepEqual ( tokens ( JSON . parse ( body ) . query ) , tokens ( queryString ) ) ;
306
365
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 ) ;
308
368
assert . ok ( fetchMock . done ( ) ) ;
309
369
} ) ;
310
370
} ) ;
0 commit comments