Skip to content

Commit

Permalink
Merge pull request #77 from wednesday-solutions/feature/rootscreen-re…
Browse files Browse the repository at this point in the history
…factor

Refactored root screen
  • Loading branch information
alichherawalla authored Apr 12, 2024
2 parents 230157c + 262ae8f commit 9f18cdc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 35 deletions.
35 changes: 14 additions & 21 deletions app/scenes/RootScreen/index.js
Original file line number Diff line number Diff line change
@@ -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 (
<Container testID="root-screen">
<AppNavigator />
</Container>
);
}
}
return (
<Container testID="root-screen">
<AppNavigator ref={setRefForTopLevelNavigtor} />
</Container>
);
};

RootScreen.propTypes = {
startup: PropTypes.func
Expand All @@ -33,5 +26,5 @@ RootScreen.propTypes = {
const mapDispatchToProps = dispatch => ({
startup: () => dispatch(rootScreenActions.startup())
});

export default connect(null, mapDispatchToProps)(RootScreen);
export { RootScreen as RootScreenTest };
4 changes: 2 additions & 2 deletions app/scenes/RootScreen/saga.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
13 changes: 5 additions & 8 deletions app/scenes/RootScreen/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<HomeScreen /> container', () => {
let submitSpy;

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

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

it('should call the startup prop on mount', () => {
const { submitSpy } = setupJest();
renderWithIntl(<RootScreenTest startup={submitSpy} />);
expect(submitSpy).toHaveBeenCalled();
});

it('should not render rootSceen Container', () => {
const { submitSpy } = setupJest();
const { getByTestId } = renderWithIntl(
<RootScreenTest startup={submitSpy} />
);
Expand Down
17 changes: 13 additions & 4 deletions app/scenes/RootScreen/tests/reducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,30 @@ 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, {
type: rootScreenTypes.STARTUP
})
).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);
});
});

0 comments on commit 9f18cdc

Please sign in to comment.