有一个组件正在使用我在apollo中创建的缓存,该缓存存储在client
内。
在某个时候,作为输入的回调,我进行以下调用:
const originalName = client.readQuery<NamesCached>({ query: NamesCached })!.names
这直接返回对象数组。
但是,我不知道如何在我的组件中对此进行测试。我的第一个假设是专门模拟client.readQuery
以从我的__fixtures__
返回一个数组。那没用。
这是我最好的模拟镜头:
it('filters the name in the search', () => {
jest.doMock('@/ApolloClient', () => ({
readQuery: jest.fn()
}))
const client = require('@/ApolloClient')
client.readQuery.mockImplementation(()=> Promise.resolve(names()))
const { getByPlaceholderText, queryByText } = renderIndexNames()
const input = getByPlaceholderText('Search…')
fireEvent.change(input, { target: { value: 'Second Name' } })
expect(queryByText('Title for a name')).not.toBeInTheDocument()
})
对于此测试,我使用的是react-testing-library
。这是我迄今为止最好的镜头...
jest.doMock('@/ApolloClient', () => ({
readQuery: jest.fn()
}))
const client = require('@/ApolloClient')
client.readQuery.mockImplementation(()=> Promise.resolve(names()))
重要的是要依赖单个IT,而不是最顶层,因为还有其他测试正在使用客户端并且可以正常工作。
有了那个,我本可以在开发发票client.readQuery()
时在测试中嘲笑,但随后组件本身又回到其原始状态
我的目标
找到一种可以模拟client.readQuery
并返回我要查找的数据的方式。我以模拟的方式来考虑,但是任何其他可用于单个或一组测试(没有全部测试)的解决方案都将受到欢迎。
我也尝试过在顶部进行模拟,但是其他方法都失败了,我无法重现真实的实现方式