Skip to content

Commit

Permalink
Fix broken tests for todo component (#466)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pankajjs authored Oct 15, 2024
1 parent 9b8c6c4 commit 04bd7d1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
27 changes: 21 additions & 6 deletions __tests__/Goals/components/TodoComponent-text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<NavigationContainer>
Expand All @@ -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(
<NavigationContainer>
<TodoComponent navigation={navigationProp} />
<TodoComponent />
</NavigationContainer>,
);
const addButton = await getByTestId('addButton');
const addButton = getByText('Add');
fireEvent.press(addButton);
expect(navigationProp.navigate).toHaveBeenCalledWith('CreatingGoals');
expect(navigate).toHaveBeenCalledWith('CreatingGoals');
});
});
2 changes: 1 addition & 1 deletion jest-setup.js
Original file line number Diff line number Diff line change
@@ -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');

0 comments on commit 04bd7d1

Please sign in to comment.