-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtc1
85 lines (69 loc) · 2.82 KB
/
tc1
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import React from 'react';
import { render, fireEvent, waitFor, screen } from '@testing-library/react';
import axios from 'axios';
import ChatInterface from './ChatInterface'; // Adjust the import based on your file structure
jest.mock('axios');
const mockConversations = [{
id: 1,
title: 'Test Conversation',
messages: [{ role: 'user', content: 'Test Message' }]
}];
const mockProps = {
theme: 'dark',
currentConversationId: 1,
setCurrentConversationId: jest.fn(),
conversations: mockConversations,
setConversations: jest.fn(),
};
describe('ChatInterface Component', () => {
// Setup before each test
beforeEach(() => {
axios.post.mockClear();
});
// Test if the component renders without crashing
it('renders without crashing', () => {
render(<ChatInterface {...mockProps} />);
expect(screen.getByText(/Test Conversation/i)).toBeInTheDocument();
});
// Test state changes on events
it('updates inputValue on text input change', () => {
render(<ChatInterface {...mockProps} />);
const inputField = screen.getByRole('textbox');
fireEvent.change(inputField, { target: { value: 'New Message' } });
expect(inputField.value).toBe('New Message');
});
// Test sending a message
it('calls handleSendMessage on send button click', async () => {
render(<ChatInterface {...mockProps} />);
const sendButton = screen.getByRole('button', { name: /send/i });
fireEvent.click(sendButton);
await waitFor(() => {
expect(mockProps.setConversations).toHaveBeenCalled();
});
});
// Test file upload functionality
it('handles file upload', async () => {
axios.post.mockResolvedValueOnce({ data: { requestId: '123' } });
axios.get.mockResolvedValueOnce({ data: { status: 'completed' } });
render(<ChatInterface {...mockProps} />);
const uploadButton = screen.getByText(/Upload/i);
fireEvent.click(uploadButton);
const fileInput = screen.getByLabelText(/Choose a file/i);
const file = new File(['hello'], 'hello.png', { type: 'image/png' });
fireEvent.change(fileInput, { target: { files: [file] } });
await waitFor(() => {
expect(axios.post).toHaveBeenCalled();
});
});
// Test for conditional rendering
it('renders CircularProgress when isLoading is true', () => {
render(<ChatInterface {...{ ...mockProps, isLoading: true }} />);
expect(screen.getByRole('progressbar')).toBeInTheDocument();
});
// Snapshot Test
it('matches snapshot', () => {
const { asFragment } = render(<ChatInterface {...mockProps} />);
expect(asFragment()).toMatchSnapshot();
});
// More tests for each feature like useEffect, useRef, error handling, etc.
});