Skip to content

Commit

Permalink
Fix Element effect cleanup when using async stripe prop (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
christopher-stripe authored Jan 28, 2021
1 parent 8c94669 commit 65b05dd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
17 changes: 16 additions & 1 deletion src/components/createElementComponent.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import {render} from '@testing-library/react';
import {render, act} from '@testing-library/react';

import {Elements} from './Elements';
import createElementComponent from './createElementComponent';
Expand Down Expand Up @@ -405,5 +405,20 @@ describe('createElementComponent', () => {
unmount2();
expect(mockElement.destroy).toHaveBeenCalled();
});

it('destroys an existing Element when the component unmounts with an async stripe prop', async () => {
const stripePromise = Promise.resolve(mockStripe);

const {unmount} = render(
<Elements stripe={stripePromise}>
<CardElement />
</Elements>
);

await act(() => stripePromise);

unmount();
expect(mockElement.destroy).toHaveBeenCalled();
});
});
});
6 changes: 2 additions & 4 deletions src/components/createElementComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,9 @@ const createElementComponent = (
}, [options]);

React.useEffect(() => {
const element = elementRef.current;

return () => {
if (element) {
element.destroy();
if (elementRef.current) {
elementRef.current.destroy();
}
};
}, []);
Expand Down

0 comments on commit 65b05dd

Please sign in to comment.