diff --git a/app/services/tests/navigate.test.js b/app/services/tests/navigate.test.js new file mode 100644 index 0000000..55d9fa6 --- /dev/null +++ b/app/services/tests/navigate.test.js @@ -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 } + }); + }); +}); diff --git a/app/services/tests/navigateAndReset.test.js b/app/services/tests/navigateAndReset.test.js new file mode 100644 index 0000000..e63460c --- /dev/null +++ b/app/services/tests/navigateAndReset.test.js @@ -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 } + }); + }); +}); diff --git a/app/services/tests/setTopLevelNavigator.test.js b/app/services/tests/setTopLevelNavigator.test.js new file mode 100644 index 0000000..60d2644 --- /dev/null +++ b/app/services/tests/setTopLevelNavigator.test.js @@ -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(); + }); +}); diff --git a/app/services/userService.test.js b/app/services/tests/userService.test.js similarity index 94% rename from app/services/userService.test.js rename to app/services/tests/userService.test.js index 270b8a6..fc453f5 100644 --- a/app/services/userService.test.js +++ b/app/services/tests/userService.test.js @@ -1,6 +1,6 @@ import MockAdapter from 'axios-mock-adapter'; import { getApiClient } from 'app/utils/apiUtils'; -import { getUser } from './userService'; +import { getUser } from '../userService'; describe('UserService tests', () => { it('should make the api call to "/quotes?count=1"', async () => {