Skip to content

Commit

Permalink
pre pull-request
Browse files Browse the repository at this point in the history
  • Loading branch information
kristo-baricevic committed Apr 5, 2024
1 parent a4af5be commit 828061e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 135 deletions.
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript',
'@babel/preset-react',
],
};

177 changes: 42 additions & 135 deletions frontend/src/pages/PatientManager/NewPatientForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@ jest.mock('../../config/envConfig', () => ({
}),
}));

import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import React from 'react';
// mock React's useState function
jest.mock('react', () => {
const originalReact = jest.requireActual('react'); // Import actual React module
return {
...originalReact, // Spread all original exports
useState: jest.fn(), // Override only useState
};
});


import { render } from '@testing-library/react';
import '@testing-library/jest-dom';
import NewPatientForm from './NewPatientForm';


// Define mock data
export interface PatientInfo {
ID?: string;
Expand Down Expand Up @@ -67,142 +79,37 @@ const mockSetAllPatientInfo = jest.fn();
const mockAllPatientInfo = [mockPatientInfo];

// At the top of your test file
jest.mock('axios');
import axios from 'axios';
const mockedAxios = axios as jest.Mocked<typeof axios>;
// jest.mock('axios');
// import axios from 'axios';
// const mockedAxios = axios as jest.Mocked<typeof axios>;

describe('NewPatientForm Component', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('submits form data using mocked API URL', async () => {
<NewPatientForm
patientInfo={mockPatientInfo}
setPatientInfo={mockSetPatientInfo}
allPatientInfo={mockAllPatientInfo}
setAllPatientInfo={mockSetAllPatientInfo}
/>

// await waitFor(() => {
// expect(screen.getByText(/submission success message/i)).toBeInTheDocument();

// expect(axios.post).toHaveBeenCalledWith('http://localhost:8000/api/listMeds/', expect.any(Object));
// });
});

it('renders and shows all fields', () => {
render(
<NewPatientForm
patientInfo={mockPatientInfo}
setPatientInfo={mockSetPatientInfo}
allPatientInfo={mockAllPatientInfo}
setAllPatientInfo={mockSetAllPatientInfo}
/>
)

// expect(screen.getByLabelText(/current state/i)).toBeInTheDocument();
// expect(screen.getByLabelText(/bipolar history/i)).toBeInTheDocument();
// Add assertions for each field you expect to be rendered
});

it('updates the Diagnosis field on user input', () => {
render(
<NewPatientForm
patientInfo={mockPatientInfo}
setPatientInfo={mockSetPatientInfo}
allPatientInfo={mockAllPatientInfo}
setAllPatientInfo={mockSetAllPatientInfo}
/>
);
// const diagnosisSelect = screen.getByLabelText(/current state/i);
// fireEvent.change(diagnosisSelect, { target: { value: 'Depressed' } });
// expect(diagnosisSelect).toHaveValue('Depressed');
});

it('calls setPatientInfo on form submit with updated values', async () => {
render(
<NewPatientForm
patientInfo={mockPatientInfo}
setPatientInfo={mockSetPatientInfo}
allPatientInfo={mockAllPatientInfo}
setAllPatientInfo={mockSetAllPatientInfo}
/>
);

// const submitButton = screen.getByRole('button', { name: /submit/i });
// fireEvent.click(submitButton);

// // Check if setPatientInfo was called.
// expect(mockSetPatientInfo).toHaveBeenCalled();
});

it('submits the form data to the backend', async () => {
mockedAxios.post.mockResolvedValue({ /* mock your expected response */ });

render(
<NewPatientForm
patientInfo={mockPatientInfo}
setPatientInfo={mockSetPatientInfo}
allPatientInfo={mockAllPatientInfo}
setAllPatientInfo={mockSetAllPatientInfo}
/>
);

// // Replace 'inputFieldName' and 'submitButton' with actual selectors as necessary
// const inputField = screen.getByLabelText(/current state/i);
// fireEvent.change(inputField, { target: { value: 'New Value' } });
// const submitButton = screen.getByRole('button', { name: /submit/i });
// fireEvent.click(submitButton);

// Wait for the expected asynchronous action to complete
// await waitFor(() => {
// expect(mockedAxios.post).toHaveBeenCalledWith(
// "http://localhost:8000/api/listMeds/",
// { Diagnosis: 'New Value' }
// );
// });
});

it('displays a validation error for empty required fields on submit', async () => {
render(
<NewPatientForm
patientInfo={mockPatientInfo}
setPatientInfo={mockSetPatientInfo}
allPatientInfo={mockAllPatientInfo}
setAllPatientInfo={mockSetAllPatientInfo}
/>
);

// Assuming Diagnosis is a required field and you have validation implemented
// const submitButton = screen.getByRole('button', { name: /submit/i });
// fireEvent.click(submitButton);
describe('NewPatientForm Component', () => {
beforeEach(() => {
// Clear all mocks before each test
jest.clearAllMocks();

// Reset useState mock to a clean state
const originalReact = jest.requireActual('react');
(React.useState as jest.Mock)
.mockImplementation((initial: any) => originalReact.useState(initial));
});

// await waitFor(() => {
// const validationError = screen.getByText(/Diagnosis is required/i); // Adjust based on your actual validation message
// expect(validationError).toBeInTheDocument();
// });
});

it('displays an error message on API failure', async () => {
mockedAxios.post.mockRejectedValue(new Error("Mock error"));

render(
<NewPatientForm
patientInfo={mockPatientInfo}
setPatientInfo={mockSetPatientInfo}
allPatientInfo={mockAllPatientInfo}
setAllPatientInfo={mockSetAllPatientInfo}
/>
);

const submitButton = screen.getByRole('button', { name: /submit/i });
fireEvent.click(submitButton);

await waitFor(() => {
const errorMessage = screen.getByText(/an error occurred/i); // Use your actual error message text
expect(errorMessage).toBeInTheDocument();
});
});

});
it('renders without crashing', () => {
(React.useState as jest.Mock)
.mockImplementationOnce(() => [false, jest.fn()]) // Mock for isPressed
.mockImplementationOnce(() => [false, jest.fn()]);
render(<NewPatientForm
patientInfo={mockPatientInfo}
setPatientInfo={mockSetPatientInfo}
allPatientInfo={mockAllPatientInfo}
setAllPatientInfo={mockSetAllPatientInfo}
/>);

// Look for a static element that should always be present
// For example, if your form always renders a submit button, check for that
// expect(screen.getByRole('button', { name: /submit/i })).toBeInTheDocument();
});
});

0 comments on commit 828061e

Please sign in to comment.