From 33c833907d4dede2dcec725f309d631d3408e63e Mon Sep 17 00:00:00 2001 From: HIMANSHU Date: Fri, 12 Apr 2024 02:51:53 +0530 Subject: [PATCH 1/2] Refactored root screen --- app/scenes/RootScreen/index.js | 35 +++++++++------------ app/scenes/RootScreen/reducer.js | 9 +++--- app/scenes/RootScreen/saga.js | 4 +-- app/scenes/RootScreen/tests/index.test.js | 13 +++----- app/scenes/RootScreen/tests/reducer.test.js | 17 +++++++--- 5 files changed, 39 insertions(+), 39 deletions(-) diff --git a/app/scenes/RootScreen/index.js b/app/scenes/RootScreen/index.js index 963caae..1351e91 100644 --- a/app/scenes/RootScreen/index.js +++ b/app/scenes/RootScreen/index.js @@ -1,30 +1,23 @@ -import React, { Component } from 'react'; import { connect } from 'react-redux'; import { PropTypes } from 'prop-types'; -import NavigationService from '@services/NavigationService'; import AppNavigator from '@navigators/AppNavigator'; import Container from '@atoms/Container'; - +import React, { useEffect } from 'react'; +import { setRefForTopLevelNavigtor } from '@app/services/NavigationService'; import { rootScreenActions } from './reducer'; -export class RootScreen extends Component { - componentDidMount() { - // Run the startup saga when the application is starting - this.props.startup(); - } - - setRefForTopLevelNavigtor = navigatorRef => { - NavigationService.setTopLevelNavigator(navigatorRef); - }; +const RootScreen = props => { + useEffect(() => { + // Run the startup function when the component mounts + props.startup(); + }, []); - render() { - return ( - - - - ); - } -} + return ( + + + + ); +}; RootScreen.propTypes = { startup: PropTypes.func @@ -33,5 +26,5 @@ RootScreen.propTypes = { const mapDispatchToProps = dispatch => ({ startup: () => dispatch(rootScreenActions.startup()) }); - export default connect(null, mapDispatchToProps)(RootScreen); +export { RootScreen as RootScreenTest }; diff --git a/app/scenes/RootScreen/reducer.js b/app/scenes/RootScreen/reducer.js index bb30247..702a595 100644 --- a/app/scenes/RootScreen/reducer.js +++ b/app/scenes/RootScreen/reducer.js @@ -1,6 +1,7 @@ import { createActions } from 'reduxsauce'; import { fromJS } from 'immutable'; import produce from 'immer'; +import get from 'lodash/get'; export const { Types: rootScreenTypes, Creators: rootScreenActions @@ -15,8 +16,8 @@ export const initialState = fromJS({ export const rootContainerReducer = (state = initialState, action) => produce(state, () => { - switch (action.type) { - default: - return state; - } + const stateReturn = { + [rootScreenTypes.STARTUP]: state + }; + return get(stateReturn, [action.type], state); }); diff --git a/app/scenes/RootScreen/saga.js b/app/scenes/RootScreen/saga.js index 0eadf9b..e9fa3e9 100644 --- a/app/scenes/RootScreen/saga.js +++ b/app/scenes/RootScreen/saga.js @@ -1,12 +1,12 @@ import { takeLatest } from 'redux-saga/effects'; -import NavigationService from '@app/services/NavigationService'; +import { navigateAndReset } from '@app/services/NavigationService'; import { rootScreenTypes } from './reducer'; /** * The startup saga is the place to define behavior to execute when the application starts. */ export function* startup() { - setTimeout(() => NavigationService.navigateAndReset('MainScreen'), 1000); + setTimeout(() => navigateAndReset('MainScreen'), 1000); } export default function* startUpSaga() { diff --git a/app/scenes/RootScreen/tests/index.test.js b/app/scenes/RootScreen/tests/index.test.js index d363424..a93ddc6 100644 --- a/app/scenes/RootScreen/tests/index.test.js +++ b/app/scenes/RootScreen/tests/index.test.js @@ -6,26 +6,23 @@ import React from 'react'; import { renderWithIntl } from 'app/utils/testUtils'; -import { RootScreen as RootScreenTest } from '../index'; - +import { RootScreenTest } from '../index'; +export const setupJest = () => ({ submitSpy: jest.fn() }); describe(' container', () => { - let submitSpy; - - beforeAll(() => { - submitSpy = jest.fn(); - }); - it('should render and match the snapshot', () => { + const { submitSpy } = setupJest(); const baseElement = renderWithIntl(); expect(baseElement).toMatchSnapshot(); }); it('should call the startup prop on mount', () => { + const { submitSpy } = setupJest(); renderWithIntl(); expect(submitSpy).toHaveBeenCalled(); }); it('should not render rootSceen Container', () => { + const { submitSpy } = setupJest(); const { getByTestId } = renderWithIntl( ); diff --git a/app/scenes/RootScreen/tests/reducer.test.js b/app/scenes/RootScreen/tests/reducer.test.js index 40243db..90666cb 100644 --- a/app/scenes/RootScreen/tests/reducer.test.js +++ b/app/scenes/RootScreen/tests/reducer.test.js @@ -6,16 +6,15 @@ import { /* eslint-disable default-case, no-param-reassign */ describe('Tests for RootScreen reducers', () => { - let state; - beforeEach(() => { - state = initialState; - }); + const setupTests = () => ({ state: initialState }); it('should return the initial state', () => { + const { state } = setupTests(); expect(rootContainerReducer(undefined, {})).toEqual(state); }); it('should return the initial state when an action of type STARTUP is dispatched', () => { + const { state } = setupTests(); // since startup is called to initiate screen navigation the store should remain intact expect( rootContainerReducer(state, { @@ -23,4 +22,14 @@ describe('Tests for RootScreen reducers', () => { }) ).toEqual(state); }); + + it('should return the initial state when an action of type NONEXIST is dispatched', () => { + const { state } = setupTests(); + // since startup is called to initiate screen navigation the store should remain intact + expect( + rootContainerReducer(state, { + type: rootScreenTypes.NONEXIST + }) + ).toEqual(state); + }); }); From 262ae8fc0ad2ec0bd25b34b4d32c8575f76a9fba Mon Sep 17 00:00:00 2001 From: HIMANSHU Date: Fri, 12 Apr 2024 16:04:23 +0530 Subject: [PATCH 2/2] json to switch reducer.js --- app/scenes/RootScreen/reducer.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/app/scenes/RootScreen/reducer.js b/app/scenes/RootScreen/reducer.js index 702a595..bb30247 100644 --- a/app/scenes/RootScreen/reducer.js +++ b/app/scenes/RootScreen/reducer.js @@ -1,7 +1,6 @@ import { createActions } from 'reduxsauce'; import { fromJS } from 'immutable'; import produce from 'immer'; -import get from 'lodash/get'; export const { Types: rootScreenTypes, Creators: rootScreenActions @@ -16,8 +15,8 @@ export const initialState = fromJS({ export const rootContainerReducer = (state = initialState, action) => produce(state, () => { - const stateReturn = { - [rootScreenTypes.STARTUP]: state - }; - return get(stateReturn, [action.type], state); + switch (action.type) { + default: + return state; + } });