-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Michael Peyper <[email protected]> Co-authored-by: Kent C. Dodds <[email protected]>
- Loading branch information
1 parent
2c451b3
commit 9535eff
Showing
4 changed files
with
161 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React from 'react' | ||
import {renderHook} from '../pure' | ||
|
||
test('gives comitted result', () => { | ||
const {result} = renderHook(() => { | ||
const [state, setState] = React.useState(1) | ||
|
||
React.useEffect(() => { | ||
setState(2) | ||
}, []) | ||
|
||
return [state, setState] | ||
}) | ||
|
||
expect(result.current).toEqual([2, expect.any(Function)]) | ||
}) | ||
|
||
test('allows rerendering', () => { | ||
const {result, rerender} = renderHook( | ||
({branch}) => { | ||
const [left, setLeft] = React.useState('left') | ||
const [right, setRight] = React.useState('right') | ||
|
||
// eslint-disable-next-line jest/no-if | ||
switch (branch) { | ||
case 'left': | ||
return [left, setLeft] | ||
case 'right': | ||
return [right, setRight] | ||
|
||
default: | ||
throw new Error( | ||
'No Props passed. This is a bug in the implementation', | ||
) | ||
} | ||
}, | ||
{initialProps: {branch: 'left'}}, | ||
) | ||
|
||
expect(result.current).toEqual(['left', expect.any(Function)]) | ||
|
||
rerender({branch: 'right'}) | ||
|
||
expect(result.current).toEqual(['right', expect.any(Function)]) | ||
}) | ||
|
||
test('allows wrapper components', async () => { | ||
const Context = React.createContext('default') | ||
function Wrapper({children}) { | ||
return <Context.Provider value="provided">{children}</Context.Provider> | ||
} | ||
const {result} = renderHook( | ||
() => { | ||
return React.useContext(Context) | ||
}, | ||
{ | ||
wrapper: Wrapper, | ||
}, | ||
) | ||
|
||
expect(result.current).toEqual('provided') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters