简体   繁体   中英

Testing Stripe with React Testing Library

I've the following React components:

const PaymentModal = () => {
    
    const stripePromise = loadStripe(apiKey);
    
    return(
        <div className = 'Modal'>
            <Elements stripe = {stripePromise}>
                <CheckoutForm/>
            </Elements>
        </div>
    );
    
}

const CheckoutForm = () => {

    return(
        <CardElement options = {{hidePostalCode: true}}/>
    );

}

And the following test:

import React                                  from 'react';
import { render, fireEvent, screen, waitFor } from '@testing-library/react';
import PaymentModal                           from '../Components/PaymentModal';

test('PaymentModal -> Payment works in test mode', async () => {
    
    render(
          <PaymentModal/>
    );
    
    await screen.getByPlaceholderText('Número de tarjeta');
    
});

If I render the component without testing it, it renders the following:

<input placeholder = "Número de tarjeta">

The problem is that the test fails and I'm getting the following error:

TestingLibraryElementError: Unable to find an element with the placeholder text of: Número de tarjeta

Please, bear in mind that I don't want to mock-up the Stripe payment form. I want to test it by simulating a user introducing a dummy credit card number.

import React from 'react';
import { render, fireEvent, screen, waitFor } from '@testing-library/react';
import PaymentModal from '../Components/PaymentModal';

test('PaymentModal -> Payment works in test mode', async () => {
  render(<PaymentModal />);

  await waitFor(() =>
    expect(screen.getByPlaceholderText('Número de tarjeta')).toBeInTheDocument()
  );
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM