Skip to content

Commit 89c3a43

Browse files
committed
merge moved to testVariation controller
1 parent 3a04fd6 commit 89c3a43

File tree

7 files changed

+214
-197
lines changed

7 files changed

+214
-197
lines changed

src/test-runs/test-runs.controller.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,4 @@ export class TestRunsController {
9696
postTestRun(@Body() createTestRequestDto: CreateTestRequestDto): Promise<TestRunResultDto> {
9797
return this.testRunsService.postTestRun(createTestRequestDto);
9898
}
99-
100-
@Get('merge')
101-
@ApiQuery({ name: 'projectId', required: true })
102-
@ApiQuery({ name: 'branchName', required: true })
103-
@ApiBearerAuth()
104-
@UseGuards(JwtAuthGuard)
105-
merge(
106-
@Query('projectId', new ParseUUIDPipe()) projectId: string,
107-
@Query('branchName') branchName: string
108-
): Promise<void> {
109-
return this.testRunsService.merge(projectId, branchName);
110-
}
11199
}

src/test-runs/test-runs.service.spec.ts

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -959,127 +959,4 @@ describe('TestRunsService', () => {
959959
expect(createMock).toHaveBeenCalledWith(testVariation, createTestRequestDto);
960960
expect(mocked(TestRunResultDto)).toHaveBeenCalledWith(testRun, testVariation);
961961
});
962-
963-
it('merge', async () => {
964-
const mergedBranch = 'develop';
965-
const project: Project = {
966-
id: 'some id',
967-
name: 'some name',
968-
mainBranchName: 'master',
969-
updatedAt: new Date(),
970-
createdAt: new Date(),
971-
};
972-
const build: Build = {
973-
id: 'a9385fc1-884d-4f9f-915e-40da0e7773d5',
974-
number: null,
975-
branchName: project.mainBranchName,
976-
status: null,
977-
projectId: project.id,
978-
updatedAt: new Date(),
979-
createdAt: new Date(),
980-
userId: null,
981-
};
982-
const testVariation: TestVariation = {
983-
id: '123',
984-
projectId: project.id,
985-
name: 'Test name',
986-
baselineName: 'baselineName',
987-
os: 'OS',
988-
browser: 'browser',
989-
viewport: 'viewport',
990-
device: 'device',
991-
ignoreAreas: '[]',
992-
comment: 'some comment',
993-
createdAt: new Date(),
994-
updatedAt: new Date(),
995-
branchName: mergedBranch,
996-
};
997-
const testVariationNoBaseline: TestVariation = {
998-
id: '123',
999-
projectId: project.id,
1000-
name: 'Test name',
1001-
baselineName: null,
1002-
os: 'OS',
1003-
browser: 'browser',
1004-
viewport: 'viewport',
1005-
device: 'device',
1006-
ignoreAreas: '[]',
1007-
comment: 'some comment',
1008-
createdAt: new Date(),
1009-
updatedAt: new Date(),
1010-
branchName: mergedBranch,
1011-
};
1012-
const testVariationMainBranch: TestVariation = {
1013-
id: '123',
1014-
projectId: project.id,
1015-
name: 'Test name',
1016-
baselineName: 'baselineName',
1017-
os: 'OS',
1018-
browser: 'browser',
1019-
viewport: 'viewport',
1020-
device: 'device',
1021-
ignoreAreas: '[]',
1022-
comment: 'some comment',
1023-
createdAt: new Date(),
1024-
updatedAt: new Date(),
1025-
branchName: project.mainBranchName,
1026-
};
1027-
const projectFindOneMock = jest.fn().mockResolvedValueOnce(project);
1028-
const buildCreateMock = jest.fn().mockResolvedValueOnce(build);
1029-
const eventBuildCreatedMock = jest.fn();
1030-
const testVariationFindManyMock = jest.fn().mockResolvedValueOnce([testVariation, testVariationNoBaseline]);
1031-
const image = new PNG({
1032-
width: 10,
1033-
height: 10,
1034-
});
1035-
const getImageMock = jest
1036-
.fn()
1037-
.mockReturnValueOnce(image)
1038-
.mockReturnValueOnce(null);
1039-
const testVariationFindOrCreateMock = jest.fn().mockResolvedValueOnce(testVariationMainBranch);
1040-
const createMock = jest.fn();
1041-
const service = await initService({
1042-
projectFindOneMock,
1043-
buildCreateMock,
1044-
eventBuildCreatedMock,
1045-
testVariationFindManyMock,
1046-
testVariationFindOrCreateMock,
1047-
getImageMock,
1048-
});
1049-
service.create = createMock;
1050-
1051-
await service.merge(project.id, mergedBranch);
1052-
1053-
expect(projectFindOneMock).toHaveBeenCalledWith({ where: { id: project.id } });
1054-
expect(buildCreateMock).toHaveBeenCalledWith({
1055-
data: {
1056-
branchName: project.mainBranchName,
1057-
project: {
1058-
connect: {
1059-
id: project.id,
1060-
},
1061-
},
1062-
},
1063-
});
1064-
expect(eventBuildCreatedMock).toHaveBeenCalledWith(new BuildDto(build));
1065-
expect(testVariationFindManyMock).toHaveBeenCalledWith({
1066-
where: { projectId: project.id, branchName: mergedBranch },
1067-
});
1068-
expect(getImageMock).toHaveBeenCalledWith(testVariation.baselineName);
1069-
expect(testVariationFindOrCreateMock).toHaveBeenCalledWith(project.id, {
1070-
name: testVariation.name,
1071-
os: testVariation.os,
1072-
device: testVariation.device,
1073-
browser: testVariation.browser,
1074-
viewport: testVariation.viewport,
1075-
branchName: project.mainBranchName,
1076-
});
1077-
expect(createMock).toHaveBeenCalledWith(testVariationMainBranch, {
1078-
...testVariation,
1079-
buildId: build.id,
1080-
imageBase64: PNG.sync.write(image).toString('base64'),
1081-
diffTollerancePercent: 0,
1082-
merge: true,
1083-
});
1084-
});
1085962
});

