Added tests for Refunds API
This commit is contained in:
parent
b3410791e5
commit
515b073834
|
@ -1,5 +1,6 @@
|
||||||
const { order, getOrderExample } = require('./order');
|
const { order, getOrderExample } = require('./order');
|
||||||
const { coupon } = require('./coupon');
|
const { coupon } = require('./coupon');
|
||||||
|
const { refund } = require('./refund');
|
||||||
const shared = require('./shared');
|
const shared = require('./shared');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
@ -7,4 +8,5 @@ module.exports = {
|
||||||
getOrderExample,
|
getOrderExample,
|
||||||
coupon,
|
coupon,
|
||||||
shared,
|
shared,
|
||||||
|
refund,
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
/**
|
||||||
|
* A basic refund.
|
||||||
|
*
|
||||||
|
* For more details on the order refund properties, see:
|
||||||
|
*
|
||||||
|
* https://woocommerce.github.io/woocommerce-rest-api-docs/#order-refund-properties
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
const refund = {
|
||||||
|
api_refund: false,
|
||||||
|
amount: '1.00',
|
||||||
|
reason: 'Late delivery refund.',
|
||||||
|
line_items: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
refund: refund,
|
||||||
|
};
|
|
@ -1,9 +1,11 @@
|
||||||
const { ordersApi } = require('./orders');
|
const { ordersApi } = require('./orders');
|
||||||
const { couponsApi } = require('./coupons');
|
const { couponsApi } = require('./coupons');
|
||||||
const { productsApi } = require('./products');
|
const { productsApi } = require('./products');
|
||||||
|
const { refundsApi } = require('./refunds');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
ordersApi,
|
ordersApi,
|
||||||
couponsApi,
|
couponsApi,
|
||||||
productsApi,
|
productsApi,
|
||||||
|
refundsApi,
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
/**
|
||||||
|
* Internal dependencies
|
||||||
|
*/
|
||||||
|
const {
|
||||||
|
getRequest,
|
||||||
|
postRequest,
|
||||||
|
putRequest,
|
||||||
|
deleteRequest,
|
||||||
|
} = require( '../utils/request' );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WooCommerce Refunds endpoints.
|
||||||
|
*
|
||||||
|
* https://woocommerce.github.io/woocommerce-rest-api-docs/#refunds
|
||||||
|
*/
|
||||||
|
const refundsApi = {
|
||||||
|
name: 'Refunds',
|
||||||
|
create: {
|
||||||
|
name: 'Create a refund',
|
||||||
|
method: 'POST',
|
||||||
|
path: 'orders/<id>/refunds',
|
||||||
|
responseCode: 201,
|
||||||
|
refund: async ( orderId, refundDetails ) =>
|
||||||
|
postRequest( `orders/${ orderId }/refunds`, refundDetails ),
|
||||||
|
},
|
||||||
|
retrieve: {
|
||||||
|
name: 'Retrieve a refund',
|
||||||
|
method: 'GET',
|
||||||
|
path: 'orders/<id>/refunds/<refund_id>',
|
||||||
|
responseCode: 200,
|
||||||
|
refund: async ( orderId, refundId ) =>
|
||||||
|
getRequest( `orders/${ orderId }/refunds/${ refundId }` ),
|
||||||
|
},
|
||||||
|
listAll: {
|
||||||
|
name: 'List all refunds',
|
||||||
|
method: 'GET',
|
||||||
|
path: 'orders/<id>/refunds',
|
||||||
|
responseCode: 200,
|
||||||
|
refunds: async ( orderId ) =>
|
||||||
|
getRequest( `orders/${ orderId }/refunds` ),
|
||||||
|
},
|
||||||
|
delete: {
|
||||||
|
name: 'Delete a refund',
|
||||||
|
method: 'DELETE',
|
||||||
|
path: 'orders/<id>/refunds/<refund_id>',
|
||||||
|
responseCode: 200,
|
||||||
|
payload: {
|
||||||
|
force: false,
|
||||||
|
},
|
||||||
|
refund: async ( orderId, refundId, deletePermanently ) =>
|
||||||
|
deleteRequest(
|
||||||
|
`orders/${ orderId }/refunds/${ refundId }`,
|
||||||
|
deletePermanently
|
||||||
|
),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
refundsApi: refundsApi,
|
||||||
|
};
|
|
@ -0,0 +1,123 @@
|
||||||
|
const { refundsApi } = require( '../../endpoints/refunds' );
|
||||||
|
const { ordersApi } = require( '../../endpoints/orders' );
|
||||||
|
const { productsApi } = require( '../../endpoints/products' );
|
||||||
|
const { refund } = require( '../../data' );
|
||||||
|
const { expect } = require( '@jest/globals' );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for the WooCommerce Refunds API.
|
||||||
|
*
|
||||||
|
* @group api
|
||||||
|
* @group refunds
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
describe( 'Refunds API tests', () => {
|
||||||
|
let expectedRefund;
|
||||||
|
let orderId;
|
||||||
|
let productId;
|
||||||
|
|
||||||
|
beforeAll( async () => {
|
||||||
|
// Create a product and save its product ID
|
||||||
|
const product = {
|
||||||
|
name: 'Simple Product for Refunds API tests',
|
||||||
|
regular_price: '100',
|
||||||
|
};
|
||||||
|
const createProductResponse = await productsApi.create.product(
|
||||||
|
product
|
||||||
|
);
|
||||||
|
productId = createProductResponse.body.id;
|
||||||
|
|
||||||
|
// Create an order with a product line item, and save its Order ID
|
||||||
|
const order = {
|
||||||
|
status: 'pending',
|
||||||
|
line_items: [
|
||||||
|
{
|
||||||
|
product_id: productId,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
const createOrderResponse = await ordersApi.create.order( order );
|
||||||
|
orderId = createOrderResponse.body.id;
|
||||||
|
|
||||||
|
// Setup the expected refund object
|
||||||
|
expectedRefund = {
|
||||||
|
...refund,
|
||||||
|
line_items: [
|
||||||
|
{
|
||||||
|
product_id: productId,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
} );
|
||||||
|
|
||||||
|
afterAll( async () => {
|
||||||
|
// Cleanup the created product and order
|
||||||
|
await productsApi.delete.product( productId, true );
|
||||||
|
await ordersApi.delete.order( orderId, true );
|
||||||
|
} );
|
||||||
|
|
||||||
|
it( 'can create a refund', async () => {
|
||||||
|
const { status, body } = await refundsApi.create.refund(
|
||||||
|
orderId,
|
||||||
|
expectedRefund
|
||||||
|
);
|
||||||
|
expect( status ).toEqual( refundsApi.create.responseCode );
|
||||||
|
expect( body.id ).toBeDefined();
|
||||||
|
|
||||||
|
// Save the refund ID
|
||||||
|
expectedRefund.id = body.id;
|
||||||
|
|
||||||
|
// Verify that the order was refunded.
|
||||||
|
const getOrderResponse = await ordersApi.retrieve.order( orderId );
|
||||||
|
expect( getOrderResponse.body.refunds ).toHaveLength( 1 );
|
||||||
|
expect( getOrderResponse.body.refunds[ 0 ].id ).toEqual(
|
||||||
|
expectedRefund.id
|
||||||
|
);
|
||||||
|
expect( getOrderResponse.body.refunds[ 0 ].reason ).toEqual(
|
||||||
|
expectedRefund.reason
|
||||||
|
);
|
||||||
|
expect( getOrderResponse.body.refunds[ 0 ].total ).toEqual(
|
||||||
|
`-${ expectedRefund.amount }`
|
||||||
|
);
|
||||||
|
} );
|
||||||
|
|
||||||
|
it( 'can retrieve a refund', async () => {
|
||||||
|
const { status, body } = await refundsApi.retrieve.refund(
|
||||||
|
orderId,
|
||||||
|
expectedRefund.id
|
||||||
|
);
|
||||||
|
|
||||||
|
expect( status ).toEqual( refundsApi.retrieve.responseCode );
|
||||||
|
expect( body.id ).toEqual( expectedRefund.id );
|
||||||
|
} );
|
||||||
|
|
||||||
|
it( 'can list all refunds', async () => {
|
||||||
|
const { status, body } = await refundsApi.listAll.refunds( orderId );
|
||||||
|
|
||||||
|
expect( status ).toEqual( refundsApi.listAll.responseCode );
|
||||||
|
expect( body ).toHaveLength( 1 );
|
||||||
|
expect( body[ 0 ].id ).toEqual( expectedRefund.id );
|
||||||
|
} );
|
||||||
|
|
||||||
|
it( 'can delete a refund', async () => {
|
||||||
|
const { status, body } = await refundsApi.delete.refund(
|
||||||
|
orderId,
|
||||||
|
expectedRefund.id,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
expect( status ).toEqual( refundsApi.delete.responseCode );
|
||||||
|
expect( body.id ).toEqual( expectedRefund.id );
|
||||||
|
|
||||||
|
// Verify that the refund cannot be retrieved
|
||||||
|
const retrieveRefundResponse = await refundsApi.retrieve.refund(
|
||||||
|
orderId,
|
||||||
|
expectedRefund.id
|
||||||
|
);
|
||||||
|
expect( retrieveRefundResponse.status ).toEqual( 404 );
|
||||||
|
|
||||||
|
// Verify that the order no longer has a refund
|
||||||
|
const retrieveOrderResponse = await ordersApi.retrieve.order( orderId );
|
||||||
|
expect( retrieveOrderResponse.body.refunds ).toHaveLength( 0 );
|
||||||
|
} );
|
||||||
|
} );
|
Loading…
Reference in New Issue