Skip to content

Commit

Permalink
Merge branch 'main' into react-18-upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
snowystinger authored Sep 1, 2021
2 parents 274932b + 4a03704 commit 9f296b1
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 40 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,15 @@
"contributions": [
"test"
]
},
{
"login": "masious",
"name": "Masious",
"avatar_url": "https://avatars.githubusercontent.com/u/6429009?v=4",
"profile": "https://www.facebook.com/masoud.bonabi",
"contributions": [
"doc"
]
}
],
"skipCi": true,
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
run: npm run validate

- name: ⬆️ Upload coverage report
uses: codecov/[email protected].2
uses: codecov/[email protected].3

release:
needs: main
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center"><a href="https://github.com/bdwain"><img src="https://avatars.githubusercontent.com/u/3982094?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Bryan Wain</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/issues?q=author%3Abdwain" title="Bug reports">🐛</a> <a href="https://github.com/testing-library/react-hooks-testing-library/pulls?q=is%3Apr+reviewed-by%3Abdwain" title="Reviewed Pull Requests">👀</a></td>
<td align="center"><a href="https://github.com/snowystinger"><img src="https://avatars.githubusercontent.com/u/698229?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Robert Snow</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=snowystinger" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/chris110408"><img src="https://avatars.githubusercontent.com/u/10645051?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Chris Chen</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=chris110408" title="Tests">⚠️</a></td>
<td align="center"><a href="https://www.facebook.com/masoud.bonabi"><img src="https://avatars.githubusercontent.com/u/6429009?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Masious</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=masious" title="Documentation">📖</a></td>
</tr>
</table>

Expand Down
2 changes: 1 addition & 1 deletion docs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ The `renderHook` function returns an object that has the following properties:
}
```

The `current` value or the `result` will reflect the latest of whatever is returned from the
The `current` value of the `result` will reflect the latest of whatever is returned from the
`callback` passed to `renderHook`. Any thrown values from the latest call will be reflected in the
`error` value of the `result`. The `all` value is an array containing all the returns (including the
most recent) from the callback. These could be `result` or an `error` depending on what the callback
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
"react-error-boundary": "^3.1.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "4.28.5",
"@typescript-eslint/parser": "4.28.5",
"@typescript-eslint/eslint-plugin": "4.30.0",
"@typescript-eslint/parser": "4.30.0",
"all-contributors-cli": "6.20.0",
"codecov": "3.8.3",
"cross-env": "^7.0.3",
Expand All @@ -71,8 +71,8 @@
"react": "17.0.2",
"react-dom": "17.0.2",
"react-test-renderer": "17.0.2",
"ts-node": "10.1.0",
"typescript": "4.3.5"
"ts-node": "10.2.1",
"typescript": "4.4.2"
},
"peerDependencies": {
"react": ">=16.9.0",
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/utils/runForRenderers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ const rendererResolvers = {
native: () => requireRenderer('native'),
server: () => requireRenderer<ReactHooksServerRenderer>('server'),
'default/pure': () => requireRenderer('default/pure'),
'dom/pure': () => requireRenderer('default/pure'),
'native/pure': () => requireRenderer('default/pure'),
'dom/pure': () => requireRenderer('dom/pure'),
'native/pure': () => requireRenderer('native/pure'),
'server/pure': () => requireRenderer<ReactHooksServerRenderer>('server/pure'),
'server/hydrated': () => hydratedServerRenderer('server'),
'server/hydrated/pure': () => hydratedServerRenderer('server/pure')
Expand Down
33 changes: 11 additions & 22 deletions src/core/asyncUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
AsyncUtils
} from '../types'

import { resolveAfter, callAfter } from '../helpers/promises'
import { createTimeoutController } from '../helpers/createTimeoutController'
import { TimeoutError } from '../helpers/error'

const DEFAULT_INTERVAL = 50
Expand All @@ -20,37 +20,26 @@ function asyncUtils(act: Act, addResolver: (callback: () => void) => void): Asyn
return callbackResult ?? callbackResult === undefined
}

const timeoutSignal = createTimeoutController(timeout)

const waitForResult = async () => {
while (true) {
await Promise.race(
[
new Promise<void>((resolve) => addResolver(resolve)),
interval && resolveAfter(interval)
].filter(Boolean)
)

if (checkResult()) {
const intervalSignal = createTimeoutController(interval)
timeoutSignal.onTimeout(() => intervalSignal.cancel())

await intervalSignal.wrap(new Promise<void>(addResolver))

if (checkResult() || timeoutSignal.timedOut) {
return
}
}
}

let timedOut = false

if (!checkResult()) {
if (timeout) {
const timeoutPromise = () =>
callAfter(() => {
timedOut = true
}, timeout)

await act(() => Promise.race([waitForResult(), timeoutPromise()]))
} else {
await act(waitForResult)
}
await act(() => timeoutSignal.wrap(waitForResult()))
}

return !timedOut
return !timeoutSignal.timedOut
}

const waitFor = async (
Expand Down
39 changes: 39 additions & 0 deletions src/helpers/createTimeoutController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { WaitOptions } from '../types'

function createTimeoutController(timeout: WaitOptions['timeout']) {
let timeoutId: NodeJS.Timeout
const timeoutCallbacks: Array<() => void> = []

const timeoutController = {
onTimeout(callback: () => void) {
timeoutCallbacks.push(callback)
},
wrap(promise: Promise<void>) {
return new Promise<void>((resolve, reject) => {
timeoutController.timedOut = false
timeoutController.onTimeout(resolve)

if (timeout) {
timeoutId = setTimeout(() => {
timeoutController.timedOut = true
timeoutCallbacks.forEach((callback) => callback())
resolve()
}, timeout)
}

promise
.then(resolve)
.catch(reject)
.finally(() => timeoutController.cancel())
})
},
cancel() {
clearTimeout(timeoutId)
},
timedOut: false
}

return timeoutController
}

export { createTimeoutController }
10 changes: 0 additions & 10 deletions src/helpers/promises.ts

This file was deleted.

0 comments on commit 9f296b1

Please sign in to comment.