2021-03-01 18:36:04 +00:00
|
|
|
/* eslint-disable jest/no-export, jest/no-disabled-tests */
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
const { HTTPClientFactory, Coupon } = require( '@woocommerce/api' );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
const config = require( 'config' );
|
|
|
|
const {
|
|
|
|
it,
|
|
|
|
describe,
|
|
|
|
beforeAll,
|
|
|
|
} = require( '@jest/globals' );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the default coupon and tests interactions with it via the API.
|
|
|
|
*/
|
|
|
|
const runCouponApiTest = () => {
|
|
|
|
describe('REST API > Coupon', () => {
|
|
|
|
let client;
|
2021-03-02 15:57:30 +00:00
|
|
|
let percentageCoupon;
|
2021-03-01 18:36:04 +00:00
|
|
|
let coupon;
|
|
|
|
let repository;
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2021-03-02 15:57:30 +00:00
|
|
|
percentageCoupon = config.get( 'coupons.percentage' );
|
2021-03-01 18:36:04 +00:00
|
|
|
const admin = config.get( 'users.admin' );
|
|
|
|
const url = config.get( 'url' );
|
|
|
|
|
|
|
|
client = HTTPClientFactory.build( url )
|
|
|
|
.withBasicAuth( admin.username, admin.password )
|
|
|
|
.withIndexPermalinks()
|
|
|
|
.create();
|
|
|
|
} );
|
|
|
|
|
|
|
|
it('can create a coupon', async () => {
|
|
|
|
repository = Coupon.restRepository( client );
|
|
|
|
|
|
|
|
// Check properties of the coupon in the create coupon response.
|
2021-03-02 15:57:30 +00:00
|
|
|
coupon = await repository.create( percentageCoupon );
|
|
|
|
expect( coupon ).toEqual( expect.objectContaining( percentageCoupon ) );
|
2021-03-01 18:36:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can retrieve a coupon', async () => {
|
|
|
|
const couponProperties = {
|
|
|
|
id: coupon.id,
|
2021-03-02 15:57:30 +00:00
|
|
|
code: percentageCoupon.code,
|
|
|
|
discount_type: percentageCoupon.discountType,
|
|
|
|
amount: percentageCoupon.amount,
|
2021-03-01 18:36:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Read coupon directly from API to compare.
|
|
|
|
const response = await client.get( `/wc/v3/coupons/${coupon.id}` );
|
|
|
|
expect( response.statusCode ).toBe( 200 );
|
|
|
|
expect( response.data ).toEqual( expect.objectContaining( couponProperties ) );
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can update a coupon', async () => {
|
|
|
|
const updatedCouponProperties = {
|
|
|
|
amount: '75.00',
|
|
|
|
discount_type: 'fixed_cart',
|
|
|
|
free_shipping: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
await repository.update( coupon.id, updatedCouponProperties );
|
|
|
|
|
|
|
|
// Check the coupon response for the updated values.
|
|
|
|
const response = await client.get( `/wc/v3/coupons/${coupon.id}` );
|
|
|
|
expect( response.statusCode ).toBe( 200 );
|
|
|
|
expect( response.data ).toEqual( expect.objectContaining( updatedCouponProperties ) );
|
|
|
|
});
|
|
|
|
|
2021-03-02 17:24:25 +00:00
|
|
|
it('can delete a coupon', async () => {
|
2021-03-01 18:36:04 +00:00
|
|
|
// Delete the coupon
|
2021-03-04 18:43:15 +00:00
|
|
|
const status = await repository.delete( coupon.id );
|
2021-03-01 18:36:04 +00:00
|
|
|
|
|
|
|
// If the delete is successful, the response comes back truthy
|
2021-03-04 18:43:15 +00:00
|
|
|
expect( status ).toBeTruthy();
|
2021-03-01 18:36:04 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = runCouponApiTest;
|