Skip to content

Commit e30c130

Browse files
committed
Error component added
1 parent cccd6ea commit e30c130

File tree

4 files changed

+133
-1
lines changed

4 files changed

+133
-1
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import React from 'react';
2+
import { Provider } from 'react-redux';
3+
import createStore from 'redux-mock-store';
4+
import ReactTestRender from 'react-test-renderer';
5+
6+
import ErrorMessage from '~/components/ErrorMessage';
7+
8+
const mockStore = createStore();
9+
10+
const INITIAL_STATE = {
11+
error: {
12+
visible: true,
13+
message: "Message error test",
14+
}
15+
};
16+
17+
const INVISIBLE_STATE = {
18+
error: {
19+
visible: false,
20+
message: "Message error test",
21+
}
22+
};
23+
24+
let wrapper;
25+
let invisibleWrapper;
26+
27+
beforeEach(() => {
28+
wrapper = ReactTestRender.create(
29+
<Provider store={mockStore(INITIAL_STATE)}>
30+
<ErrorMessage />
31+
</Provider>,
32+
);
33+
34+
invisibleWrapper = ReactTestRender.create(
35+
<Provider store={mockStore(INVISIBLE_STATE)}>
36+
<ErrorMessage />
37+
</Provider>,
38+
);
39+
});
40+
41+
describe('ErrorMessage Component', () => {
42+
describe('Smoke tests', () => {
43+
it('Should render ErrorMessage component correctly', () => {
44+
expect(wrapper.toJSON()).toMatchSnapshot();
45+
});
46+
});
47+
48+
describe('Component structure', () => {
49+
it('Should render message and Icon error', () => {
50+
expect(wrapper.root.findAllByType('Text').length).toBe(2);
51+
});
52+
53+
it('Should not render component when visible prop is false', () => {
54+
expect(invisibleWrapper.root.findAllByType('Text').length).toEqual(0);
55+
});
56+
});
57+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
import { connect } from 'react-redux';
5+
import { bindActionCreators } from 'redux';
6+
import ErrorActions from '~/store/ducks/error';
7+
8+
import {
9+
Container, Error, HideButton, HideIcon,
10+
} from './styles';
11+
12+
const ErrorMessage = ({ error: { message, visible }, hideError }) => visible && (
13+
<Container>
14+
<Error>{message}</Error>
15+
<HideButton onPress={hideError}>
16+
<HideIcon />
17+
</HideButton>
18+
</Container>
19+
);
20+
21+
ErrorMessage.propTypes = {
22+
hideError: PropTypes.func.isRequired,
23+
error: PropTypes.shape({
24+
visible: PropTypes.bool.isRequired,
25+
message: PropTypes.string,
26+
}).isRequired,
27+
};
28+
29+
const mapStateToProps = state => ({
30+
error: state.error,
31+
});
32+
33+
const mapDispatchToProps = dispatch => bindActionCreators(ErrorActions, dispatch);
34+
35+
export default connect(
36+
mapStateToProps,
37+
mapDispatchToProps,
38+
)(ErrorMessage);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import styled from 'styled-components/native';
2+
import { Platform } from 'react-native';
3+
import Icon from 'react-native-vector-icons/FontAwesome';
4+
5+
import { colors, metrics } from '~/styles';
6+
7+
export const Container = styled.View`
8+
width: ${metrics.screenWidth - metrics.basePadding}px;
9+
position: absolute;
10+
top: ${Platform.OS === 'android'
11+
? metrics.baseMargin
12+
: metrics.baseMargin + 20}px;
13+
left: ${metrics.baseMargin}px;
14+
padding: ${metrics.baseMargin}px ${metrics.basePadding}px;
15+
background-color: ${colors.danger};
16+
font-size: 14px;
17+
border-radius: ${metrics.baseRadius};
18+
flex-direction: row;
19+
justify-content: space-between;
20+
align-items: center;
21+
`;
22+
23+
export const Error = styled.Text`
24+
flex: 1;
25+
color: ${colors.white};
26+
`;
27+
28+
export const HideButton = styled.TouchableOpacity``;
29+
30+
export const HideIcon = styled(Icon).attrs({
31+
size: 20,
32+
color: colors.white,
33+
name: 'times',
34+
})``;

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import '~/config/ReactotronConfig';
44
import '~/config/StatusBarConfig';
55

66
import { Provider } from 'react-redux';
7-
import store from './store';
7+
import store from '~/store';
88

99
import Routes from '~/routes';
1010

11+
import ErrorMessage from '~/components/ErrorMessage';
12+
1113
const App = () => (
1214
<Provider store={store}>
1315
<Routes />
16+
<ErrorMessage />
1417
</Provider>
1518
);
1619

0 commit comments

Comments
 (0)