Skip to content

Commit

Permalink
Improve BaseController
Browse files Browse the repository at this point in the history
  • Loading branch information
BoscoDomingo committed Apr 16, 2023
1 parent 6d2fc73 commit 4fe6407
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/controllers/base.controller.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import { Request, Response } from 'express';
import { NextFunction, Request, Response } from 'express';

import BaseService from '../services/base.service';

export default class BaseController {
constructor(private readonly baseService: BaseService) {}

public getSampleData(req: Request, res: Response): void {
const sampleData = this.baseService.getSampleData();
res.json(sampleData);
public getSampleData(req: Request, res: Response, next: NextFunction): void {
try {
const sampleData = this.baseService.getSampleData();
res.json(sampleData);
} catch (error) {
next(error);
}
}

public postReceivedData(req: Request, res: Response): void {
const data = JSON.stringify(req.body, null, 2);
res.json({ message: `Received data: ${data}` });
public postReceivedData(
req: Request,
res: Response,
next: NextFunction,
): void {
try {
const data = JSON.stringify(req.body, null, 2);
res.json({ message: `Message received`, data });
} catch (error) {
next(error);
}
}
}
81 changes: 81 additions & 0 deletions test/controllers/base.controller.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { NextFunction, Request, Response } from 'express';

import sampleData from '../../data/sample.json';
import BaseController from '../../src/controllers/base.controller';
import BaseService from '../../src/services/base.service';

jest.mock('../../src/services/base.service', () => {
return jest.fn().mockImplementation(() => {
return {
getSampleData: jest.fn(() => {
return sampleData;
}),
};
});
});

describe('BaseController', () => {
let baseService: BaseService;
let baseController: BaseController;
let req: Request;
let res: Response;
let next: NextFunction;

beforeEach(() => {
baseService = new BaseService(sampleData);
baseController = new BaseController(baseService);
req = {} as Request;
res = {
json: jest.fn(),
send: jest.fn(),
} as unknown as Response;
next = jest.fn() as NextFunction;
});

afterEach(() => {
jest.clearAllMocks();
});

describe('getSampleData', () => {
it('should call baseService.getSampleData() and return JSON response', () => {
baseController.getSampleData(req, res, next);

expect(baseService.getSampleData).toHaveBeenCalledTimes(1);
expect(res.json).toHaveBeenCalledWith(sampleData);
});

it('should return an error when baseService.getSampleData() fails', () => {
baseService.getSampleData = jest.fn(() => {
throw new Error('Failed to get sample data');
});

baseController.getSampleData(req, res, next);

expect(next).toHaveBeenCalledWith(Error('Failed to get sample data'));
});
});

describe('postReceivedData', () => {
it('should stringify and return received data in JSON response', () => {
const receivedData = { data: 'Received data' };
req.body = receivedData;

baseController.postReceivedData(req, res, next);

expect(res.json).toHaveBeenCalledWith({
message: `Message received`,
data: JSON.stringify(receivedData, null, 2),
});
});

it('should return an error when the received data is not a valid format', () => {
JSON.stringify = jest.fn(() => {
throw new Error('Failed to parse');
});

baseController.postReceivedData(req, res, next);

expect(next).toHaveBeenCalledWith(Error('Failed to parse'));
});
});
});

0 comments on commit 4fe6407

Please sign in to comment.