Merge pull request #31141 from woocommerce/add/api-tests-shipping-zones-and-methods
API tests for shipping zones and methods
This commit is contained in:
commit
b82fec4198
|
@ -1,3 +1,9 @@
|
|||
# Unreleased
|
||||
|
||||
## Added
|
||||
- Shipping Zones API Tests
|
||||
- Shipping Methods API Tests
|
||||
|
||||
# 0.1.0
|
||||
|
||||
- Initial/beta release
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
const { order, getOrderExample } = require('./order');
|
||||
const { coupon } = require('./coupon');
|
||||
const { refund } = require('./refund');
|
||||
const shared = require('./shared');
|
||||
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 = {
|
||||
order,
|
||||
|
@ -9,4 +11,6 @@ module.exports = {
|
|||
coupon,
|
||||
shared,
|
||||
refund,
|
||||
getShippingZoneExample,
|
||||
getShippingMethodExample,
|
||||
};
|
||||
|
|
|
@ -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,
|
||||
};
|
|
@ -0,0 +1,24 @@
|
|||
/**
|
||||
* Default shipping zone object.
|
||||
*
|
||||
* For more details on shipping zone properties, see:
|
||||
*
|
||||
* https://woocommerce.github.io/woocommerce-rest-api-docs/#shipping-zone-properties
|
||||
*
|
||||
*/
|
||||
const shippingZone = {
|
||||
name: 'US Domestic',
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs a default shipping zone object.
|
||||
*
|
||||
* @returns default shipping zone
|
||||
*/
|
||||
const getShippingZoneExample = () => {
|
||||
return shippingZone;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getShippingZoneExample,
|
||||
};
|
|
@ -1,11 +1,15 @@
|
|||
const { ordersApi } = require('./orders');
|
||||
const { couponsApi } = require('./coupons');
|
||||
const { productsApi } = require('./products');
|
||||
const { refundsApi } = require('./refunds');
|
||||
const { ordersApi } = require( './orders' );
|
||||
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,
|
||||
couponsApi,
|
||||
productsApi,
|
||||
refundsApi,
|
||||
shippingZonesApi,
|
||||
shippingMethodsApi,
|
||||
};
|
||||
|
|
|
@ -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: 'Delete a shipping method from a shipping zone',
|
||||
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,
|
||||
};
|
|
@ -0,0 +1,71 @@
|
|||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
const {
|
||||
getRequest,
|
||||
postRequest,
|
||||
putRequest,
|
||||
deleteRequest,
|
||||
} = require( '../utils/request' );
|
||||
|
||||
/**
|
||||
* WooCommerce Shipping zone endpoints.
|
||||
*
|
||||
* https://woocommerce.github.io/woocommerce-rest-api-docs/#shipping-zones
|
||||
*/
|
||||
const shippingZonesApi = {
|
||||
name: 'Shipping zones',
|
||||
create: {
|
||||
name: 'Create a shipping zone',
|
||||
method: 'POST',
|
||||
path: 'shipping/zones',
|
||||
responseCode: 201,
|
||||
shippingZone: async ( shippingZone ) =>
|
||||
postRequest( `shipping/zones`, shippingZone ),
|
||||
},
|
||||
retrieve: {
|
||||
name: 'Retrieve a shipping zone',
|
||||
method: 'GET',
|
||||
path: 'shipping/zones/<id>',
|
||||
responseCode: 200,
|
||||
shippingZone: async ( shippingZoneId ) =>
|
||||
getRequest( `shipping/zones/${ shippingZoneId }` ),
|
||||
},
|
||||
listAll: {
|
||||
name: 'List all shipping zones',
|
||||
method: 'GET',
|
||||
path: 'shipping/zones',
|
||||
responseCode: 200,
|
||||
shippingZones: async ( params = {} ) =>
|
||||
getRequest( `shipping/zones`, params ),
|
||||
},
|
||||
update: {
|
||||
name: 'Update a shipping zone',
|
||||
method: 'PUT',
|
||||
path: 'shipping/zones/<id>',
|
||||
responseCode: 200,
|
||||
shippingZone: async ( shippingZoneId, updatedShippingZone ) =>
|
||||
putRequest(
|
||||
`shipping/zones/${ shippingZoneId }`,
|
||||
updatedShippingZone
|
||||
),
|
||||
},
|
||||
delete: {
|
||||
name: 'Delete a shipping zone',
|
||||
method: 'DELETE',
|
||||
path: 'shipping/zones/<id>',
|
||||
responseCode: 200,
|
||||
payload: {
|
||||
force: false,
|
||||
},
|
||||
shippingZone: async ( shippingZoneId, deletePermanently ) =>
|
||||
deleteRequest(
|
||||
`shipping/zones/${ shippingZoneId }`,
|
||||
deletePermanently
|
||||
),
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
shippingZonesApi,
|
||||
};
|
|
@ -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
|
||||
);
|
||||
}
|
||||
);
|
||||
} );
|
|
@ -0,0 +1,124 @@
|
|||
const { shippingZonesApi } = require( '../../endpoints' );
|
||||
const { getShippingZoneExample } = require( '../../data' );
|
||||
|
||||
/**
|
||||
* Shipping zone to be created, retrieved, updated, and deleted by the tests.
|
||||
*/
|
||||
const shippingZone = getShippingZoneExample();
|
||||
|
||||
/**
|
||||
* Tests for the WooCommerce Shipping zones API.
|
||||
*
|
||||
* @group api
|
||||
* @group shipping-zones
|
||||
*
|
||||
*/
|
||||
describe( 'Shipping zones API tests', () => {
|
||||
it( 'cannot delete the default shipping zone "Locations not covered by your other zones"', async () => {
|
||||
// Delete all pre-existing shipping zones
|
||||
const { body } = await shippingZonesApi.listAll.shippingZones( {
|
||||
_fields: 'id',
|
||||
} );
|
||||
const ids = body.map( ( { id } ) => id );
|
||||
|
||||
for ( const id of ids ) {
|
||||
await shippingZonesApi.delete.shippingZone( id, true );
|
||||
}
|
||||
|
||||
// Verify that the default shipping zone remains
|
||||
const {
|
||||
body: remainingZones,
|
||||
} = await shippingZonesApi.listAll.shippingZones( {
|
||||
_fields: 'id',
|
||||
} );
|
||||
|
||||
expect( remainingZones ).toHaveLength( 1 );
|
||||
expect( remainingZones[ 0 ].id ).toEqual( 0 );
|
||||
} );
|
||||
|
||||
it( 'cannot update the default shipping zone', async () => {
|
||||
const newZoneDetails = {
|
||||
name: 'Default shipping zone',
|
||||
};
|
||||
const { status, body } = await shippingZonesApi.update.shippingZone(
|
||||
0,
|
||||
newZoneDetails
|
||||
);
|
||||
|
||||
expect( status ).toEqual( 403 );
|
||||
expect( body.code ).toEqual(
|
||||
'woocommerce_rest_shipping_zone_invalid_zone'
|
||||
);
|
||||
expect( body.message ).toEqual(
|
||||
'The "locations not covered by your other zones" zone cannot be updated.'
|
||||
);
|
||||
} );
|
||||
|
||||
it( 'can create a shipping zone', async () => {
|
||||
const { status, body } = await shippingZonesApi.create.shippingZone(
|
||||
shippingZone
|
||||
);
|
||||
|
||||
expect( status ).toEqual( shippingZonesApi.create.responseCode );
|
||||
expect( typeof body.id ).toEqual( 'number' );
|
||||
expect( body.name ).toEqual( shippingZone.name );
|
||||
|
||||
// Save the shipping zone ID. It will be used by other tests.
|
||||
shippingZone.id = body.id;
|
||||
} );
|
||||
|
||||
it( 'can retrieve a shipping zone', async () => {
|
||||
const { status, body } = await shippingZonesApi.retrieve.shippingZone(
|
||||
shippingZone.id
|
||||
);
|
||||
|
||||
expect( status ).toEqual( shippingZonesApi.retrieve.responseCode );
|
||||
expect( body.id ).toEqual( shippingZone.id );
|
||||
} );
|
||||
|
||||
it( 'can list all shipping zones', async () => {
|
||||
const param = {
|
||||
_fields: 'id',
|
||||
};
|
||||
const { status, body } = await shippingZonesApi.listAll.shippingZones(
|
||||
param
|
||||
);
|
||||
|
||||
expect( body ).toHaveLength( 2 ); // the test shipping zone, and the default 'Locations not covered by your other zones'
|
||||
expect( status ).toEqual( shippingZonesApi.listAll.responseCode );
|
||||
expect( body ).toEqual(
|
||||
expect.arrayContaining( [ { id: shippingZone.id } ] )
|
||||
);
|
||||
} );
|
||||
|
||||
it( 'can update a shipping zone', async () => {
|
||||
const updatedShippingZone = {
|
||||
name: 'United States (Domestic)',
|
||||
};
|
||||
|
||||
const { status, body } = await shippingZonesApi.update.shippingZone(
|
||||
shippingZone.id,
|
||||
updatedShippingZone
|
||||
);
|
||||
|
||||
expect( status ).toEqual( shippingZonesApi.retrieve.responseCode );
|
||||
expect( body.id ).toEqual( shippingZone.id );
|
||||
expect( body.name ).toEqual( updatedShippingZone.name );
|
||||
} );
|
||||
|
||||
it( 'can delete a shipping zone', async () => {
|
||||
const { status, body } = await shippingZonesApi.delete.shippingZone(
|
||||
shippingZone.id,
|
||||
true
|
||||
);
|
||||
|
||||
expect( status ).toEqual( shippingZonesApi.delete.responseCode );
|
||||
expect( body.id ).toEqual( shippingZone.id );
|
||||
|
||||
// Verify that the shipping zone can no longer be retrieved
|
||||
const {
|
||||
status: retrieveStatus,
|
||||
} = await shippingZonesApi.retrieve.shippingZone( shippingZone.id );
|
||||
expect( retrieveStatus ).toEqual( 404 );
|
||||
} );
|
||||
} );
|
Loading…
Reference in New Issue