2020-06-10 18:21:34 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { render, screen, waitFor } from '@testing-library/react';
|
|
|
|
import { previewCart } from '@woocommerce/resource-previews';
|
|
|
|
import { dispatch } from '@wordpress/data';
|
|
|
|
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
|
2021-02-24 01:36:24 +00:00
|
|
|
import { default as fetchMock } from 'jest-fetch-mock';
|
|
|
|
|
2020-06-10 18:21:34 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import CartBlock from '../block';
|
|
|
|
import { defaultCartState } from '../../../../data/default-states';
|
|
|
|
|
|
|
|
describe( 'Testing cart', () => {
|
|
|
|
beforeEach( async () => {
|
2020-07-02 19:10:19 +00:00
|
|
|
fetchMock.mockResponse( ( req ) => {
|
2020-06-10 18:21:34 +00:00
|
|
|
if ( req.url.match( /wc\/store\/cart/ ) ) {
|
|
|
|
return Promise.resolve( JSON.stringify( previewCart ) );
|
|
|
|
}
|
2021-02-24 01:36:24 +00:00
|
|
|
return Promise.resolve( '' );
|
2020-06-10 18:21:34 +00:00
|
|
|
} );
|
|
|
|
// need to clear the store resolution state between tests.
|
|
|
|
await dispatch( storeKey ).invalidateResolutionForStore();
|
2021-02-04 17:05:47 +00:00
|
|
|
await dispatch( storeKey ).receiveCart( defaultCartState.cartData );
|
2020-06-10 18:21:34 +00:00
|
|
|
} );
|
2021-02-22 13:16:45 +00:00
|
|
|
|
2020-07-02 19:10:19 +00:00
|
|
|
afterEach( () => {
|
|
|
|
fetchMock.resetMocks();
|
|
|
|
} );
|
2021-02-22 13:16:45 +00:00
|
|
|
|
2020-06-10 18:21:34 +00:00
|
|
|
it( 'renders cart if there are items in the cart', async () => {
|
|
|
|
render(
|
|
|
|
<CartBlock
|
|
|
|
emptyCart={ null }
|
|
|
|
attributes={ {
|
|
|
|
isShippingCalculatorEnabled: false,
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
);
|
2020-07-02 19:10:19 +00:00
|
|
|
await waitFor( () => expect( fetchMock ).toHaveBeenCalled() );
|
|
|
|
expect(
|
2020-09-26 21:59:51 +00:00
|
|
|
screen.getByText( /Proceed to Checkout/i )
|
2020-07-02 19:10:19 +00:00
|
|
|
).toBeInTheDocument();
|
|
|
|
|
2020-10-27 14:37:18 +00:00
|
|
|
expect( fetchMock ).toHaveBeenCalledTimes( 1 );
|
2021-03-22 17:13:48 +00:00
|
|
|
// ["`select` control in `@wordpress/data-controls` is deprecated. Please use built-in `resolveSelect` control in `@wordpress/data` instead."]
|
|
|
|
expect( console ).toHaveWarned();
|
2020-06-10 18:21:34 +00:00
|
|
|
} );
|
2021-02-22 13:16:45 +00:00
|
|
|
|
2020-06-10 18:21:34 +00:00
|
|
|
it( 'renders empty cart if there are no items in the cart', async () => {
|
2020-07-02 19:10:19 +00:00
|
|
|
fetchMock.mockResponse( ( req ) => {
|
2020-06-10 18:21:34 +00:00
|
|
|
if ( req.url.match( /wc\/store\/cart/ ) ) {
|
2021-02-04 17:05:47 +00:00
|
|
|
return Promise.resolve(
|
|
|
|
JSON.stringify( defaultCartState.cartData )
|
|
|
|
);
|
2020-06-10 18:21:34 +00:00
|
|
|
}
|
2021-02-24 01:36:24 +00:00
|
|
|
return Promise.resolve( '' );
|
2020-06-10 18:21:34 +00:00
|
|
|
} );
|
|
|
|
render(
|
|
|
|
<CartBlock
|
|
|
|
emptyCart={ '<div>Empty Cart</div>' }
|
|
|
|
attributes={ {
|
|
|
|
isShippingCalculatorEnabled: false,
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
);
|
2020-07-02 19:10:19 +00:00
|
|
|
|
|
|
|
await waitFor( () => expect( fetchMock ).toHaveBeenCalled() );
|
2020-09-26 21:59:51 +00:00
|
|
|
expect( screen.getByText( /Empty Cart/i ) ).toBeInTheDocument();
|
2020-07-02 19:10:19 +00:00
|
|
|
expect( fetchMock ).toHaveBeenCalledTimes( 1 );
|
2020-06-10 18:21:34 +00:00
|
|
|
} );
|
2021-02-22 13:16:45 +00:00
|
|
|
|
|
|
|
it( 'renders correct cart line subtotal when currency has 0 decimals', async () => {
|
|
|
|
fetchMock.mockResponse( ( req ) => {
|
|
|
|
if ( req.url.match( /wc\/store\/cart/ ) ) {
|
|
|
|
const cart = {
|
|
|
|
...previewCart,
|
|
|
|
// Make it so there is only one item to simplify things.
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
...previewCart.items[ 0 ],
|
|
|
|
totals: {
|
|
|
|
...previewCart.items[ 0 ].totals,
|
|
|
|
// Change price format so there are no decimals.
|
|
|
|
currency_minor_unit: 0,
|
|
|
|
currency_prefix: '',
|
|
|
|
currency_suffix: '€',
|
|
|
|
line_subtotal: '16',
|
2021-03-02 14:47:43 +00:00
|
|
|
line_total: '18',
|
2021-02-22 13:16:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
return Promise.resolve( JSON.stringify( cart ) );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
render( <CartBlock emptyCart={ null } attributes={ {} } /> );
|
|
|
|
|
|
|
|
await waitFor( () => expect( fetchMock ).toHaveBeenCalled() );
|
|
|
|
expect( screen.getAllByRole( 'cell' )[ 1 ] ).toHaveTextContent( '16€' );
|
|
|
|
} );
|
2020-06-10 18:21:34 +00:00
|
|
|
} );
|