-
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
cebd4ac
commit 2d68dd4
Showing
9 changed files
with
680 additions
and
41 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
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
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
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
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,65 @@ | ||
import app from '../../src/app'; | ||
import { Config } from '../../src/config'; | ||
import { AppDataSource } from '../../src/config/data-source'; | ||
import logger from '../../src/config/logger'; | ||
|
||
// Mock the dependencies | ||
jest.mock('../../src/config/data-source'); | ||
jest.mock('../../src/config/logger'); | ||
jest.mock('../../src/app'); | ||
|
||
describe('Server', () => { | ||
it('should initialize the database and start the server', async () => { | ||
const initializeMock = jest.fn().mockResolvedValue(undefined); | ||
AppDataSource.initialize = initializeMock; | ||
|
||
const listenMock = jest.fn().mockImplementation((port, callback) => { | ||
callback(); | ||
}); | ||
(app.listen as jest.Mock) = listenMock; | ||
|
||
const loggerInfoMock = jest.fn(); | ||
logger.info = loggerInfoMock; | ||
|
||
const loggerErrorMock = jest.fn(); | ||
logger.error = loggerErrorMock; | ||
|
||
// Import the server file to run the startServer function | ||
await import('../../src/server'); | ||
|
||
expect(initializeMock).toHaveBeenCalled(); | ||
expect(loggerInfoMock).toHaveBeenCalledWith( | ||
'Database connection established successfully.', | ||
); | ||
expect(listenMock).toHaveBeenCalledWith(Config.PORT, expect.any(Function)); | ||
expect(loggerInfoMock).toHaveBeenCalledWith( | ||
`Server is running on port ${Config.PORT}.`, | ||
); | ||
expect(loggerErrorMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should log an error and exit the process if database initialization fails', async () => { | ||
const error = new Error('Database initialization failed'); | ||
const initializeMock = jest.fn().mockRejectedValue(error); | ||
AppDataSource.initialize = initializeMock; | ||
|
||
const loggerInfoMock = jest.fn(); | ||
logger.info = loggerInfoMock; | ||
|
||
const loggerErrorMock = jest.fn(); | ||
logger.error = loggerErrorMock; | ||
|
||
const processExitMock = jest | ||
.spyOn(process, 'exit') | ||
.mockImplementation((code?: number | null | string) => code as never); | ||
|
||
// Import the server file to run the startServer function | ||
await import('../../src/server'); | ||
|
||
// Wait for the error handling timeout to complete | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
|
||
// Restore the original process.exit implementation | ||
processExitMock.mockRestore(); | ||
}); | ||
}); |
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,135 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { TenantController } from '../../src/controllers/TenantController'; | ||
|
||
const mockTenantService = { | ||
create: jest.fn(), | ||
update: jest.fn(), | ||
getById: jest.fn(), | ||
deleteById: jest.fn(), | ||
}; | ||
|
||
const mockLogger = { | ||
debug: jest.fn(), | ||
info: jest.fn(), | ||
}; | ||
|
||
const mockRequest = (body = {}, params = {}) => | ||
({ | ||
body, | ||
params, | ||
}) as unknown as Request; | ||
|
||
const mockResponse = () => { | ||
const res = {} as Response; | ||
res.status = jest.fn().mockReturnValue(res); | ||
res.json = jest.fn().mockReturnValue(res); | ||
res.send = jest.fn().mockReturnValue(res); | ||
return res; | ||
}; | ||
|
||
const mockNext: NextFunction = jest.fn(); | ||
|
||
describe('TenantController', () => { | ||
let tenantController: TenantController; | ||
|
||
beforeEach(() => { | ||
tenantController = new TenantController( | ||
mockTenantService as any, | ||
mockLogger as any, | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should create a tenant successfully', async () => { | ||
const req = mockRequest({ name: 'Test Tenant', address: '123 Test St' }); | ||
const res = mockResponse(); | ||
const tenant = { id: 1, name: 'Test Tenant', address: '123 Test St' }; | ||
mockTenantService.create.mockResolvedValue(tenant); | ||
|
||
await tenantController.create(req, res, mockNext); | ||
|
||
expect(mockTenantService.create).toHaveBeenCalledWith({ | ||
name: 'Test Tenant', | ||
address: '123 Test St', | ||
}); | ||
expect(mockLogger.debug).toHaveBeenCalledWith( | ||
'Request for creating a tenant', | ||
{ name: 'Test Tenant', address: '123 Test St' }, | ||
); | ||
expect(mockLogger.info).toHaveBeenCalledWith( | ||
'Tenant has been registered:', | ||
tenant, | ||
); | ||
expect(res.send).toHaveBeenCalledWith(201); | ||
expect(res.json).toHaveBeenCalledWith({ id: tenant.id }); | ||
}); | ||
|
||
it('should handle error during tenant creation', async () => { | ||
const req = mockRequest({ name: 'Test Tenant', address: '123 Test St' }); | ||
const res = mockResponse(); | ||
const error = new Error('Create tenant failed'); | ||
mockTenantService.create.mockRejectedValue(error); | ||
|
||
await tenantController.create(req, res, mockNext); | ||
|
||
expect(mockTenantService.create).toHaveBeenCalledWith({ | ||
name: 'Test Tenant', | ||
address: '123 Test St', | ||
}); | ||
expect(mockNext).toHaveBeenCalledWith(error); | ||
}); | ||
|
||
it('should update a tenant successfully', async () => { | ||
const req = mockRequest( | ||
{ name: 'Updated Tenant', address: '456 Updated St' }, | ||
{ id: '1' }, | ||
); | ||
const res = mockResponse(); | ||
mockTenantService.update.mockResolvedValue(undefined); | ||
|
||
await tenantController.update(req, res, mockNext); | ||
|
||
expect(mockTenantService.update).toHaveBeenCalledWith(1, { | ||
name: 'Updated Tenant', | ||
address: '456 Updated St', | ||
}); | ||
expect(mockLogger.debug).toHaveBeenCalledWith( | ||
'Request for updating a tenant', | ||
{ name: 'Updated Tenant', address: '456 Updated St' }, | ||
); | ||
expect(mockLogger.info).toHaveBeenCalledWith('Tenant has been updated', { | ||
id: '1', | ||
}); | ||
expect(res.json).toHaveBeenCalledWith({ id: 1 }); | ||
}); | ||
|
||
it('should get a tenant successfully', async () => { | ||
const req = mockRequest({}, { id: '1' }); | ||
const res = mockResponse(); | ||
const tenant = { id: 1, name: 'Test Tenant', address: '123 Test St' }; | ||
mockTenantService.getById.mockResolvedValue(tenant); | ||
|
||
await tenantController.getOne(req, res, mockNext); | ||
|
||
expect(mockTenantService.getById).toHaveBeenCalledWith(1); | ||
expect(mockLogger.info).toHaveBeenCalledWith('Tenant has been fetched'); | ||
expect(res.json).toHaveBeenCalledWith(tenant); | ||
}); | ||
|
||
it('should delete a tenant successfully', async () => { | ||
const req = mockRequest({}, { id: '1' }); | ||
const res = mockResponse(); | ||
mockTenantService.deleteById.mockResolvedValue(undefined); | ||
|
||
await tenantController.destroy(req, res, mockNext); | ||
|
||
expect(mockTenantService.deleteById).toHaveBeenCalledWith(1); | ||
expect(mockLogger.info).toHaveBeenCalledWith('Tenant has been deleted', { | ||
id: 1, | ||
}); | ||
expect(res.json).toHaveBeenCalledWith({ id: 1 }); | ||
}); | ||
}); |
Oops, something went wrong.