在测试 withApollo 中包装的组件时,我使用 Apollo 文档中指定的 Apollo 的 MockedProvider,但是当酶尝试渲染该组件时,渲染器找不到client
MockedProvider 应该已经提供了。
我有一个使用的组件useQuery
在阿波罗的背景下。
export const QUERY = gql`{ value }`
export const Component = withApollo(({ client ) => {
const { data } = useQuery(QUERY)
return <p>{ data }</p>
})
所以我想测试一下:
describe('Component', () => {
const mocks = [{
request: { query: QUERY },
result: { data: 42 }
}]
const WrappingComponent = ({ children }) => <MockedProvider mocks={ mocks } addTypename={ false }>
{ children }
</MockedProvider>
WrappingComponent.propTypes = {
children: PropTypes.any
}
const component = shallow(<Component />, { wrappingComponent: WrappingComponent })
it('matches the snapshot', () => {
expect(enzymeToJson(component)).toMatchSnapshot()
})
}
在这种情况下,快照结果是
exports[`CruxProductSetup matches the snapshot 1`] = `
<ApolloConsumer>
<Component />
</ApolloConsumer>
`;
不是我的组件,所以我做了一个技巧并尝试进入内部:
const component = shallow(shallow(<Component />, { wrappingComponent: WrappingComponent }).get(0))
快照是:
exports[`Component matches the snapshot 1`] = `
<ContextConsumer>
<Component />
</ContextConsumer>
`;
还是没用。我的组件还没有渲染。我再次使用这个技巧:
const component = shallow(shallow(shallow(<Component />, { wrappingComponent: WrappingComponent }).get(0)).get(0))
在拍摄快照之前,酶会抛出以下错误:
FAIL Component/__tests__/index.js
Component
✕ encountered a declaration exception (75ms)
● Component › encountered a declaration exception
Invariant Violation: Could not find "client" in the context of ApolloConsumer. Wrap the root component in an <ApolloProvider>.
85 | })
86 |
> 87 | const component = shallow(shallow(shallow(<Component />, { wrappingComponent: WrappingComponent }).get(0)).get(0))
88 | // const component = shallow(shallow(shallow(<Component />, { wrappingComponent: WrappingComponent }).get(0)).get(0))
89 | // const component = shallow(shallow(shallow(<CruxProductSetup summaryPanel={ null } />, { wrappingComponent: WrappingComponent }).get(0)).get(0))
90 |
at new InvariantError (node_modules/ts-invariant/lib/invariant.esm.js:12:28)
at invariant (node_modules/ts-invariant/lib/invariant.esm.js:24:15)
at Object.children (node_modules/@apollo/react-common/lib/react-common.cjs.js:55:132)
at Object.type (node_modules/enzyme-adapter-react-16/src/ReactSixteenAdapter.js:612:30)
at ReactShallowRenderer.render (node_modules/enzyme-adapter-react-16/node_modules/react-test-renderer/cjs/react-test-renderer-shallow.development.js:104:34)
at node_modules/enzyme-adapter-react-16/src/ReactSixteenAdapter.js:615:53
at withSetStateAllowed (node_modules/enzyme-adapter-utils/src/Utils.js:99:18)
at Object.render (node_modules/enzyme-adapter-react-16/src/ReactSixteenAdapter.js:615:18)
at new ShallowWrapper (node_modules/enzyme/src/ShallowWrapper.js:411:22)
at shallow (node_modules/enzyme/src/shallow.js:10:10)
at Suite.<anonymous> (Component/__tests__/index.js:87:23)
at Object.<anonymous> (Component/__tests__/index.js:61:1)
"Could not find "client" in the context of ApolloConsumer. Wrap the root component in an <ApolloProvider>
“?我以为就是这样MockedProvider
正在做。
我的依赖项:
@apollo/react-testing: 3.1.3
enzyme: 3.11.0
graphql-tag: 2.10.1
jest: 22.4.4
react: 16.12.0
react-apollo: 3.1.3