I have a React component that uses the Apollo hooks lib's useQuery
hook. I'm having trouble testing this component. Here is my current setup.
import React from 'react';
import { BrowserRouter as Router } from 'react-router-dom';
import DashboardBar from './DashboardBar';
import { render, cleanup } from '@testing-library/react';
import { QUERY } from '../../queries';
import { ApolloClient } from 'apollo-client';
import { ApolloProvider as ApolloHooksProvider } from '@apollo/react-hooks';
import { MockedProvider } from 'react-apollo/test-utils';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { MockLink } from 'apollo-link-mock';
import wait from 'waait';
import casual from 'casual';
describe('<DashboardBar />', () => {
it('renders and matches snapshot', async () => {
const mocks = [
{
request: { query: QUERY, variables: { id: 'WLYhM' } },
result: {
data: {
q: {
brand: fakeBrand,
claimByAction: casual.boolean,
claimRules: fakeClaimRules,
wantIn: fakeWantIn,
},
},
},
},
];
function createClient(mocks) {
return new ApolloClient({
cache: new InMemoryCache(),
link: new MockLink(mocks),
});
}
const client = createClient(mocks);
const { container } = render(
<ApolloHooksProvider client={client}>
<Router>
<DashboardBar {...props} store={reduxStore.store} />
</Router>
</ApolloHooksProvider>
);
console.log(container.firstChild);
expect(container.firstChild).toBe(null);
await wait(200);
console.log(container.firstChild);
});
});
When I run the test, I'm getting the following error
TypeError: Cannot read property 'q' of undefined
Even though the data is being returned fine in the actual component.
Does anyone have an example of how the successfully set up and executed tests w/ components using hooks from the Apollo hooks lib?
Thanks!
MockedProvider
from'@apollo/react-testing'
however i've hit the same issues as you on this one – Deficient