2023-04-06 12:21:43 +00:00
|
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
|
|
import { HealthCheckError, HealthIndicatorResult } from '@nestjs/terminus';
|
2023-08-16 10:28:20 +00:00
|
|
|
import { RepositoriesHealthIndicatorUseCase } from '../../domain/usecases/repositories.health-indicator.usecase';
|
2023-04-25 15:49:47 +00:00
|
|
|
import { AdRepository } from '../../../ad/adapters/secondaries/ad.repository';
|
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(() => {
|
2023-08-16 10:28:20 +00:00
|
|
|
throw new Error('an error occured in the repository');
|
2023-04-06 12:21:43 +00:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2023-08-16 10:28:20 +00:00
|
|
|
describe('RepositoriesHealthIndicatorUseCase', () => {
|
|
|
|
let repositoriesHealthIndicatorUseCase: RepositoriesHealthIndicatorUseCase;
|
2023-04-06 12:21:43 +00:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
|
|
providers: [
|
2023-08-16 10:28:20 +00:00
|
|
|
RepositoriesHealthIndicatorUseCase,
|
2023-04-06 12:21:43 +00:00
|
|
|
{
|
2023-04-06 15:05:25 +00:00
|
|
|
provide: AdRepository,
|
|
|
|
useValue: mockAdRepository,
|
2023-04-06 12:21:43 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
}).compile();
|
|
|
|
|
2023-08-16 10:28:20 +00:00
|
|
|
repositoriesHealthIndicatorUseCase =
|
|
|
|
module.get<RepositoriesHealthIndicatorUseCase>(
|
|
|
|
RepositoriesHealthIndicatorUseCase,
|
|
|
|
);
|
2023-04-06 12:21:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be defined', () => {
|
2023-08-16 10:28:20 +00:00
|
|
|
expect(repositoriesHealthIndicatorUseCase).toBeDefined();
|
2023-04-06 12:21:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('execute', () => {
|
|
|
|
it('should check health successfully', async () => {
|
|
|
|
const healthIndicatorResult: HealthIndicatorResult =
|
2023-08-16 10:28:20 +00:00
|
|
|
await repositoriesHealthIndicatorUseCase.isHealthy('repositories');
|
2023-04-06 12:21:43 +00:00
|
|
|
|
2023-08-16 10:28:20 +00:00
|
|
|
expect(healthIndicatorResult['repositories'].status).toBe('up');
|
2023-04-06 12:21:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if database is unavailable', async () => {
|
|
|
|
await expect(
|
2023-08-16 10:28:20 +00:00
|
|
|
repositoriesHealthIndicatorUseCase.isHealthy('repositories'),
|
2023-04-06 12:21:43 +00:00
|
|
|
).rejects.toBeInstanceOf(HealthCheckError);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|