File size: 2,115 Bytes
7fd84a8 58973c7 7fd84a8 58973c7 7fd84a8 58973c7 a2e4e8f 58973c7 7fd84a8 58973c7 7fd84a8 58973c7 a2e4e8f 58973c7 |
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 57 58 59 60 61 |
import { render, screen, fireEvent, act } from '@testing-library/react';
import '@testing-library/jest-dom';
import DocumentInput from '../../components/DocumentInput';
describe('DocumentInput', () => {
const mockFetch = jest.fn();
global.fetch = mockFetch;
beforeEach(() => {
mockFetch.mockClear();
mockFetch.mockResolvedValue({ ok: true, json: () => Promise.resolve({}) });
});
test('submits URL and shows success message while keeping URL in input', async () => {
render(<DocumentInput />);
const input = screen.getByLabelText('Source Documentation');
const button = screen.getByText('Pull Source Docs');
await act(async () => {
await fireEvent.change(input, { target: { value: 'https://example.com' } });
await fireEvent.click(button);
});
expect(mockFetch).toHaveBeenCalledWith('/api/crawl/', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ url: 'https://example.com' }),
});
// Check if success message is shown
expect(screen.getByText('Source documentation imported successfully!')).toBeInTheDocument();
// Verify URL is still in the input
expect(input).toHaveValue('https://example.com');
});
test('hides success message when starting to type new URL', async () => {
render(<DocumentInput />);
// First submit a URL
const input = screen.getByLabelText('Source Documentation');
const button = screen.getByText('Pull Source Docs');
await act(async () => {
await fireEvent.change(input, { target: { value: 'https://example.com' } });
await fireEvent.click(button);
});
// Verify success message is shown
expect(screen.getByText('Source documentation imported successfully!')).toBeInTheDocument();
// Start typing new URL
await act(async () => {
await fireEvent.change(input, { target: { value: 'https://new' } });
});
// Verify success message is hidden
expect(screen.queryByText('Source documentation imported successfully!')).not.toBeInTheDocument();
});
}); |