woocommerce/tests/e2e/core-tests/specs/api/coupon.test.js

85 lines
2.3 KiB
JavaScript
Raw Normal View History

/**
* 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;
let coupon;
let repository;
beforeAll(async () => {
2021-03-02 15:57:30 +00:00
percentageCoupon = config.get( 'coupons.percentage' );
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 ) );
});
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,
};
// Read coupon directly from API to compare.
const response = await client.get( `/wc/v3/coupons/${coupon.id}` );
2021-10-14 21:45:39 +00:00
expect( response.status ).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}` );
2021-10-14 21:45:39 +00:00
expect( response.status ).toBe( 200 );
expect( response.data ).toEqual( expect.objectContaining( updatedCouponProperties ) );
});
2021-03-02 17:24:25 +00:00
it('can delete a coupon', async () => {
// Delete the coupon
2021-03-04 18:43:15 +00:00
const status = await repository.delete( coupon.id );
// If the delete is successful, the response comes back truthy
2021-03-04 18:43:15 +00:00
expect( status ).toBeTruthy();
});
});
};
module.exports = runCouponApiTest;