59 lines
1.8 KiB
TypeScript
59 lines
1.8 KiB
TypeScript
|
import { Test, TestingModule } from '@nestjs/testing';
|
||
|
import { PrismaHealthIndicatorUseCase } from '../../domain/usecases/prisma.health-indicator.usecase';
|
||
|
import { TerritoriesRepository } from '../../../territory/adapters/secondaries/territories.repository';
|
||
|
import { PrismaClientKnownRequestError } from '@prisma/client/runtime';
|
||
|
import { HealthCheckError, HealthIndicatorResult } from '@nestjs/terminus';
|
||
|
|
||
|
const mockTerritoriesRepository = {
|
||
|
healthCheck: jest
|
||
|
.fn()
|
||
|
.mockImplementationOnce(() => {
|
||
|
return Promise.resolve(true);
|
||
|
})
|
||
|
.mockImplementation(() => {
|
||
|
throw new PrismaClientKnownRequestError('Service unavailable', {
|
||
|
code: 'code',
|
||
|
clientVersion: 'version',
|
||
|
});
|
||
|
}),
|
||
|
};
|
||
|
|
||
|
describe('PrismaHealthIndicatorUseCase', () => {
|
||
|
let prismaHealthIndicatorUseCase: PrismaHealthIndicatorUseCase;
|
||
|
|
||
|
beforeAll(async () => {
|
||
|
const module: TestingModule = await Test.createTestingModule({
|
||
|
providers: [
|
||
|
{
|
||
|
provide: TerritoriesRepository,
|
||
|
useValue: mockTerritoriesRepository,
|
||
|
},
|
||
|
PrismaHealthIndicatorUseCase,
|
||
|
],
|
||
|
}).compile();
|
||
|
|
||
|
prismaHealthIndicatorUseCase = module.get<PrismaHealthIndicatorUseCase>(
|
||
|
PrismaHealthIndicatorUseCase,
|
||
|
);
|
||
|
});
|
||
|
|
||
|
it('should be defined', () => {
|
||
|
expect(prismaHealthIndicatorUseCase).toBeDefined();
|
||
|
});
|
||
|
|
||
|
describe('execute', () => {
|
||
|
it('should check health successfully', async () => {
|
||
|
const healthIndicatorResult: HealthIndicatorResult =
|
||
|
await prismaHealthIndicatorUseCase.isHealthy('prisma');
|
||
|
|
||
|
expect(healthIndicatorResult['prisma'].status).toBe('up');
|
||
|
});
|
||
|
|
||
|
it('should throw an error if database is unavailable', async () => {
|
||
|
await expect(
|
||
|
prismaHealthIndicatorUseCase.isHealthy('prisma'),
|
||
|
).rejects.toBeInstanceOf(HealthCheckError);
|
||
|
});
|
||
|
});
|
||
|
});
|