Skip to content

Commit

Permalink
Added tests for Button Component
Browse files Browse the repository at this point in the history
- Added snapshot test for Text Component
- Updated tests for Text to use loop to search for style
  • Loading branch information
binoy14 authored and Monte9 committed Apr 3, 2017
1 parent 28c7c2f commit 42b8b8c
Show file tree
Hide file tree
Showing 6 changed files with 631 additions and 6 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 2
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"jest": "^18.1.0",
"react": "^15.4.2",
"react-addons-test-utils": "^15.4.2",
"react-dom": "^15.4.2"
"react-dom": "^15.4.2",
"react-native-vector-icons": "^4.0.0"
},
"jest": {
"preset": "react-native",
Expand Down
117 changes: 117 additions & 0 deletions src/buttons/__tests__/Button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import React from 'react';
import {shallow} from 'enzyme';
import Button from '../Button';
import colors from '../../config/colors';

describe('Button Component', () => {
it('should render without issues', () => {
const component = shallow(<Button />);

expect(component.length).toBe(1);
expect(component).toMatchSnapshot();
});

it('should show loading indicator', () => {
const component = shallow(<Button loading />);

expect(component.find('ActivityIndicator').length).toBe(1);
});

it('should have onPress event', () => {
const onPress = jest.fn();
const component = shallow(<Button onPress={onPress} />);

component.simulate('press');
expect(onPress).toHaveBeenCalled();
});

it('should render primary color', () => {
const component = shallow(<Button primary />);

expect(component.find('View').props().style[0].backgroundColor).toBe(colors.primary)
});

it('should render primary1 color', () => {
const component = shallow(<Button primary1 />);
const styles = component.find('View').props().style
let backgroundColorStyles = []
for(let i = 0; i < styles.length; i++) {
if(styles[i] && styles[i].hasOwnProperty('backgroundColor')) {
backgroundColorStyles.push(styles[i].backgroundColor);
}
}

expect(backgroundColorStyles[1]).toBe(colors.primary1)
});

it('should render primary2 color', () => {
const component = shallow(<Button primary2 />);
const styles = component.find('View').props().style
let backgroundColorStyles = []
for(let i = 0; i < styles.length; i++) {
if(styles[i] && styles[i].hasOwnProperty('backgroundColor')) {
backgroundColorStyles.push(styles[i].backgroundColor);
}
}

expect(backgroundColorStyles[1]).toBe(colors.primary2)
});

it('should render secondary color', () => {
const component = shallow(<Button secondary />);
const styles = component.find('View').props().style
let backgroundColorStyles = []
for(let i = 0; i < styles.length; i++) {
if(styles[i] && styles[i].hasOwnProperty('backgroundColor')) {
backgroundColorStyles.push(styles[i].backgroundColor);
}
}

expect(backgroundColorStyles[1]).toBe(colors.secondary)
});

it('should render secondary2 color', () => {
const component = shallow(<Button secondary2 />);
const styles = component.find('View').props().style
let backgroundColorStyles = []
for(let i = 0; i < styles.length; i++) {
if(styles[i] && styles[i].hasOwnProperty('backgroundColor')) {
backgroundColorStyles.push(styles[i].backgroundColor);
}
}

expect(backgroundColorStyles[1]).toBe(colors.secondary2)
});

it('should render secondary3 color', () => {
const component = shallow(<Button secondary3 />);
const styles = component.find('View').props().style
let backgroundColorStyles = []
for(let i = 0; i < styles.length; i++) {
if(styles[i] && styles[i].hasOwnProperty('backgroundColor')) {
backgroundColorStyles.push(styles[i].backgroundColor);
}
}

expect(backgroundColorStyles[1]).toBe(colors.secondary3)
});

it('should render custom background color', () => {
const component = shallow(<Button backgroundColor='#777' />);
const styles = component.find('View').props().style
let backgroundColorStyles = []
for(let i = 0; i < styles.length; i++) {
if(styles[i] && styles[i].hasOwnProperty('backgroundColor')) {
backgroundColorStyles.push(styles[i].backgroundColor);
}
}

expect(backgroundColorStyles[1]).toBe('#777')
});

it('should render title as text inside the button', () => {
const component = shallow(<Button title="My Button" />);

expect(component.find("View").props().children[2].props.children).toBe("My Button");
});
});
Loading

0 comments on commit 42b8b8c

Please sign in to comment.