Added Provider.FromValue, to enable mocking in Storybook and Unit Tests#127
Added Provider.FromValue, to enable mocking in Storybook and Unit Tests#127scottrippey wants to merge 2 commits into
Provider.FromValue, to enable mocking in Storybook and Unit Tests#127Conversation
Provider.FromValue, to enable easy dependency mockingProvider.FromValue, to enable mocking in Storybook and Unit Tests
|
@diegohaz Thoughts on this PR? |
|
I’m facing exactly this same issue. Seems like a great solution! |
|
@scottrippey hey, how are you solving this problem until this PR is not approved? Can you share a solution? :) |
|
@luiznasciment0 this whole lib is just 1 file, so I just copied my fork into my project. |
|
lol hahaha awesome! actually that's a pretty good solution! |
|
Thanks for working on this. There are several ways to deal with this problem. For example, a more explicit approach would be accepting the overrides through the hook/Provider props: function useCounter(props) {
const [_count, _setCount] = useState(props.initialCount);
const count = props.count ?? _count;
const setCount = props.setCount || _setCount;
const _increment = useCallback(() => setCount((prevCount) => prevCount + 1), [setCount]);
const increment = props.increment || _increment;
return { count, increment };
}
const [CounterProvider, useCounterContext] = constate(useCounter);
<CounterProvider count={0} increment={action("increment")} />I haven't tried it, but I guess you can create a wrapper around constate to abstract this logic. Anyway, I don't think this library should encourage mocking the state like that, which usually leads to implementation detail tests, which is the case of the |
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #127 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 1 1
Lines 31 34 +3
Branches 5 5
=========================================
+ Hits 31 34 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
My intent with this PR is just to expose the I don't think this makes |
|
FWIW, there is an alternative approach, similar to what you described. I've authored This plays nicely with |
|
Just came here looking for the exact solution @scottrippey is proposing in the PR. As someone who inherited this library as dependency, I can't believe there is no other way to mock or inject the value into provider. The solution proposed by @diegohaz is just ... ugly. |
|
Hi! I've started a new project, and am using React's Context API is essentially a dependency-injection API, which is extremely useful in testing frameworks. I am confident that this is a solid solution, and I would really appreciate if you could accept this PR. WDYT? |
I have started using
constateto separate my data-fetching logic from my UI, and it's perfect for the job! Thank you.However, in Storybook and in Unit tests, I'd like to be able to manually provide (mock) values to the context provider.
So, in this PR, I've exposed a property on the
ProvidercalledProvider.FromValue, which is just a Provider that ignores the hook data and uses the provided data instead.Storybook example:
React Testing Library example:
I'm open to feedback, especially regarding naming things, so please let me know!
TODO:
FromValuefeature