Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only call element.on when the merchant passes in a callback #372

Merged
merged 7 commits into from
Jan 10, 2023

Conversation

awalker-stripe
Copy link
Contributor

@awalker-stripe awalker-stripe commented Jan 9, 2023

Summary & motivation

See #360. Re-implements the functionality, fixing two issues:

Testing & documentation

  • Manually tested in storybook to see that events still fire
  • Manually tested in local integration with useCallback, strict mode, & changing/adding/removing callbacks for an event.
  • Tested it on a reproduction of the issue in [BUG]: LinkAuthenticationElement no longer fires onChange event when it is mounted. #365: I verified events didn't fire correctly in v1.16.1 (version with the bug), did in v1.16.2 (latest version that reverted the bad change), and still fire correctly with these changes.

// we need to cast because typescript is not able to infer which overloaded method is used based off param type
setCart((newElement as unknown) as stripeJs.StripeCartElement);
}
setElement(newElement);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes an extra render compared to before, but this seemed cleaner than modifying useAttachEvent to return some sort of addEvent function that is manually called here.

elementRef.current = null;
if (element) {
element.destroy();
setElement(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't remember the behavior of this in the later versions of react. does this throw a warning/error indicating setState being called after a component is unmounted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, removed this line, since it's a no-op.

Interestingly, I tested rendering an element, and then after a timeout, unmounting. It seems to just work — no logs/errors.

src/utils/useAttachEvent.ts Show resolved Hide resolved
const changeEventMock = Symbol('change');
simulateEvent('change', changeEventMock);
expect(mockHandler).toHaveBeenCalledWith(changeEventMock);
});
Copy link
Contributor

@bmathews-stripe bmathews-stripe Jan 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To test the failure scenario you were able to create repro steps for, maybe try creating a wrapper that simulates your repro steps? Can't remember the details exactly, but something like this?

      const mockHandler = jest.fn();
      const Wrapper = () => {
        const component =  useMemo(() => <CardElement onChange={mockHandler} />, []);
        return component;
      }
      
      render(
        <Elements stripe={mockStripe}>
          <Wrapper />
        </Elements>
      );

      const changeEventMock = Symbol('change');
      simulateEvent('change', changeEventMock);
      expect(mockHandler).toHaveBeenCalledWith(changeEventMock);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to do useMemo like the repro but unfortunately didn't have much luck. However, along this same idea of copying the repro, I got a test together that leaves elements undefined, preventing it from creating an element on the first render. This test fails on v1.16.1 but passes in 1.16.2 & here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants