Skip to content

Instantly share code, notes, and snippets.

@orYoffe
Last active January 1, 2018 13:19
Show Gist options
  • Save orYoffe/676ad6311390c7c6f43605de5b671146 to your computer and use it in GitHub Desktop.
Save orYoffe/676ad6311390c7c6f43605de5b671146 to your computer and use it in GitHub Desktop.
Button.test.js functionality test
import React from 'react';
import { Platform, Text, TouchableHighlight, StyleSheet } from 'react-native';
import { shallow } from 'enzyme';
import Button from './Button';
const text = 'test button';
const styles = StyleSheet.create({
button: {
height: 20,
},
buttonText: {
color: '#04f',
fontSize: 16,
}
});
describe('Button component', () => {
it('receives props', () => {
const mockFunc = jest.fn();
const tree = shallow(
<Button text={text} onClick={mockFunc} style={styles.button} textStyle={styles.buttonText} />
);
const content = shallow(
<TouchableHighlight onPress={mockFunc} style={styles.button} >
<Text style={styles.buttonText}>{text}</Text>
</TouchableHighlight>
);
expect(tree.html()).toEqual(content.html());
});
it('trigger onClick callback', () => {
const mockFunc = jest.fn();
const tree = shallow(
<Button text={text} onClick={mockFunc} style={styles.button} textStyle={styles.buttonText} />
);
const btn = tree.find(TouchableHighlight);
btn.simulate('press'); // simulate native "press" event
expect(mockFunc.mock.calls.length).toEqual(1);
expect(mockFunc).toBeCalled();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment