admin管理员组文章数量:1333487
I have a React ponent that uses the Apollo hooks lib's useQuery
hook. I'm having trouble testing this ponent. 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 ponent.
Does anyone have an example of how the successfully set up and executed tests w/ ponents using hooks from the Apollo hooks lib?
Thanks!
I have a React ponent that uses the Apollo hooks lib's useQuery
hook. I'm having trouble testing this ponent. 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 ponent.
Does anyone have an example of how the successfully set up and executed tests w/ ponents using hooks from the Apollo hooks lib?
Thanks!
Share Improve this question asked Jul 16, 2019 at 15:33 maxwellgovermaxwellgover 7,1519 gold badges41 silver badges66 bronze badges 1-
I think you should use
MockedProvider
from'@apollo/react-testing'
however i've hit the same issues as you on this one – Damian Green Commented Jul 26, 2019 at 16:35
1 Answer
Reset to default 4Actually I managed to get this working using act()
Example as follows:
import { MockedProvider } from '@apollo/react-testing'
import query from 'data/graphql/Device/psaButton.graphql'
import React from 'react'
import { act, create, ReactTestRenderer } from 'react-test-renderer'
import { MockAppNoLayout } from 'testHelpers/testHelpers'
import waitForExpect from 'wait-for-expect'
import PSAButton from './PSAButton'
const mocks = [
{
request: {
query,
variables: {
deviceId: '1',
},
},
result: {
data: {
deviceById: {
id: '1',
info: {
psaUrl: 'asdsadsad',
lastSyncDate: 'asdasd',
lastVerifyDate: 'asdsad',
},
},
user: {
timeZone: 'Europe/London',
},
},
},
},
]
describe('PSAButton', () => {
it('renders correctly', async () => {
let wrapper: ReactTestRenderer
act(() => {
wrapper = create(
<MockAppNoLayout>
<MockedProvider mocks={mocks} addTypename={false}>
<PSAButton deviceId="1" />
</MockedProvider>
</MockAppNoLayout>,
)
})
await waitForExpect(() => {
const testInstance = wrapper.root
expect(testInstance.findByType('span').children[0]).toBe('Open In PSA')
})
})
})
MockAppNoLayout
is a React ponent tree containing amock IntlProvider
and Styleprovider
and the such like, (not important for this question).
本文标签: javascriptExample of how to test a component that uses useQueryStack Overflow
版权声明:本文标题:javascript - Example of how to test a component that uses useQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742235664a2438032.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论