Skip to content

Commit

Permalink
added vite test
Browse files Browse the repository at this point in the history
  • Loading branch information
markfortes2025 committed Nov 16, 2024
1 parent 0b3263c commit 97a0d1a
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
111 changes: 111 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"devDependencies": {
"@testing-library/dom": "^10.4.0",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.0.1",
"@vitejs/plugin-react": "^4.3.1",
"@vitest/coverage-v8": "^2.1.1",
Expand Down
25 changes: 25 additions & 0 deletions src/FormComponent.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { push, ref } from 'firebase/database';
import { uploadBytes, getDownloadURL } from 'firebase/storage';
import FormComponent from './FormComponent';
import { vi } from 'vitest';
import '@testing-library/jest-dom';

vi.mock('firebase/database', async (importOriginal) => {
const actual = await importOriginal();
Expand Down Expand Up @@ -78,6 +79,30 @@ describe('FormComponent post upload', () => {
expect(screen.queryByText(/Invalid/i)).toBeNull();
});
});

it('should prevent submission with an invalid phone number format', async () => {
const mockImage = new File(['(⌐□_□)'], 'test-image.png', { type: 'image/png' });

render(<FormComponent user={mockUser} />, { wrapper: MemoryRouter });

fireEvent.change(screen.getByLabelText(/Name/i), { target: { value: 'Fern' } });
fireEvent.change(screen.getByLabelText(/Phone Number/i), { target: { value: 'invalid-phone' } }); // Invalid phone number
fireEvent.change(screen.getByLabelText(/Duration/i), { target: { value: '2' } });
fireEvent.change(screen.getByLabelText(/Price/i), { target: { value: '15.99' } });
fireEvent.change(screen.getByLabelText(/Care Details/i), { target: { value: 'Water daily' } });
fireEvent.change(screen.getByLabelText(/Upload Image/i), { target: { files: [mockImage] } });

fireEvent.click(screen.getByRole('button', { name: /Submit/i }));

await waitFor(() => {
expect(screen.getByText(/Invalid phone number format/i)).toBeInTheDocument();

expect(uploadBytes).not.toHaveBeenCalled();
expect(push).not.toHaveBeenCalled();
});
});





Expand Down

0 comments on commit 97a0d1a

Please sign in to comment.