-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from wednesday-solutions/feature/services-tests
feat: Added Tests for services
- Loading branch information
Showing
4 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { NavigationActions } from '@react-navigation/compat'; | ||
import { navigate, setTopLevelNavigator } from '@services/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 } | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { StackActions } from '@react-navigation/compat'; | ||
import { | ||
setTopLevelNavigator, | ||
navigateAndReset | ||
} from '@services/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 } | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { setTopLevelNavigator } from '@services/navigationService'; // Import the function to be tested | ||
|
||
describe('setTopLevelNavigator', () => { | ||
it('should update navigatorObject with the provided navigatorRef using mocked Object.assign', () => { | ||
const mockNavigatorRef = { navigator: 'test_navigator' }; | ||
const mockAssign = jest.spyOn(Object, 'assign'); | ||
setTopLevelNavigator(mockNavigatorRef); | ||
expect(mockAssign).toHaveBeenCalledTimes(1); | ||
mockAssign.mockRestore(); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
app/services/userService.test.js → app/services/tests/userService.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
efa2b17
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.
Coverage report
Test suite run success
52 tests passing in 24 suites.
Report generated by 🧪jest coverage report action from efa2b17