/** * External dependencies */ import { render, screen, fireEvent, waitFor } from '@testing-library/react'; import { registerPaymentMethod, __experimentalDeRegisterPaymentMethod, } from '@woocommerce/blocks-registry'; import { PaymentMethodDataProvider, usePaymentMethodDataContext, } from '@woocommerce/base-context'; /** * Internal dependencies */ import * as useStoreCartHook from '../../../../base/context/hooks/cart/use-store-cart'; // Somewhere in your test case or test suite useStoreCartHook.useStoreCart = jest .fn() .mockReturnValue( useStoreCartHook.defaultCartData ); /** * Internal dependencies */ import PaymentMethods from '../payment-methods'; jest.mock( '../saved-payment-method-options', () => ( { onChange } ) => { return ( <> Saved payment method options ); } ); jest.mock( '@woocommerce/base-components/radio-control-accordion', () => ( { onChange } ) => ( <> Payment method options ) ); const registerMockPaymentMethods = () => { [ 'stripe' ].forEach( ( name ) => { registerPaymentMethod( { name, label: name, content:
A payment method
, edit:
A payment method
, icons: null, canMakePayment: () => true, supports: { showSavedCards: true, showSaveOption: true, features: [ 'products' ], }, ariaLabel: name, } ); } ); }; const resetMockPaymentMethods = () => { [ 'stripe' ].forEach( ( name ) => { __experimentalDeRegisterPaymentMethod( name ); } ); }; describe( 'PaymentMethods', () => { test( 'should show no payment methods component when there are no payment methods', async () => { render( ); await waitFor( () => { const noPaymentMethods = screen.queryAllByText( /no payment methods available/ ); // We might get more than one match because the `speak()` function // creates an extra `div` with the notice contents used for a11y. expect( noPaymentMethods.length ).toBeGreaterThanOrEqual( 1 ); } ); } ); test( 'selecting new payment method', async () => { const ShowActivePaymentMethod = () => { const { activePaymentMethod, activeSavedToken, } = usePaymentMethodDataContext(); return ( <>
{ 'Active Payment Method: ' + activePaymentMethod }
{ 'Active Saved Token: ' + activeSavedToken }
); }; registerMockPaymentMethods(); render( ); await waitFor( () => { const savedPaymentMethodOptions = screen.queryByText( /Saved payment method options/ ); const paymentMethodOptions = screen.queryByText( /Payment method options/ ); expect( savedPaymentMethodOptions ).not.toBeNull(); expect( paymentMethodOptions ).not.toBeNull(); const savedToken = screen.queryByText( /Active Payment Method: stripe/ ); expect( savedToken ).toBeNull(); } ); fireEvent.click( screen.getByText( 'Select new payment' ) ); await waitFor( () => { const activePaymentMethod = screen.queryByText( /Active Payment Method: stripe/ ); expect( activePaymentMethod ).not.toBeNull(); } ); resetMockPaymentMethods(); } ); } );