gpt4 book ai didi

reactjs - react Apollo 错误 : No more mocked responses for the query: mutation

转载 作者:行者123 更新时间:2023-11-28 20:10:24 24 4
gpt4 key购买 nike

预期结果:

MockedProvider 应该模拟我的 createPost 突变。

实际结果:

Error: No more mocked responses for the query: mutation...

如何重现问题:

我有一个非常简单的存储库。我还创建了一个单独的分支,其中包含示例提交,这打破了 apollo 模拟提供程序。

1) 突变定义在这里:https://github.com/developer239/react-apollo-graphql/blob/create-post-integration-tests/src/modules/blog/gql.js#L23

export const CREATE_POST = gql`
mutation createPost($title: String!, $text: String!) {
createPost(title: $title, text: $text) {
id
title
text
}
}
`

2) 虚假请求在这里:https://github.com/developer239/react-apollo-graphql/blob/create-post-integration-tests/test/utils/gql-posts.js#L68

export const fakeCreatePostSuccess = {
request: {
query: CREATE_POST,
variables: {
title: 'Mock Title',
text: 'Mock lorem ipsum text. And another paragraph.',
}
},
result: {
data: {
createPost: {
id: '1',
title: 'Mock Title',
text: 'Mock lorem ipsum text. And another paragraph.',
},
},
},

3) 我正在测试的组件位于此处:https://github.com/developer239/react-apollo-graphql/blob/create-post-integration-tests/src/pages/Blog/PostCreate/index.js#L24

    <Mutation
mutation={CREATE_POST}
update={updatePostCache}
onCompleted={({ createPost: { id } }) => push(`/posts/${id}`)}
>
{mutate => (
<>
<H2>Create New Post</H2>
<PostForm submit={values => mutate({ variables: values })} />
</>
)}
</Mutation>

4) 失败的测试用例在这里:https://github.com/developer239/react-apollo-graphql/blob/create-post-integration-tests/src/pages/Blog/PostCreate/index.test.js#L33

  describe('on form submit', () => {
it('should handle success', async () => {
const renderer = renderApp(<App />, ROUTE_PATHS.createPost, [
fakeCreatePostSuccess,
])
const { formSubmitButton } = fillCreatePostForm(renderer)
fireEvent.click(formSubmitButton)
await waitForElement(() => renderer.getByTestId(POST_DETAIL_TEST_ID))
expect(renderer.getByTestId(POST_DETAIL_TEST_ID)).toBeTruthy()
})
})

看来我遵循了 official documentation 中的所有步骤但我仍然无法完成这项工作。你有什么建议吗? 🙂

最佳答案

在官方文档中说我们应该添加 addTypename={false}<MockedProvider> .当我查看错误消息时,我可以看到 __typename已添加到它要查找的查询中。

类似于:

{ Error: Network error: No more mocked responses for the query: query getDog($dogId: ID!) {
dog(dogId: $dogId) {
name
__typename
}
}

所以删除addTypename={false}之后我收到另一个错误:

Missing field __typename in {
"name": "dog"
}

现在添加 __typename对于我 mock 的回应,它开始工作了。

例如

const mocks = [
{
request: {
query: dogQuery,
variables: {
dogId: 1,
},
},
result: {
data: {
dog: {
name: 'dog',
__typename: 'Dog',
},
},
},
},
];

关于reactjs - react Apollo 错误 : No more mocked responses for the query: mutation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55904192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com