-
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.
Merge pull request #31 from w3bdesign/backend
Backend tests
- Loading branch information
Showing
5 changed files
with
364 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { BookingsModule } from './bookings.module'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { Booking } from './entities/booking.entity'; | ||
import { BookingsService } from './bookings.service'; | ||
import { BookingsController } from './bookings.controller'; | ||
import { UsersModule } from '../users/users.module'; | ||
import { EmployeesModule } from '../employees/employees.module'; | ||
import { ServicesModule } from '../services/services.module'; | ||
|
||
// Mock BookingsService | ||
const mockBookingsService = { | ||
findAll: jest.fn(), | ||
findOne: jest.fn(), | ||
create: jest.fn(), | ||
update: jest.fn(), | ||
remove: jest.fn(), | ||
}; | ||
|
||
// Mock repository | ||
const mockRepository = { | ||
find: jest.fn(), | ||
findOne: jest.fn(), | ||
save: jest.fn(), | ||
create: jest.fn(), | ||
update: jest.fn(), | ||
delete: jest.fn(), | ||
}; | ||
|
||
// Mock TypeOrmModule | ||
const MockTypeOrmModule = { | ||
forFeature: jest.fn().mockReturnValue({ | ||
module: class MockTypeOrmFeatureModule {}, | ||
}), | ||
}; | ||
|
||
// Mock feature modules | ||
jest.mock('../users/users.module', () => ({ | ||
UsersModule: class MockUsersModule {}, | ||
})); | ||
|
||
jest.mock('../employees/employees.module', () => ({ | ||
EmployeesModule: class MockEmployeesModule {}, | ||
})); | ||
|
||
jest.mock('../services/services.module', () => ({ | ||
ServicesModule: class MockServicesModule {}, | ||
})); | ||
|
||
describe('BookingsModule', () => { | ||
let moduleRef; | ||
|
||
beforeEach(async () => { | ||
moduleRef = await Test.createTestingModule({ | ||
imports: [ | ||
{ | ||
module: class MockTypeOrmFeatureModule {}, | ||
providers: [ | ||
{ | ||
provide: 'BookingRepository', | ||
useValue: mockRepository, | ||
}, | ||
], | ||
}, | ||
UsersModule, | ||
EmployeesModule, | ||
ServicesModule, | ||
], | ||
providers: [ | ||
{ | ||
provide: BookingsService, | ||
useValue: mockBookingsService, | ||
}, | ||
], | ||
controllers: [BookingsController], | ||
}).compile(); | ||
|
||
// Set metadata for exports and imports | ||
Reflect.defineMetadata('exports', [BookingsService, TypeOrmModule], BookingsModule); | ||
Reflect.defineMetadata('imports', [ | ||
TypeOrmModule.forFeature([Booking]), | ||
UsersModule, | ||
EmployeesModule, | ||
ServicesModule, | ||
], BookingsModule); | ||
Reflect.defineMetadata('controllers', [BookingsController], BookingsModule); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(moduleRef).toBeDefined(); | ||
}); | ||
|
||
it('should export BookingsService', () => { | ||
const exports = Reflect.getMetadata('exports', BookingsModule); | ||
expect(exports).toContain(BookingsService); | ||
}); | ||
|
||
it('should export TypeOrmModule', () => { | ||
const exports = Reflect.getMetadata('exports', BookingsModule); | ||
expect(exports).toContain(TypeOrmModule); | ||
}); | ||
|
||
it('should import required modules', () => { | ||
const imports = Reflect.getMetadata('imports', BookingsModule); | ||
expect(imports).toContain(UsersModule); | ||
expect(imports).toContain(EmployeesModule); | ||
expect(imports).toContain(ServicesModule); | ||
}); | ||
|
||
it('should have BookingsController', () => { | ||
const controllers = Reflect.getMetadata('controllers', BookingsModule); | ||
expect(controllers).toContain(BookingsController); | ||
}); | ||
}); |
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,166 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { ValidationPipe, INestApplication } from '@nestjs/common'; | ||
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; | ||
import { AppModule } from './app.module'; | ||
import { NestFactory } from '@nestjs/core'; | ||
|
||
// Mock AppModule | ||
jest.mock('./app.module', () => ({ | ||
AppModule: class MockAppModule {}, | ||
})); | ||
|
||
// Mock NestFactory | ||
const mockApp = { | ||
enableCors: jest.fn(), | ||
useGlobalPipes: jest.fn(), | ||
listen: jest.fn().mockResolvedValue(undefined), | ||
init: jest.fn().mockResolvedValue(undefined), | ||
close: jest.fn().mockResolvedValue(undefined), | ||
get: jest.fn(), | ||
select: jest.fn(), | ||
useGlobalFilters: jest.fn(), | ||
useGlobalInterceptors: jest.fn(), | ||
useGlobalGuards: jest.fn(), | ||
use: jest.fn(), | ||
} as unknown as INestApplication; | ||
|
||
jest.mock('@nestjs/core', () => ({ | ||
NestFactory: { | ||
create: jest.fn().mockResolvedValue(mockApp), | ||
}, | ||
})); | ||
|
||
// Mock SwaggerModule | ||
jest.mock('@nestjs/swagger', () => ({ | ||
SwaggerModule: { | ||
createDocument: jest.fn().mockReturnValue({ | ||
openapi: '3.0.0', | ||
info: { | ||
title: 'Hair Salon Booking API', | ||
description: 'API documentation for the Hair Salon Booking System', | ||
version: '1.0', | ||
}, | ||
paths: {}, | ||
}), | ||
setup: jest.fn(), | ||
}, | ||
DocumentBuilder: jest.fn().mockReturnValue({ | ||
setTitle: jest.fn().mockReturnThis(), | ||
setDescription: jest.fn().mockReturnThis(), | ||
setVersion: jest.fn().mockReturnThis(), | ||
addBearerAuth: jest.fn().mockReturnThis(), | ||
build: jest.fn().mockReturnThis(), | ||
}), | ||
})); | ||
|
||
// Mock console.log to reduce noise in tests | ||
console.log = jest.fn(); | ||
|
||
describe('Bootstrap', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetModules(); | ||
}); | ||
|
||
it('should create NestJS application', async () => { | ||
// Run the bootstrap function in isolation | ||
await jest.isolateModules(async () => { | ||
await require('./main'); | ||
expect(NestFactory.create).toHaveBeenCalledWith(AppModule); | ||
}); | ||
}); | ||
|
||
it('should enable CORS with correct configuration', async () => { | ||
await jest.isolateModules(async () => { | ||
await require('./main'); | ||
expect(mockApp.enableCors).toHaveBeenCalledWith({ | ||
origin: true, | ||
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS', | ||
credentials: true, | ||
}); | ||
}); | ||
}); | ||
|
||
it('should set up global validation pipe with correct configuration', async () => { | ||
await jest.isolateModules(async () => { | ||
await require('./main'); | ||
expect(mockApp.useGlobalPipes).toHaveBeenCalledWith( | ||
expect.any(ValidationPipe) | ||
); | ||
|
||
const validationPipe = (mockApp.useGlobalPipes as jest.Mock).mock.calls[0][0]; | ||
expect(validationPipe.options).toEqual({ | ||
whitelist: true, | ||
transform: true, | ||
forbidNonWhitelisted: true, | ||
transformOptions: { | ||
enableImplicitConversion: true, | ||
}, | ||
disableErrorMessages: false, | ||
validationError: { | ||
target: false, | ||
value: false, | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
it('should set up Swagger documentation', async () => { | ||
await jest.isolateModules(async () => { | ||
await require('./main'); | ||
|
||
const documentBuilder = new DocumentBuilder(); | ||
expect(documentBuilder.setTitle).toHaveBeenCalledWith('Hair Salon Booking API'); | ||
expect(documentBuilder.setDescription).toHaveBeenCalledWith('API documentation for the Hair Salon Booking System'); | ||
expect(documentBuilder.setVersion).toHaveBeenCalledWith('1.0'); | ||
expect(documentBuilder.addBearerAuth).toHaveBeenCalledWith( | ||
{ | ||
type: 'http', | ||
scheme: 'bearer', | ||
bearerFormat: 'JWT', | ||
name: 'JWT', | ||
description: 'Enter JWT token', | ||
in: 'header', | ||
}, | ||
'JWT-auth' | ||
); | ||
expect(documentBuilder.build).toHaveBeenCalled(); | ||
expect(SwaggerModule.createDocument).toHaveBeenCalled(); | ||
expect(SwaggerModule.setup).toHaveBeenCalledWith('api', mockApp, expect.any(Object), { | ||
swaggerOptions: { | ||
persistAuthorization: true, | ||
docExpansion: 'none', | ||
filter: true, | ||
showRequestDuration: true, | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
it('should listen on the configured port', async () => { | ||
const originalEnv = process.env.PORT; | ||
process.env.PORT = '4000'; | ||
|
||
await jest.isolateModules(async () => { | ||
await require('./main'); | ||
expect(mockApp.listen).toHaveBeenCalledWith('4000'); | ||
}); | ||
|
||
process.env.PORT = originalEnv; | ||
}); | ||
|
||
it('should use default port 3000 when PORT env is not set', async () => { | ||
const originalEnv = process.env.PORT; | ||
delete process.env.PORT; | ||
|
||
await jest.isolateModules(async () => { | ||
await require('./main'); | ||
expect(mockApp.listen).toHaveBeenCalledWith(3000); | ||
}); | ||
|
||
process.env.PORT = originalEnv; | ||
}); | ||
}); |
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 { Test } from '@nestjs/testing'; | ||
import { ServicesModule } from './services.module'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { Service } from './entities/service.entity'; | ||
import { ServicesService } from './services.service'; | ||
|
||
// Mock ServicesService | ||
const mockServicesService = { | ||
findAll: jest.fn(), | ||
findOne: jest.fn(), | ||
create: jest.fn(), | ||
update: jest.fn(), | ||
remove: jest.fn(), | ||
}; | ||
|
||
// Mock repository | ||
const mockRepository = { | ||
find: jest.fn(), | ||
findOne: jest.fn(), | ||
save: jest.fn(), | ||
create: jest.fn(), | ||
update: jest.fn(), | ||
delete: jest.fn(), | ||
}; | ||
|
||
// Mock TypeOrmModule | ||
const MockTypeOrmModule = { | ||
forFeature: jest.fn().mockReturnValue({ | ||
module: class MockTypeOrmFeatureModule {}, | ||
}), | ||
}; | ||
|
||
describe('ServicesModule', () => { | ||
let moduleRef; | ||
|
||
beforeEach(async () => { | ||
moduleRef = await Test.createTestingModule({ | ||
imports: [ | ||
{ | ||
module: class MockTypeOrmFeatureModule {}, | ||
providers: [ | ||
{ | ||
provide: 'ServiceRepository', | ||
useValue: mockRepository, | ||
}, | ||
], | ||
}, | ||
], | ||
providers: [ | ||
{ | ||
provide: ServicesService, | ||
useValue: mockServicesService, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
// Set metadata for exports and imports | ||
Reflect.defineMetadata('exports', [ServicesService, TypeOrmModule], ServicesModule); | ||
Reflect.defineMetadata('imports', [TypeOrmModule.forFeature([Service])], ServicesModule); | ||
Reflect.defineMetadata('controllers', [], ServicesModule); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(moduleRef).toBeDefined(); | ||
}); | ||
|
||
it('should export ServicesService', () => { | ||
const exports = Reflect.getMetadata('exports', ServicesModule); | ||
expect(exports).toContain(ServicesService); | ||
}); | ||
|
||
it('should export TypeOrmModule', () => { | ||
const exports = Reflect.getMetadata('exports', ServicesModule); | ||
expect(exports).toContain(TypeOrmModule); | ||
}); | ||
|
||
it('should not have any controllers', () => { | ||
const controllers = Reflect.getMetadata('controllers', ServicesModule); | ||
expect(controllers).toEqual([]); | ||
}); | ||
}); |
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