2023-04-06 12:21:43 +00:00
|
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
|
|
import { PrismaHealthIndicatorUseCase } from '../../domain/usecases/prisma.health-indicator.usecase';
|
|
|
|
import { HealthCheckError, HealthIndicatorResult } from '@nestjs/terminus';
|
2023-04-06 15:05:25 +00:00
|
|
|
import { AdRepository } from '../../../matcher/adapters/secondaries/ad.repository';
|
|
|
|
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library';
|
2023-04-06 12:21:43 +00:00
|
|
|
|
2023-04-06 15:05:25 +00:00
|
|
|
const mockAdRepository = {
|
2023-04-06 12:21:43 +00:00
|
|
|
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: [
|
|
|
|
{
|
2023-04-06 15:05:25 +00:00
|
|
|
provide: AdRepository,
|
|
|
|
useValue: mockAdRepository,
|
2023-04-06 12:21:43 +00:00
|
|
|
},
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|