-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Use real data provider for testing #4644
Comments
It's a good idea, feel free to open a PR, I would love to help you on this one :) |
The const TextApp = () => (
<DataProviderContext.Provider value={myDataProvider}>
<TestContext>
{({ store, history }) => {
...
}}
</TestContext>
</DataProviderContext.Provider>
) See for instance Mutation.spec.js for an example of this in the react-admin codebase. That being said, this piece of advice should be in the Unit Testing documentation. Marking this issue as Documentation. |
Thanks for your input @fzaninotto I managed to write my own I would have submitted a solution along those lines, but I agree that it is easy enough to wrap the TestContext itself to achieve the same thing: // imports omitted
const fakeDataProvider = () => Promise.resolve({ data: null });
export default class TestContext extends Component {
storeWithDefault = null;
history = null;
finalDataProvider = convertLegacyDataProvider(fakeDataProvider);
constructor(props) {
super(props);
this.history = props.history || createMemoryHistory();
const { initialState = {}, enableReducers = false, dataProvider = null, customReducers = {} } = props;
if (dataProvider) {
this.finalDataProvider = dataProvider instanceof Function
? convertLegacyDataProvider(dataProvider)
: dataProvider;
}
this.storeWithDefault = enableReducers
? createAdminStore({
initialState: merge({}, defaultStore, initialState),
dataProvider: this.finalDataProvider,
history: createMemoryHistory(),
customReducers,
})
: createStore(() => merge({}, defaultStore, initialState));
}
renderChildren = () => {
const { children } = this.props;
return typeof children === "function"
? children({
store: this.storeWithDefault,
history: this.history,
})
: children;
};
render() {
return (
<Provider store={ this.storeWithDefault }>
<Router history={ this.history }>
<DataProviderContext.Provider value={ this.finalDataProvider }>
{ this.renderChildren() }
</DataProviderContext.Provider>
</Router>
</Provider>
);
}
} As you can see I need to use |
I don't think we should update the TestContext, but we should add a section in the Unit Testing documentation to explain how to pass a dataProvider in tests using the context. And yes, we're open to PRs! |
I agree that it is sufficient to add documentation about the usage of
Should I open a new issue for that one? |
On second thought: Why not use |
I actually had the same issue recently. It is indeed a pain when you have custom reducers and we should do something about it. I personally agree with you about the |
I'm afraid that adding a DataProviderContext (and i18n) inside TestContext is a breaking change - some existing tests will break. |
I see, but how about wrapping the children passed to Do you think passing custom reducers on to |
It seems react-admin already offers several utilities for unit testing. I'd like to know more about your testing needs, so that we can design better test utilities. |
I'm also interesting in writing integration tests for my components used in ReactAdmin. I'm using RTL and msw for this and I'm having a hard time setting up the required context to have everything working. Here's what I tried so far: const dataProviders = jsonServerProvider(window.location.origin)
// my msw server setup
const server = setupServer(
rest.get('/categories', (req, res, ctx) => {
if (req.url.searchParams.get('q')) {
return res(
ctx.set('X-Total-Count', '2'),
ctx.json([
{ id: '1', name: 'test 1' },
{ id: '2', name: 'test 2' },
])
)
}
return res(ctx.set('X-Total-Count', '0'), ctx.json([]))
})
)
render(
<CoreAdminContext dataProvider={dataProviders}>
<MyBulkActionComponent selectedIds={['111', '222']} />
</CoreAdminContext>
)
const { ids, data: categories, loading: isLoading } = useGetList<Category>(
'categories',
{
page: 1,
perPage: 20,
},
{ field: 'name', order: 'ASC' },
{
q: debouncedSearchText,
}
) From what I see, the |
Joining the club having to implement own version of the TestContext only because of the @fzaninotto Are there any future plans on this no matter if it is yes or no and what kind of more info is needed? |
Adding support to |
Sure - my pleasure! |
While adding the |
Yes, it would be really appreciated. Thanks! |
Also done :)) Willing to fix any change request the you would might have |
Shall we consider this one for closing after #6067 is merged? |
Yes indeed, I'm closing this issue, thanks! |
Is your feature request related to a problem? Please describe.
I would like to enable my data provider for testing, so I can test my custom components and their side effects better (i.e. requests that are triggered). I'm using jest and react-testing-library and by default when I wrap the components I'd like to test using the
<TestContext enableReducers>
component, all data provider requests always respond with{ data: null }
.Describe the solution you'd like
I think it would be ideal to be able to pass the real data provider to the
TestContext
component so it will be used by the test application.Describe alternatives you've considered
I tried to rewrite the
TestContext
component and used my data provider. It never triggered any requests though and failed inside the react admin data reducer (for a create request inside the addOneRecord function, with newRecord set to null)I'm not sure why my data provider wasn't triggering any requests and ended up inside the react admin data reducer before doing so. With some guidance why this happened, I would be happy to implement this feature if it is doable.
The text was updated successfully, but these errors were encountered: