-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added configs for Jest library with typescript and written test cases…
… for DonorForm
- Loading branch information
anjaliputta
committed
Sep 30, 2024
1 parent
e1a0046
commit 9bab259
Showing
9 changed files
with
155 additions
and
144 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module.exports = { | ||
verbose: true, | ||
preset: 'ts-jest', | ||
testEnvironment: 'jsdom', | ||
roots: ["<rootDir>/src"], | ||
transform: { | ||
'^.+\\.(ts|tsx)$': 'ts-jest', | ||
}, | ||
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$", | ||
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"], | ||
moduleNameMapper: { | ||
'\\.(css|less|scss|sass)$': 'identity-obj-proxy' | ||
} | ||
}; |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,132 @@ | ||
import '@testing-library/jest-dom'; | ||
import { render, screen, fireEvent, waitFor } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import axios from 'axios'; | ||
import { act } from '@testing-library/react'; | ||
import DonorForm from '../Components/DonorForm'; | ||
|
||
jest.mock('axios'); | ||
const mockedAxios = axios as jest.Mocked<typeof axios>; | ||
|
||
describe('DonorForm', () => { | ||
beforeEach(() => { | ||
// Reset mocks before each test | ||
mockedAxios.post.mockReset(); | ||
}); | ||
|
||
it('renders correctly', () => { | ||
render(<DonorForm />); | ||
expect(screen.getByText(/Add Donor Details/i)).toBeInTheDocument(); | ||
expect(screen.getByLabelText(/First Name/i)).toBeInTheDocument(); | ||
expect(screen.getByLabelText(/Last Name/i)).toBeInTheDocument(); | ||
expect(screen.getByLabelText(/Contact/i)).toBeInTheDocument(); | ||
expect(screen.getByLabelText(/Email ID/)).toBeInTheDocument(); | ||
expect(screen.getByLabelText(/Address Line 1/i)).toBeInTheDocument(); | ||
expect(screen.getByLabelText(/State/i)).toBeInTheDocument(); | ||
expect(screen.getByLabelText(/City/i)).toBeInTheDocument(); | ||
expect(screen.getByLabelText(/Zip Code/i)).toBeInTheDocument(); | ||
expect(screen.getByLabelText(/Email Opt-in/i)).toBeInTheDocument(); | ||
expect(screen.getByRole('button', { name: /Add Donor/i })).toBeInTheDocument(); | ||
}); | ||
|
||
it('allows input to be entered', () => { | ||
render(<DonorForm />); | ||
const firstNameInput = screen.getByLabelText(/First Name/i); | ||
act(() => { | ||
userEvent.type(firstNameInput, 'John'); | ||
}); | ||
|
||
expect(firstNameInput).toHaveValue('John'); | ||
}); | ||
|
||
it('validates and submits the form data', async () => { | ||
render(<DonorForm />); | ||
const submitButton = screen.getByRole('button', { name: /Add Donor/i }); | ||
act(() => { | ||
// Fill out the form | ||
userEvent.type(screen.getByLabelText(/First Name/i), 'John'); | ||
userEvent.type(screen.getByLabelText(/Last Name/i), 'Doe'); | ||
userEvent.type(screen.getByLabelText(/Contact/i), '1234567890'); | ||
userEvent.type(screen.getByLabelText(/Email ID/), '[email protected]'); | ||
userEvent.type(screen.getByLabelText(/Address Line 1/i), '1234 Main St'); | ||
userEvent.type(screen.getByLabelText(/State/i), 'State'); | ||
userEvent.type(screen.getByLabelText(/City/i), 'City'); | ||
userEvent.type(screen.getByLabelText(/Zip Code/i), '12345'); | ||
userEvent.click(screen.getByLabelText(/Email Opt-in/i)); | ||
}) | ||
|
||
// Set up axios mock to simulate successful form submission | ||
mockedAxios.post.mockResolvedValue({ status: 201 }); | ||
|
||
act(() => { | ||
// Click submit | ||
userEvent.click(submitButton); | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(mockedAxios.post).toHaveBeenCalledWith( | ||
`${process.env.REACT_APP_BACKEND_API_BASE_URL}donor`, | ||
{ | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
contact: '1234567890', | ||
email: '[email protected]', | ||
addressLine1: '1234 Main St', | ||
addressLine2: '', | ||
state: 'State', | ||
city: 'City', | ||
zipcode: '12345', | ||
emailOptIn: true, | ||
}, | ||
); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText(/Donor added successfully!/i)).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('displays an error message on submission failure', async () => { | ||
render(<DonorForm />); | ||
act(() => { | ||
userEvent.type(screen.getByLabelText(/First Name/i), 'John'); | ||
userEvent.type(screen.getByLabelText(/Last Name/i), 'Doe'); | ||
userEvent.type(screen.getByLabelText(/Contact/i), '1234567890'); | ||
userEvent.type(screen.getByLabelText(/Email ID/), '[email protected]'); | ||
userEvent.type(screen.getByLabelText(/Address Line 1/i), '1234 Main St'); | ||
userEvent.type(screen.getByLabelText(/State/i), 'State'); | ||
userEvent.type(screen.getByLabelText(/City/i), 'City'); | ||
userEvent.type(screen.getByLabelText(/Zip Code/i), '12345'); | ||
userEvent.click(screen.getByLabelText(/Email Opt-in/i)); | ||
}) | ||
|
||
// Mock failure response | ||
mockedAxios.post.mockRejectedValue({ | ||
response: { | ||
data: { | ||
message: 'Error adding donor' | ||
} | ||
} | ||
}); | ||
|
||
act(() => { | ||
userEvent.click(screen.getByRole('button', { name: /Add Donor/i })); | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText(/Error adding donor/i)).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('shows validation errors when fields are incomplete', async () => { | ||
render(<DonorForm />); | ||
act(() => { | ||
userEvent.click(screen.getByRole('button', { name: /Add Donor/i })); | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText(/First Name is required/i)).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); | ||
|
This file was deleted.
Oops, something went wrong.
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 @@ | ||
import "@testing-library/jest-dom/extend-expect"; |