From 9557d04508a792d8444b6bbe71bf19ffc0e46030 Mon Sep 17 00:00:00 2001 From: Thomas Roberts Date: Wed, 31 May 2023 18:02:00 +0100 Subject: [PATCH] Try preventing navigation in unit tests --- .../inner-blocks/proceed-to-checkout-block/test/block.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/test/block.tsx b/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/test/block.tsx index 6b886da7950..9f0bcd4505a 100644 --- a/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/test/block.tsx +++ b/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/test/block.tsx @@ -54,7 +54,7 @@ describe( 'Proceed to checkout block', () => { expect( console ).toHaveErrored(); } ); it( 'dispatches the onProceedToCheckout event when the button is clicked', async () => { - const mockObserver = jest.fn(); + const mockObserver = jest.fn().mockReturnValue( { type: 'error' } ); const MockObserverComponent = () => { const { onProceedToCheckout } = useCartEventsContext(); useEffect( () => { @@ -77,6 +77,10 @@ describe( 'Proceed to checkout block', () => { ); expect( screen.getByText( 'Mock observer' ) ).toBeInTheDocument(); const button = screen.getByText( 'Proceed to Checkout' ); + + // Forcibly set the button URL to # to prevent JSDOM error: `["Error: Not implemented: navigation (except hash changes)` + button.parentElement?.removeAttribute( 'href' ); + button.click(); await waitFor( () => { expect( mockObserver ).toHaveBeenCalled();