2021-10-15 10:53:44 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-10-25 16:05:01 +00:00
|
|
|
import {
|
|
|
|
act,
|
|
|
|
render,
|
|
|
|
screen,
|
2022-08-02 14:28:52 +00:00
|
|
|
queryByText,
|
2021-10-25 16:05:01 +00:00
|
|
|
waitFor,
|
|
|
|
waitForElementToBeRemoved,
|
|
|
|
} from '@testing-library/react';
|
2021-10-15 10:53:44 +00:00
|
|
|
import { previewCart } from '@woocommerce/resource-previews';
|
|
|
|
import { dispatch } from '@wordpress/data';
|
|
|
|
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
|
|
|
|
import { SlotFillProvider } from '@woocommerce/blocks-checkout';
|
|
|
|
import { default as fetchMock } from 'jest-fetch-mock';
|
2022-01-31 10:53:21 +00:00
|
|
|
import userEvent from '@testing-library/user-event';
|
2021-10-15 10:53:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import Block from '../block';
|
2022-07-08 05:53:24 +00:00
|
|
|
import { defaultCartState } from '../../../data/cart/default-state';
|
2021-10-15 10:53:44 +00:00
|
|
|
|
|
|
|
const MiniCartBlock = ( props ) => (
|
|
|
|
<SlotFillProvider>
|
2021-11-19 11:47:48 +00:00
|
|
|
<Block
|
|
|
|
contents='<div class="wc-block-mini-cart-contents"></div>'
|
|
|
|
{ ...props }
|
|
|
|
/>
|
2021-10-15 10:53:44 +00:00
|
|
|
</SlotFillProvider>
|
|
|
|
);
|
2021-10-25 16:05:01 +00:00
|
|
|
|
|
|
|
const mockEmptyCart = () => {
|
|
|
|
fetchMock.mockResponse( ( req ) => {
|
2022-02-23 12:00:45 +00:00
|
|
|
if ( req.url.match( /wc\/store\/v1\/cart/ ) ) {
|
2021-10-25 16:05:01 +00:00
|
|
|
return Promise.resolve(
|
|
|
|
JSON.stringify( defaultCartState.cartData )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return Promise.resolve( '' );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockFullCart = () => {
|
|
|
|
fetchMock.mockResponse( ( req ) => {
|
2022-02-23 12:00:45 +00:00
|
|
|
if ( req.url.match( /wc\/store\/v1\/cart/ ) ) {
|
2021-10-25 16:05:01 +00:00
|
|
|
return Promise.resolve( JSON.stringify( previewCart ) );
|
|
|
|
}
|
|
|
|
return Promise.resolve( '' );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2023-05-11 12:52:03 +00:00
|
|
|
describe( 'Testing Mini-Cart', () => {
|
2021-12-07 15:47:50 +00:00
|
|
|
beforeEach( () => {
|
|
|
|
act( () => {
|
|
|
|
mockFullCart();
|
|
|
|
// need to clear the store resolution state between tests.
|
|
|
|
dispatch( storeKey ).invalidateResolutionForStore();
|
|
|
|
dispatch( storeKey ).receiveCart( defaultCartState.cartData );
|
|
|
|
} );
|
2021-10-15 10:53:44 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
afterEach( () => {
|
|
|
|
fetchMock.resetMocks();
|
|
|
|
} );
|
|
|
|
|
2023-05-11 12:52:03 +00:00
|
|
|
it( 'opens Mini-Cart drawer when clicking on button', async () => {
|
2021-10-15 10:53:44 +00:00
|
|
|
render( <MiniCartBlock /> );
|
|
|
|
await waitFor( () => expect( fetchMock ).toHaveBeenCalled() );
|
2022-04-08 13:47:19 +00:00
|
|
|
userEvent.click( screen.getByLabelText( /items/i ) );
|
2021-10-15 10:53:44 +00:00
|
|
|
|
|
|
|
expect( fetchMock ).toHaveBeenCalledTimes( 1 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'renders empty cart if there are no items in the cart', async () => {
|
2021-10-25 16:05:01 +00:00
|
|
|
mockEmptyCart();
|
2021-10-15 10:53:44 +00:00
|
|
|
render( <MiniCartBlock /> );
|
|
|
|
|
|
|
|
await waitFor( () => expect( fetchMock ).toHaveBeenCalled() );
|
2022-04-08 13:47:19 +00:00
|
|
|
userEvent.click( screen.getByLabelText( /items/i ) );
|
2021-10-15 10:53:44 +00:00
|
|
|
|
|
|
|
expect( fetchMock ).toHaveBeenCalledTimes( 1 );
|
|
|
|
} );
|
2021-10-25 16:05:01 +00:00
|
|
|
|
|
|
|
it( 'updates contents when removed from cart event is triggered', async () => {
|
|
|
|
render( <MiniCartBlock /> );
|
|
|
|
await waitFor( () => expect( fetchMock ).toHaveBeenCalled() );
|
|
|
|
|
|
|
|
mockEmptyCart();
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
const removedFromCartEvent = new Event( 'wc-blocks_removed_from_cart' );
|
|
|
|
act( () => {
|
|
|
|
document.body.dispatchEvent( removedFromCartEvent );
|
|
|
|
} );
|
|
|
|
|
|
|
|
await waitForElementToBeRemoved( () =>
|
2021-11-19 11:47:48 +00:00
|
|
|
screen.queryByLabelText( /3 items in cart/i )
|
2021-10-25 16:05:01 +00:00
|
|
|
);
|
|
|
|
await waitFor( () =>
|
2021-11-19 11:47:48 +00:00
|
|
|
expect(
|
|
|
|
screen.getByLabelText( /0 items in cart/i )
|
|
|
|
).toBeInTheDocument()
|
2021-10-25 16:05:01 +00:00
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'updates contents when added to cart event is triggered', async () => {
|
|
|
|
mockEmptyCart();
|
|
|
|
render( <MiniCartBlock /> );
|
|
|
|
await waitFor( () => expect( fetchMock ).toHaveBeenCalled() );
|
|
|
|
|
|
|
|
mockFullCart();
|
|
|
|
// eslint-disable-next-line no-undef
|
2021-11-19 11:47:48 +00:00
|
|
|
const addedToCartEvent = new Event( 'wc-blocks_added_to_cart' );
|
2021-10-25 16:05:01 +00:00
|
|
|
act( () => {
|
2021-11-19 11:47:48 +00:00
|
|
|
document.body.dispatchEvent( addedToCartEvent );
|
2021-10-25 16:05:01 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
await waitForElementToBeRemoved( () =>
|
2021-11-19 11:47:48 +00:00
|
|
|
screen.queryByLabelText( /0 items in cart/i )
|
2021-10-25 16:05:01 +00:00
|
|
|
);
|
|
|
|
await waitFor( () =>
|
2021-11-19 11:47:48 +00:00
|
|
|
expect(
|
|
|
|
screen.getByLabelText( /3 items in cart/i )
|
|
|
|
).toBeInTheDocument()
|
2021-10-25 16:05:01 +00:00
|
|
|
);
|
|
|
|
} );
|
2022-08-02 14:28:52 +00:00
|
|
|
|
|
|
|
it( 'renders cart price if "Hide Cart Price" setting is not enabled', async () => {
|
|
|
|
mockEmptyCart();
|
|
|
|
render( <MiniCartBlock /> );
|
|
|
|
await waitFor( () => expect( fetchMock ).toHaveBeenCalled() );
|
|
|
|
|
|
|
|
await waitFor( () =>
|
|
|
|
expect( screen.getByText( '$0.00' ) ).toBeInTheDocument()
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'does not render cart price if "Hide Cart Price" setting is enabled', async () => {
|
|
|
|
mockEmptyCart();
|
|
|
|
const { container } = render(
|
|
|
|
<MiniCartBlock hasHiddenPrice={ true } />
|
|
|
|
);
|
|
|
|
await waitFor( () => expect( fetchMock ).toHaveBeenCalled() );
|
|
|
|
|
|
|
|
await waitFor( () =>
|
|
|
|
expect( queryByText( container, '$0.00' ) ).not.toBeInTheDocument()
|
|
|
|
);
|
|
|
|
} );
|
2021-10-15 10:53:44 +00:00
|
|
|
} );
|