Tweak some logic in our cart Integration test. (https://github.com/woocommerce/woocommerce-blocks/pull/2778)
* Fix Cart test async logic * tweak test * refactor test to not call response twice * add comment for why we're calling fetch twice * fix duplicate calls * reset empty test to normal value * switch to mockResponse * convert comment to todo block Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
This commit is contained in:
parent
76ab006219
commit
81570d733c
|
@ -13,7 +13,7 @@ import { defaultCartState } from '../../../../data/default-states';
|
||||||
|
|
||||||
describe( 'Testing cart', () => {
|
describe( 'Testing cart', () => {
|
||||||
beforeEach( async () => {
|
beforeEach( async () => {
|
||||||
fetchMock.mockResponseOnce( ( req ) => {
|
fetchMock.mockResponse( ( req ) => {
|
||||||
if ( req.url.match( /wc\/store\/cart/ ) ) {
|
if ( req.url.match( /wc\/store\/cart/ ) ) {
|
||||||
return Promise.resolve( JSON.stringify( previewCart ) );
|
return Promise.resolve( JSON.stringify( previewCart ) );
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,9 @@ describe( 'Testing cart', () => {
|
||||||
await dispatch( storeKey ).invalidateResolutionForStore();
|
await dispatch( storeKey ).invalidateResolutionForStore();
|
||||||
await dispatch( storeKey ).receiveCart( defaultCartState );
|
await dispatch( storeKey ).receiveCart( defaultCartState );
|
||||||
} );
|
} );
|
||||||
|
afterEach( () => {
|
||||||
|
fetchMock.resetMocks();
|
||||||
|
} );
|
||||||
it( 'renders cart if there are items in the cart', async () => {
|
it( 'renders cart if there are items in the cart', async () => {
|
||||||
render(
|
render(
|
||||||
<CartBlock
|
<CartBlock
|
||||||
|
@ -32,14 +35,21 @@ describe( 'Testing cart', () => {
|
||||||
} }
|
} }
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
await waitFor( () => {
|
await waitFor( () => expect( fetchMock ).toHaveBeenCalled() );
|
||||||
expect(
|
expect(
|
||||||
screen.getByText( /Proceed to Checkout/ )
|
await screen.getByText( /Proceed to Checkout/i )
|
||||||
).toBeInTheDocument();
|
).toBeInTheDocument();
|
||||||
} );
|
|
||||||
|
/**
|
||||||
|
* @todo Investigate extra POST request on initial cart render.
|
||||||
|
*
|
||||||
|
* We have an unfixed bug in our test in which the full cart triggers a POST
|
||||||
|
* request to `wc/store/cart/update-item` causing the fetch to be called twice.
|
||||||
|
*/
|
||||||
|
expect( fetchMock ).toHaveBeenCalledTimes( 2 );
|
||||||
} );
|
} );
|
||||||
it( 'renders empty cart if there are no items in the cart', async () => {
|
it( 'renders empty cart if there are no items in the cart', async () => {
|
||||||
fetchMock.mockResponseOnce( ( req ) => {
|
fetchMock.mockResponse( ( req ) => {
|
||||||
if ( req.url.match( /wc\/store\/cart/ ) ) {
|
if ( req.url.match( /wc\/store\/cart/ ) ) {
|
||||||
return Promise.resolve( JSON.stringify( defaultCartState ) );
|
return Promise.resolve( JSON.stringify( defaultCartState ) );
|
||||||
}
|
}
|
||||||
|
@ -53,6 +63,9 @@ describe( 'Testing cart', () => {
|
||||||
} }
|
} }
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
expect( await screen.findByText( /Empty Cart/ ) ).toBeInTheDocument();
|
|
||||||
|
await waitFor( () => expect( fetchMock ).toHaveBeenCalled() );
|
||||||
|
expect( await screen.getByText( /Empty Cart/i ) ).toBeInTheDocument();
|
||||||
|
expect( fetchMock ).toHaveBeenCalledTimes( 1 );
|
||||||
} );
|
} );
|
||||||
} );
|
} );
|
||||||
|
|
Loading…
Reference in New Issue