-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
137 additions
and
5 deletions.
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
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,38 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react-native'; | ||
import AddExerciseScreen from './AddExerciseScreen' | ||
|
||
jest.mock('mobx-react-lite', () => ({ | ||
observer: jest.fn((component) => component), | ||
})); | ||
|
||
jest.mock('react-native-toast-message', () => ({ | ||
show: jest.fn(), | ||
})); | ||
|
||
// Mock external dependencies | ||
jest.mock('../services/db', () => ({ | ||
addExercise: jest.fn(), | ||
})); | ||
|
||
describe('<AddExerciseScreen />', () => { | ||
let navigation: any; | ||
|
||
beforeEach(() => { | ||
navigation = { navigate: jest.fn() }; | ||
}); | ||
|
||
it('renders correctly', () => { | ||
const { getByTestId } = render(<AddExerciseScreen navigation={navigation} route={navigation.route} />); | ||
expect(getByTestId('result-muscle')).toBeTruthy(); | ||
expect(getByTestId('result-title')).toBeTruthy(); | ||
expect(getByTestId('result-createExercise')).toBeTruthy(); | ||
}); | ||
|
||
it('disables the save button when fields are empty', () => { | ||
const { getByTestId } = render(<AddExerciseScreen navigation={navigation} route={navigation.route} />); | ||
const saveButton = getByTestId('result-createExercise'); | ||
expect(saveButton.props.accessibilityState.disabled).toBe(true); | ||
}); | ||
|
||
}); |
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
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,89 @@ | ||
import React from 'react'; | ||
import { render, fireEvent, waitFor, act } from '@testing-library/react-native'; | ||
import EditResultScreen from './EditResultScreen'; | ||
import { Alert } from 'react-native'; | ||
|
||
// Mocking modules | ||
jest.mock('../services/db', () => ({ | ||
updateResult: jest.fn(), | ||
})); | ||
|
||
jest.mock('../services/db', () => ({ | ||
fetchExercises: jest.fn(() => Promise.resolve([{ title: 'Squats', type: 'Legs' }])), | ||
})); | ||
|
||
jest.mock('react-native-toast-message', () => ({ | ||
Toast: { | ||
show: jest.fn(), | ||
}, | ||
})); | ||
|
||
jest.spyOn(Alert, 'alert'); | ||
|
||
describe('EditResultScreen', () => { | ||
const mockNavigation = { navigate: jest.fn() }; | ||
const mockRoute = { | ||
params: { | ||
id: '1', | ||
date: new Date().toISOString(), | ||
exercise: 'Squats', | ||
muscleGroup: 'Legs', | ||
reps: 10, | ||
weight: 50, | ||
units: 'kg', | ||
}, | ||
}; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('renders correctly and fetches exercises', async () => { | ||
const { findByText } = render( | ||
<EditResultScreen navigation={mockNavigation} route={mockRoute} /> | ||
); | ||
|
||
expect(await findByText('Legs')).toBeTruthy(); // Muscle group dropdown | ||
expect(await findByText('Squats')).toBeTruthy(); // Exercise dropdown | ||
expect(await findByText('kg')).toBeTruthy(); // Exercise dropdown | ||
}); | ||
|
||
it('displays an error for invalid reps input', async () => { | ||
const { getByTestId, getByText } = render( | ||
<EditResultScreen navigation={mockNavigation} route={mockRoute} /> | ||
); | ||
|
||
await waitFor(() => { | ||
const repsInput = getByTestId('input-reps') | ||
expect(repsInput).toBeTruthy() | ||
}) | ||
|
||
act(() => { | ||
fireEvent.changeText(getByTestId('input-reps'), '-5') | ||
|
||
}) | ||
|
||
await waitFor(() => { | ||
expect(getByText('Error: Repetitions must be greater than 0')).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
it('displays an error for invalid weight input', async () => { | ||
const { getByTestId, getByText } = render( | ||
<EditResultScreen navigation={mockNavigation} route={mockRoute} /> | ||
); | ||
|
||
await waitFor(() => { | ||
const weightInput = getByTestId('input-weight') | ||
expect(weightInput).toBeTruthy() | ||
}) | ||
|
||
act(() => { | ||
fireEvent.changeText(getByTestId('input-weight'), 'abc'); | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(getByText('Error: Weight must be a number')).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
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