Skip to content

chore(ui-react-core): Migrate useInitMachine to ui-react-core #2754

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

Merged
merged 5 commits into from
Oct 18, 2022

Conversation

joebuono
Copy link
Contributor

Description of changes

Migrates useInitMachine hook to the ui-react-core package.

  • Renames the hook to useAuthenticatorInitMachine

Issue #, if available

Description of how you validated changes

Wrote unit tests, and tested for false positive.

Checklist

  • PR description included
  • yarn test passes
  • Tests are updated

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@joebuono joebuono requested a review from a team as a code owner October 18, 2022 02:22
@changeset-bot
Copy link

changeset-bot bot commented Oct 18, 2022

⚠️ No Changeset found

Latest commit: db910b2

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Member

@calebpollman calebpollman left a comment

Choose a reason for hiding this comment

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

Thanks for doing this and adding the unit tests. Left some feedback/questions

expect(initializeMachine).toHaveBeenCalledTimes(1);

// change the input props of the hook to get the useEffect to run again
data = { mutated: 'dataObject' };
Copy link
Member

Choose a reason for hiding this comment

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

Prefer to avoid mutating test variables to achieve a desired behavior as it increases fragility and the behavior itself can be difficult for future maintainers to comprehend the context of.

As an alternative, the rerender API probably provides handling it through the testing library, maybe try the docs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call. I found a way to use the rerender API to handle this

export default function useAuthenticatorInitMachine(
data: AuthenticatorMachineOptions
): void {
const { route, initializeMachine } = useAuthenticator(({ route }) => [route]);
Copy link
Member

Choose a reason for hiding this comment

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

Not like a huge deal or anything but we could declare the selector param outside the scope of useAuthenticatorInitMachine to give it a static reference between renders

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"React Hook useAuthenticator cannot be called at the top level. React Hooks must be called in a React function component or a custom React Hook function."

@joebuono joebuono temporarily deployed to ci October 18, 2022 02:42 Inactive
@joebuono joebuono temporarily deployed to ci October 18, 2022 02:42 Inactive
@joebuono joebuono temporarily deployed to ci October 18, 2022 02:42 Inactive
@joebuono joebuono temporarily deployed to ci October 18, 2022 02:42 Inactive
@joebuono joebuono temporarily deployed to ci October 18, 2022 02:44 Inactive
@joebuono joebuono temporarily deployed to ci October 18, 2022 02:44 Inactive
@joebuono joebuono temporarily deployed to ci October 18, 2022 02:44 Inactive
@joebuono joebuono temporarily deployed to ci October 18, 2022 02:44 Inactive
@joebuono joebuono temporarily deployed to ci October 18, 2022 17:28 Inactive
@joebuono joebuono temporarily deployed to ci October 18, 2022 17:28 Inactive
@joebuono joebuono temporarily deployed to ci October 18, 2022 17:28 Inactive
@joebuono joebuono temporarily deployed to ci October 18, 2022 17:28 Inactive
Copy link
Member

@calebpollman calebpollman left a comment

Choose a reason for hiding this comment

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

LGTM 🚢


expect(initializeMachine).toHaveBeenCalledTimes(1);

// change the input props of the hook to get the useEffect to run again
data = { mutated: 'dataObject' };
rerender();
rerender({ data: modifiedData });
Copy link
Member

Choose a reason for hiding this comment

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

🔥

Copy link
Contributor

@ioanabrooks ioanabrooks left a comment

Choose a reason for hiding this comment

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

LGTM!

@joebuono joebuono merged commit 80f50ed into rna/main Oct 18, 2022
@joebuono joebuono deleted the rna/migrate-useInitMachine branch October 18, 2022 20:41
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.

3 participants