Ruf*_*eng 6 unit-testing reactjs jestjs graphql react-apollo
我正在使用我在 apollo 中创建的缓存的一个组件,它存储在client
.
在某些时候,作为来自输入的回调,我执行以下调用:
const originalName = client.readQuery<NamesCached>({ query: NamesCached })!.names
Run Code Online (Sandbox Code Playgroud)
这直接返回一个对象数组。
但是,我不知道如何在我的组件中测试它。我的第一个假设是专门模拟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()
})
Run Code Online (Sandbox Code Playgroud)
对于这个测试,我使用react-testing-library
. 这是我目前最好的一次投篮...
jest.doMock('@/ApolloClient', () => ({
readQuery: jest.fn()
}))
const client = require('@/ApolloClient')
client.readQuery.mockImplementation(()=> Promise.resolve(names()))
Run Code Online (Sandbox Code Playgroud)
重要的是依赖于单个 IT,而不是顶部,因为还有其他测试正在使用客户端并正常工作。有了那个,我可以在调用时在测试中嘲笑,client.readQuery()
但随后在组件本身中又回到了它自己的原始状态
我的目标
找到一种client.readQuery
可以模拟并返回我正在寻找的数据的方法。我想在模拟中,但是任何其他可以用于单个或一组测试(没有所有测试)的解决方案都非常受欢迎。
我也尝试在顶部进行嘲笑,但是其他人都失败了,我无法重现以回到真正的实现
归档时间: |
|
查看次数: |
917 次 |
最近记录: |