Skip to content

Commit

Permalink
fix: improve React mountHook type (#17241)
Browse files Browse the repository at this point in the history
* feat(react) : improve mountHook types

* chore: Remove wrapping which causes test failing

Co-authored-by: Lachlan Miller <lachlan.miller.1990@outlook.com>
  • Loading branch information
edimitchel and lmiller1990 committed Jul 12, 2021
1 parent 8fca932 commit e40969a
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions npm/react/src/mountHook.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import * as React from 'react'

import { mount } from './mount'

type MountHookResult<T> = {
readonly current: T | null | undefined
readonly error: Error | null
}

type ResultContainer<T> = {
result: MountHookResult<T>
addResolver: (resolver: () => void) => void
setValue: (val: T) => void
setError: (err: Error) => void
}

// mounting hooks inside a test component mostly copied from
// https://github.com/testing-library/react-hooks-testing-library/blob/master/src/pure.js
function resultContainer<T> () {
function resultContainer<T> (): ResultContainer<T> {
let value: T | undefined | null = null
let error: Error | null = null
const resolvers: any[] = []
Expand All @@ -29,7 +42,7 @@ function resultContainer<T> () {

return {
result,
addResolver: (resolver: any) => {
addResolver: (resolver: (() => void)) => {
resolvers.push(resolver)
},
setValue: (val: T) => updateResult(val),
Expand All @@ -46,36 +59,29 @@ type TestHookProps = {
function TestHook ({ callback, onError, children }: TestHookProps) {
try {
children(callback())
} catch (err) {
if (err.then) {
} catch (err: any) {
if ('then' in err) {
throw err
} else {
onError(err)
}
}

// TODO decide what the test hook component should show
// maybe nothing, or maybe useful information about the hook?
// maybe its current properties?
// return <div>TestHook</div>
return null
}

/**
* Mounts a React hook function in a test component for testing.
*
* @see https://github.com/bahmutov/@cypress/react#advanced-examples
*/
export const mountHook = (hookFn: (...args: any[]) => any) => {
const { result, setValue, setError } = resultContainer()
export const mountHook = <T>(hookFn: (...args: any[]) => T) => {
const { result, setValue, setError } = resultContainer<T>()

return mount(
React.createElement(TestHook, {
callback: hookFn,
onError: setError,
children: setValue,
}),
).then(() => {
cy.wrap(result)
const componentTest: React.ReactElement = React.createElement(TestHook, {
callback: hookFn,
onError: setError,
children: setValue,
})

return mount(componentTest).then(() => result)
}

4 comments on commit e40969a

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on e40969a Jul 12, 2021

Choose a reason for hiding this comment

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

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.8.0/circle-develop-e40969abe39424585fd2075b17e4e7d189f53c3a/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on e40969a Jul 12, 2021

Choose a reason for hiding this comment

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

AppVeyor has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.8.0/appveyor-develop-e40969abe39424585fd2075b17e4e7d189f53c3a/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on e40969a Jul 12, 2021

Choose a reason for hiding this comment

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

AppVeyor has built the win32 ia32 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.8.0/appveyor-develop-e40969abe39424585fd2075b17e4e7d189f53c3a/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on e40969a Jul 12, 2021

Choose a reason for hiding this comment

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

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.8.0/circle-develop-e40969abe39424585fd2075b17e4e7d189f53c3a/cypress.tgz

Please sign in to comment.