Skip to content

Commit f0a724e

Browse files
authored
Merge pull request #10 from Visual-Regression-Tracker/build-service-tests
Build service tests
2 parents fe6d85f + 1edc39d commit f0a724e

File tree

4 files changed

+508
-24
lines changed

4 files changed

+508
-24
lines changed

src/builds/builds.service.spec.ts

Lines changed: 153 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,164 @@ import { BuildsService } from './builds.service';
33
import { PrismaService } from '../prisma/prisma.service';
44
import { TestRunsService } from '../test-runs/test-runs.service';
55
import { EventsGateway } from '../events/events.gateway';
6+
import { CreateBuildDto } from './dto/build-create.dto';
7+
import { Build, TestRun } from '@prisma/client';
8+
import { mocked } from 'ts-jest/utils';
9+
import { BuildDto } from './dto/build.dto';
10+
11+
jest.mock('./dto/build.dto');
12+
13+
const initService = async ({
14+
buildFindManyMock = jest.fn(),
15+
buildCreateMock = jest.fn(),
16+
buildFindOneMock = jest.fn(),
17+
buildDeleteMock = jest.fn(),
18+
testRunDeleteMock = jest.fn(),
19+
eventsBuildCreatedMock = jest.fn(),
20+
}) => {
21+
const module: TestingModule = await Test.createTestingModule({
22+
providers: [
23+
BuildsService,
24+
{
25+
provide: PrismaService,
26+
useValue: {
27+
build: {
28+
findMany: buildFindManyMock,
29+
create: buildCreateMock,
30+
findOne: buildFindOneMock,
31+
delete: buildDeleteMock,
32+
},
33+
},
34+
},
35+
{
36+
provide: TestRunsService,
37+
useValue: {
38+
delete: testRunDeleteMock,
39+
},
40+
},
41+
{
42+
provide: EventsGateway,
43+
useValue: {
44+
buildCreated: eventsBuildCreatedMock,
45+
},
46+
},
47+
],
48+
}).compile();
49+
50+
return module.get<BuildsService>(BuildsService);
51+
};
652

753
describe('BuildsService', () => {
854
let service: BuildsService;
955

10-
beforeEach(async () => {
11-
const module: TestingModule = await Test.createTestingModule({
12-
providers: [
13-
BuildsService,
14-
{ provide: PrismaService, useValue: {} },
15-
{ provide: TestRunsService, useValue: {} },
16-
{ provide: EventsGateway, useValue: {} },
17-
],
18-
}).compile();
19-
20-
service = module.get<BuildsService>(BuildsService);
56+
const build: Build & {
57+
testRuns: TestRun[];
58+
} = {
59+
id: 'a9385fc1-884d-4f9f-915e-40da0e7773d5',
60+
number: null,
61+
branchName: 'develop',
62+
status: null,
63+
projectId: 'e0a37894-6f29-478d-b13e-6182fecc715e',
64+
updatedAt: new Date(),
65+
createdAt: new Date(),
66+
userId: null,
67+
testRuns: [
68+
{
69+
id: '10fb5e02-64e0-4cf5-9f17-c00ab3c96658',
70+
imageName: '1592423768112.screenshot.png',
71+
diffName: null,
72+
diffPercent: null,
73+
diffTollerancePercent: 1,
74+
pixelMisMatchCount: null,
75+
status: 'new',
76+
buildId: '146e7a8d-89f0-4565-aa2c-e61efabb0afd',
77+
testVariationId: '3bc4a5bc-006e-4d43-8e4e-eaa132627fca',
78+
updatedAt: new Date(),
79+
createdAt: new Date(),
80+
name: 'ss2f77',
81+
browser: 'chromium',
82+
device: null,
83+
os: null,
84+
viewport: '1800x1600',
85+
baselineName: null,
86+
ignoreAreas: '[]',
87+
},
88+
],
89+
};
90+
91+
const buildDto: BuildDto = {
92+
id: 'a9385fc1-884d-4f9f-915e-40da0e7773d5',
93+
number: null,
94+
branchName: 'develop',
95+
status: 'new',
96+
projectId: 'e0a37894-6f29-478d-b13e-6182fecc715e',
97+
updatedAt: new Date(),
98+
createdAt: new Date(),
99+
userId: null,
100+
passedCount: 0,
101+
unresolvedCount: 0,
102+
failedCount: 0,
103+
};
104+
105+
it('findMany', async () => {
106+
const buildFindManyMock = jest.fn().mockResolvedValueOnce([build]);
107+
const projectId = 'someId';
108+
mocked(BuildDto).mockReturnValueOnce(buildDto);
109+
service = await initService({ buildFindManyMock });
110+
111+
const result = await service.findMany(projectId);
112+
113+
expect(buildFindManyMock).toHaveBeenCalledWith({
114+
include: { testRuns: true },
115+
orderBy: { createdAt: 'desc' },
116+
where: { projectId },
117+
});
118+
expect(result).toEqual([buildDto]);
21119
});
22120

23-
it('should be defined', () => {
24-
expect(service).toBeDefined();
121+
it('create', async () => {
122+
const createBuildDto: CreateBuildDto = {
123+
branchName: 'branchName',
124+
projectId: 'projectId',
125+
};
126+
const buildCreateMock = jest.fn().mockResolvedValueOnce(build);
127+
const eventsBuildCreatedMock = jest.fn();
128+
mocked(BuildDto).mockReturnValueOnce(buildDto);
129+
service = await initService({ buildCreateMock, eventsBuildCreatedMock });
130+
131+
const result = await service.create(createBuildDto);
132+
133+
expect(buildCreateMock).toHaveBeenCalledWith({
134+
data: {
135+
branchName: createBuildDto.branchName,
136+
project: {
137+
connect: {
138+
id: createBuildDto.projectId,
139+
},
140+
},
141+
},
142+
});
143+
expect(eventsBuildCreatedMock).toHaveBeenCalledWith(buildDto);
144+
expect(result).toBe(buildDto);
145+
});
146+
147+
it('delete', async () => {
148+
const buildFindOneMock = jest.fn().mockResolvedValueOnce(build);
149+
const buildDeleteMock = jest.fn();
150+
const testRunDeleteMock = jest.fn();
151+
service = await initService({ buildFindOneMock, buildDeleteMock, testRunDeleteMock });
152+
153+
await service.remove(build.id);
154+
155+
expect(buildFindOneMock).toHaveBeenCalledWith({
156+
where: { id: build.id },
157+
include: {
158+
testRuns: true,
159+
},
160+
});
161+
expect(testRunDeleteMock).toHaveBeenCalledWith(build.testRuns[0].id);
162+
expect(buildDeleteMock).toHaveBeenCalledWith({
163+
where: { id: build.id },
164+
});
25165
});
26166
});

src/builds/builds.service.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ export class BuildsService {
3636
},
3737
},
3838
},
39-
include: {
40-
testRuns: true,
41-
},
4239
});
4340
const buildDto = new BuildDto(build);
4441
this.eventsGateway.buildCreated(buildDto);
@@ -53,11 +50,7 @@ export class BuildsService {
5350
},
5451
});
5552

56-
try {
57-
await Promise.all(build.testRuns.map(testRun => this.testRunsService.delete(testRun.id)));
58-
} catch (err) {
59-
console.log(err);
60-
}
53+
await Promise.all(build.testRuns.map(testRun => this.testRunsService.delete(testRun.id)));
6154

6255
return this.prismaService.build.delete({
6356
where: { id },

0 commit comments

Comments
 (0)