Skip to content
This repository was archived by the owner on Jan 28, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions backend/_test_/publications-api.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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,
});
});
});
});
40 changes: 40 additions & 0 deletions backend/_test_/routes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const request = require('supertest');
const express = require('express');
const publicationsRouter = require('../routes/publications');

// Setup a minimal express app for testing
const app = express();
app.use('/api/publications', publicationsRouter);

describe('Backend API Routes', () => {

// Test #1: Route Handler Returns Expected Response
describe('GET /api/publications', () => {
it('should return a 200 OK status and a valid JSON response for the base route', async () => {
const response = await request(app).get('/api/publications');
expect(response.status).toBe(200);
expect(response.headers['content-type']).toMatch(/json/);
expect(response.body).toBeDefined();
});
});

// Test #2: Validation Middleware Works
describe('GET /api/publications/search', () => {
it('should return a 400 Bad Request for invalid page parameters', async () => {
const response = await request(app)
.get('/api/publications/search?filter=title.search:test&page=invalid-page-number');

expect(response.status).toBe(400);
expect(response.body).toEqual({ error: 'Invalid page number provided.' });
});
});

// Test #3: Error Handling for Non-existent Routes
describe('GET /api/non-existent-route', () => {
it('should return a 404 Not Found for routes that do not exist', async () => {
const response = await request(app).get('/api/this-route-does-not-exist');
expect(response.status).toBe(404);
});
});

});
Loading