-
Notifications
You must be signed in to change notification settings - Fork 308
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
fix(react): hub listener fix #2183
Conversation
This reverts commit 6b8a77f.
🦋 Changeset detectedLatest commit: 0550a95 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
jest.mock('@aws-amplify/ui', () => ({ | ||
...(jest.requireActual('@aws-amplify/ui') as {}), | ||
defaultAuthHubHandler: jest.fn(), | ||
})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without this,
const hubHandlerSpy = jest.spyOn(UI, 'defaultAuthHubHandler');
fails with
TypeError: Cannot redefine property: defaultAuthHubHandler
event: 'tokenRefresh', | ||
}); | ||
|
||
expect(hubHandlerSpy).toHaveBeenCalledTimes(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been failing due to
Expected number of calls: 1
Received number of calls: 0
but I could verify the function does get called. Looks like jest is not correctly mocking it?
packages/react/src/components/Authenticator/hooks/useAuthenticator/__tests__/Provider.test.tsx
Show resolved
Hide resolved
(data) => { | ||
handler(data, service); | ||
}, | ||
'authenticator-hub-handler' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the hub handler name; added mostly for debugging purposes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🚢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Description of changes
This PR ensures that Authenticator correctly attaches and reattaches hub listener on React 18 StrictMode.
Problem
In React 18 StrictMode, React will always simulate unmounting and remounting whenever components initializes. This persist states and refs between the remounts:
This broke the hub listener pattern we used in React, because we've been expecting
ref
value to be reset between remounts.Fix
This PR fixes by this by not using
useRef
at all. Even if component were to remount, it'll reattach immediately.Issue #, if available
Description of how you validated changes
Verified manually on
@experimental
tag. Added some unit test as well.Checklist
yarn test
passessideEffects
field updatedBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.