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

refactor(Autocomplete): update tests and add safeTimeout to input #2545

Merged
merged 5 commits into from
Nov 10, 2022

Conversation

joshblack
Copy link
Member

Context #2461

Update the tests for Autocomplete so that they no longer error out when running tests. These changes capture a variety of warnings, notably:

Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()
Warning: It looks like you're using the wrong act() around your test interactions.
    Be sure to use the matching version of act() corresponding to your renderer:

    // for react-dom:
    import {act} from 'react-dom/test-utils';
    // ...
    act(() => ...);

    // for react-test-renderer:
    import TestRenderer from react-test-renderer';
    const {act} = TestRenderer;
    // ...
    act(() => ...);
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
console.error
    Error: Uncaught [Error: AutocompleteContext returned null values]

Note

In order to get the snapshot tests and testing-library tests working together, I needed to split them up into a separate test suite. Otherwise, the mismatch act() warning would not go away. Let me know if there is a better approach when everything is in one test suite, I couldn't figure out a way to make it work 🤔

Changelog

New

Changed

  • Update AutocompleteInput to use the useSafeTimeout hook instead of setTimeout directly
  • Update Autocomplete tests to no longer emit errors in console

Removed

@joshblack joshblack requested review from a team and josepmartins November 7, 2022 17:19
@changeset-bot
Copy link

changeset-bot bot commented Nov 7, 2022

🦋 Changeset detected

Latest commit: d4a005e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Patch

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

@github-actions
Copy link
Contributor

github-actions bot commented Nov 7, 2022

size-limit report 📦

Path Size
dist/browser.esm.js 78.9 KB (+0.02% 🔺)
dist/browser.umd.js 79.54 KB (+0.01% 🔺)

@joshblack joshblack temporarily deployed to github-pages November 7, 2022 17:25 Inactive
@joshblack joshblack temporarily deployed to github-pages November 7, 2022 23:10 Inactive
@josepmartins josepmartins temporarily deployed to github-pages November 10, 2022 09:15 Inactive
@@ -12,6 +12,7 @@ import {AutocompleteContext} from './AutocompleteContext'
import TextInput from '../TextInput'
import {useRefObjectAsForwardedRef} from '../hooks/useRefObjectAsForwardedRef'
import {ComponentProps} from '../utils/types'
import useSafeTimeout from '../hooks/useSafeTimeout'
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a great hook. I didn't even realize we had it!

@@ -56,13 +58,13 @@ const AutocompleteInput = React.forwardRef(
// HACK: wait a tick and check the focused element before hiding the autocomplete menu
// this prevents the menu from hiding when the user is clicking an option in the Autoselect.Menu,
// but still hides the menu when the user blurs the input by tabbing out or clicking somewhere else on the page
setTimeout(() => {
safeSetTimeout(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be even better if we could refactor this setTimeout away entirely. InlineAutocomplete managed to do it with a synthetic change event: https://github.com/primer/react/blob/main/src/drafts/InlineAutocomplete/InlineAutocomplete.tsx#L155

I don't think that should block the rest of these changes though.

})
})

describe('snapshots', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Has this intentionally been moved outside of the "Autocomplete" describe block?

Copy link
Member Author

Choose a reason for hiding this comment

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

@mperrotti yeah, for a super odd reason too 🤔 Let me know if you have any ideas!

In order to get the snapshot tests and testing-library tests working together, I needed to split them up into a separate test suite. Otherwise, the mismatch act() warning would not go away. Let me know if there is a better approach when everything is in one test suite, I couldn't figure out a way to make it work 🤔

@joshblack joshblack enabled auto-merge (squash) November 10, 2022 17:36
@joshblack joshblack temporarily deployed to github-pages November 10, 2022 17:42 Inactive
@joshblack joshblack merged commit eb30fda into main Nov 10, 2022
@joshblack joshblack deleted the test/update-autocomplete-test branch November 10, 2022 17:42
@primer-css primer-css mentioned this pull request Nov 10, 2022
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.

None yet

3 participants