-
-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Provider.FromValue
, to enable mocking in Storybook and Unit Tests
#127
base: master
Are you sure you want to change the base?
Conversation
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
@@ Coverage Diff @@
## master #127 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 1 1
Lines 31 34 +3
Branches 5 5
=========================================
+ Hits 31 34 +3
Continue to review full report at Codecov.
|
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. |
I have started using
constate
to 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
Provider
calledProvider.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:
FromValue
feature