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

Upstream useSafeTimeout & add tests #1041

Merged
merged 34 commits into from
Feb 16, 2021
Merged

Upstream useSafeTimeout & add tests #1041

merged 34 commits into from
Feb 16, 2021

Conversation

emplums
Copy link

@emplums emplums commented Feb 9, 2021

This PR upstreams the useSafeTimeouts hook and adds some simple tests.

I've also added documentation, including adding a new Hooks subsection to the documentation site 🎉 We might want to rethink how we structure the sidebar later but I think this is fine for now.

Related issue: #1013

Merge checklist

  • Added or updated TypeScript definitions (index.d.ts) if necessary
  • Added/updated tests
  • Added/updated documentation
  • Tested in Chrome
  • Tested in Firefox
  • Tested in Safari
  • Tested in Edge

Take a look at the What we look for in reviews section of the contributing guidelines for more information on how we review PRs.

@changeset-bot
Copy link

changeset-bot bot commented Feb 9, 2021

🦋 Changeset detected

Latest commit: 0a392a0

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

This PR includes changesets to release 1 package
Name Type
@primer/components Minor

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

@vercel
Copy link

vercel bot commented Feb 9, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/primer/primer-components/q1oh7cujf
✅ Preview: https://primer-components-git-upstream-behaviors.primer.now.sh

@vercel vercel bot temporarily deployed to Preview February 10, 2021 00:07 Inactive
@vercel vercel bot temporarily deployed to Preview February 10, 2021 00:10 Inactive
@vercel vercel bot temporarily deployed to Preview February 10, 2021 17:35 Inactive
index.d.ts Outdated Show resolved Hide resolved
@@ -0,0 +1,33 @@
import {useCallback, useEffect, useRef} from 'react'

type SetTimeout = (handler: TimerHandler, timeout?: number, ...args: any[]) => number
Copy link
Contributor

Choose a reason for hiding this comment

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

Same suggestion about using typeof window.setTimeout as above.

Copy link
Author

Choose a reason for hiding this comment

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

I tried that, but there are some required properties on the native type that doesn't make sense to pass in manually to these wrapper functions:

Property '__promisify__' is missing in type '(handler: TimerHandler, timeout?: number | undefined, ...args: any[]) => number' but required in type 'typeof setTimeout'.ts(2322)

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh weird! Thanks for checking.

src/hooks/useSafeTimeout.ts Outdated Show resolved Hide resolved
src/hooks/useSafeTimeout.ts Outdated Show resolved Hide resolved
import useSafeTimeout from '../hooks/useSafeTimeout'

test('should call callback after time', async () => {
const { result, waitFor } = renderHook(() => useSafeTimeout())
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason for not using fake timers on this test?

@vercel vercel bot temporarily deployed to Preview February 11, 2021 00:22 Inactive
@vercel vercel bot temporarily deployed to Preview February 11, 2021 00:40 Inactive
Co-authored-by: Trevor Gau <t-hugs@github.com>
@vercel vercel bot temporarily deployed to Preview February 11, 2021 22:52 Inactive
@vercel vercel bot temporarily deployed to Preview February 11, 2021 22:57 Inactive
@vercel vercel bot temporarily deployed to Preview February 11, 2021 23:00 Inactive
@vercel vercel bot temporarily deployed to Preview February 11, 2021 23:08 Inactive
@vercel vercel bot temporarily deployed to Preview February 11, 2021 23:10 Inactive
@vercel vercel bot temporarily deployed to Preview February 11, 2021 23:48 Inactive
@emplums
Copy link
Author

emplums commented Feb 12, 2021

Note: I had to do an extensive refactoring of our tests to get them to work nicely with typescript after adding the @testing-library/react-hooks library 😅

Copy link
Contributor

@colebemis colebemis left a comment

Choose a reason for hiding this comment

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

Nice work! Thanks for dealing with the tests ❤️ Just left a few minor comments.

src/__tests__/FilterList.tsx Outdated Show resolved Hide resolved
src/__tests__/FilterListItem.tsx Outdated Show resolved Hide resolved
src/__tests__/useSafeTimeout.tsx Outdated Show resolved Hide resolved
src/utils/testing.tsx Outdated Show resolved Hide resolved
src/utils/testing.tsx Outdated Show resolved Hide resolved
@vercel vercel bot temporarily deployed to Preview February 16, 2021 20:40 Inactive
emplums and others added 5 commits February 16, 2021 12:40
Co-authored-by: Cole Bemis <colebemis@github.com>
Co-authored-by: Cole Bemis <colebemis@github.com>
Co-authored-by: Cole Bemis <colebemis@github.com>
Co-authored-by: Cole Bemis <colebemis@github.com>
Co-authored-by: Cole Bemis <colebemis@github.com>
@vercel vercel bot temporarily deployed to Preview February 16, 2021 20:43 Inactive
@emplums emplums merged commit 1a95178 into main Feb 16, 2021
@emplums emplums deleted the upstream-behaviors branch February 16, 2021 20:54
@github-actions github-actions bot mentioned this pull request Feb 16, 2021
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