This commit is contained in:
36
tests/helpers/renderHook.tsx
Normal file
36
tests/helpers/renderHook.tsx
Normal file
@@ -0,0 +1,36 @@
|
||||
import { act } from 'react';
|
||||
import { createRoot } from 'react-dom/client';
|
||||
|
||||
export function renderHook<T>(useHook: () => T) {
|
||||
let currentValue: T;
|
||||
|
||||
function TestComponent() {
|
||||
currentValue = useHook();
|
||||
return null;
|
||||
}
|
||||
|
||||
const container = document.createElement('div');
|
||||
const root = createRoot(container);
|
||||
|
||||
act(() => {
|
||||
root.render(<TestComponent />);
|
||||
});
|
||||
|
||||
return {
|
||||
result: {
|
||||
get current() {
|
||||
return currentValue;
|
||||
},
|
||||
},
|
||||
rerender() {
|
||||
act(() => {
|
||||
root.render(<TestComponent />);
|
||||
});
|
||||
},
|
||||
unmount() {
|
||||
act(() => {
|
||||
root.unmount();
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user