-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
184 additions
and
93 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
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,29 @@ | ||
/* | ||
* @since 2024-10-31 20:01:40 | ||
* @author junbao <[email protected]> | ||
*/ | ||
import { renderHook, type RenderHookResult } from '@testing-library/react'; | ||
import { createStore, Snapshot, Store } from 'amos-core'; | ||
import { createElement } from 'react'; | ||
import { Provider } from './context'; | ||
|
||
export function renderDynamicHook<P, T>( | ||
fn: (props: P) => T, | ||
preloadedState?: Snapshot, | ||
initialProps?: P, | ||
): RenderHookResult<T, P> & Store & { mockFn: jest.Mock } { | ||
const store = createStore({ preloadedState }); | ||
const mockFn = jest.fn(); | ||
const hook = renderHook( | ||
(props: P) => { | ||
const value = fn(props); | ||
mockFn(props, value); | ||
return value; | ||
}, | ||
{ | ||
wrapper: (props) => createElement(Provider, { store, children: props.children }), | ||
initialProps, | ||
}, | ||
); | ||
return Object.assign(hook, store, { mockFn: mockFn }); | ||
} |
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
Oops, something went wrong.