Spaces:
Sleeping
Sleeping
Download frontend-backup/src/services/__tests__/api.test.ts from edichat/edi.chat.test: direct link, hf CLI and curl.
- Browser
- Download file 12.6 kB
-
https://huggingface.co/spaces/edichat/edi.chat.test/resolve/main/frontend-backup/src/services/__tests__/api.test.ts
- Command line
-
hf download hf://spaces/edichat/edi.chat.test/frontend-backup/src/services/__tests__/api.test.ts
-
curl -L -o api.test.ts https://huggingface.co/spaces/edichat/edi.chat.test/resolve/main/frontend-backup/src/services/__tests__/api.test.ts
12.6 kB
| import axios from 'axios'; | |
| import { | |
| authAPI, | |
| chatAPI, | |
| userAPI, | |
| facultyAPI, | |
| studentAPI, | |
| knowledgeAPI, | |
| settingsAPI | |
| } from '../api'; | |
| // Mock axios | |
| jest.mock('axios'); | |
| const mockedAxios = axios as jest.Mocked<typeof axios>; | |
| // Mock localStorage | |
| const localStorageMock = { | |
| getItem: jest.fn(), | |
| setItem: jest.fn(), | |
| removeItem: jest.fn(), | |
| clear: jest.fn(), | |
| }; | |
| Object.defineProperty(window, 'localStorage', { | |
| value: localStorageMock, | |
| }); | |
| describe('API Services', () => { | |
| beforeEach(() => { | |
| jest.clearAllMocks(); | |
| localStorageMock.getItem.mockReturnValue('mock-token'); | |
| }); | |
| describe('authAPI', () => { | |
| test('login makes correct API call', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| data: { | |
| token: 'new-token', | |
| user: { id: '1', email: 'test@uga.edu' }, | |
| }, | |
| }, | |
| }; | |
| mockedAxios.post.mockResolvedValue(mockResponse); | |
| const credentials = { username: 'testuser', password: 'password' }; | |
| const result = await authAPI.login(credentials); | |
| expect(mockedAxios.post).toHaveBeenCalledWith('/api/auth/login', credentials); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| test('loginWithMyID makes correct API call', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| data: { | |
| token: 'myid-token', | |
| user: { id: '1', ugaId: '811123456', email: 'test@uga.edu' }, | |
| }, | |
| }, | |
| }; | |
| mockedAxios.post.mockResolvedValue(mockResponse); | |
| const credentials = { username: 'testuser', password: 'password' }; | |
| const result = await authAPI.loginWithMyID(credentials); | |
| expect(mockedAxios.post).toHaveBeenCalledWith('/api/auth/myid-login', credentials); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| test('logout makes correct API call', async () => { | |
| const mockResponse = { data: { success: true } }; | |
| mockedAxios.post.mockResolvedValue(mockResponse); | |
| const result = await authAPI.logout(); | |
| expect(mockedAxios.post).toHaveBeenCalledWith('/api/auth/logout'); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| test('getCurrentUser makes correct API call', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| data: { id: '1', email: 'test@uga.edu' }, | |
| }, | |
| }; | |
| mockedAxios.get.mockResolvedValue(mockResponse); | |
| const result = await authAPI.getCurrentUser(); | |
| expect(mockedAxios.get).toHaveBeenCalledWith('/api/auth/me'); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| test('refreshToken makes correct API call', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| data: { token: 'new-token', refreshToken: 'new-refresh-token' }, | |
| }, | |
| }; | |
| mockedAxios.post.mockResolvedValue(mockResponse); | |
| localStorageMock.getItem.mockReturnValue('old-refresh-token'); | |
| const result = await authAPI.refreshToken(); | |
| expect(mockedAxios.post).toHaveBeenCalledWith('/api/auth/refresh', { | |
| refreshToken: 'old-refresh-token', | |
| }); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| }); | |
| describe('chatAPI', () => { | |
| test('sendMessage makes correct API call', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| data: { | |
| id: '1', | |
| role: 'assistant', | |
| content: 'Hello! How can I help you?', | |
| }, | |
| }, | |
| }; | |
| mockedAxios.post.mockResolvedValue(mockResponse); | |
| const result = await chatAPI.sendMessage('conv1', 'Hello'); | |
| expect(mockedAxios.post).toHaveBeenCalledWith('/api/chat/message', { | |
| conversationId: 'conv1', | |
| message: 'Hello', | |
| }); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| test('getConversations makes correct API call', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| data: [ | |
| { id: 'conv1', title: 'Test Conversation', category: 'general' }, | |
| ], | |
| }, | |
| }; | |
| mockedAxios.get.mockResolvedValue(mockResponse); | |
| const result = await chatAPI.getConversations(); | |
| expect(mockedAxios.get).toHaveBeenCalledWith('/api/chat/conversations'); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| test('createConversation makes correct API call', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| data: { id: 'new-conv', title: 'New Conversation' }, | |
| }, | |
| }; | |
| mockedAxios.post.mockResolvedValue(mockResponse); | |
| const result = await chatAPI.createConversation(); | |
| expect(mockedAxios.post).toHaveBeenCalledWith('/api/chat/conversations'); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| test('deleteConversation makes correct API call', async () => { | |
| const mockResponse = { data: { success: true } }; | |
| mockedAxios.delete.mockResolvedValue(mockResponse); | |
| const result = await chatAPI.deleteConversation('conv1'); | |
| expect(mockedAxios.delete).toHaveBeenCalledWith('/api/chat/conversations/conv1'); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| }); | |
| describe('userAPI', () => { | |
| test('getStats makes correct API call', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| data: { | |
| totalConversations: 5, | |
| totalQuestions: 25, | |
| favoriteTopics: ['academics', 'housing'], | |
| }, | |
| }, | |
| }; | |
| mockedAxios.get.mockResolvedValue(mockResponse); | |
| const result = await userAPI.getStats(); | |
| expect(mockedAxios.get).toHaveBeenCalledWith('/api/users/stats'); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| test('updateProfile makes correct API call', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| data: { id: '1', firstName: 'Updated', lastName: 'User' }, | |
| }, | |
| }; | |
| mockedAxios.put.mockResolvedValue(mockResponse); | |
| const profileData = { firstName: 'Updated', lastName: 'User' }; | |
| const result = await authAPI.updateProfile(profileData); | |
| expect(mockedAxios.put).toHaveBeenCalledWith('/api/users/profile', profileData); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| test('getConversationsSummary makes correct API call with params', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| data: { recentConversations: [] }, | |
| }, | |
| }; | |
| mockedAxios.get.mockResolvedValue(mockResponse); | |
| const result = await userAPI.getConversationsSummary({ limit: 5 }); | |
| expect(mockedAxios.get).toHaveBeenCalledWith('/api/users/conversations/summary', { | |
| params: { limit: 5 }, | |
| }); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| }); | |
| describe('facultyAPI', () => { | |
| test('getDashboard makes correct API call', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| data: { | |
| summary: { totalCourses: 3, totalStudents: 150 }, | |
| courses: [], | |
| notifications: [], | |
| }, | |
| }, | |
| }; | |
| mockedAxios.get.mockResolvedValue(mockResponse); | |
| const result = await facultyAPI.getDashboard(); | |
| expect(mockedAxios.get).toHaveBeenCalledWith('/api/faculty/dashboard'); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| test('getAnalytics makes correct API call with course filter', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| data: { | |
| gradeDistribution: [], | |
| attendanceData: [], | |
| performanceData: [], | |
| }, | |
| }, | |
| }; | |
| mockedAxios.get.mockResolvedValue(mockResponse); | |
| const result = await facultyAPI.getAnalytics('course123'); | |
| expect(mockedAxios.get).toHaveBeenCalledWith('/api/faculty/analytics', { | |
| params: { courseId: 'course123' }, | |
| }); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| }); | |
| describe('studentAPI', () => { | |
| test('getCourses makes correct API call', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| data: { | |
| enrolled: [], | |
| available: [], | |
| }, | |
| }, | |
| }; | |
| mockedAxios.get.mockResolvedValue(mockResponse); | |
| const result = await studentAPI.getCourses(); | |
| expect(mockedAxios.get).toHaveBeenCalledWith('/api/students/courses'); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| test('registerCourse makes correct API call', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| message: 'Successfully registered for course', | |
| }, | |
| }; | |
| mockedAxios.post.mockResolvedValue(mockResponse); | |
| const result = await studentAPI.registerCourse('course123'); | |
| expect(mockedAxios.post).toHaveBeenCalledWith('/api/students/courses/register', { | |
| courseId: 'course123', | |
| }); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| test('getFinancialInfo makes correct API call', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| data: { | |
| balance: 2500, | |
| dueDate: '2024-04-15', | |
| aid: [], | |
| }, | |
| }, | |
| }; | |
| mockedAxios.get.mockResolvedValue(mockResponse); | |
| const result = await studentAPI.getFinancialInfo(); | |
| expect(mockedAxios.get).toHaveBeenCalledWith('/api/students/financial'); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| }); | |
| describe('knowledgeAPI', () => { | |
| test('searchArticles makes correct API call', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| data: [ | |
| { id: '1', title: 'Course Registration', content: 'How to register...' }, | |
| ], | |
| }, | |
| }; | |
| mockedAxios.get.mockResolvedValue(mockResponse); | |
| const result = await knowledgeAPI.searchArticles('registration'); | |
| expect(mockedAxios.get).toHaveBeenCalledWith('/api/knowledge/search', { | |
| params: { q: 'registration' }, | |
| }); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| test('toggleBookmark makes correct API call', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| message: 'Article bookmarked', | |
| }, | |
| }; | |
| mockedAxios.post.mockResolvedValue(mockResponse); | |
| const result = await knowledgeAPI.toggleBookmark('article123'); | |
| expect(mockedAxios.post).toHaveBeenCalledWith('/api/knowledge/articles/article123/bookmark'); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| }); | |
| describe('settingsAPI', () => { | |
| test('getSettings makes correct API call', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| data: { | |
| notifications: { email: true, push: false }, | |
| appearance: { theme: 'light' }, | |
| }, | |
| }, | |
| }; | |
| mockedAxios.get.mockResolvedValue(mockResponse); | |
| const result = await settingsAPI.getSettings(); | |
| expect(mockedAxios.get).toHaveBeenCalledWith('/api/settings'); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| test('updateSettings makes correct API call', async () => { | |
| const mockResponse = { | |
| data: { | |
| success: true, | |
| data: { | |
| notifications: { email: false, push: true }, | |
| }, | |
| }, | |
| }; | |
| mockedAxios.put.mockResolvedValue(mockResponse); | |
| const settings = { notifications: { email: false, push: true } }; | |
| const result = await settingsAPI.updateSettings(settings); | |
| expect(mockedAxios.put).toHaveBeenCalledWith('/api/settings', settings); | |
| expect(result).toEqual(mockResponse.data); | |
| }); | |
| }); | |
| describe('Error handling', () => { | |
| test('handles API errors gracefully', async () => { | |
| const errorResponse = { | |
| response: { | |
| data: { | |
| success: false, | |
| message: 'Server error', | |
| }, | |
| }, | |
| }; | |
| mockedAxios.get.mockRejectedValue(errorResponse); | |
| await expect(authAPI.getCurrentUser()).rejects.toEqual(errorResponse); | |
| }); | |
| test('handles network errors gracefully', async () => { | |
| const networkError = new Error('Network Error'); | |
| mockedAxios.get.mockRejectedValue(networkError); | |
| await expect(authAPI.getCurrentUser()).rejects.toEqual(networkError); | |
| }); | |
| }); | |
| }); |