/** * External dependencies */ import { render, screen, fireEvent, waitFor } from '@testing-library/react'; import { registerPaymentMethod, __experimentalDeRegisterPaymentMethod, } from '@woocommerce/blocks-registry'; import { PaymentMethodDataProvider } from '@woocommerce/base-context'; /** * Internal dependencies */ import PaymentMethods from '../payment-methods'; jest.mock( '../payment-method-options', () => () => ( Payment method options ) ); jest.mock( '../saved-payment-method-options', () => ( { onChange } ) => ( <> Saved payment method options ) ); const registerMockPaymentMethods = () => { [ 'cheque' ].forEach( ( name ) => { registerPaymentMethod( { name, label: name, content:
A payment method
, edit:
A payment method
, icons: null, canMakePayment: () => true, ariaLabel: name, } ); } ); }; const resetMockPaymentMethods = () => { [ 'cheque' ].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( 'should hide/show PaymentMethodOptions when a saved payment method is checked/unchecked', async () => { 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(); } ); fireEvent.click( screen.getByText( 'Select saved' ) ); await waitFor( () => { const savedPaymentMethodOptions = screen.queryByText( /Saved payment method options/ ); const paymentMethodOptions = screen.queryByText( /Payment method options/ ); expect( savedPaymentMethodOptions ).not.toBeNull(); expect( paymentMethodOptions ).toBeNull(); } ); fireEvent.click( screen.getByText( 'Select not saved' ) ); 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(); } ); resetMockPaymentMethods(); } ); } );