Never thought I would have to do this before!
We accept card-not-present payment via a Stripe, and we use the API provided by Stripe.js library to render Stripe iframes that handle the user input for us.
The overall structure is:
1 | <Element stripe> //provider |
Challenge 1: Mock all the moving parts of the package
Found a mock that got me to render the CardCollectionForm (but not the iframe) at this GitHub issue with this tweak to make TS happy
The most helpful takeaway is the usage of jest.requireActual
, which largely reduces the volume of job.
1 | const mockElement = () => ({ |
Challenge 2: The child component CardElement
needs to invoke callback from the tested parent component
I mocked the implementation of the CardElement
:
1 | jest.mock('@stripe/react-stripe-js', () => { |
Then I could fire click event to simulate onChange
invocations.
Are these practices actually good?
Follow up readings
https://maksimivanov.com/posts/dont-mock-what-you-dont-own/