forked from CodelyTV/typescript-api-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d2fc73
commit 4fe6407
Showing
2 changed files
with
100 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); | ||
}); | ||
}); |