预期结果:
MockedProvider应该模拟我的createPost突变。
实际结果:
Error: No more mocked responses for the query: mutation...
如何重现该问题:
我有一个非常简单的存储库。我还用示例commit创建了一个单独的分支,这破坏了apollo模拟提供程序。
export const CREATE_POST = gql`
mutation createPost($title: String!, $text: String!) {
createPost(title: $title, text: $text) {
id
title
text
}
}
`
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>
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的所有步骤,但仍然无法完成这项工作。你有什么建议吗?
答案 0 :(得分:1)
在官方文档中,我们应在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
时,它开始起作用。
例如
const mocks = [
{
request: {
query: dogQuery,
variables: {
dogId: 1,
},
},
result: {
data: {
dog: {
name: 'dog',
__typename: 'Dog',
},
},
},
},
];