test: alle functies in learningObjects.ts zijn getest en de testen slagen
This commit is contained in:
		
							parent
							
								
									fca8e7ff7a
								
							
						
					
					
						commit
						71cf652f28
					
				
					 2 changed files with 102 additions and 6 deletions
				
			
		
							
								
								
									
										98
									
								
								backend/tests/service/learning-objects.test.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										98
									
								
								backend/tests/service/learning-objects.test.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,98 @@ | |||
| import {describe, it, expect, vi} from 'vitest'; | ||||
| import { | ||||
|     LearningObjectMetadata, | ||||
| } from "../../src/interfaces/learningPath"; | ||||
| import {fetchWithLogging} from "../../src/util/apiHelper"; | ||||
| import {getLearningObjectById, getLearningObjectsFromPath} from "../../src/services/learningObjects"; | ||||
| import {fetchLearningPaths} from "../../src/services/learningPaths"; | ||||
| 
 | ||||
| // Mock API functions
 | ||||
| vi.mock('../../src/util/apiHelper', () => ({ | ||||
|     fetchWithLogging: vi.fn(), | ||||
| })); | ||||
| 
 | ||||
| vi.mock('../../src/services/learningPaths', () => ({ | ||||
|     fetchLearningPaths: vi.fn(), | ||||
| })); | ||||
| 
 | ||||
| 
 | ||||
| describe('getLearningObjectById', () => { | ||||
|     const hruid = 'test-object'; | ||||
|     const language = 'en'; | ||||
|     const mockMetadata: LearningObjectMetadata = { | ||||
|         hruid, | ||||
|         _id: '123', | ||||
|         uuid: 'uuid-123', | ||||
|         version: 1, | ||||
|         title: 'Test Object', | ||||
|         language, | ||||
|         difficulty: 5, | ||||
|         estimated_time: 120, | ||||
|         available: true, | ||||
|         teacher_exclusive: false, | ||||
|         educational_goals: [{source: 'source', id: 'id'}], | ||||
|         keywords: ['robotics'], | ||||
|         description: 'A test object', | ||||
|         target_ages: [10, 12], | ||||
|         content_type: 'markdown', | ||||
|         content_location: '', | ||||
|         skos_concepts: [], | ||||
|         return_value: undefined, | ||||
|     }; | ||||
| 
 | ||||
|     it('✅ Should return a filtered learning object when API provides data', async () => { | ||||
|         vi.mocked(fetchWithLogging).mockResolvedValueOnce(mockMetadata); | ||||
| 
 | ||||
|         const result = await getLearningObjectById(hruid, language); | ||||
| 
 | ||||
|         expect(result).toEqual({ | ||||
|             key: hruid, | ||||
|             _id: '123', | ||||
|             uuid: 'uuid-123', | ||||
|             version: 1, | ||||
|             title: 'Test Object', | ||||
|             htmlUrl: expect.stringContaining('/learningObject/getRaw?hruid=test-object&language=en'), | ||||
|             language, | ||||
|             difficulty: 5, | ||||
|             estimatedTime: 120, | ||||
|             available: true, | ||||
|             teacherExclusive: false, | ||||
|             educationalGoals: [{source: 'source', id: 'id'}], | ||||
|             keywords: ['robotics'], | ||||
|             description: 'A test object', | ||||
|             targetAges: [10, 12], | ||||
|             contentType: 'markdown', | ||||
|             contentLocation: '', | ||||
|             skosConcepts: [], | ||||
|             returnValue: undefined, | ||||
|         }); | ||||
|     }); | ||||
| 
 | ||||
|     it('⚠️ Should return null if API returns no metadata', async () => { | ||||
|         vi.mocked(fetchWithLogging).mockResolvedValueOnce(null); | ||||
|         const result = await getLearningObjectById(hruid, language); | ||||
|         expect(result).toBeNull(); | ||||
|     }); | ||||
| }); | ||||
| 
 | ||||
| 
 | ||||
| describe('getLearningObjectsFromPath', () => { | ||||
|     const hruid = 'test-path'; | ||||
|     const language = 'en'; | ||||
| 
 | ||||
|     it('⚠️ Should return an empty array if API returns an empty path response', async () => { | ||||
|         vi.mocked(fetchLearningPaths).mockResolvedValueOnce({success: false, source: 'Test Source', data: []}); | ||||
| 
 | ||||
|         const result = await getLearningObjectsFromPath(hruid, language); | ||||
| 
 | ||||
|         expect(result).toEqual([]); | ||||
|     }); | ||||
| 
 | ||||
|     it('❌ Should return an empty array and log an error if fetchLearningPaths fails', async () => { | ||||
|         vi.mocked(fetchLearningPaths).mockRejectedValueOnce(new Error('API Error')); | ||||
| 
 | ||||
|         const result = await getLearningObjectsFromPath(hruid, language); | ||||
| 
 | ||||
|         expect(result).toEqual([]); | ||||
|     }); | ||||
| }); | ||||
|  | @ -3,21 +3,19 @@ import { fetchLearningPaths, searchLearningPaths } from '../../src/services/lear | |||
| import { fetchWithLogging } from '../../src/util/apiHelper'; | ||||
| import { LearningPathResponse } from '../../src/interfaces/learningPath'; | ||||
| 
 | ||||
| // Function to mock the fetchWithLogging module using vi
 | ||||
| // Mock the fetchWithLogging module using vi
 | ||||
| vi.mock('../../src/util/apiHelper', () => ({ | ||||
|     fetchWithLogging: vi.fn(), | ||||
| })); | ||||
| 
 | ||||
| describe('fetchLearningPaths', () => { | ||||
|     // Mock data and response
 | ||||
|     const mockHruids = ['pn_werking', 'art1']; | ||||
|     const language = 'en'; | ||||
|     const source = 'Test Source'; | ||||
|     const mockResponse = [{ title: 'Test Path', hruids: mockHruids }]; | ||||
| 
 | ||||
|     it('✅ Should return a successful response when HRUIDs are provided', async () => { | ||||
|         // Mock response van fetchWithLogging
 | ||||
|         const mockResponse = [{ title: 'Test Path', hruids: mockHruids }]; | ||||
| 
 | ||||
|         // Mock the function to return mockResponse
 | ||||
|         vi.mocked(fetchWithLogging).mockResolvedValue(mockResponse); | ||||
| 
 | ||||
|  | @ -29,10 +27,10 @@ describe('fetchLearningPaths', () => { | |||
|     }); | ||||
| 
 | ||||
|     it('⚠️ Should return an error when no HRUIDs are provided', async () => { | ||||
|         const result: LearningPathResponse = await fetchLearningPaths([], language, source); | ||||
| 
 | ||||
|         vi.mocked(fetchWithLogging).mockResolvedValue(mockResponse); | ||||
| 
 | ||||
|         const result: LearningPathResponse = await fetchLearningPaths([], language, source); | ||||
| 
 | ||||
|         expect(result.success).toBe(false); | ||||
|         expect(result.data).toBeNull(); | ||||
|         expect(result.message).toBe(`No HRUIDs provided for ${source}.`); | ||||
|  |  | |||
		Reference in a new issue
	
	 Joyelle Ndagijimana
						Joyelle Ndagijimana