Reformat code for readability
This commit is contained in:
1 parent
58a96f6d19
commit
d505320c50
2 files changed
+94
-94
No files matched your search
@@ -6,99 +6,99 @@ import { Request, Response } from 'express';
|
||||
|
||||
// 1. Generate a mock factory for the Express Response object
|
||||
function createMockResponse() {
|
||||
const res: Partial<Response> = {};
|
||||
const data: { statusCode: number; jsonPayload: any } = { statusCode: 200, jsonPayload: null };
|
||||
const res: Partial<Response> = {};
|
||||
const data: { statusCode: number; jsonPayload: any } = { statusCode: 200, jsonPayload: null };
|
||||
|
||||
res.status = function (code: number) {
|
||||
data.statusCode = code;
|
||||
return this as Response;
|
||||
};
|
||||
res.status = function (code: number) {
|
||||
data.statusCode = code;
|
||||
return this as Response;
|
||||
};
|
||||
|
||||
res.json = function (payload: any) {
|
||||
data.jsonPayload = payload;
|
||||
return this as Response;
|
||||
};
|
||||
res.json = function (payload: any) {
|
||||
data.jsonPayload = payload;
|
||||
return this as Response;
|
||||
};
|
||||
|
||||
return { mockRes: res as Response, data };
|
||||
return { mockRes: res as Response, data };
|
||||
}
|
||||
|
||||
// 2. Generate a mock factory for the repository layer
|
||||
function createMockRepository(behavior: 'success' | 'failure') {
|
||||
return {
|
||||
findAllDescendants: async (path: string) => {
|
||||
if (behavior === 'failure') throw new Error('Database connection failed');
|
||||
// Return a valid single-root array format mimicking real ltree database outputs
|
||||
return [
|
||||
{ id: 1, name: 'Top', path: 'Top' },
|
||||
{ id: 2, name: 'Science', path: 'Top.Science' }
|
||||
];
|
||||
}
|
||||
} as unknown as CategoryRepository;
|
||||
return {
|
||||
findAllDescendants: async (path: string) => {
|
||||
if (behavior === 'failure') throw new Error('Database connection failed');
|
||||
// Return a valid single-root array format mimicking real ltree database outputs
|
||||
return [
|
||||
{ id: 1, name: 'Top', path: 'Top' },
|
||||
{ id: 2, name: 'Science', path: 'Top.Science' }
|
||||
];
|
||||
}
|
||||
} as unknown as CategoryRepository;
|
||||
}
|
||||
|
||||
// Helper utility to safely extract the Express handler bypassing strict undefined types
|
||||
function getRouteHandler(router: any): Function {
|
||||
const routeLayer = router.stack.find((layer: any) => layer.route);
|
||||
if (!routeLayer || !routeLayer.route || !routeLayer.route.stack) {
|
||||
throw new Error('Could not find route handler in mock router stack');
|
||||
}
|
||||
return routeLayer.route.stack[0].handle; // Target the primary callback handler array element
|
||||
const routeLayer = router.stack.find((layer: any) => layer.route);
|
||||
if (!routeLayer || !routeLayer.route || !routeLayer.route.stack) {
|
||||
throw new Error('Could not find route handler in mock router stack');
|
||||
}
|
||||
return routeLayer.route.stack[0].handle; // Target the primary callback handler array element
|
||||
}
|
||||
|
||||
describe('Category Router Endpoints', () => {
|
||||
|
||||
it('GET /tree{/:path} - should return 200 and structural JSON array data on valid requests', async () => {
|
||||
const mockRepo = createMockRepository('success');
|
||||
const router = createCategoryRouter(mockRepo);
|
||||
|
||||
const mockReq = { params: { path: 'Top' } } as unknown as Request;
|
||||
const { mockRes, data } = createMockResponse();
|
||||
|
||||
const routeHandler = getRouteHandler(router);
|
||||
await routeHandler(mockReq, mockRes);
|
||||
it('GET /tree{/:path} - should return 200 and structural JSON array data on valid requests', async () => {
|
||||
const mockRepo = createMockRepository('success');
|
||||
const router = createCategoryRouter(mockRepo);
|
||||
|
||||
assert.equal(data.statusCode, 200);
|
||||
assert.equal(data.jsonPayload.success, true);
|
||||
|
||||
// Correct alignment: buildCategoryTree returns an ARRAY of root nodes
|
||||
assert.equal(Array.isArray(data.jsonPayload.data), true);
|
||||
assert.equal(data.jsonPayload.data[0].name, 'Top');
|
||||
});
|
||||
const mockReq = { params: { path: 'Top' } } as unknown as Request;
|
||||
const { mockRes, data } = createMockResponse();
|
||||
|
||||
it('GET /tree{/:path} - should return 400 Bad Request if ltree path contains malformed formatting', async () => {
|
||||
const mockRepo = createMockRepository('success');
|
||||
const router = createCategoryRouter(mockRepo);
|
||||
|
||||
const mockReq = { params: { path: 'Top.Bad Path!' } } as unknown as Request;
|
||||
const { mockRes, data } = createMockResponse();
|
||||
const routeHandler = getRouteHandler(router);
|
||||
await routeHandler(mockReq, mockRes);
|
||||
|
||||
const routeHandler = getRouteHandler(router);
|
||||
await routeHandler(mockReq, mockRes);
|
||||
assert.equal(data.statusCode, 200);
|
||||
assert.equal(data.jsonPayload.success, true);
|
||||
|
||||
assert.equal(data.statusCode, 400);
|
||||
assert.equal(data.jsonPayload.success, false);
|
||||
assert.match(data.jsonPayload.error, /Invalid path format/);
|
||||
});
|
||||
// Correct alignment: buildCategoryTree returns an ARRAY of root nodes
|
||||
assert.equal(Array.isArray(data.jsonPayload.data), true);
|
||||
assert.equal(data.jsonPayload.data[0].name, 'Top');
|
||||
});
|
||||
|
||||
it('GET /tree{/:path} - should handle runtime database crashes gracefully with a 500 error status', async () => {
|
||||
const mockRepo = createMockRepository('failure');
|
||||
const router = createCategoryRouter(mockRepo);
|
||||
|
||||
const mockReq = { params: { path: 'Top' } } as unknown as Request;
|
||||
const { mockRes, data } = createMockResponse();
|
||||
it('GET /tree{/:path} - should return 400 Bad Request if ltree path contains malformed formatting', async () => {
|
||||
const mockRepo = createMockRepository('success');
|
||||
const router = createCategoryRouter(mockRepo);
|
||||
|
||||
// Prevent console.error from cluttering the test runner feedback during intentional failures
|
||||
const originalConsoleError = console.error;
|
||||
console.error = () => {};
|
||||
const mockReq = { params: { path: 'Top.Bad Path!' } } as unknown as Request;
|
||||
const { mockRes, data } = createMockResponse();
|
||||
|
||||
const routeHandler = getRouteHandler(router);
|
||||
await routeHandler(mockReq, mockRes);
|
||||
const routeHandler = getRouteHandler(router);
|
||||
await routeHandler(mockReq, mockRes);
|
||||
|
||||
// Restore original logger tracking
|
||||
console.error = originalConsoleError;
|
||||
assert.equal(data.statusCode, 400);
|
||||
assert.equal(data.jsonPayload.success, false);
|
||||
assert.match(data.jsonPayload.error, /Invalid path format/);
|
||||
});
|
||||
|
||||
assert.equal(data.statusCode, 500);
|
||||
assert.equal(data.jsonPayload.success, false);
|
||||
assert.equal(data.jsonPayload.error, 'Internal Server Error');
|
||||
});
|
||||
it('GET /tree{/:path} - should handle runtime database crashes gracefully with a 500 error status', async () => {
|
||||
const mockRepo = createMockRepository('failure');
|
||||
const router = createCategoryRouter(mockRepo);
|
||||
|
||||
const mockReq = { params: { path: 'Top' } } as unknown as Request;
|
||||
const { mockRes, data } = createMockResponse();
|
||||
|
||||
// Prevent console.error from cluttering the test runner feedback during intentional failures
|
||||
const originalConsoleError = console.error;
|
||||
console.error = () => { };
|
||||
|
||||
const routeHandler = getRouteHandler(router);
|
||||
await routeHandler(mockReq, mockRes);
|
||||
|
||||
// Restore original logger tracking
|
||||
console.error = originalConsoleError;
|
||||
|
||||
assert.equal(data.statusCode, 500);
|
||||
assert.equal(data.jsonPayload.success, false);
|
||||
assert.equal(data.jsonPayload.error, 'Internal Server Error');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user