Skip to content

Commit 1d6b2ce

Browse files
committed
devops: More unit tests added for product pagination
1 parent da6fc94 commit 1d6b2ce

File tree

1 file changed

+101
-15
lines changed

1 file changed

+101
-15
lines changed

tests/wpunit/ConnectionPaginationTest.php

Lines changed: 101 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,11 @@ static function ( $key_a, $key_b ) {
248248

249249
public function testProductsPaginationWithOrderby() {
250250
$products = [
251-
$this->factory->product->createSimple( [ 'menu_order' => 0 ] ),
252-
$this->factory->product->createSimple( [ 'menu_order' => 1 ] ),
253-
$this->factory->product->createSimple( [ 'menu_order' => 2 ] ),
254-
$this->factory->product->createSimple( [ 'menu_order' => 3 ] ),
255-
$this->factory->product->createSimple( [ 'menu_order' => 4 ] ),
251+
$this->factory->product->createSimple( [ 'menu_order' => 0, 'regular_price' => 5 ] ),
252+
$this->factory->product->createSimple( [ 'menu_order' => 1, 'regular_price' => 4 ] ),
253+
$this->factory->product->createSimple( [ 'menu_order' => 2, 'regular_price' => 3, 'sale_price' => 0.50 ] ),
254+
$this->factory->product->createSimple( [ 'menu_order' => 3, 'regular_price' => 2 ] ),
255+
$this->factory->product->createSimple( [ 'menu_order' => 4, 'regular_price' => 1 ] ),
256256
];
257257

258258
usort(
@@ -283,7 +283,7 @@ static function ( $key_a, $key_b ) {
283283
/**
284284
* Assertion One
285285
*
286-
* Test "first" parameter.
286+
* Test "first" parameter with the "orderby.MENU_ORDER".
287287
*/
288288
$variables = [ 'first' => 2, 'where' => [ 'orderby' => [ [ 'field' => 'MENU_ORDER', 'order' => 'DESC' ] ] ] ];
289289
$response = $this->graphql( compact( 'query', 'variables' ) );
@@ -302,7 +302,7 @@ static function ( $key_a, $key_b ) {
302302
/**
303303
* Assertion Two
304304
*
305-
* Test "after" parameter.
305+
* Test "after" parameter with the "orderby.MENU_ORDER".
306306
*/
307307
$variables = [
308308
'first' => 3,
@@ -326,43 +326,129 @@ static function ( $key_a, $key_b ) {
326326
/**
327327
* Assertion Three
328328
*
329-
* Test "last" parameter.
329+
* Test "last" parameter with the "orderby.MENU_ORDER".
330330
*/
331-
$variables = [ 'last' => 2 ];
331+
$variables = [ 'last' => 2, 'where' => [ 'orderby' => [ [ 'field' => 'MENU_ORDER', 'order' => 'DESC' ] ] ] ];
332332
$response = $this->graphql( compact( 'query', 'variables' ) );
333333
$expected = [
334334
$this->expectedField( 'products.found', 5 ),
335335
$this->expectedField( 'products.pageInfo.hasPreviousPage', true ),
336336
$this->expectedField( 'products.pageInfo.hasNextPage', false ),
337-
$this->expectedField( 'products.pageInfo.startCursor', $this->toCursor( $products[3] ) ),
338-
$this->expectedField( 'products.pageInfo.endCursor', $this->toCursor( $products[4] ) ),
339-
$this->expectedField( 'products.nodes.0.databaseId', $products[3] ),
340-
$this->expectedField( 'products.nodes.1.databaseId', $products[4] ),
337+
$this->expectedField( 'products.pageInfo.startCursor', $this->toCursor( $products[1] ) ),
338+
$this->expectedField( 'products.pageInfo.endCursor', $this->toCursor( $products[0] ) ),
339+
$this->expectedField( 'products.nodes.0.databaseId', $products[1] ),
340+
$this->expectedField( 'products.nodes.1.databaseId', $products[0] ),
341341
];
342342

343343
$this->assertQuerySuccessful( $response, $expected );
344344

345345
/**
346346
* Assertion Four
347347
*
348-
* Test "before" parameter.
348+
* Test "before" parameter with the "orderby.MENU_ORDER".
349349
*/
350350
$variables = [
351351
'last' => 2,
352-
'before' => $this->toCursor( $products[3] ),
352+
'before' => $this->toCursor( $products[1] ),
353+
'where' => [ 'orderby' => [ [ 'field' => 'MENU_ORDER', 'order' => 'DESC' ] ] ]
353354
];
354355
$response = $this->graphql( compact( 'query', 'variables' ) );
355356
$expected = [
356357
$this->expectedField( 'products.found', static::IS_NULL ),
357358
$this->expectedField( 'products.pageInfo.hasPreviousPage', true ),
358359
$this->expectedField( 'products.pageInfo.hasNextPage', true ),
360+
$this->expectedField( 'products.pageInfo.startCursor', $this->toCursor( $products[3] ) ),
361+
$this->expectedField( 'products.pageInfo.endCursor', $this->toCursor( $products[2] ) ),
362+
$this->expectedField( 'products.nodes.0.databaseId', $products[3] ),
363+
$this->expectedField( 'products.nodes.1.databaseId', $products[2] ),
364+
];
365+
366+
$this->assertQuerySuccessful( $response, $expected );
367+
368+
/**
369+
* Assertion Five
370+
*
371+
* Test "first" parameter with the "orderby.PRICE".
372+
*/
373+
$variables = [ 'first' => 2, 'where' => [ 'orderby' => [ [ 'field' => 'PRICE', 'order' => 'DESC' ] ] ] ];
374+
$response = $this->graphql( compact( 'query', 'variables' ) );
375+
$expected = [
376+
$this->expectedField( 'products.found', 5 ),
377+
$this->expectedField( 'products.pageInfo.hasPreviousPage', false ),
378+
$this->expectedField( 'products.pageInfo.hasNextPage', true ),
379+
$this->expectedField( 'products.pageInfo.startCursor', $this->toCursor( $products[4] ) ),
380+
$this->expectedField( 'products.pageInfo.endCursor', $this->toCursor( $products[3] ) ),
381+
$this->expectedField( 'products.nodes.0.databaseId', $products[4] ),
382+
$this->expectedField( 'products.nodes.1.databaseId', $products[3] ),
383+
];
384+
385+
$this->assertQuerySuccessful( $response, $expected );
386+
387+
/**
388+
* Assertion Six
389+
*
390+
* Test "after" parameter with the "orderby.PRICE".
391+
*/
392+
$variables = [
393+
'first' => 3,
394+
'after' => $this->toCursor( $products[3] ),
395+
'where' => [ 'orderby' => [ [ 'field' => 'PRICE', 'order' => 'DESC' ] ] ],
396+
];
397+
$response = $this->graphql( compact( 'query', 'variables' ) );
398+
$expected = [
399+
$this->expectedField( 'products.found', static::IS_NULL ),
400+
$this->expectedField( 'products.pageInfo.hasPreviousPage', true ),
401+
$this->expectedField( 'products.pageInfo.hasNextPage', false ),
359402
$this->expectedField( 'products.pageInfo.startCursor', $this->toCursor( $products[1] ) ),
360403
$this->expectedField( 'products.pageInfo.endCursor', $this->toCursor( $products[2] ) ),
361404
$this->expectedField( 'products.nodes.0.databaseId', $products[1] ),
405+
$this->expectedField( 'products.nodes.1.databaseId', $products[0] ),
406+
$this->expectedField( 'products.nodes.2.databaseId', $products[2] ),
407+
];
408+
409+
$this->assertQuerySuccessful( $response, $expected );
410+
411+
/**
412+
* Assertion Seven
413+
*
414+
* Test "last" parameter with the "orderby.PRICE".
415+
*/
416+
$variables = [ 'last' => 2, 'where' => [ 'orderby' => [ [ 'field' => 'PRICE', 'order' => 'ASC' ] ] ] ];
417+
$response = $this->graphql( compact( 'query', 'variables' ) );
418+
$expected = [
419+
$this->expectedField( 'products.found', 5 ),
420+
$this->expectedField( 'products.pageInfo.hasPreviousPage', true ),
421+
$this->expectedField( 'products.pageInfo.hasNextPage', false ),
422+
$this->expectedField( 'products.pageInfo.startCursor', $this->toCursor( $products[0] ) ),
423+
$this->expectedField( 'products.pageInfo.endCursor', $this->toCursor( $products[2] ) ),
424+
$this->expectedField( 'products.nodes.0.databaseId', $products[0] ),
362425
$this->expectedField( 'products.nodes.1.databaseId', $products[2] ),
363426
];
364427

365428
$this->assertQuerySuccessful( $response, $expected );
429+
430+
/**
431+
* Assertion Eight
432+
*
433+
* Test "before" parameter with the "orderby.PRICE".
434+
*/
435+
$variables = [
436+
'last' => 2,
437+
'before' => $this->toCursor( $products[0] ),
438+
'where' => [ 'orderby' => [ [ 'field' => 'PRICE', 'order' => 'ASC' ] ] ]
439+
];
440+
$response = $this->graphql( compact( 'query', 'variables' ) );
441+
$expected = [
442+
$this->expectedField( 'products.found', static::IS_NULL ),
443+
$this->expectedField( 'products.pageInfo.hasPreviousPage', true ),
444+
$this->expectedField( 'products.pageInfo.hasNextPage', true ),
445+
$this->expectedField( 'products.pageInfo.startCursor', $this->toCursor( $products[3] ) ),
446+
$this->expectedField( 'products.pageInfo.endCursor', $this->toCursor( $products[1] ) ),
447+
$this->expectedField( 'products.nodes.0.databaseId', $products[3] ),
448+
$this->expectedField( 'products.nodes.1.databaseId', $products[1] ),
449+
];
450+
451+
$this->assertQuerySuccessful( $response, $expected );
366452
}
367453

368454
public function testOrdersPagination() {

0 commit comments

Comments
 (0)