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: 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/app/scenes/RootScreen/tests/saga.test.js b/app/scenes/RootScreen/tests/saga.test.js index b872d9d..8a682ff 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 { setTopLevelNavigator } 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(); - setTopLevelNavigator({ dispatch: submitSpy }); method.next(); - await timeout(2000); - expect(submitSpy).toHaveBeenCalled(); + await timeout(1000); + expect(navigateAndReset).toHaveBeenCalled(); + expect(navigateAndReset).toHaveBeenCalledWith('MainScreen'); }); it('should ensure that the navigation service is called after waiting for 1000ms', async () => { const method = startup(); - setTopLevelNavigator({ dispatch: 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 3bfe938..e962c7e 100644 --- a/app/services/navigationService.js +++ b/app/services/navigationService.js @@ -1,18 +1,19 @@ 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. */ export function setTopLevelNavigator(navigatorRef) { - navigator = navigatorRef; + Object.assign(navigatorObject, { navigator: navigatorRef }); } /** @@ -22,7 +23,7 @@ export function setTopLevelNavigator(navigatorRef) { * @param params Route parameters. */ export function navigate(routeName, params) { - navigator.dispatch( + navigatorObject.navigator.dispatch( NavigationActions.navigate({ routeName, params @@ -40,7 +41,7 @@ export function navigate(routeName, params) { * @param params Route parameters. */ export function navigateAndReset(routeName, params) { - navigator.dispatch( + navigatorObject.navigator.dispatch( StackActions.replace({ routeName, params 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