Files
fennec-be/src/pipelines/pipelines.service.spec.ts
Ivan Li cba4c0464c feat(pipelines): list commit logs 使用直接订阅。
替代了先查询再订阅,确保数据不丢失。
2021-03-10 21:41:47 +08:00

70 lines
2.1 KiB
TypeScript

import { Test, TestingModule } from '@nestjs/testing';
import { PipelinesService } from './pipelines.service';
import { Pipeline } from './pipeline.entity';
import { getRepositoryToken } from '@nestjs/typeorm';
import { getQueueToken } from '@nestjs/bull';
import { LIST_LOGS_TASK } from '../repos/repos.constants';
import { Repository } from 'typeorm';
import { Project } from '../projects/project.entity';
import { Job, Queue } from 'bull';
import { ListLogsOption } from '../repos/models/list-logs.options';
describe('PipelinesService', () => {
let service: PipelinesService;
let repository: Repository<Pipeline>;
let pipeline: Pipeline;
let queue: Queue<ListLogsOption>;
beforeEach(async () => {
pipeline = Object.assign(new Pipeline(), {
id: 'test-pipeline',
name: 'pipeline',
branch: 'master',
projectId: 'test-project',
project: Object.assign(new Project(), {
id: 'test-project',
name: 'project',
} as Project),
} as Pipeline);
const module: TestingModule = await Test.createTestingModule({
providers: [
PipelinesService,
{
provide: getRepositoryToken(Pipeline),
useValue: {
findOneOrFail: jest.fn().mockImplementation(() => pipeline),
},
},
{
provide: getQueueToken(LIST_LOGS_TASK),
useValue: {
add: jest.fn().mockImplementation(() => ({ id: 1 } as Job)),
},
},
],
}).compile();
service = module.get<PipelinesService>(PipelinesService);
repository = module.get(getRepositoryToken(Pipeline));
queue = module.get(getQueueToken(LIST_LOGS_TASK));
});
it('should be defined', () => {
expect(service).toBeDefined();
});
describe('listLogsForPipeline', () => {
it('should send task to queue.', async () => {
const add = jest.spyOn(queue, 'add');
await expect(
service.listLogsForPipeline('test-pipeline'),
).resolves.toEqual({ id: 1 });
expect(add).toBeCalledWith({
project: pipeline.project,
branch: pipeline.branch,
});
});
});
});