Enzyme: Test component with style via props

Created on 13 Jun 2016  路  3Comments  路  Source: enzymejs/enzyme

Hi all,

I'm trying to test a component that has an object with an style via props.

The component:
render(){ return( <View > <Text style={this.style.title}>{this.props.rowData.title}</Text> </View> ) }

Inside my test, i have an object with the style and other for the data:
const styles = StyleSheet.create({ post: { alignItems: 'flex-start', alignSelf: 'stretch', padding: 10, }, title: { fontSize: 18 }, content: { fontSize: 14 } });

var data={ title:'This is the title', content:'This is the content' }

Here, i render the component:
const wrapper = shallow(<Post rowData={data} style={styles}/>);

So when i try to check if the component it's ok :
it("Should have a title", () => { //Title expect(wrapper.contains(<Text style={fontSize: 18} >This is the title</Text>)).to.equal(true); });

i've obtain an unexpected token error :
captura de pantalla 2016-06-13 a las 10 21 52

If i remove the style (from test and component) it works, so it seem that doesn't understand my styles object but the data object works perfectly.

Some ideas to make it work or anybody know what i'm doing wrong?

Thanks in advance!

invalid

Most helpful comment

Yep was that, thanks for answer with a literal object inside it works.

All 3 comments

And if you do it("Should have a title", () => { //Title expect(wrapper.contains(<Text style={{fontSize: 18}} >This is the title</Text>)).to.equal(true); });, does it work? You need to write an object literal inside the style={} :) This has nothing to do with your tests, it's just not JSX valid!

@AugustinLF is exactly right, your test contains a syntax error 馃憤 Closing as this isn't an actual issue with enzyme.

Yep was that, thanks for answer with a literal object inside it works.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aweary picture aweary  路  3Comments

thurt picture thurt  路  3Comments

AdamYahid picture AdamYahid  路  3Comments

rexonms picture rexonms  路  3Comments

blainekasten picture blainekasten  路  3Comments