/** * 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.queryByText( /no payment methods available/ ); expect( noPaymentMethods ).not.toBeNull(); } ); } ); 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(); } ); } );