简体   繁体   English

如何使用 readQuery/writeQuery 为 ApolloClient 缓存更新编写单元测试?

[英]How to write unit test for ApolloClient cache update using readQuery/writeQuery?

Given this contrived example React component using apollo-client useMutation hook with an update method to customize the cache update, how can I write a unit test to test the update method?鉴于这个人为的示例 React 组件使用 apollo-client useMutation挂钩和update方法来自定义缓存更新,我如何编写单元测试来测试update方法?

Because readQuery will return null if anything is query is not in the cache, I want a unit test to verify the read/writeQuery work as expected so we can prevent possible regressions.因为如果任何查询不在缓存中, readQuery将返回null ,所以我想要一个单元测试来验证读/写查询是否按预期工作,这样我们就可以防止可能的回归。

Ideally I would like to test the customUpdateFunction , but if that is not possible we can test the ExampleComponent in a way to verify the cache update works as expected.理想情况下,我想测试customUpdateFunction ,但如果这不可能,我们可以以某种方式测试ExampleComponent以验证缓存更新是否按预期工作。 As long as the result is adding some safety to the custom cache update in the useMutation update method.只要结果是在useMutation update方法中为自定义缓存更新增加了一些安全性。

function ExampleComponent() {
    const {data} = useMutation(REMOVE_ITEM_MUTATION, {
        variables: VARIABLES,
        update: customUpdateFunction,
    });
    
    return ...
}

function customUpdateFunction(cache, data) {
    const cachedData = readQuery({
        query: LIST_QUERY,
    });

    if (!cachedData) {
        return;
    }

    // Some immutable update to remove an item from a list
    const updatedData = immutableUpdate(cachedData, data)

    writeQuery({
        query: LIST_QUERY,
        data: updatedData,
    });
}

I don't understand what is readQuery and writeQuery in your code.我不明白您的代码中的readQuerywriteQuery是什么。

If instead of readQuery and writeQuery you can use cache.readQuery and cache.writeQuery , then you can use import { InMemoryCache } from '@apollo/client' to create cache and test it like this:如果您可以使用cache.readQuery cache.writeQuery readQuery writeQuery您可以使用import { InMemoryCache } from '@apollo/client'创建缓存并像这样测试它:

describe("customUpdateFunction", () => {
    it("should remove data from LIST_QUERY in the cache", async () => {
        //easy way to recive an initialCacheState is cache.extract() 
        const initialCacheState = ....
        const cache = new InMemoryCache().restore(initialCacheState);
        const data = .... // mutation result

        customUpdateFunction(cache, data)
        const cachedData = cache.readQuery({
            query: LIST_QUERY,
        });
        expect(cachedData.something).not.toContain(data.something);
        //or map array  
        //expect(cachedData.something.map(i=>i.id)).not.toContain(data.something.id);
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在cache.readQuery或cache.writeQuery中使用this.state.rent值而不是this.props.house.rent值 - How to use this.state.rent value instead of this.props.house.rent value in cache.readQuery or cache.writeQuery ApolloClient:如何在服务器上更新后同步Apollo缓存? - ApolloClient: How to synchronize Apollo cache after update on the server? 处理Graphql 变异更新,缓存读写查询,如果查询是动态的? - Handling Graphql Mutation update, cache read and writeQuery, if the query is dynamic? 如何在反应中使用 jest 编写单元测试用例? - How to write unit test case using jest in react? 如何使用带有Jest的酶在React Component中编写DataTable的单元测试? - How to write unit test for DataTable in React Component using Enzyme with Jest? Apollo useQuery 钩子不会使用 writeQuery() 方法从缓存中获取更新。 没有看到错误 - Apollo useQuery hook doesn't get update from cache with writeQuery() method. No errors seen 如何最好地使用 ApolloClient / InMemoryCache 并为 API 启用按需缓存? - How to best use ApolloClient / InMemoryCache and enable Cache on demand for API's? apollo-link-state writeQuery 不会更新缓存中的 ROOT_QUERY,防止重新渲染组件 - apollo-link-state writeQuery does't update ROOT_QUERY in cache, preventing re-render component 登录成功后如何更新ApolloClient授权头? - How to update ApolloClient authorization header after successful login? 如何使用 React、Jest 和 React-testing-library 为带有令牌的 api 调用编写单元测试? - How can I write unit test for api call with token using React, Jest and React-testing-library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM