From 04bd7d1f26fce7e985539d17a836a3e1d813b73a Mon Sep 17 00:00:00 2001 From: Pankaj Date: Tue, 15 Oct 2024 23:57:00 +0530 Subject: [PATCH] Fix broken tests for todo component (#466) * fix: tests * refactor: remove skip test * fix: test - remove skip from test - added mock to test navigation * fix: changed expected value * fix: restore all mocks after each test --- .../Goals/components/TodoComponent-text.tsx | 27 ++++++++++++++----- jest-setup.js | 2 +- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/__tests__/Goals/components/TodoComponent-text.tsx b/__tests__/Goals/components/TodoComponent-text.tsx index 7887800a..b0638ce7 100644 --- a/__tests__/Goals/components/TodoComponent-text.tsx +++ b/__tests__/Goals/components/TodoComponent-text.tsx @@ -3,7 +3,18 @@ import { render, fireEvent } from '@testing-library/react-native'; import TodoComponent from '../../../src/components/ToDoComponent/TodoComponent'; import { NavigationContainer } from '@react-navigation/native'; +jest.mock('@react-navigation/native', () => { + return { + ...jest.requireActual('@react-navigation/native'), + useNavigation: jest.fn(), + }; +}); + describe('TodoComponent', () => { + afterEach(() => { + jest.restoreAllMocks(); + }); + test('renders title correctly', () => { const { getByText } = render( @@ -24,15 +35,19 @@ describe('TodoComponent', () => { expect(addButton).toBeTruthy(); }); - test.skip('calls navigationProp.navigate when "Add" button is pressed', async () => { - const navigationProp = { navigate: jest.fn() }; - const { getByTestId } = render( + test('calls navigationProp.navigate when "Add" button is pressed', async () => { + const navigate = jest.fn(); + require('@react-navigation/native').useNavigation.mockReturnValue({ + navigate, + }); + + const { getByText } = render( - + , ); - const addButton = await getByTestId('addButton'); + const addButton = getByText('Add'); fireEvent.press(addButton); - expect(navigationProp.navigate).toHaveBeenCalledWith('CreatingGoals'); + expect(navigate).toHaveBeenCalledWith('CreatingGoals'); }); }); diff --git a/jest-setup.js b/jest-setup.js index 120782ec..d3bfbbdb 100644 --- a/jest-setup.js +++ b/jest-setup.js @@ -1,6 +1,6 @@ import { jest } from '@jest/globals'; import mockRNDeviceInfo from 'react-native-device-info/jest/react-native-device-info-mock'; -require('react-native-reanimated/lib/reanimated2/jestUtils').setUpTests(); +require('react-native-reanimated/src/jestUtils').setUpTests(); jest.mock('react-native-device-info', () => mockRNDeviceInfo); jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter');