Just started with ra, and now I'm trying to make tests (jest + testing library) run with data from mock server (ra-data-graphql-simple). I need exactly this setup for various reasons.
Unfortunately, this case is not covered in documentation, and it's not intuitive when you just started with ra.
I'm making wrapper function, which will be reusable in all tests. I have two versions of code, both of them return two different errors.
1: Wrapper func v1:
const [dataProvider, setDataProvider] = useState<any>(null);
useEffect(() => {
if (!dataProvider) {
buildGraphQLProvider({
clientOptions: { uri: 'http://localhost:4000' },
}).then((newProvider: any) => setDataProvider(() => newProvider));
}
});
return dataProvider ? (
<DataProviderContext.Provider value={dataProvider}>
<ThemeProvider theme={theme}>
<TestContext enableReducers>{children}</TestContext>
</ThemeProvider>
</DataProviderContext.Provider>
) :
null;
Usage in test:
...
const defaultEditProps = {
basePath: '/Client',
resource: 'Client',
hasList: true,
location: {
hash: '',
key: 'igsiaesg',
pathname: '/Client',
search: '',
state: null,
},
match: {
isExact: true,
params: {},
path: '/Client',
url: '/Client',
},
};
render(
<TestWrapper>
<ClientList {...defaultEditProps} />
</TestWrapper>,
);
...
ClientList is just List with filters. In dev environment it's work as expected and most probably is not the reason of my problems.
Error I get: Unknown dataProvider function: getList
Ok, this covered in https://github.com/Steams/ra-data-hasura-graphql/issues/19 The only working solution, it seems, is to replace dataProvider with dataProvider()
So, wrapper, v2:
const [dataProvider, setDataProvider] = useState<any>(null);
useEffect(() => {
if (!dataProvider) {
buildGraphQLProvider({
clientOptions: { uri: 'http://localhost:4000' },
// here I replaced dataProvider with dataProvider()
}).then((newProvider: any) => setDataProvider(() => newProvider());
}
});
return dataProvider ? (
<DataProviderContext.Provider value={dataProvider}>
<ThemeProvider theme={theme}>
<TestContext enableReducers>{children}</TestContext>
</ThemeProvider>
</DataProviderContext.Provider>
) :
null;
But, this version give me other error: Error: Unknown resource undefined. Make sure it has been declared on your server side schema
It's covered here - https://github.com/Steams/ra-data-hasura-graphql/issues/6 But for some reason, changes in useEfect, described there, doesn’t resolve this issue in my case.
Will really appreciate any advice. Maybe, I'm doing it wrong at all.