Closed TimeRaider closed 1 year ago
Yeah, this is a known limitation with shallow rendering.
Using useContext
directly is a no go, because it internally checks if it’s running during the correct phase and if not it blows up. Also, removing support for class components is a bit of a shame as sometimes we are still required to use them (eg error boundaries).
I’d much rather prefer finding a way to detect test renderer and do something special for it
How about providing both di
and useDi
for explicit (but optional) use in class and functional components?
I think I've found a solution to make it work. Can you please test it out (haven't published a beta but it's a 1 line change) and if working I'll make a new release 😊
Dependency injection doesn't work properly when a component is being rendered via
react-test-renderer
:Supposedly due to how
react-test-renderer
'screate
works, thedi
consumer does not get a value from aProvider
in https://github.com/albertogasparin/react-magnetic-di/blob/41949762bd5ba3682c4e12f5f35bf37ae72bcb50/src/react/consumer.js#L10 and ends up using a default implementation from https://github.com/albertogasparin/react-magnetic-di/blob/41949762bd5ba3682c4e12f5f35bf37ae72bcb50/src/react/context.js#L4-L6Replacing
const { getDependencies = (v) => v } = Context._currentValue || {}
withconst { getDependencies = (v) => v } = useContext(Context)
works, but breaks the ability to usedi
in both functional and class components.Would you consider a major version release to remove the hack in
di
function and providingdi
anduseDi
separately?