From 49647ea8523243d114ee8fffb5625a1a76287ef7 Mon Sep 17 00:00:00 2001 From: Rodel Date: Tue, 9 Nov 2021 18:19:44 +0800 Subject: [PATCH 1/8] Added tests for order search --- packages/js/api-core-tests/CHANGELOG.md | 1 + packages/js/api-core-tests/data/order.js | 2 +- .../js/api-core-tests/data/shared/customer.js | 2 +- .../js/api-core-tests/endpoints/orders.js | 16 +++-- .../tests/orders/order-search.test.js | 68 +++++++++++++++++++ 5 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 packages/js/api-core-tests/tests/orders/order-search.test.js diff --git a/packages/js/api-core-tests/CHANGELOG.md b/packages/js/api-core-tests/CHANGELOG.md index f9c672179e7..72832818f24 100644 --- a/packages/js/api-core-tests/CHANGELOG.md +++ b/packages/js/api-core-tests/CHANGELOG.md @@ -6,3 +6,4 @@ - Coupons API Tests - Refunds API Tests - Products API Tests +- Order Search API Tests diff --git a/packages/js/api-core-tests/data/order.js b/packages/js/api-core-tests/data/order.js index b3cbb0e6d8b..7fc326660dc 100644 --- a/packages/js/api-core-tests/data/order.js +++ b/packages/js/api-core-tests/data/order.js @@ -24,7 +24,7 @@ const order = { }; const productLineItems = { - name: '', + name: 'Handmade Wooden Gloves', product_id: 93, variation_id: 0, quantity: 2, diff --git a/packages/js/api-core-tests/data/shared/customer.js b/packages/js/api-core-tests/data/shared/customer.js index 1c49bd797d7..ed0ac026aa9 100644 --- a/packages/js/api-core-tests/data/shared/customer.js +++ b/packages/js/api-core-tests/data/shared/customer.js @@ -29,7 +29,7 @@ const customerBilling = { const customerShipping = { first_name: 'Tim', last_name: 'Clark', - company: 'Automattic', + company: 'Murphy LLC', country: 'US', address_1: 'Oxford Ave', address_2: 'Linwood Ave', diff --git a/packages/js/api-core-tests/endpoints/orders.js b/packages/js/api-core-tests/endpoints/orders.js index 1ff3cc81854..2233fec0113 100644 --- a/packages/js/api-core-tests/endpoints/orders.js +++ b/packages/js/api-core-tests/endpoints/orders.js @@ -24,14 +24,15 @@ const ordersApi = { method: 'GET', path: 'orders/', responseCode: 200, - order: async ( orderId ) => getRequest( `orders/${orderId}` ), + order: async ( orderId ) => getRequest( `orders/${ orderId }` ), }, listAll: { name: 'List all orders', method: 'GET', path: 'orders', responseCode: 200, - orders: async () => getRequest( 'orders' ), + orders: async ( ordersQuery = {} ) => + getRequest( 'orders', ordersQuery ), }, update: { name: 'Update an order', @@ -39,7 +40,8 @@ const ordersApi = { path: 'orders/', responseCode: 200, payload: getOrderExample(), - order: async ( orderId, orderDetails ) => putRequest( `orders/${orderId}`, orderDetails ), + order: async ( orderId, orderDetails ) => + putRequest( `orders/${ orderId }`, orderDetails ), }, delete: { name: 'Delete an order', @@ -47,9 +49,10 @@ const ordersApi = { path: 'orders/', responseCode: 200, payload: { - force: false + force: false, }, - order: async ( orderId, deletePermanently ) => deleteRequest( `orders/${orderId}`, deletePermanently ), + order: async ( orderId, deletePermanently ) => + deleteRequest( `orders/${ orderId }`, deletePermanently ), }, batch: { name: 'Batch update orders', @@ -57,7 +60,8 @@ const ordersApi = { path: 'orders/batch', responseCode: 200, payload: shared.getBatchPayloadExample( getOrderExample() ), - orders: async ( batchUpdatePayload ) => postRequest( `orders/batch`, batchUpdatePayload ), + orders: async ( batchUpdatePayload ) => + postRequest( `orders/batch`, batchUpdatePayload ), }, }; diff --git a/packages/js/api-core-tests/tests/orders/order-search.test.js b/packages/js/api-core-tests/tests/orders/order-search.test.js new file mode 100644 index 00000000000..4b439a18950 --- /dev/null +++ b/packages/js/api-core-tests/tests/orders/order-search.test.js @@ -0,0 +1,68 @@ +const { ordersApi } = require( '../../endpoints' ); +const { getOrderExample } = require( '../../data' ); + +/** + * Order to be searched + */ +const order = { + ...getOrderExample(), + shipping_lines: [], + fee_lines: [], + coupon_lines: [], +}; + +/** + * Search parameters to be used + */ +const searchParams = [ + [ 'orderId', 'orderId' ], + [ 'billing first name', order.billing.first_name ], + [ 'billing last name', order.billing.last_name ], + [ 'billing company name', order.billing.company ], + [ 'billing address 1', order.billing.address_1 ], + [ 'billing address 2', order.billing.address_2 ], + [ 'billing city name', order.billing.city ], + [ 'billing post code', order.billing.postcode ], + [ 'billing email', order.billing.email ], + [ 'billing phone', order.billing.phone ], + [ 'billing state', order.billing.state ], + [ 'shipping first name', order.shipping.first_name ], + [ 'shipping last name', order.shipping.last_name ], + [ 'shipping address 1', order.shipping.address_1 ], + [ 'shipping address 2', order.shipping.address_2 ], + [ 'shipping city', order.shipping.city ], + [ 'shipping post code', order.shipping.postcode ], + [ 'shipping state', order.shipping.state ], + [ 'item name', order.line_items[ 0 ].name ], +]; + +/** + * Tests for the WooCommerce Order Search API. + * + * @group api + * @group orders + * + */ +describe( 'Order Search API tests', () => { + beforeAll( async () => { + // Create an order and save its ID + const { body } = await ordersApi.create.order( order ); + order.id = body.id; + } ); + + afterAll( async () => { + // Cleanup: Delete the order + await ordersApi.delete.order( order.id, true ); + } ); + + it.each( searchParams )( 'can search by %s', async ( title, param ) => { + const searchValue = param === 'orderId' ? order.id : param; + + const { body } = await ordersApi.listAll.orders( { + search: searchValue, + } ); + + expect( body ).toHaveLength( 1 ); + expect( body[ 0 ].id ).toEqual( order.id ); + } ); +} ); From ee3b315eaae14c9eb5c6bfb9a965e55fb15f317c Mon Sep 17 00:00:00 2001 From: Rodel Date: Wed, 10 Nov 2021 22:57:50 +0800 Subject: [PATCH 2/8] Changed shipping phone data --- packages/js/api-core-tests/data/shared/customer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/js/api-core-tests/data/shared/customer.js b/packages/js/api-core-tests/data/shared/customer.js index ed0ac026aa9..3ed3d58a096 100644 --- a/packages/js/api-core-tests/data/shared/customer.js +++ b/packages/js/api-core-tests/data/shared/customer.js @@ -36,7 +36,7 @@ const customerShipping = { city: 'Buffalo', state: 'NY', postcode: '14201', - phone: '123456789', + phone: '6146524353', }; module.exports = { From 988618056dfb09646e809e355fabe1e2b86a9d42 Mon Sep 17 00:00:00 2001 From: Rodel Date: Wed, 10 Nov 2021 22:58:04 +0800 Subject: [PATCH 3/8] Added test for empty result set --- .../js/api-core-tests/tests/orders/order-search.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/js/api-core-tests/tests/orders/order-search.test.js b/packages/js/api-core-tests/tests/orders/order-search.test.js index 4b439a18950..d23d73ed17c 100644 --- a/packages/js/api-core-tests/tests/orders/order-search.test.js +++ b/packages/js/api-core-tests/tests/orders/order-search.test.js @@ -65,4 +65,12 @@ describe( 'Order Search API tests', () => { expect( body ).toHaveLength( 1 ); expect( body[ 0 ].id ).toEqual( order.id ); } ); + + it( 'can return an empty result set when no matches were found', async () => { + const { body } = await ordersApi.listAll.orders( { + search: 'Chauncey Smith Kunde', + } ); + + expect( body ).toEqual( [] ); + } ); } ); From a33a1d7c87878ecd6b70f0a4b61574537ea842c5 Mon Sep 17 00:00:00 2001 From: Rodel Date: Thu, 11 Nov 2021 15:44:25 +0800 Subject: [PATCH 4/8] Removed duplicate tests --- .../tests/orders/order-search.test.js | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/js/api-core-tests/tests/orders/order-search.test.js b/packages/js/api-core-tests/tests/orders/order-search.test.js index d23d73ed17c..dd460d7e0af 100644 --- a/packages/js/api-core-tests/tests/orders/order-search.test.js +++ b/packages/js/api-core-tests/tests/orders/order-search.test.js @@ -12,28 +12,31 @@ const order = { }; /** - * Search parameters to be used + * Search parameters to be used. + * The following scenarios are not covered in this test suite because they're already covered in the `List all orders > search` test in `orders.test.js` + * ``` + * can search by billing address 1 + * can search by shipping address 1 + * can search by billing last name + * can search by billing email + * can search by item name + * ``` */ const searchParams = [ [ 'orderId', 'orderId' ], [ 'billing first name', order.billing.first_name ], - [ 'billing last name', order.billing.last_name ], [ 'billing company name', order.billing.company ], - [ 'billing address 1', order.billing.address_1 ], [ 'billing address 2', order.billing.address_2 ], [ 'billing city name', order.billing.city ], [ 'billing post code', order.billing.postcode ], - [ 'billing email', order.billing.email ], [ 'billing phone', order.billing.phone ], [ 'billing state', order.billing.state ], [ 'shipping first name', order.shipping.first_name ], [ 'shipping last name', order.shipping.last_name ], - [ 'shipping address 1', order.shipping.address_1 ], [ 'shipping address 2', order.shipping.address_2 ], [ 'shipping city', order.shipping.city ], [ 'shipping post code', order.shipping.postcode ], [ 'shipping state', order.shipping.state ], - [ 'item name', order.line_items[ 0 ].name ], ]; /** @@ -58,19 +61,21 @@ describe( 'Order Search API tests', () => { it.each( searchParams )( 'can search by %s', async ( title, param ) => { const searchValue = param === 'orderId' ? order.id : param; - const { body } = await ordersApi.listAll.orders( { + const { status, body } = await ordersApi.listAll.orders( { search: searchValue, } ); + expect( status ).toEqual( ordersApi.listAll.responseCode ); expect( body ).toHaveLength( 1 ); expect( body[ 0 ].id ).toEqual( order.id ); } ); it( 'can return an empty result set when no matches were found', async () => { - const { body } = await ordersApi.listAll.orders( { + const { status, body } = await ordersApi.listAll.orders( { search: 'Chauncey Smith Kunde', } ); + expect( status ).toEqual( ordersApi.listAll.responseCode ); expect( body ).toEqual( [] ); } ); } ); From e551881817695a0e18eab866799f283bdb8f4174 Mon Sep 17 00:00:00 2001 From: Rodel Date: Wed, 1 Dec 2021 23:26:02 +0800 Subject: [PATCH 5/8] Revert unnecessary change --- packages/js/api-core-tests/data/order.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/js/api-core-tests/data/order.js b/packages/js/api-core-tests/data/order.js index 7fc326660dc..b3cbb0e6d8b 100644 --- a/packages/js/api-core-tests/data/order.js +++ b/packages/js/api-core-tests/data/order.js @@ -24,7 +24,7 @@ const order = { }; const productLineItems = { - name: 'Handmade Wooden Gloves', + name: '', product_id: 93, variation_id: 0, quantity: 2, From 3f269b91292250354fc2d6728d1fdecc9b302ba4 Mon Sep 17 00:00:00 2001 From: Rodel Date: Wed, 1 Dec 2021 23:40:18 +0800 Subject: [PATCH 6/8] Set test data within the test --- .../js/api-core-tests/data/shared/customer.js | 4 ++-- .../tests/orders/order-search.test.js | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/js/api-core-tests/data/shared/customer.js b/packages/js/api-core-tests/data/shared/customer.js index 3ed3d58a096..1c49bd797d7 100644 --- a/packages/js/api-core-tests/data/shared/customer.js +++ b/packages/js/api-core-tests/data/shared/customer.js @@ -29,14 +29,14 @@ const customerBilling = { const customerShipping = { first_name: 'Tim', last_name: 'Clark', - company: 'Murphy LLC', + company: 'Automattic', country: 'US', address_1: 'Oxford Ave', address_2: 'Linwood Ave', city: 'Buffalo', state: 'NY', postcode: '14201', - phone: '6146524353', + phone: '123456789', }; module.exports = { diff --git a/packages/js/api-core-tests/tests/orders/order-search.test.js b/packages/js/api-core-tests/tests/orders/order-search.test.js index dd460d7e0af..ac281319045 100644 --- a/packages/js/api-core-tests/tests/orders/order-search.test.js +++ b/packages/js/api-core-tests/tests/orders/order-search.test.js @@ -1,11 +1,22 @@ const { ordersApi } = require( '../../endpoints' ); -const { getOrderExample } = require( '../../data' ); +const { getOrderExample, shared } = require( '../../data' ); /** * Order to be searched */ const order = { ...getOrderExample(), + shipping: { + ...shared.customerShipping, + company: 'Murphy LLC', + phone: '6146524353', + }, + line_items: [ + { + name: 'Handmade Wooden Gloves', + product_id: '45', + }, + ], shipping_lines: [], fee_lines: [], coupon_lines: [], @@ -44,6 +55,8 @@ const searchParams = [ * * @group api * @group orders + * @group wip + * mytodo remove wip * */ describe( 'Order Search API tests', () => { From dd3b37265883316b40c53065955b0b7a8d495438 Mon Sep 17 00:00:00 2001 From: Rodel Date: Wed, 1 Dec 2021 23:44:34 +0800 Subject: [PATCH 7/8] Minor cleanup --- packages/js/api-core-tests/tests/orders/order-search.test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/js/api-core-tests/tests/orders/order-search.test.js b/packages/js/api-core-tests/tests/orders/order-search.test.js index ac281319045..4cd7ca2d80b 100644 --- a/packages/js/api-core-tests/tests/orders/order-search.test.js +++ b/packages/js/api-core-tests/tests/orders/order-search.test.js @@ -55,8 +55,6 @@ const searchParams = [ * * @group api * @group orders - * @group wip - * mytodo remove wip * */ describe( 'Order Search API tests', () => { From ba87378479f37dec199cc241541efcaa5f3d69e9 Mon Sep 17 00:00:00 2001 From: Rodel Date: Thu, 2 Dec 2021 00:28:55 +0800 Subject: [PATCH 8/8] Removed unnecessary property in test data --- .../js/api-core-tests/tests/orders/order-search.test.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/packages/js/api-core-tests/tests/orders/order-search.test.js b/packages/js/api-core-tests/tests/orders/order-search.test.js index 4cd7ca2d80b..500681dae0a 100644 --- a/packages/js/api-core-tests/tests/orders/order-search.test.js +++ b/packages/js/api-core-tests/tests/orders/order-search.test.js @@ -11,12 +11,6 @@ const order = { company: 'Murphy LLC', phone: '6146524353', }, - line_items: [ - { - name: 'Handmade Wooden Gloves', - product_id: '45', - }, - ], shipping_lines: [], fee_lines: [], coupon_lines: [],