Skip to content

Commit 353170a

Browse files
committed
merge covered with tests
1 parent 9497e9d commit 353170a

File tree

2 files changed

+159
-22
lines changed

2 files changed

+159
-22
lines changed

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

Lines changed: 136 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { TestRunsService } from './test-runs.service';
44
import { PrismaService } from '../prisma/prisma.service';
55
import { StaticService } from '../shared/static/static.service';
66
import { PNG } from 'pngjs';
7-
import { TestStatus, Build, TestRun, TestVariation } from '@prisma/client';
7+
import { TestStatus, Build, TestRun, TestVariation, Project } from '@prisma/client';
88
import Pixelmatch from 'pixelmatch';
99
import { CreateTestRequestDto } from './dto/create-test-request.dto';
1010
import { TestRunResultDto } from './dto/testRunResult.dto';
@@ -30,10 +30,14 @@ const initService = async ({
3030
deleteImageMock = jest.fn(),
3131
eventNewTestRunMock = jest.fn(),
3232
eventBuildUpdatedMock = jest.fn(),
33+
eventBuildCreatedMock = jest.fn(),
3334
buildFindOneMock = jest.fn(),
35+
buildCreateMock = jest.fn(),
3436
testVariationCreateMock = jest.fn(),
37+
testVariationFindManyMock = jest.fn(),
3538
baselineCreateMock = jest.fn(),
3639
testVariationFindOrCreateMock = jest.fn(),
40+
projectFindOneMock = jest.fn(),
3741
}) => {
3842
const module: TestingModule = await Test.createTestingModule({
3943
providers: [
@@ -50,13 +54,18 @@ const initService = async ({
5054
},
5155
build: {
5256
findOne: buildFindOneMock,
57+
create: buildCreateMock,
5358
},
5459
testVariation: {
5560
create: testVariationCreateMock,
61+
findMany: testVariationFindManyMock,
5662
},
5763
baseline: {
5864
create: baselineCreateMock,
5965
},
66+
project: {
67+
findOne: projectFindOneMock,
68+
},
6069
},
6170
},
6271
{
@@ -72,6 +81,7 @@ const initService = async ({
7281
useValue: {
7382
newTestRun: eventNewTestRunMock,
7483
buildUpdated: eventBuildUpdatedMock,
84+
buildCreated: eventBuildCreatedMock,
7585
},
7686
},
7787
{
@@ -159,7 +169,7 @@ describe('TestRunsService', () => {
159169
branchName: 'master',
160170
baselineBranchName: 'master',
161171
comment: 'some comment',
162-
merge: false
172+
merge: false,
163173
};
164174
const testRunUpdateMock = jest.fn();
165175
const testRunFindOneMock = jest.fn().mockResolvedValueOnce(testRun);
@@ -231,7 +241,7 @@ describe('TestRunsService', () => {
231241
branchName: 'develop',
232242
baselineBranchName: 'master',
233243
comment: 'some comment',
234-
merge: false
244+
merge: false,
235245
};
236246
const testRunUpdateMock = jest.fn();
237247
const testRunFindOneMock = jest.fn().mockResolvedValueOnce(testRun);
@@ -949,4 +959,127 @@ describe('TestRunsService', () => {
949959
expect(createMock).toHaveBeenCalledWith(testVariation, createTestRequestDto);
950960
expect(mocked(TestRunResultDto)).toHaveBeenCalledWith(testRun, testVariation);
951961
});
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+
});
9521085
});

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,29 @@ export class TestRunsService {
7070
testVariations.map(async sideBranchTestVariation => {
7171
const baseline = this.staticService.getImage(sideBranchTestVariation.baselineName);
7272
if (baseline) {
73-
// get main branch variation
74-
const baselineData = convertBaselineDataToQuery({
75-
...sideBranchTestVariation,
76-
branchName: project.mainBranchName,
77-
});
78-
const mainBranchTestVariation = await this.testVariationService.findOrCreate(projectId, baselineData);
79-
80-
// get side branch request
81-
const imageBase64 = PNG.sync.write(baseline).toString('base64');
82-
const createTestRequestDto: CreateTestRequestDto = {
83-
...sideBranchTestVariation,
84-
projectId,
85-
buildId: build.id,
86-
imageBase64,
87-
diffTollerancePercent: 0,
88-
merge: true,
89-
};
90-
91-
return this.create(mainBranchTestVariation, createTestRequestDto);
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+
}
9296
}
9397
})
9498
);

0 commit comments

Comments
 (0)