Skip to content
This repository was archived by the owner on Jan 28, 2026. It is now read-only.

Commit c3dfe58

Browse files
authored
Merge pull request #140 from DigitalProductInnovationAndDevelopment/dev_tests_trixi
Added some more tests on front- and backend
2 parents b006c36 + 379fe3d commit c3dfe58

12 files changed

Lines changed: 579 additions & 367 deletions
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const request = require('supertest');
2+
const express = require('express');
3+
const axios = require('axios');
4+
const publicationsRouter = require('../routes/publications');
5+
6+
// Mock the axios module
7+
jest.mock('axios');
8+
9+
const app = express();
10+
// Mount the router to a path
11+
app.use('/api/publications', publicationsRouter);
12+
13+
describe('Backend API: /api/publications', () => {
14+
15+
afterEach(() => {
16+
// Clear all mocks after each test
17+
jest.clearAllMocks();
18+
});
19+
20+
describe('GET /api/publications/search', () => {
21+
22+
// Test for Async Logic Handling and Data Transformation
23+
it('should return successfully with formatted data when OpenAlex API call succeeds', async () => {
24+
const mockApiResponse = {
25+
data: {
26+
results: [
27+
{ id: 'W123', display_name: 'Test Publication 1', cited_by_count: 50 },
28+
{ id: 'W456', display_name: 'Test Publication 2' /* missing cited_by_count */ },
29+
],
30+
meta: { count: 2 },
31+
},
32+
};
33+
axios.get.mockResolvedValue(mockApiResponse);
34+
35+
const response = await request(app).get('/api/publications/search?filter=test');
36+
37+
expect(response.status).toBe(200);
38+
expect(response.body.results).toHaveLength(2);
39+
// Check that data transformation (adding citation_count) works correctly
40+
expect(response.body.results[0]).toHaveProperty('citation_count', 50);
41+
expect(response.body.results[1]).toHaveProperty('citation_count', 0); // Check for default value
42+
});
43+
44+
// Test for Error Handling Middleware
45+
it('should return a 500 error when the OpenAlex API call fails', async () => {
46+
const errorMessage = 'Network Error';
47+
axios.get.mockRejectedValue({ message: errorMessage });
48+
49+
const response = await request(app).get('/api/publications/search?filter=test');
50+
51+
expect(response.status).toBe(500);
52+
expect(response.body).toEqual({
53+
error: 'Failed to fetch from OpenAlex API',
54+
details: errorMessage,
55+
});
56+
});
57+
});
58+
});

backend/_test_/routes.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const request = require('supertest');
2+
const express = require('express');
3+
const publicationsRouter = require('../routes/publications');
4+
5+
// Setup a minimal express app for testing
6+
const app = express();
7+
app.use('/api/publications', publicationsRouter);
8+
9+
describe('Backend API Routes', () => {
10+
11+
// Test #1: Route Handler Returns Expected Response
12+
describe('GET /api/publications', () => {
13+
it('should return a 200 OK status and a valid JSON response for the base route', async () => {
14+
const response = await request(app).get('/api/publications');
15+
expect(response.status).toBe(200);
16+
expect(response.headers['content-type']).toMatch(/json/);
17+
expect(response.body).toBeDefined();
18+
});
19+
});
20+
21+
// Test #2: Validation Middleware Works
22+
describe('GET /api/publications/search', () => {
23+
it('should return a 400 Bad Request for invalid page parameters', async () => {
24+
const response = await request(app)
25+
.get('/api/publications/search?filter=title.search:test&page=invalid-page-number');
26+
27+
expect(response.status).toBe(400);
28+
expect(response.body).toEqual({ error: 'Invalid page number provided.' });
29+
});
30+
});
31+
32+
// Test #3: Error Handling for Non-existent Routes
33+
describe('GET /api/non-existent-route', () => {
34+
it('should return a 404 Not Found for routes that do not exist', async () => {
35+
const response = await request(app).get('/api/this-route-does-not-exist');
36+
expect(response.status).toBe(404);
37+
});
38+
});
39+
40+
});

0 commit comments

Comments
 (0)