Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Refactor example screen #88

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 26 additions & 30 deletions app/scenes/ExampleScreen/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import { Button, Platform, View, ActivityIndicator } from 'react-native';
import { connect } from 'react-redux';
import { compose } from 'redux';
import { PropTypes } from 'prop-types';
import styled from 'styled-components/native';
import { createStructuredSelector } from 'reselect';
import { injectIntl } from 'react-intl';
import React, { useEffect } from 'react';

import AppContainer from '@atoms/Container';
import SimpsonsLoveWednesday from '@organisms/SimpsonsLoveWednesday';
Expand Down Expand Up @@ -44,36 +44,32 @@ const instructions = Platform.select({
'Double tap R on your keyboard to reload,\nShake or press menu button for dev menu.'
});

class ExampleScreen extends React.Component {
componentDidMount() {
this.requestFetchUser()();
}

requestFetchUser = () => () => {
this.props.fetchUser();
const ExampleScreen = props => {
const requestFetchUser = () => {
props.fetchUser();
};

render() {
return (
<Container>
{this.props.userIsLoading ? (
<ActivityIndicator testID="loader" size="large" color="#0000ff" />
) : (
<View testID="example-container-content">
<SimpsonsLoveWednesday
instructions={instructions}
userErrorMessage={this.props.userErrorMessage}
user={this.props.user}
/>
<CustomButtonParentView>
<Button onPress={this.requestFetchUser()} title="Refresh" />
</CustomButtonParentView>
</View>
)}
</Container>
);
}
}
useEffect(() => {
requestFetchUser();
}, []);
return (
<Container>
{props.userIsLoading ? (
<ActivityIndicator testID="loader" size="large" color="#0000ff" />
) : (
<View testID="example-container-content">
<SimpsonsLoveWednesday
instructions={instructions}
userErrorMessage={props.userErrorMessage}
user={props.user}
/>
<CustomButtonParentView>
<Button onPress={requestFetchUser} title="Refresh" />
</CustomButtonParentView>
</View>
)}
</Container>
);
};

ExampleScreen.propTypes = {
user: PropTypes.object,
Expand Down
2 changes: 1 addition & 1 deletion app/scenes/ExampleScreen/saga.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { put, call, takeLatest } from 'redux-saga/effects';
import { get } from 'lodash';
import get from 'lodash/get';
import { getUser } from '@app/services/userService';
import { exampleScreenActions, exampleScreenTypes } from './reducer';

Expand Down
12 changes: 5 additions & 7 deletions app/scenes/ExampleScreen/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,23 @@
import React from 'react';
import { renderProvider } from 'app/utils/testUtils';
import { ExampleScreenTest } from '../index';

const setupJest = () => ({ submitSpy: jest.fn() });
describe('<ExampleScreen /> Container tests', () => {
let submitSpy;

beforeAll(() => {
submitSpy = jest.fn();
});

it('should render and match the snapshot', () => {
const { submitSpy } = setupJest();
const baseElement = renderProvider(
<ExampleScreenTest fetchUser={submitSpy} />
);
expect(baseElement).toMatchSnapshot();
});

it('should fetch the user data on mount', () => {
const { submitSpy } = setupJest();
renderProvider(<ExampleScreenTest fetchUser={submitSpy} />);
expect(submitSpy).toHaveBeenCalled();
});
it('should render ActivityIndicator if userIsLoading is true', () => {
const { submitSpy } = setupJest();
const { getByTestId } = renderProvider(
<ExampleScreenTest fetchUser={submitSpy} userIsLoading />
);
Expand All @@ -36,6 +33,7 @@ describe('<ExampleScreen /> Container tests', () => {
});

it('should not render ActivityIndicator if userIsLoading is false, should instead render exampleContainerContent', () => {
const { submitSpy } = setupJest();
const { getByTestId } = renderProvider(
<ExampleScreenTest fetchUser={submitSpy} userIsLoading={false} />
);
Expand Down
23 changes: 13 additions & 10 deletions app/scenes/ExampleScreen/tests/reducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,53 @@ import {
} from '../reducer';

/* eslint-disable default-case, no-param-reassign */
const setupState = () => ({
state: initialState,
user: 'Mohammed Ali Chherawalla'
});
describe('Tests for reducers used in the ExampleScreen', () => {
let state;
beforeEach(() => {
state = initialState;
});

it('should return the initial state', () => {
const { state } = setupState();
expect(exampleContainerReducer(undefined, {})).toEqual(state);
});

it('should ensure that userLoading = true when an action of type REQUEST_FETCH_USER is dispatched', () => {
const { state, user } = setupState();
const expectedResult = state
.set('userIsLoading', true)
.set('userErrorMessage', null);
expect(
exampleContainerReducer(state, {
type: exampleScreenTypes.REQUEST_FETCH_USER,
user: 'Mohammed Ali Chherawalla'
user
})
).toEqual(expectedResult);
});

it('should ensure that the user data is present and userLoading = false when SUCCESS_FETCH_USER is dispatched', () => {
const { state, user } = setupState();
const expectedResult = state
.set('user', { name: 'Mohammed Ali Chherawalla' })
.set('user', { name: user })
.set('userIsLoading', false)
.set('userErrorMessage', null);
expect(
exampleContainerReducer(state, {
type: exampleScreenTypes.SUCCESS_FETCH_USER,
user: { name: 'Mohammed Ali Chherawalla' }
user: { name: user }
})
).toEqual(expectedResult);
});

it('should ensure that the userErrorMessage has some data and userLoading = false when FAILURE_FETCH_USER is dispatched', () => {
const { state } = setupState();
const expectedResult = state
.set('user', {})
.set('userIsLoading', false)
.set('userErrorMessage', 'There was some error bro');
.set('userErrorMessage', 'There was some error');
expect(
exampleContainerReducer(state, {
type: exampleScreenTypes.FAILURE_FETCH_USER,
errorMessage: 'There was some error bro'
errorMessage: 'There was some error'
})
).toEqual(expectedResult);
});
Expand Down
31 changes: 16 additions & 15 deletions app/scenes/ExampleScreen/tests/selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,40 @@ import {
selectUserErrorMessage
} from '../selectors';

describe('Tests for selectors to get data from state for the ExampleScreen', () => {
let mockedState;
let username;
let userIsLoading;
let userErrorMessage;

beforeEach(() => {
username = 'Mohammed Ali Chherawalla';
userErrorMessage = 'Some error';
userIsLoading = false;

mockedState = {
const setupMockedState = () => {
const username = 'Mohammed Ali Chherawalla';
const userErrorMessage = 'Some error';
const userIsLoading = false;
return {
username,
userErrorMessage,
userIsLoading,
mockedState: {
example: fromJS({
user: {
username
},
userErrorMessage,
userIsLoading
})
};
});

}
};
};
describe('Tests for selectors to get data from state for the ExampleScreen', () => {
it('should select the user state', () => {
const { mockedState, username } = setupMockedState();
const userSelector = selectUser();
expect(userSelector(mockedState)).toEqual({ username });
});

it('should select userIsLoading', () => {
const { mockedState, userIsLoading } = setupMockedState();
const userIsLoadingSelector = selectUserIsLoading();
expect(userIsLoadingSelector(mockedState)).toEqual(userIsLoading);
});

it('should select the userErrorMessage', () => {
const { mockedState, userErrorMessage } = setupMockedState();
const userErrorMessageSelector = selectUserErrorMessage();
expect(userErrorMessageSelector(mockedState)).toEqual(userErrorMessage);
});
Expand Down