为React和Apollo编写测试

时间:2017-06-29 16:21:45

标签: reactjs graphql apollo react-apollo apollo-client

我正在尝试用React和Apollo编写一个单元测试。

我从https://dev-blog.apollodata.com/seamless-integration-for-graphql-and-react-6ffc0ad3fead

找到了一个例子

尝试时,我遇到了错误:

错误:

Uncaught (in react-apollo) Error: Network error: No more mocked responses for the query: query people {
      allPeople(first: 1) {
        people {
          name
          __typename
        }
        __typename
      }
    }

测试:

it('executes a query', (done) => {

  const query = gql` query people { allPeople(first: 1) { people { name } } }`;
  const data = { allPeople: { people: [ { name: 'Luke Skywalker' } ] } };
  const networkInterface = mockNetworkInterface({ request: { query }, result: { data } });
  const client = new ApolloClient({ networkInterface });

  const withGraphQL = graphql(query);

  class Container extends Component {
    componentWillReceiveProps(props) {
      expect(props.data.loading).to.be.false;
      expect(props.data.allPeople).to.deep.equal(data.allPeople);
      done();
    }
    render() {
      return null;
    }
  };

  const ContainerWithData = withGraphQL(Container);

  mount(<ApolloProvider client={client}><ContainerWithData /></ApolloProvider>);

});

1 个答案:

答案 0 :(得分:1)

我知道这是一个老问题,但也许有人像我一样来到这里:)这些__typename字段很可能是你的问题,查询最终看起来与你传递给你模拟界面的查询不同。该错误基本上意味着它找不到该查询的任何匹配模拟。

无论如何,这是这个测试的一个工作示例,更新为在Apollo 2中工作。

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
 {
     var text = ""

    if inSearchMode {

        text = filteredData[indexPath.row]

        let correctIndex = data.index(of:filteredData[indexPath.row])

        print("selected value is : \(correctIndex)")


    } else {

        text = data[indexPath.row]
    }


    performSegue(withIdentifier: "segue", sender: text)
 }
 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     if let secondController = segue.destination as? SecondController {
         secondController.sendedValue =  sender as! String
     }
 }

 class secondController:UIViewController
 {
   var sendedValue:String?
 }