Added shipping method tests

This commit is contained in:
Rodel 2021-11-11 21:11:03 +08:00
parent 820110dbff
commit c7bab0d3cd
7 changed files with 174 additions and 2 deletions

View File

@ -2,6 +2,7 @@ const { order, getOrderExample } = require( './order' );
const { coupon } = require( './coupon' );
const { refund } = require( './refund' );
const { getShippingZoneExample } = require( './shipping-zone' );
const { getShippingMethodExample } = require( './shipping-method' );
const shared = require( './shared' );
module.exports = {
@ -11,4 +12,5 @@ module.exports = {
shared,
refund,
getShippingZoneExample,
getShippingMethodExample,
};

View File

@ -0,0 +1,27 @@
/**
* Constructs a shipping method based on the given `methodId` and `cost`.
*
* `methodId` should be one of the following:
* - `free_shipping`
* - `flat_rate`
* - `local_pickup`
*
* @returns shipping method object that can serve as a request payload for adding a shipping method to a shipping zone.
*/
const getShippingMethodExample = ( methodId, cost ) => {
const shippingMethodExample = {
method_id: methodId,
};
if ( cost !== undefined ) {
shippingMethodExample.settings = {
cost: cost,
};
}
return shippingMethodExample;
};
module.exports = {
getShippingMethodExample,
};

View File

@ -3,6 +3,7 @@ const { couponsApi } = require( './coupons' );
const { productsApi } = require( './products' );
const { refundsApi } = require( './refunds' );
const { shippingZonesApi } = require( './shipping-zones' );
const { shippingMethodsApi } = require( './shipping-methods' );
module.exports = {
ordersApi,
@ -10,4 +11,5 @@ module.exports = {
productsApi,
refundsApi,
shippingZonesApi,
shippingMethodsApi,
};

View File

@ -0,0 +1,84 @@
/**
* Internal dependencies
*/
const {
getRequest,
postRequest,
putRequest,
deleteRequest,
} = require( '../utils/request' );
/**
* WooCommerce Shipping method endpoints.
*
* https://woocommerce.github.io/woocommerce-rest-api-docs/#shipping-methods
*/
const shippingMethodsApi = {
name: 'Shipping methods',
create: {
name: 'Include a shipping method to a shipping zone',
method: 'POST',
path: 'shipping/zones/<id>/methods',
responseCode: 200,
shippingMethod: async ( shippingZoneId, shippingMethod ) =>
postRequest(
`shipping/zones/${ shippingZoneId }/methods`,
shippingMethod
),
},
retrieve: {
name: 'Retrieve a shipping method from a shipping zone',
method: 'GET',
path: 'shipping/zones/<zone_id>/methods/<id>',
responseCode: 200,
shippingMethod: async ( shippingZoneId, shippingMethodInstanceId ) =>
getRequest(
`shipping/zones/${ shippingZoneId }/methods/${ shippingMethodInstanceId }`
),
},
listAll: {
name: 'List all shipping methods from a shipping zone',
method: 'GET',
path: 'shipping/zones/<id>/methods',
responseCode: 200,
shippingMethods: async ( shippingZoneId, params = {} ) =>
getRequest( `shipping/zones/${ shippingZoneId }/methods`, params ),
},
update: {
name: 'Update a shipping method of a shipping zone',
method: 'PUT',
path: 'shipping/zones/<zone_id>/methods/<id>',
responseCode: 200,
shippingMethod: async (
shippingZoneId,
shippingMethodInstanceId,
updatedShippingMethod
) =>
putRequest(
`shipping/zones/${ shippingZoneId }/methods/${ shippingMethodInstanceId }`,
updatedShippingMethod
),
},
delete: {
name: 'shipping/zones/<zone_id>/methods/<id>',
method: 'DELETE',
path: 'shipping/zones/<zone_id>/methods/<id>>',
responseCode: 200,
payload: {
force: false,
},
shippingMethod: async (
shippingZoneId,
shippingMethodInstanceId,
deletePermanently
) =>
deleteRequest(
`shipping/zones/${ shippingZoneId }/methods/${ shippingMethodInstanceId }`,
deletePermanently
),
},
};
module.exports = {
shippingMethodsApi,
};

View File

@ -9,7 +9,7 @@ const {
} = require( '../utils/request' );
/**
* WooCommerce Refunds endpoints.
* WooCommerce Shipping zone endpoints.
*
* https://woocommerce.github.io/woocommerce-rest-api-docs/#shipping-zones
*/

View File

@ -0,0 +1,57 @@
const { shippingMethodsApi } = require( '../../endpoints' );
const { getShippingMethodExample } = require( '../../data' );
/**
* Shipping zone id for "Locations not covered by your other zones".
*/
const shippingZoneId = 0;
/**
* Data table for shipping methods.
*/
const shippingMethods = [
[ 'Flat rate', 'flat_rate', '10' ],
[ 'Free shipping', 'free_shipping', undefined ],
[ 'Local pickup', 'local_pickup', '30' ],
];
/**
* Tests for the WooCommerce Shipping methods API.
*
* @group api
* @group shipping-methods
*
*/
describe( 'Shipping methods API tests', () => {
it.each( shippingMethods )(
"can add a '%s' shipping method",
async ( methodTitle, methodId, cost ) => {
const shippingMethod = getShippingMethodExample( methodId, cost );
const {
status,
body,
} = await shippingMethodsApi.create.shippingMethod(
shippingZoneId,
shippingMethod
);
expect( status ).toEqual( shippingMethodsApi.create.responseCode );
expect( typeof body.id ).toEqual( 'number' );
expect( body.method_id ).toEqual( methodId );
expect( body.method_title ).toEqual( methodTitle );
expect( body.enabled ).toEqual( true );
if ( [ 'flat_rate', 'local_pickup' ].includes( methodId ) ) {
expect( body.settings.cost.value ).toEqual( cost );
}
// Cleanup: Delete the shipping method
await shippingMethodsApi.delete.shippingMethod(
shippingZoneId,
body.id,
true
);
}
);
} );

View File

@ -10,7 +10,7 @@ const shippingZone = getShippingZoneExample();
* Tests for the WooCommerce Shipping zones API.
*
* @group api
* @group shipping-zone
* @group shipping-zones
*
*/
describe( 'Shipping zones API tests', () => {