1

我有相当简单的测试设置,非常类似于 Apollo GraphQL 的教程 repo中的内容,并在他们的文档中进行了描述。

这是查询:

const GET_USER_WITH_SPEND_CONTROL = gql`
    query GetUserWithSpendControl($userToken: ID!) {
        me(token: $userToken) {
            firstName
            lastName
            spendControl {
                amountAvailable
                currencyCode
            }
        }
    }
`;

这是测试:

const mockUserSpendControl = {
  me: {
    firstName: "Andrii",
    lastName: "Gorishnii",
    spendControl: {
      amountAvailable: 1000,
      currencyCode: "USD",
    }
  }
};

describe('Account Summary', () => {
  afterEach(cleanup);
  it('renders account summary', async () => {
    const mocks = [
      {
        request: { query: GET_USER_WITH_SPEND_CONTROL },
        result: {
          data: mockUserSpendControl
        },
      },
    ];
    const { getByText } = await renderApollo(<AccountSummary/>, {
      mocks,
      cache,
      addTypename: false
    });
    await waitForElement(() => getByText(/Gorishnii/i));
  });
});

这个测试的错误之处在于它在不应该的情况下工作:GQL 模式声明了必需的变量userToken,但是如果我像这样将它添加到测试中:

...
request: { query: GET_USER_WITH_SPEND_CONTROL, variables: {userToken: "56aed"} },
...

它实际上没有通过测试。

有没有人在测试 React Apollo 组件时遇到过类似的问题?

PS我有更大的问题,我的原始查询是这样的:

const GET_USER_WITH_SPEND_CONTROL = gql`
    query GetUserWithSpendControl($userToken: ID!) {
        currentUserToken @client @export(as: "userToken")
        me(token: $userToken) {
            firstName
            lastName
            spendControl {
                amountAvailable
                currencyCode
            }
        }
    }
`;

而且我永远无法将模拟结果与使用从客户端缓存导出的变量的查询匹配。但这可能是我解决“更简单”问题后的下一个问题。

4

0 回答 0