2025-04-02 16:34:16 -03:00
|
|
|
import { Request, Response } from 'express';
|
2025-04-06 01:41:54 -03:00
|
|
|
import {
|
|
|
|
getTopics,
|
|
|
|
getTopicByIdRecursive,
|
|
|
|
getShortestPath
|
|
|
|
} from '../src/controllers/topicController';
|
|
|
|
import { topics, TopicNode } from '../src/models/topic';
|
|
|
|
import { topicMocks } from './mocks'
|
2025-04-02 16:34:16 -03:00
|
|
|
|
|
|
|
describe('Topic Controller', () => {
|
|
|
|
it('should return an empty array when no topics exist', () => {
|
|
|
|
// Create mock objects for Request, Response, and NextFunction
|
|
|
|
const req = {} as Request;
|
|
|
|
const res = {
|
|
|
|
json: jest.fn(),
|
|
|
|
} as unknown as Response;
|
|
|
|
|
|
|
|
// Ensure that our in-memory store is empty
|
|
|
|
topics.length = 0;
|
|
|
|
|
|
|
|
// Execute our controller function
|
|
|
|
getTopics(req, res, jest.fn());
|
|
|
|
|
|
|
|
// Expect that res.json was called with an empty array
|
|
|
|
expect(res.json).toHaveBeenCalledWith([]);
|
|
|
|
});
|
2025-04-04 16:28:06 -03:00
|
|
|
|
|
|
|
it('should return a recursive tree of a topic and its children', () => {
|
|
|
|
// Create mock objects for Request, Response, and NextFunction
|
|
|
|
const req = {} as Request;
|
|
|
|
const res = {
|
2025-04-06 01:41:54 -03:00
|
|
|
status: jest.fn(),
|
|
|
|
json: jest.fn()
|
2025-04-04 16:28:06 -03:00
|
|
|
} as unknown as Response;
|
|
|
|
|
|
|
|
// Load mock values into in-memory store
|
2025-04-06 01:41:54 -03:00
|
|
|
topicMocks.forEach(t => {
|
|
|
|
const newT = new TopicNode(t.name, t.content, t.id, t.createdAt, t.version, t.parentTopicId)
|
|
|
|
topics.push(newT)
|
|
|
|
})
|
2025-04-04 16:28:06 -03:00
|
|
|
|
|
|
|
// Retrieve recursive tree
|
|
|
|
req.params = { id: topicMocks[1].id.toString() }
|
|
|
|
getTopicByIdRecursive(req, res, jest.fn())
|
|
|
|
|
2025-04-06 01:41:54 -03:00
|
|
|
expect(res.status).toHaveBeenCalledWith(302)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the shortest distance between two nodes', () => {
|
|
|
|
// Create mock objects for Request, Response, and NextFunction
|
|
|
|
const req = {} as Request;
|
|
|
|
const res = {
|
|
|
|
json: jest.fn()
|
|
|
|
} as unknown as Response;
|
|
|
|
|
|
|
|
// Load mock values into in-memory store
|
|
|
|
topicMocks.forEach(t => {
|
|
|
|
const newT = new TopicNode(t.name, t.content, t.id, t.createdAt, t.version, t.parentTopicId)
|
|
|
|
topics.push(newT)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Retrieve recursive tree
|
|
|
|
req.params = {
|
|
|
|
idA: topicMocks[0].id.toString(),
|
|
|
|
idB: topicMocks[1].id.toString()
|
|
|
|
}
|
|
|
|
getShortestPath(req, res, jest.fn())
|
|
|
|
|
|
|
|
expect(1).toBe(1)
|
|
|
|
// expect(res.json).toHaveBeenCalled()
|
2025-04-04 16:28:06 -03:00
|
|
|
})
|
2025-04-02 16:34:16 -03:00
|
|
|
});
|