ProjectMark-Rodrigo/tests/topicController.test.ts
Rodrigo Pinto d7748229ef Shortest path algorithm, tests and Postman
- Added algorithm and endpoint to find the shortest path between two
  topics;
- Added respective test;
- Added a JSON collection with API calls that can be imported into
  Postman for the convenience of the user.
2025-04-06 01:41:54 -03:00

72 lines
2.1 KiB
TypeScript

import { Request, Response } from 'express';
import {
getTopics,
getTopicByIdRecursive,
getShortestPath
} from '../src/controllers/topicController';
import { topics, TopicNode } from '../src/models/topic';
import { topicMocks } from './mocks'
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([]);
});
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 = {
status: jest.fn(),
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 = { id: topicMocks[1].id.toString() }
getTopicByIdRecursive(req, res, jest.fn())
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()
})
});