Version control and retrieval endpoints

This commit is contained in:
Rodrigo Pinto 2025-04-04 16:28:06 -03:00
commit 8ef27f33d6
6 changed files with 261 additions and 5 deletions

101
tests/mocks.ts Normal file
View file

@ -0,0 +1,101 @@
export interface Topic {
id: number
name: string
content: string
createdAt: string
updatedAt: string
version: number
parentTopicId?: number
}
export const topicMocks: Topic[] = [
{
"id": 1743738847018,
"name": "First topic",
"content": "Topic content",
"createdAt": "Fri Apr 04 2025 00:54:07 GMT-0300 (Brasilia Standard Time)",
"updatedAt": "Fri Apr 04 2025 00:54:07 GMT-0300 (Brasilia Standard Time)",
"version": 1
},
{
"id": 1743738900320,
"name": "Second topic",
"content": "Topic content",
"createdAt": "Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)",
"updatedAt": "Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)",
"version": 1
},
{
"id": 1743738900599,
"name": "Third topic",
"content": "Topic content",
"createdAt": "Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)",
"updatedAt": "Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)",
"version": 1,
"parentTopicId": 1743738900320
},
{
"id": 1743738900800,
"name": "Fourth topic",
"content": "Topic content",
"createdAt": "Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)",
"updatedAt": "Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)",
"version": 1,
"parentTopicId": 1743738900599
},
{
"id": 1743739100103,
"name": "Fifth topic",
"content": "Topic content",
"createdAt": "Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)",
"updatedAt": "Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)",
"version": 1,
"parentTopicId": 1743738900320
}
];
export const treeMock = [
{
id: 1743738847018,
name: 'First topic',
content: 'Topic content',
createdAt: 'Fri Apr 04 2025 00:54:07 GMT-0300 (Brasilia Standard Time)',
updatedAt: 'Fri Apr 04 2025 00:54:07 GMT-0300 (Brasilia Standard Time)',
version: 1
},
{
id: 1743738900320,
name: 'Second topic',
content: 'Topic content',
createdAt: 'Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)',
updatedAt: 'Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)',
version: 1
},
{
id: 1743738900599,
name: 'Third topic',
content: 'Topic content',
createdAt: 'Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)',
updatedAt: 'Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)',
version: 1,
parentTopicId: 1743738900320
},
{
id: 1743738900800,
name: 'Fourth topic',
content: 'Topic content',
createdAt: 'Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)',
updatedAt: 'Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)',
version: 1,
parentTopicId: 1743738900599
},
{
id: 1743739100103,
name: 'Fifth topic',
content: 'Topic content',
createdAt: 'Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)',
updatedAt: 'Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)',
version: 1,
parentTopicId: 1743738900320
}
]

View file

@ -1,6 +1,7 @@
import { Request, Response } from 'express';
import { getTopics } from '../src/controllers/topicController';
import { getTopics, getTopicByIdRecursive } from '../src/controllers/topicController';
import { topics } from '../src/models/topic';
import { topicMocks, treeMock } from './mocks'
describe('Topic Controller', () => {
it('should return an empty array when no topics exist', () => {
@ -19,4 +20,21 @@ describe('Topic Controller', () => {
// 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 = {
json: topicMocks
} as unknown as Response;
// Load mock values into in-memory store
topicMocks.forEach(t => topics.push(t))
// Retrieve recursive tree
req.params = { id: topicMocks[1].id.toString() }
getTopicByIdRecursive(req, res, jest.fn())
expect(res.json).toMatchObject(treeMock)
})
});