src/test-runs/test-runs.service.ts

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { CreateTestRequestDto } from './dto/create-test-request.dto';
55
import { IgnoreAreaDto } from './dto/ignore-area.dto';
66
import { StaticService } from '../shared/static/static.service';
77
import { PrismaService } from '../prisma/prisma.service';
8-
import { TestRun, TestStatus, TestVariation, Project, Build } from '@prisma/client';
8+
import { TestRun, TestStatus, TestVariation } from '@prisma/client';
99
import { DiffResult } from './diffResult';
1010
import { EventsGateway } from '../events/events.gateway';
1111
import { CommentDto } from '../shared/dto/comment.dto';
@@ -44,60 +44,6 @@ export class TestRunsService {
4444
});
4545
}
4646

47-
async merge(projectId: string, branchName: string): Promise<void> {
48-
const project: Project = await this.prismaService.project.findOne({ where: { id: projectId } });
49-
50-
// create build
51-
const build: Build = await this.prismaService.build.create({
52-
data: {
53-
branchName: project.mainBranchName,
54-
project: {
55-
connect: {
56-
id: project.id,
57-
},
58-
},
59-
},
60-
});
61-
this.eventsGateway.buildCreated(new BuildDto(build));
62-
63-
// find side branch variations
64-
const testVariations: TestVariation[] = await this.prismaService.testVariation.findMany({
65-
where: { projectId, branchName },
66-
});
67-
68-
// compare to main branch variations
69-
await Promise.all(
70-
testVariations.map(async sideBranchTestVariation => {
71-
const baseline = this.staticService.getImage(sideBranchTestVariation.baselineName);
72-
if (baseline) {
73-
try {
74-
let imageBase64 = PNG.sync.write(baseline).toString('base64');
75-
76-
// get main branch variation
77-
const baselineData = convertBaselineDataToQuery({
78-
...sideBranchTestVariation,
79-
branchName: project.mainBranchName,
80-
});
81-
const mainBranchTestVariation = await this.testVariationService.findOrCreate(projectId, baselineData);
82-
83-
// get side branch request
84-
const createTestRequestDto: CreateTestRequestDto = {
85-
...sideBranchTestVariation,
86-
buildId: build.id,
87-
imageBase64,
88-
diffTollerancePercent: 0,
89-
merge: true,
90-
};
91-
92-
return this.create(mainBranchTestVariation, createTestRequestDto);
93-
} catch (err) {
94-
console.log(err);
95-
}
96-
}
97-
})
98-
);
99-
}
100-
10147
async postTestRun(createTestRequestDto: CreateTestRequestDto): Promise<TestRunResultDto> {
10248
const baselineData = convertBaselineDataToQuery(createTestRequestDto);
10349

src/test-variations/test-variations.controller.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,16 @@ export class TestVariationsController {
4848
updateComment(@Param('id', new ParseUUIDPipe()) id: string, @Body() body: CommentDto): Promise<TestVariation> {
4949
return this.testVariations.updateComment(id, body);
5050
}
51+
52+
@Get('merge')
53+
@ApiQuery({ name: 'projectId', required: true })
54+
@ApiQuery({ name: 'branchName', required: true })
55+
@ApiBearerAuth()
56+
@UseGuards(JwtAuthGuard)
57+
merge(
58+
@Query('projectId', new ParseUUIDPipe()) projectId: string,
59+
@Query('branchName') branchName: string
60+
): Promise<void> {
61+
return this.testVariations.merge(projectId, branchName);
62+
}
5163
}

src/test-variations/test-variations.module.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import { Module } from '@nestjs/common';
22
import { TestVariationsService } from './test-variations.service';
33
import { TestVariationsController } from './test-variations.controller';
44
import { PrismaService } from '../prisma/prisma.service';
5+
import { TestRunsService } from '../test-runs/test-runs.service';
6+
import { EventsGateway } from '../events/events.gateway';
7+
import { BuildsService } from '../builds/builds.service';
58

69
@Module({
7-
providers: [TestVariationsService, PrismaService],
10+
providers: [TestVariationsService, PrismaService, TestRunsService, EventsGateway, BuildsService],
811
controllers: [TestVariationsController],
9-
exports: [TestVariationsService]
12+
exports: [TestVariationsService],
1013
})
1114
export class TestVariationsModule {}

0 commit comments

Comments
 (0)