From f012b0b55b4f55de527bcc56329afd690fa50720 Mon Sep 17 00:00:00 2001 From: HIMANSHU Date: Mon, 15 Apr 2024 17:28:21 +0530 Subject: [PATCH 1/6] feat: Navigation Service fix --- app/navigators/AppNavigator.js | 4 ++-- app/scenes/RootScreen/tests/saga.test.js | 24 ++++++++++++------------ app/services/NavigationService.js | 21 +++++++++------------ 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/app/navigators/AppNavigator.js b/app/navigators/AppNavigator.js index d3baa1c..5a3f924 100644 --- a/app/navigators/AppNavigator.js +++ b/app/navigators/AppNavigator.js @@ -3,7 +3,7 @@ import { createStackNavigator } from '@react-navigation/stack'; import SplashScreen from '@scenes/SplashScreen/'; import ExampleScreen from '@scenes/ExampleScreen'; import { NavigationContainer } from '@react-navigation/native'; -import NavigationService from '../services/NavigationService'; +import { setTopLevelNavigator } from '@app/services/NavigationService'; const Stack = createStackNavigator(); /** * The root screen contains the application's navigation. @@ -12,7 +12,7 @@ const Stack = createStackNavigator(); */ export default function AppNavigator() { return ( - + diff --git a/app/scenes/RootScreen/tests/saga.test.js b/app/scenes/RootScreen/tests/saga.test.js index 0bed1dc..10e90c9 100644 --- a/app/scenes/RootScreen/tests/saga.test.js +++ b/app/scenes/RootScreen/tests/saga.test.js @@ -5,21 +5,22 @@ /* eslint-disable redux-saga/yield-effects */ import { takeLatest } from 'redux-saga/effects'; -import NavigationService from 'app/services/NavigationService'; +import { navigateAndReset } from '@app/services/NavigationService'; import { timeout } from 'app/utils/testUtils'; import rootScreenSaga, { startup } from '../saga'; import { rootScreenTypes } from '../reducer'; +jest.mock('@app/services/NavigationService', () => ({ + ...jest.requireActual('@app/services/NavigationService'), + navigateAndReset: jest.fn() +})); describe('Tests for RootScreen sagas', () => { - let generator; - let submitSpy; - - beforeEach(() => { - generator = rootScreenSaga(); - submitSpy = jest.fn(); + afterEach(() => { + jest.clearAllMocks(); }); it('should start task to watch for STARTUP action', () => { + const generator = rootScreenSaga(); expect(generator.next().value).toEqual( takeLatest(rootScreenTypes.STARTUP, startup) ); @@ -27,19 +28,18 @@ describe('Tests for RootScreen sagas', () => { it('should ensure that the navigation service is called after waiting for 1000ms', async () => { const method = startup(); - NavigationService.navigateAndReset = submitSpy; method.next(); await timeout(1000); - expect(submitSpy).toHaveBeenCalled(); + expect(navigateAndReset).toHaveBeenCalled(); + expect(navigateAndReset).toHaveBeenCalledWith('MainScreen'); }); it('should ensure that the navigation service is called after waiting for 1000ms', async () => { const method = startup(); - NavigationService.navigateAndReset = submitSpy; method.next(); await timeout(650); - expect(submitSpy).not.toHaveBeenCalled(); + expect(navigateAndReset).not.toHaveBeenCalled(); await timeout(200); - expect(submitSpy).not.toHaveBeenCalled(); + expect(navigateAndReset).not.toHaveBeenCalled(); }); }); diff --git a/app/services/NavigationService.js b/app/services/NavigationService.js index 49c90a2..42e8d33 100644 --- a/app/services/NavigationService.js +++ b/app/services/NavigationService.js @@ -1,19 +1,20 @@ import { NavigationActions, StackActions } from '@react-navigation/compat'; - /** * The navigation is implemented as a service so that it can be used outside of components, for example in sagas. * * @see https://reactnavigation.org/docs/en/navigating-without-navigation-prop.html */ -let navigator; +const navigatorObject = { + navigator: null +}; /** * This function is called when the RootScreen is created to set the navigator instance to use. */ -function setTopLevelNavigator(navigatorRef) { - navigator = navigatorRef; -} +const setTopLevelNavigator = navigatorRef => { + Object.assign(navigatorObject, { navigator: navigatorRef }); +}; /** * Call this function when you want to navigate to a specific route. @@ -22,7 +23,7 @@ function setTopLevelNavigator(navigatorRef) { * @param params Route parameters. */ function navigate(routeName, params) { - navigator.dispatch( + navigatorObject.navigator.dispatch( NavigationActions.navigate({ routeName, params @@ -40,7 +41,7 @@ function navigate(routeName, params) { * @param params Route parameters. */ function navigateAndReset(routeName, params) { - navigator.dispatch( + navigatorObject.navigator.dispatch( StackActions.replace({ routeName, params @@ -48,8 +49,4 @@ function navigateAndReset(routeName, params) { ); } -export default { - navigate, - navigateAndReset, - setTopLevelNavigator -}; +export { navigate, navigateAndReset, setTopLevelNavigator }; From 097c4a8794c273c9e3816591b8f7429d84ab8ac5 Mon Sep 17 00:00:00 2001 From: HIMANSHU Date: Mon, 15 Apr 2024 19:53:56 +0530 Subject: [PATCH 2/6] feat: workflow edit --- .github/workflows/build.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c8b7fe5..8e4c0fa 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,7 +1,8 @@ name: react-native-template on: - pull_request_target: - branches: [ master ] + pull_request: + branches: + - master jobs: install-and-test: runs-on: ubuntu-latest From 94bae6d1101c3f51b2cf57673fce4bd88c6eeb48 Mon Sep 17 00:00:00 2001 From: HIMANSHU Date: Mon, 15 Apr 2024 20:05:12 +0530 Subject: [PATCH 3/6] feat: workflow edit --- .github/workflows/build.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8e4c0fa..c9914b9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -8,6 +8,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + with: + fetch-depth: 0 - name: Install npm dependencies run: | yarn From 612f8fb99ba421d7996b938fab0366b5737c265c Mon Sep 17 00:00:00 2001 From: HIMANSHU Date: Mon, 15 Apr 2024 20:12:29 +0530 Subject: [PATCH 4/6] feat: use latest action --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c9914b9..43442f3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -7,7 +7,7 @@ jobs: install-and-test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 with: fetch-depth: 0 - name: Install npm dependencies From e7dd56633077281d148f13d3c6e4f1a1fa3809fd Mon Sep 17 00:00:00 2001 From: Mac Date: Tue, 16 Apr 2024 01:37:48 +0530 Subject: [PATCH 5/6] feat: explicit tests since jest-coverage cleans them --- .github/workflows/build.yml | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8191128..6f093a8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,11 +10,11 @@ jobs: steps: - uses: actions/checkout@v4 - name: Install npm dependencies - run: | - yarn + run: yarn + - name: Lint - run: | - npm run lint + run: npm run lint + - name: Test and generate coverage report uses: artiomtr/jest-coverage-report-action@v2.2.9 with: @@ -23,6 +23,10 @@ jobs: package-manager: yarn skip-step: install test-script: yarn test + + - name: Tests + run: npm run test + - name: SonarQube Scan uses: sonarsource/sonarqube-scan-action@master env: From 0b529aeb0b7c18766fe4cda693ed7889b1527051 Mon Sep 17 00:00:00 2001 From: Mac Date: Tue, 16 Apr 2024 01:45:37 +0530 Subject: [PATCH 6/6] feat: sonar report fix --- app/scenes/ExampleScreen/reducer.js | 2 +- sonar-project.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/scenes/ExampleScreen/reducer.js b/app/scenes/ExampleScreen/reducer.js index 85bc437..44bf660 100644 --- a/app/scenes/ExampleScreen/reducer.js +++ b/app/scenes/ExampleScreen/reducer.js @@ -41,7 +41,7 @@ export const exampleContainerReducer = (state = initialState, action) => produce(state, () => { switch (action.type) { case exampleScreenTypes.REQUEST_FETCH_USER: - return fetchUser(state, action); + return fetchUser(state); case exampleScreenTypes.SUCCESS_FETCH_USER: return successFetchUser(state, action); case exampleScreenTypes.FAILURE_FETCH_USER: diff --git a/sonar-project.properties b/sonar-project.properties index 1b4bb43..c710942 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,6 +1,6 @@ sonar.projectKey=wednesday-solutions_react-native-template_AY7hdnRSB2n8RRmGoU2M sonar.language=js -sonar.sources=app +sonar.sources=. sonar.tests=app sonar.exclusions=**/android/**,**/ios/**,**/node_modules/** sonar.test.inclusions=**/*.test.js