Skip to content

Commit c0016f5

Browse files
authored
242 latest main branch approve (#112)
* Compare vs latest main branch baseline after autoApprove closes: Visual-Regression-Tracker/Visual-Regression-Tracker#242 * tests are added
1 parent bcf9f97 commit c0016f5

File tree

4 files changed

+214
-95
lines changed

4 files changed

+214
-95
lines changed

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

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ describe('TestRunsService', () => {
226226
});
227227
service.findOne = testRunFindOneMock;
228228

229-
await service.approve(testRun.id, false);
229+
await service.approve(testRun.id);
230230

231231
expect(testRunFindOneMock).toHaveBeenCalledWith(testRun.id);
232232
expect(getImageMock).toHaveBeenCalledWith(testRun.imageName);
@@ -238,14 +238,14 @@ describe('TestRunsService', () => {
238238
testVariation: {
239239
update: {
240240
baselineName,
241-
baselines: {
242-
create: {
243-
baselineName,
244-
testRun: {
245-
connect: {
246-
id: testRun.id,
247-
},
248-
},
241+
},
242+
},
243+
baseline: {
244+
create: {
245+
baselineName,
246+
testVariation: {
247+
connect: {
248+
id: testRun.testVariationId,
249249
},
250250
},
251251
},
@@ -318,14 +318,14 @@ describe('TestRunsService', () => {
318318
testVariation: {
319319
update: {
320320
baselineName,
321-
baselines: {
322-
create: {
323-
baselineName,
324-
testRun: {
325-
connect: {
326-
id: testRun.id,
327-
},
328-
},
321+
},
322+
},
323+
baseline: {
324+
create: {
325+
baselineName,
326+
testVariation: {
327+
connect: {
328+
id: testRun.testVariationId,
329329
},
330330
},
331331
},
@@ -427,39 +427,37 @@ describe('TestRunsService', () => {
427427
expect(testRunFindOneMock).toHaveBeenCalledWith(testRun.id);
428428
expect(getImageMock).toHaveBeenCalledWith(testRun.imageName);
429429
expect(saveImageMock).toHaveBeenCalledTimes(1);
430-
expect(testVariationCreateMock).toBeCalledWith({
431-
data: {
432-
project: { connect: { id: testRun.testVariation.projectId } },
433-
baselineName,
434-
name: testRun.name,
435-
browser: testRun.browser,
436-
device: testRun.device,
437-
os: testRun.os,
438-
viewport: testRun.viewport,
439-
ignoreAreas: testRun.ignoreAreas,
440-
comment: testRun.comment,
441-
branchName: testRun.branchName,
442-
},
443-
});
444-
expect(baselineCreateMock).toHaveBeenCalledWith({
445-
data: {
446-
baselineName,
447-
testVariation: {
448-
connect: { id: newTestVariation.id },
449-
},
450-
testRun: {
451-
connect: {
452-
id: testRun.id,
453-
},
454-
},
455-
},
456-
});
457430
expect(testRunUpdateMock).toHaveBeenCalledWith({
458431
where: { id: testRun.id },
459432
data: {
460433
status: TestStatus.approved,
461-
testVariation: {
462-
connect: { id: newTestVariation.id },
434+
baseline: {
435+
create: {
436+
baselineName,
437+
testVariation: {
438+
create: {
439+
baselineName,
440+
name: testRun.name,
441+
browser: testRun.browser,
442+
device: testRun.device,
443+
os: testRun.os,
444+
viewport: testRun.viewport,
445+
ignoreAreas: testRun.ignoreAreas,
446+
comment: testRun.comment,
447+
branchName: testRun.branchName,
448+
project: {
449+
connect: {
450+
id: testRun.testVariation.projectId,
451+
},
452+
},
453+
testRuns: {
454+
connect: {
455+
id: testRun.id,
456+
},
457+
},
458+
},
459+
},
460+
},
463461
},
464462
},
465463
});

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

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,15 @@ export class TestRunsService {
7878
return new TestRunResultDto(testRunWithResult, testVariation);
7979
}
8080

81-
async approve(id: string, merge: boolean, autoApprove?: boolean): Promise<TestRun> {
81+
/**
82+
* Confirm difference for testRun
83+
*
84+
* @param id
85+
* @param merge replaces main branch baseline with feature one
86+
* @param autoApprove set auto approve status
87+
* @returns
88+
*/
89+
async approve(id: string, merge = false, autoApprove = false): Promise<TestRun> {
8290
this.logger.log(`Approving testRun: ${id} merge: ${merge} autoApprove: ${autoApprove}`);
8391
const status = autoApprove ? TestStatus.autoApproved : TestStatus.approved;
8492
const testRun = await this.findOne(id);
@@ -88,58 +96,60 @@ export class TestRunsService {
8896
const baselineName = this.staticService.saveImage('baseline', PNG.sync.write(baseline));
8997
let testRunUpdated: TestRun;
9098
if (merge || testRun.branchName === testRun.baselineBranchName) {
99+
// update existing test variation
91100
testRunUpdated = await this.prismaService.testRun.update({
92101
where: { id },
93102
data: {
94103
status,
95104
testVariation: {
96105
update: {
97106
baselineName,
98-
baselines: {
99-
create: {
100-
baselineName,
101-
testRun: {
102-
connect: {
103-
id: testRun.id,
104-
},
105-
},
107+
},
108+
},
109+
baseline: {
110+
create: {
111+
baselineName,
112+
testVariation: {
113+
connect: {
114+
id: testRun.testVariationId,
106115
},
107116
},
108117
},
109118
},
110119
},
111120
});
112121
} else {
113-
const newTestVariation = await this.prismaService.testVariation.create({
114-
data: {
115-
project: { connect: { id: testRun.testVariation.projectId } },
116-
baselineName,
117-
...getTestVariationUniqueData(testRun),
118-
ignoreAreas: testRun.ignoreAreas,
119-
comment: testRun.comment,
120-
branchName: testRun.branchName,
121-
},
122-
});
123-
await this.prismaService.baseline.create({
124-
data: {
125-
baselineName,
126-
testVariation: {
127-
connect: { id: newTestVariation.id },
128-
},
129-
testRun: {
130-
connect: {
131-
id: testRun.id,
132-
},
133-
},
134-
},
135-
});
122+
// create new feature branch test variation
136123
testRunUpdated = await this.prismaService.testRun.update({
137124
where: { id },
138125
data: {
139126
status,
140-
testVariation: {
141-
connect: { id: newTestVariation.id },
142-
},
127+
baseline: autoApprove
128+
? undefined
129+
: {
130+
create: {
131+
baselineName,
132+
testVariation: {
133+
create: {
134+
baselineName,
135+
...getTestVariationUniqueData(testRun),
136+
ignoreAreas: testRun.ignoreAreas,
137+
comment: testRun.comment,
138+
branchName: testRun.branchName,
139+
project: {
140+
connect: {
141+
id: testRun.testVariation.projectId,
142+
},
143+
},
144+
testRuns: {
145+
connect: {
146+
id,
147+
},
148+
},
149+
},
150+
},
151+
},
152+
},
143153
},
144154
});
145155
}

0 commit comments

Comments
 (0)