-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHero.test.tsx
More file actions
56 lines (45 loc) · 1.76 KB
/
Hero.test.tsx
File metadata and controls
56 lines (45 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { describe, it, expect, vi, afterEach } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { Hero } from './src/components/landing/Hero';
// Mock useRouter
const pushMock = vi.fn();
vi.mock('next/navigation', () => ({
useRouter: () => ({
push: pushMock,
prefetch: vi.fn(),
}),
}));
describe('Hero Component', () => {
afterEach(() => {
vi.clearAllMocks();
});
it('validates valid URL and navigates', async () => {
render(<Hero />);
const input = screen.getByPlaceholderText(/Enter your website URL/i);
const button = screen.getByText('Scan');
fireEvent.change(input, { target: { value: 'example.com' } });
fireEvent.click(button);
await waitFor(() => {
expect(pushMock).toHaveBeenCalledWith('/site/https%3A%2F%2Fexample.com');
});
});
it('validates invalid URL format', async () => {
render(<Hero />);
const input = screen.getByPlaceholderText(/Enter your website URL/i);
const button = screen.getByText('Scan');
fireEvent.change(input, { target: { value: 'not-a-url' } });
fireEvent.click(button);
await screen.findByText(/Please enter a valid website URL/i);
expect(pushMock).not.toHaveBeenCalled();
});
it('handles http prefix correctly', async () => {
render(<Hero />);
const input = screen.getByPlaceholderText(/Enter your website URL/i);
const button = screen.getByText('Scan');
fireEvent.change(input, { target: { value: 'http://test.com' } });
fireEvent.click(button);
await waitFor(() => {
expect(pushMock).toHaveBeenCalledWith('/site/http%3A%2F%2Ftest.com');
});
});
});