Javascript ReactJS/Ezyme:如何测试容器是否获得数据
如何测试含有酶和摩卡咖啡的反应容器?我正在使用容器为组件提供一些数据。这就是我的容器和组件的外观: /imports/ui/components/example.jsxJavascript ReactJS/Ezyme:如何测试容器是否获得数据,javascript,reactjs,meteor,mocha.js,enzyme,Javascript,Reactjs,Meteor,Mocha.js,Enzyme,如何测试含有酶和摩卡咖啡的反应容器?我正在使用容器为组件提供一些数据。这就是我的容器和组件的外观: /imports/ui/components/example.jsx import React, { Component } from 'react' import { Header, Divider, Container, Segment, Loader } from 'semantic-ui-react' class Example extends Component { rende
import React, { Component } from 'react'
import { Header, Divider, Container, Segment, Loader } from 'semantic-ui-react'
class Example extends Component {
render() {
const { data, isLoading } = this.props,
if (isLoading)
return (<Loader active inverted size='massive' className='animated fadeIn' />)
return (
<Container text className='m-t-1 m-b-1 animated bounceInDown'>
<Segment raised padded>
<Header as='h1' floated='right'>{ data.title }</Header>
<Divider clearing />
</Segment>
</Container>
)
}
}
import Example from '/imports/ui/components/example.jsx'
export default createContainer((props) => {
const id = props.params.id,
subscription = Meteor.subscribe('something', id),
isLoading = !subscription.ready(),
data = Collection.find().fetch()
return {
data: data,
isLoading
}
}, Example)
现在,我首先想测试一下,如果数据根本没有加载,是否显示
组件,到目前为止,这是有效的
import React from 'react'
import { mount } from 'enzyme'
import { expect } from 'chai'
// Import Container
import Example from '/imports/ui/containers/example.js'
describe('Example', () => {
describe('component', () => {
describe('<Example />', () => {
it('should show the <Loader /> component by default', () => {
const wrapper = mount(<Example params={ { id: 'ueRunYgz4gwHquiYK' } } />),
container = wrapper.first('div'),
loader = wrapper.find('Loader')
expect(container).to.have.length(1)
expect(loader).to.have.length(1)
})
it('should show the <Container /> component when data has loaded', () => {
const wrapper = mount(<Example params={ { id: diseaseID } } />),
container = wrapper.find('Example')
expect(container).to.have.length(1)
})
})
})
})
从“React”导入React
从“酶”导入{mount}
从“chai”导入{expect}
//进口集装箱
从“/imports/ui/containers/Example.js”导入示例
描述('示例',()=>{
描述('组件',()=>{
描述(“”,()=>{
它('默认情况下应显示组件',()=>{
const wrapper=mount(),
container=wrapper.first('div'),
loader=wrapper.find('loader')
expect(container).to.have.length(1)
expect(loader).to.have.length(1)
})
它('加载数据时应显示组件',()=>{
const wrapper=mount(),
container=wrapper.find('Example')
expect(container).to.have.length(1)
})
})
})
})
但是第二个测试失败了,因为我不知道如何将
isLoading
设置为false
值示例作为道具接收isLoading
。你能用propisLoading={false}
挂载Example
吗
const wrapper = mount(
<Example
params={{id: diseaseID}}
isLoading={false}
/>
), container = wrapper.find('Example')
const wrapper=mount(
),container=wrapper.find('Example')
否,因为此道具将由容器使用,而不是由组件使用,组件将是“下一级”…您是说示例
不接收并且不使用isLoading
道具吗?可能是错误的代码编写或错误的命名。。。有一个Example
组件和一个Example
容器,它将数据提供给组件。我正在将容器(!)导入测试。因此,设置道具会将其发送到容器。但是在那里它会得到“真实”值,所以isLoading仍然是true
Ah!很抱歉我错过了。我明白你在说什么了。你能模拟或使用sinon在流星上创建间谍吗?subscribe
param将subscription
属性设置为你想要的状态?我认为这是正确的方法,但我对这个测试内容还不熟悉,到目前为止我还没有使用sinon。你能举个简单的例子吗?那太好了!