2021-03-01 18:36:04 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
const { HTTPClientFactory, Coupon } = require( '@woocommerce/api' );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-12-13 23:36:18 +00:00
|
|
|
const { config } = require( '@woocommerce/e2e-environment' );
|
2021-11-17 16:02:45 +00:00
|
|
|
const { it, describe, beforeAll } = require( '@jest/globals' );
|
2021-03-01 18:36:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the default coupon and tests interactions with it via the API.
|
|
|
|
*/
|
|
|
|
const runCouponApiTest = () => {
|
2021-11-17 16:02:45 +00:00
|
|
|
describe( 'REST API > Coupon', () => {
|
2021-03-01 18:36:04 +00:00
|
|
|
let client;
|
2021-03-02 15:57:30 +00:00
|
|
|
let percentageCoupon;
|
2021-03-01 18:36:04 +00:00
|
|
|
let coupon;
|
|
|
|
let repository;
|
|
|
|
|
2021-11-17 16:02:45 +00:00
|
|
|
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();
|
|
|
|
} );
|
|
|
|
|
2021-11-17 16:02:45 +00:00
|
|
|
it( 'can create a coupon', async () => {
|
2021-03-01 18:36:04 +00:00
|
|
|
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 );
|
2021-11-17 16:02:45 +00:00
|
|
|
expect( coupon ).toEqual(
|
|
|
|
expect.objectContaining( percentageCoupon )
|
|
|
|
);
|
|
|
|
} );
|
2021-03-01 18:36:04 +00:00
|
|
|
|
2021-11-17 16:02:45 +00:00
|
|
|
it( 'can retrieve a coupon', async () => {
|
2021-03-01 18:36:04 +00:00
|
|
|
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.
|
2021-11-17 16:02:45 +00:00
|
|
|
const response = await client.get(
|
|
|
|
`/wc/v3/coupons/${ coupon.id }`
|
|
|
|
);
|
|
|
|
expect( response.statusCode ).toBe( 200 );
|
|
|
|
expect( response.data ).toEqual(
|
|
|
|
expect.objectContaining( couponProperties )
|
|
|
|
);
|
|
|
|
} );
|
2021-03-01 18:36:04 +00:00
|
|
|
|
2021-11-17 16:02:45 +00:00
|
|
|
it( 'can update a coupon', async () => {
|
2021-03-01 18:36:04 +00:00
|
|
|
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.
|
2021-11-17 16:02:45 +00:00
|
|
|
const response = await client.get(
|
|
|
|
`/wc/v3/coupons/${ coupon.id }`
|
|
|
|
);
|
|
|
|
expect( response.statusCode ).toBe( 200 );
|
|
|
|
expect( response.data ).toEqual(
|
|
|
|
expect.objectContaining( updatedCouponProperties )
|
|
|
|
);
|
|
|
|
} );
|
2021-03-01 18:36:04 +00:00
|
|
|
|
2021-11-17 16:02:45 +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-11-17 16:02:45 +00:00
|
|
|
} );
|
|
|
|
} );
|
2021-03-01 18:36:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = runCouponApiTest;
|