【发布时间】:2018-06-12 05:15:01
【问题描述】:
这就是我通过 jestJS 使用 react-apollo 测试一个非常简单的 reactJS 组件的方式。我也在用jest的覆盖功能。
但报道告诉我,我缺少测试graphql() 中的options: (props) => ({ 行。
我应该如何正确地做到这一点?
Example.js
import React, { Component } from 'react'
import { graphql, compose } from 'react-apollo'
import { getPostsQuery } from './graphql/query'
export class Example extends Component {
render () {
const { data } = this.props
const { getPosts, loading } = data
return (
<div id="article">
{
getPosts.map(post => {
return (<div>{post.content}</div>)
})
}
</div>
)
}
}
export default compose(
graphql(
getPostsQuery, {
options: (props) => ({
variables: {
articleId: props.articleId
}
})
}
)
)(Example)
单元测试
import React from 'react'
import { shallow } from 'enzyme'
import { Example } from './components/Example'
describe('<Example />', () => {
it('should render #article element', () => {
wrapper = shallow(<Example data={data} />)
expect(wrapper.find('#article')).toHaveLength(1)
})
})
【问题讨论】:
标签: javascript reactjs unit-testing jestjs react-apollo