reactjs - Apollo 的 MockedProvider 不为包装在 withApollo 中的组件提供客户端
问题描述
在测试用 Apollo 包装的组件时,我正在使用 Apollo 文档中指定的 Apollo 的 MockedProvider,但是当酶尝试渲染该组件时,渲染器找不到client
MockedProvider 应该提供的。
我有一个useQuery
在 Apollo 上下文中使用的组件。
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
解决方案
我正在使用与您相同版本的@apollo/react-testing,而不是 react-apollo,我使用的是 @apollo/react-hooks,我今天遇到了同样的错误。
只需将 @apollo/react-hooks 和 @apollo/react-testing 升级到 v4.0.0 即可解决我的问题。
推荐阅读
- scala - 可以在 Spark Cluster 中两个不同 rdd 生成数据 shuffle 的 map fun 中执行 map fun 吗?
- javascript - 如何在 nivo 图表设置中添加自定义颜色主题
- javascript - 如何使用 ssl 客户端身份验证 fetch()?
- java - 为什么我的 API 没有为我的 minecraft-Plugin 加载?
- cookiecutter-django - 只是为了确定:cookiecutter-django 没有注销端点,对吗?
- c - 指针存储哪些地址?
- python - 当所有元素都已经填满时打破游戏
- html - Pug 没有将查询中的 JSON 呈现到表中
- python - Raspberry 启动脚本未运行
- swift - 根据条件从 JSON 中获取值