-
Notifications
You must be signed in to change notification settings - Fork 25
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
Feature/services refactoring #76
Changes from 7 commits
d0e4949
0b090b2
0d8dcdd
e99c2d9
e28acda
92e6ee8
28cabca
6d47b4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,41 +1,50 @@ | ||||||
/* eslint-disable sonarjs/no-duplicate-string */ | ||||||
/** | ||||||
* Test sagas | ||||||
*/ | ||||||
|
||||||
/* 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 set from 'lodash/set'; | ||||||
import rootScreenSaga, { startup } from '../saga'; | ||||||
import { rootScreenTypes } from '../reducer'; | ||||||
|
||||||
const NavigationService = '@app/services/NavigationService'; | ||||||
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(); | ||||||
}); | ||||||
const setupTests = () => ({ | ||||||
generator: rootScreenSaga(), | ||||||
submitSpy: jest.fn() | ||||||
}); | ||||||
|
||||||
Check failure on line 28 in app/scenes/RootScreen/tests/saga.test.js GitHub Actions / Tests' annotations (🧪 jest-coverage-report-action)Tests for RootScreen sagas > should ensure that the navigation service is called after waiting for 1000ms
|
||||||
it('should start task to watch for STARTUP action', () => { | ||||||
const { generator } = setupTests(); | ||||||
expect(generator.next().value).toEqual( | ||||||
takeLatest(rootScreenTypes.STARTUP, startup) | ||||||
); | ||||||
}); | ||||||
|
||||||
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 { submitSpy } = setupTests(); | ||||||
const method = startup(); | ||||||
NavigationService.navigateAndReset = submitSpy; | ||||||
set(NavigationService, 'navigateAndReset', submitSpy); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use of The use of - set(NavigationService, 'navigateAndReset', submitSpy);
+ NavigationService.navigateAndReset = submitSpy; Committable suggestion
Suggested change
|
||||||
method.next(); | ||||||
await timeout(650); | ||||||
expect(submitSpy).not.toHaveBeenCalled(); | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { NavigationActions } from '@react-navigation/compat'; | ||
import { navigate, setTopLevelNavigator } from '../NavigationService'; | ||
jest.mock('@react-navigation/compat', () => ({ | ||
NavigationActions: { | ||
navigate: jest.fn() | ||
} | ||
})); | ||
const navigatorRef = { goBack: 'goBack', dispatch: jest.fn() }; | ||
setTopLevelNavigator(navigatorRef); | ||
describe('navigate', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('dispatches navigation action with the correct routeName and params', () => { | ||
const routeName = '/test'; | ||
const params = { screen: 'MainScreen' }; | ||
NavigationActions.navigate.mockReturnValueOnce({ | ||
type: 'NAVIGATE_ACTION', | ||
payload: { routeName, params } | ||
}); | ||
navigate(routeName, params); | ||
expect(NavigationActions.navigate).toHaveBeenCalledWith({ | ||
routeName, | ||
params | ||
}); | ||
expect(navigatorRef.dispatch).toHaveBeenCalledWith({ | ||
type: 'NAVIGATE_ACTION', | ||
payload: { routeName, params } | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { StackActions } from '@react-navigation/compat'; | ||
import { setTopLevelNavigator, navigateAndReset } from '../NavigationService'; | ||
|
||
jest.mock('@react-navigation/compat', () => ({ | ||
StackActions: { | ||
replace: jest.fn() | ||
} | ||
})); | ||
const navigatorRef = { goBack: 'goBack', dispatch: jest.fn() }; | ||
setTopLevelNavigator(navigatorRef); | ||
describe('test navigateAndReset', () => { | ||
afterEach(() => { | ||
// Reset mocks after each test | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('dispatches stack action with the correct routeName and params', () => { | ||
const routeName = '/test'; | ||
const params = { screen: 'MainScreen' }; | ||
StackActions.replace.mockReturnValueOnce({ | ||
type: 'NAVIGATE_ACTION', | ||
payload: { routeName, params } | ||
}); | ||
navigateAndReset(routeName, params); | ||
expect(StackActions.replace).toHaveBeenCalledWith({ | ||
routeName, | ||
params | ||
}); | ||
expect(navigatorRef.dispatch).toHaveBeenCalledWith({ | ||
type: 'NAVIGATE_ACTION', | ||
payload: { routeName, params } | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import set from 'lodash/set'; | ||
import { setTopLevelNavigator } from '../NavigationService'; | ||
jest.mock('lodash/set', () => jest.fn()); | ||
describe('setTopLevelNavigator', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
it('sets the navigator object with the provided reference', () => { | ||
const navigatorObject = { | ||
navigator: null | ||
}; | ||
const navigatorRef = { goBack: 'goBack' }; | ||
setTopLevelNavigator(navigatorRef); | ||
expect(set).toHaveBeenCalledWith( | ||
navigatorObject, | ||
'navigator', | ||
navigatorRef | ||
); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mock setup for
NavigationService
is potentially incorrect.The mock setup for
NavigationService
uses a string instead of the actual module. This could lead to issues where the mock does not behave as expected because it's not linked to the actual module being tested. Consider importing the module and then mocking it.Committable suggestion