Merge pull request #31237 from woocommerce/fix/api-rest-response-code

Fix retrieving status code for API tests
This commit is contained in:
Ron Rennick 2021-11-17 12:40:24 -04:00 committed by GitHub
commit 66ad8774d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 143 additions and 126 deletions

View File

@ -7,11 +7,7 @@ const { HTTPClientFactory, Coupon } = require( '@woocommerce/api' );
* External dependencies
*/
const config = require( 'config' );
const {
it,
describe,
beforeAll,
} = require( '@jest/globals' );
const { it, describe, beforeAll } = require( '@jest/globals' );
/**
* Create the default coupon and tests interactions with it via the API.
@ -39,7 +35,9 @@ const runCouponApiTest = () => {
// Check properties of the coupon in the create coupon response.
coupon = await repository.create( percentageCoupon );
expect( coupon ).toEqual( expect.objectContaining( percentageCoupon ) );
expect( coupon ).toEqual(
expect.objectContaining( percentageCoupon )
);
} );
it( 'can retrieve a coupon', async () => {
@ -51,9 +49,13 @@ const runCouponApiTest = () => {
};
// Read coupon directly from API to compare.
const response = await client.get( `/wc/v3/coupons/${coupon.id}` );
expect( response.status ).toBe( 200 );
expect( response.data ).toEqual( expect.objectContaining( couponProperties ) );
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 () => {
@ -66,9 +68,13 @@ const runCouponApiTest = () => {
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.status ).toBe( 200 );
expect( response.data ).toEqual( expect.objectContaining( updatedCouponProperties ) );
const response = await client.get(
`/wc/v3/coupons/${ coupon.id }`
);
expect( response.statusCode ).toBe( 200 );
expect( response.data ).toEqual(
expect.objectContaining( updatedCouponProperties )
);
} );
it( 'can delete a coupon', async () => {

View File

@ -7,11 +7,7 @@ const { HTTPClientFactory, ExternalProduct } = require( '@woocommerce/api' );
* External dependencies
*/
const config = require( 'config' );
const {
it,
describe,
beforeAll,
} = require( '@jest/globals' );
const { it, describe, beforeAll } = require( '@jest/globals' );
/**
* Create an external product and retrieve via the API.
@ -40,7 +36,9 @@ const runExternalProductAPITest = () => {
// Check properties of product in the create product response.
product = await repository.create( defaultExternalProduct );
expect( product ).toEqual( expect.objectContaining( defaultExternalProduct ) );
expect( product ).toEqual(
expect.objectContaining( defaultExternalProduct )
);
} );
it( 'can retrieve a raw external product', async () => {
@ -52,9 +50,13 @@ const runExternalProductAPITest = () => {
};
// Read product directly from api.
const response = await client.get( `/wc/v3/products/${product.id}` );
expect( response.status ).toBe( 200 );
expect( response.data ).toEqual( expect.objectContaining( rawProperties ) );
const response = await client.get(
`/wc/v3/products/${ product.id }`
);
expect( response.statusCode ).toBe( 200 );
expect( response.data ).toEqual(
expect.objectContaining( rawProperties )
);
} );
it( 'can retrieve a transformed external product', async () => {
@ -66,7 +68,9 @@ const runExternalProductAPITest = () => {
// Read product via the repository.
const transformed = await repository.read( product.id );
expect( transformed ).toEqual( expect.objectContaining( transformedProperties ) );
expect( transformed ).toEqual(
expect.objectContaining( transformedProperties )
);
} );
it( 'can delete an external product', async () => {

View File

@ -1,17 +1,17 @@
/**
* Internal dependencies
*/
const { HTTPClientFactory, GroupedProduct, SimpleProduct } = require( '@woocommerce/api' );
const {
HTTPClientFactory,
GroupedProduct,
SimpleProduct,
} = require( '@woocommerce/api' );
/**
* External dependencies
*/
const config = require( 'config' );
const {
it,
describe,
beforeAll,
} = require( '@jest/globals' );
const { it, describe, beforeAll } = require( '@jest/globals' );
/**
* Create an external product and retrieve via the API.
@ -23,7 +23,7 @@ const runGroupedProductAPITest = () => {
let defaultGroupedProduct;
let baseGroupedProduct;
let product;
let groupedProducts = [];
const groupedProducts = [];
let repository;
beforeAll( async () => {
@ -38,8 +38,14 @@ const runGroupedProductAPITest = () => {
// Create the simple products to be grouped first.
repository = SimpleProduct.restRepository( client );
for ( let c = 0; c < defaultGroupedProduct.groupedProducts.length; c++ ) {
product = await repository.create( defaultGroupedProduct.groupedProducts[ c ] );
for (
let c = 0;
c < defaultGroupedProduct.groupedProducts.length;
c++
) {
product = await repository.create(
defaultGroupedProduct.groupedProducts[ c ]
);
groupedProducts.push( product.id );
}
} );
@ -53,27 +59,35 @@ const runGroupedProductAPITest = () => {
// Check properties of product in the create product response.
product = await repository.create( baseGroupedProduct );
expect( product ).toEqual( expect.objectContaining( baseGroupedProduct ) );
expect( product ).toEqual(
expect.objectContaining( baseGroupedProduct )
);
} );
it( 'can retrieve a raw grouped product', async () => {
let rawProperties = {
const rawProperties = {
id: product.id,
grouped_products: baseGroupedProduct.groupedProducts,
...defaultGroupedProduct,
};
delete rawProperties['groupedProducts'];
delete rawProperties.groupedProducts;
// Read product directly from api.
const response = await client.get( `/wc/v3/products/${product.id}` );
expect( response.status ).toBe( 200 );
expect( response.data ).toEqual( expect.objectContaining( rawProperties ) );
const response = await client.get(
`/wc/v3/products/${ product.id }`
);
expect( response.statusCode ).toBe( 200 );
expect( response.data ).toEqual(
expect.objectContaining( rawProperties )
);
} );
it( 'can retrieve a transformed grouped product', async () => {
// Read product via the repository.
const transformed = await repository.read( product.id );
expect( transformed ).toEqual( expect.objectContaining( baseGroupedProduct ) );
expect( transformed ).toEqual(
expect.objectContaining( baseGroupedProduct )
);
} );
it( 'can delete a grouped product', async () => {

View File

@ -7,11 +7,7 @@
* External dependencies
*/
const config = require( 'config' );
const {
it,
describe,
beforeAll,
} = require( '@jest/globals' );
const { it, describe, beforeAll } = require( '@jest/globals' );
/**
* Creates an order and tests interactions with it via the API.
@ -50,8 +46,10 @@ const runOrderApiTest = () => {
// Read order directly from API to compare.
const response = await client.get( `/wc/v3/orders/${ order.id }` );
expect( response.status ).toBe( 200 );
expect( response.data ).toEqual( expect.objectContaining( orderProperties ) );
expect( response.statusCode ).toBe( 200 );
expect( response.data ).toEqual(
expect.objectContaining( orderProperties )
);
} );
it( 'can update an order', async () => {
@ -64,8 +62,10 @@ const runOrderApiTest = () => {
// Check the order response for the updated values.
const response = await client.get( `/wc/v3/orders/${ order.id }` );
expect( response.status ).toBe( 200 );
expect( response.data ).toEqual( expect.objectContaining( updatedOrderProperties ) );
expect( response.statusCode ).toBe( 200 );
expect( response.data ).toEqual(
expect.objectContaining( updatedOrderProperties )
);
} );
it( 'can delete an order', async () => {

View File

@ -7,11 +7,7 @@ const { HTTPClientFactory } = require( '@woocommerce/api' );
* External dependencies
*/
const config = require( 'config' );
const {
it,
describe,
beforeAll,
} = require( '@jest/globals' );
const { it, describe, beforeAll } = require( '@jest/globals' );
/**
* Create the default coupon and tests interactions with it via the API.
@ -30,29 +26,26 @@ const runTelemetryAPITest = () => {
.create();
} );
it.each([
null,
{},
{ platform: 'ios' },
{ version: '1.1' },
])( 'errors for invalid request body - %p', async data => {
it.each( [ null, {}, { platform: 'ios' }, { version: '1.1' } ] )(
'errors for invalid request body - %p',
async ( data ) => {
const response = await client
.post( `/wc-telemetry/tracker`, data )
.catch( err => {
expect( err.response.status ).toBe( 400 );
.catch( ( err ) => {
expect( err.statusCode ).toBe( 400 );
} );
expect( response ).toBeUndefined();
} );
}
);
it( 'returns 200 with correct fields', async () => {
const response = await client
.post( `/wc-telemetry/tracker`, {
const response = await client.post( `/wc-telemetry/tracker`, {
platform: 'ios',
version: '1.0',
})
} );
expect( response.status ).toBe( 200 );
expect( response.statusCode ).toBe( 200 );
} );
} );
};