对Apollo进行单元测试,对突变模拟产生错误提示,“错误!该查询不再有模拟的响应:mutation`

时间:2019-05-20 03:11:36

标签: javascript unit-testing apollo react-apollo apollo-client

我已经使用MockProvider创建了针对阿波罗突变的单元测试。抛出错误:错误!该查询不再有模拟的响应:mutation createPost($ id:ID !, $ title:String,$ content:String,$ authorId:ID!){                 createPost(id:$ id,title:$ title,content:$ content,authorId:$ authorId){                   ID                   标题                   内容                   作者{                     名称                   }                 }               }               ,变量:{“ id”:“ 1558321438699”,“ title”:“”,“ content”:“”}

我创建了unitest:

const mocks = [
  {
    request: {
      query: CREATE_POST,
      variables: {
        id: '1557799396370',
        title: 'title123',
        content: 'content123',
        authorId: '1557377599679'
      }
    },
    result: {
      data: {
        createPost: {
          id: '1557799396370',
          title: 'title123',
          content: 'content123',
          author: {
            name: 'rinto'
          }
        }
      }
    }
  }
];

const props = {
  history: {
    push: jest.fn()
  },
  handleCloseModal: jest.fn()
};

describe('Test CreatePost', () => {

  it('Testing CreatePost final state:', async () => {
    const renderComponent = mount(
      <MockedProvider mocks={mocks} addTypename={false} >
        <CreatePost {...props} />
      </MockedProvider>
    );

    renderComponent.find(Form).simulate('submit', {
      preventDefault: jest.fn()
    });

    await waitForExpect(() => {

      renderComponent.update();

      console.log('renderComponent', renderComponent.debug());

      expect(renderComponent.find(Form).exists()).toBeTruthy();
    });
  });
});

graphql突变:

export const CREATE_POST = gql`
  mutation createPost($id: ID!, $title: String, $content: String, $authorId: ID!) {
    createPost(id: $id, title: $title, content: $content, authorId: $authorId) {
      id
      title
      content
      author {
        name
      }
    }
  }
`;

0 个答案:

没有答案