You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
React Magnetic DI is a library that allows dependency injection in React components and functions for testing purposes.
It looks like the wrappers for renderHook and render (from @testing-library/react) behave quite differently. While render works with custom providers other than the React context, somehow renderHook fails to do so.
renderHook does not work when a React Magnetic DI provider is used as a wrapper.
Reproducible example
importReact,{typeReactElement,typeComponentType,typeReactNode}from'react';import{render}from'@testing-library/react';import{renderHook}from'@testing-library/react-hooks';import{injectable,di,DiProvider,typeDependency}from'react-magnetic-di';constcreateRtlDiWrapper=(use: Dependency[]): ComponentType<{children: ReactNode}>=>(props: {children: ReactNode})=><DiProvideruse={use}>{props.children}</DiProvider>;
constrenderHookWithDi=<TProps,TResult>(callback: (props: TProps)=>TResult,dependencies: Dependency[]=[],)=>renderHook<TProps,TResult>(callback,{wrapper: createRtlDiWrapper(dependencies)asunknownasComponentType<TProps>,});constrenderWithDi=(element: ReactElement,dependencies: Dependency[]=[])=>render(element,{wrapper: createRtlDiWrapper(dependencies),});constexampleDependency=()=>1;describe('renderHook vs render',()=>{constdeps=[injectable(exampleDependency,()=>2)];it('should inject dependency using renderHook (but fails)',()=>{constuseExample=()=>{di(exampleDependency);return{value: exampleDependency()};};const{ result }=renderHookWithDi(()=>useExample(),deps);expect(result.current).toEqual({value: 2});});it('should inject dependency using render',()=>{constExample=()=>{di(exampleDependency);return<span>{exampleDependency()}</span>;};const{ asFragment }=renderWithDi(<Example/>, deps);
expect(asFragment()).toMatchInlineSnapshot(` <span> 2 </span> `);});});
Expectation
Both tests would pass as the dependency should be properly injected using the DI wrapper.
Actual Behaviour
Only the second test, using render, passes, as the dependency is properly injected. The first one fails because it uses the actual implementation instead.
The text was updated successfully, but these errors were encountered:
@haskellcamargo
I know you opened this ticket a long while ago, but maybe it will still help others.
Your code contains a mistake:
constrenderHookWithDi=<TProps,TResult>(callback: (props: TProps)=>TResult,dependencies: Dependency[]=[],)=>// The generics of renderHook were switched, this is the correct way:renderHook<TResult,TProps>(callback,{// No type assertion needed anymorewrapper: createRtlDiWrapper(dependencies),});
renderHook generics are first the type for the result and then followed by the type of the props. As a refactoring result your type assertion of the wrapper should not be necessary anymore. Maybe it also unreveals some other bug in your code? 🤔
react-hooks-testing-library
version:8.0.1
react
version:16.13.1
react-dom
version:16.13.1
React Magnetic DI is a library that allows dependency injection in React components and functions for testing purposes.
It looks like the wrappers for
renderHook
andrender
(from@testing-library/react
) behave quite differently. Whilerender
works with custom providers other than the React context, somehowrenderHook
fails to do so.renderHook
does not work when a React Magnetic DI provider is used as a wrapper.Reproducible example
Expectation
Both tests would pass as the dependency should be properly injected using the DI wrapper.
Actual Behaviour
Only the second test, using
render
, passes, as the dependency is properly injected. The first one fails because it uses the actual implementation instead.The text was updated successfully, but these errors were encountered: