Redux 如何测试graphql apollo组件?如何在compose()中获得完整的覆盖率?
我在全面报道阿波罗组件方面遇到了问题。istanbul报告没有调用compose()中的函数。这些是Redux connect()函数和apollo graph()函数Redux 如何测试graphql apollo组件?如何在compose()中获得完整的覆盖率?,redux,graphql,enzyme,istanbul,react-apollo,Redux,Graphql,Enzyme,Istanbul,React Apollo,我在全面报道阿波罗组件方面遇到了问题。istanbul报告没有调用compose()中的函数。这些是Redux connect()函数和apollo graph()函数 export default compose ( ... connect(mapStateToProps, mapDispatchToProps), // <-- functions not covered graphql(builderQuery, { options: (ownProps) => { //
export default compose (
...
connect(mapStateToProps, mapDispatchToProps), // <-- functions not covered
graphql(builderQuery, {
options: (ownProps) => { // <-- function not covered
...
)(ComponentName);
导出默认值(
...
连接(mapStateToProps、mapDispatchToProps),//{//测试组合的graphql/redux容器
为您的设置尝试以下方法:
// mocks.js
import configureMockStore from 'redux-mock-store'
import { ApolloClient } from 'react-apollo'
import { mockNetworkInterface } from 'apollo-test-utils'
export const mockApolloClient = new ApolloClient({
networkInterface: mockNetworkInterface(),
})
export const createMockStore = configureMockStore()
这将使您能够正确测试容器:
// container-test.js
import { mount } from 'enzyme'
import { createMockStore, mockApolloClient } from 'mocks'
beforeEach(() => {
store = createMockStore(initialState)
wrapper = mount(
<ApolloProvider client={mockApolloClient} store={store}>
<Container />
</ApolloProvider>
)
})
it('maps state & dispatch to props', () => {
const props = wrapper.find('SearchResults').props()
const expected = expect.arrayContaining([
// These props come from an HOC returning my grapqhql composition
'selectedListing',
'selectedPin',
'pathname',
'query',
'bbox',
'pageNumber',
'locationSlug',
'selectListing',
'updateCriteria',
'selectPin',
])
const actual = Object.keys(props)
expect(actual).toEqual(expected)
})
测试组合graphql/redux容器
为您的设置尝试以下方法:
// mocks.js
import configureMockStore from 'redux-mock-store'
import { ApolloClient } from 'react-apollo'
import { mockNetworkInterface } from 'apollo-test-utils'
export const mockApolloClient = new ApolloClient({
networkInterface: mockNetworkInterface(),
})
export const createMockStore = configureMockStore()
这将使您能够正确测试容器:
// container-test.js
import { mount } from 'enzyme'
import { createMockStore, mockApolloClient } from 'mocks'
beforeEach(() => {
store = createMockStore(initialState)
wrapper = mount(
<ApolloProvider client={mockApolloClient} store={store}>
<Container />
</ApolloProvider>
)
})
it('maps state & dispatch to props', () => {
const props = wrapper.find('SearchResults').props()
const expected = expect.arrayContaining([
// These props come from an HOC returning my grapqhql composition
'selectedListing',
'selectedPin',
'pathname',
'query',
'bbox',
'pageNumber',
'locationSlug',
'selectListing',
'updateCriteria',
'selectPin',
])
const actual = Object.keys(props)
expect(actual).toEqual(expected)
})
我正在尝试使用react apollo示例中的redux test utils
中的createMockStore
。createMockStore
允许您针对给定的存储测试容器,并允许您触发调度。当前的问题是,模拟存储无法与mount()和shall()配合使用不知何故从上下文中删除了客户端。如果您有进一步的信息,请告诉我。@ChrisKong我认为浅装载不再正确。如果可能的话,尝试进行完全装载。我发现UTIL不是很有用,特别是当您开始需要测试update
和updateQueries
时。我正在尝试使用使用react apollo示例从redux test utils
中de>createMockStore
。createMockStore
允许您针对给定的存储测试容器,并允许您触发调度。当前的问题是,模拟存储不能与mount()和shall()一起使用不知何故从上下文中删除了客户端。如果您有进一步的信息,请告诉我。@ChrisKong我认为浅层装载不再正确。如果可能的话,尝试完全装载。我发现UTIL不是很有用,特别是当您开始需要测试update
和updateQueries
时。