This repository was archived by the owner on Jan 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublications-api.test.js
More file actions
58 lines (47 loc) · 2.01 KB
/
publications-api.test.js
File metadata and controls
58 lines (47 loc) · 2.01 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
57
58
const request = require('supertest');
const express = require('express');
const axios = require('axios');
const publicationsRouter = require('../routes/publications');
// Mock the axios module
jest.mock('axios');
const app = express();
// Mount the router to a path
app.use('/api/publications', publicationsRouter);
describe('Backend API: /api/publications', () => {
afterEach(() => {
// Clear all mocks after each test
jest.clearAllMocks();
});
describe('GET /api/publications/search', () => {
// Test for Async Logic Handling and Data Transformation
it('should return successfully with formatted data when OpenAlex API call succeeds', async () => {
const mockApiResponse = {
data: {
results: [
{ id: 'W123', display_name: 'Test Publication 1', cited_by_count: 50 },
{ id: 'W456', display_name: 'Test Publication 2' /* missing cited_by_count */ },
],
meta: { count: 2 },
},
};
axios.get.mockResolvedValue(mockApiResponse);
const response = await request(app).get('/api/publications/search?filter=test');
expect(response.status).toBe(200);
expect(response.body.results).toHaveLength(2);
// Check that data transformation (adding citation_count) works correctly
expect(response.body.results[0]).toHaveProperty('citation_count', 50);
expect(response.body.results[1]).toHaveProperty('citation_count', 0); // Check for default value
});
// Test for Error Handling Middleware
it('should return a 500 error when the OpenAlex API call fails', async () => {
const errorMessage = 'Network Error';
axios.get.mockRejectedValue({ message: errorMessage });
const response = await request(app).get('/api/publications/search?filter=test');
expect(response.status).toBe(500);
expect(response.body).toEqual({
error: 'Failed to fetch from OpenAlex API',
details: errorMessage,
});
});
});
});