Skip to content

Commit cc0b781

Browse files
committed
tests are added
1 parent 11bcdb2 commit cc0b781

File tree

3 files changed

+116
-81
lines changed

3 files changed

+116
-81
lines changed

src/projects/projects.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class ProjectsService {
4949
try {
5050
await Promise.all(project.builds.map(build => this.buildsService.remove(build.id)));
5151
await Promise.all(
52-
project.testVariations.map(testVariation => this.testVariationsService.remove(testVariation.id))
52+
project.testVariations.map(testVariation => this.testVariationsService.delete(testVariation.id))
5353
);
5454
} catch (err) {
5555
console.log(err);

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

Lines changed: 113 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { PrismaService } from '../prisma/prisma.service';
44
import { CreateTestRequestDto } from '../test-runs/dto/create-test-request.dto';
55
import { StaticService } from '../shared/static/static.service';
66
import { IgnoreAreaDto } from '../test-runs/dto/ignore-area.dto';
7-
import { TestVariation, Baseline, Project, Build } from '@prisma/client';
7+
import { TestVariation, Baseline, Project, Build, TestRun } from '@prisma/client';
88
import { CommentDto } from '../shared/dto/comment.dto';
99
import { convertBaselineDataToQuery } from '../shared/dto/baseline-data.dto';
1010
import { PNG } from 'pngjs';
@@ -23,6 +23,8 @@ const initModule = async ({
2323
projectFindOneMock = jest.fn(),
2424
buildCreateMock = jest.fn(),
2525
testRunCreateMock = jest.fn(),
26+
testRunFindMany = jest.fn(),
27+
testRunDeleteMock = jest.fn(),
2628
}) => {
2729
const module: TestingModule = await Test.createTestingModule({
2830
providers: [
@@ -43,6 +45,7 @@ const initModule = async ({
4345
{
4446
provide: TestRunsService,
4547
useValue: {
48+
delete: testRunDeleteMock,
4649
create: testRunCreateMock,
4750
},
4851
},
@@ -62,6 +65,9 @@ const initModule = async ({
6265
project: {
6366
findOne: projectFindOneMock,
6467
},
68+
testRun: {
69+
findMany: testRunFindMany,
70+
},
6571
},
6672
},
6773
],
@@ -300,59 +306,6 @@ describe('TestVariationsService', () => {
300306
});
301307
});
302308

303-
describe('remove', () => {
304-
it('can remove', async () => {
305-
const id = 'test id';
306-
const variation: TestVariation & {
307-
baselines: Baseline[];
308-
} = {
309-
id,
310-
projectId: 'project Id',
311-
name: 'Test name',
312-
baselineName: 'baselineName',
313-
os: 'OS',
314-
browser: 'browser',
315-
viewport: 'viewport',
316-
device: 'device',
317-
ignoreAreas: '[]',
318-
comment: 'some comment',
319-
branchName: 'develop',
320-
createdAt: new Date(),
321-
updatedAt: new Date(),
322-
baselines: [
323-
{
324-
id: 'baseline id 1',
325-
baselineName: 'image name 1',
326-
testVariationId: id,
327-
testRunId: 'test run id 1',
328-
createdAt: new Date(),
329-
updatedAt: new Date(),
330-
},
331-
],
332-
};
333-
const variationFindOneMock = jest.fn();
334-
const variationDeleteMock = jest.fn();
335-
const imageDeleteMock = jest.fn();
336-
const baselineDeleteMock = jest.fn();
337-
service = await initModule({
338-
variationFindOneMock: variationFindOneMock.mockResolvedValueOnce(variation),
339-
variationDeleteMock,
340-
imageDeleteMock,
341-
baselineDeleteMock,
342-
});
343-
344-
await service.remove(id);
345-
346-
expect(imageDeleteMock).toHaveBeenCalledWith(variation.baselines[0].baselineName);
347-
expect(baselineDeleteMock).toHaveBeenCalledWith({
348-
where: { id: variation.baselines[0].id },
349-
});
350-
expect(variationDeleteMock).toHaveBeenCalledWith({
351-
where: { id: variation.id },
352-
});
353-
});
354-
});
355-
356309
it('updateComment', async () => {
357310
const id = 'some id';
358311
const commentDto: CommentDto = {
@@ -517,4 +470,110 @@ describe('TestVariationsService', () => {
517470
});
518471
expect(testRunCreateMock).toHaveBeenCalledTimes(2);
519472
});
473+
474+
it('delete', async () => {
475+
const testRunId = 'test run id';
476+
const testVariationId = 'test variation id';
477+
const testRun: TestRun = {
478+
id: testRunId,
479+
imageName: '1592423768112.screenshot.png',
480+
diffName: null,
481+
diffPercent: null,
482+
diffTollerancePercent: 1,
483+
pixelMisMatchCount: null,
484+
status: 'new',
485+
buildId: '146e7a8d-89f0-4565-aa2c-e61efabb0afd',
486+
testVariationId: testVariationId,
487+
updatedAt: new Date(),
488+
createdAt: new Date(),
489+
name: 'ss2f77',
490+
browser: 'chromium',
491+
device: null,
492+
os: null,
493+
viewport: '1800x1600',
494+
baselineName: null,
495+
ignoreAreas: '[]',
496+
comment: 'some comment',
497+
baselineBranchName: 'master',
498+
branchName: 'develop',
499+
merge: false,
500+
};
501+
const variation: TestVariation & {
502+
baselines: Baseline[];
503+
} = {
504+
id: testVariationId,
505+
projectId: 'project Id',
506+
name: 'Test name',
507+
baselineName: 'baselineName',
508+
os: 'OS',
509+
browser: 'browser',
510+
viewport: 'viewport',
511+
device: 'device',
512+
ignoreAreas: '[]',
513+
comment: 'some comment',
514+
branchName: 'develop',
515+
createdAt: new Date(),
516+
updatedAt: new Date(),
517+
baselines: [
518+
{
519+
id: 'baseline id 1',
520+
baselineName: 'image name 1',
521+
testVariationId: testVariationId,
522+
testRunId: 'test run id 1',
523+
createdAt: new Date(),
524+
updatedAt: new Date(),
525+
},
526+
],
527+
};
528+
529+
const variationDeleteMock = jest.fn();
530+
const testRunFindMany = jest.fn().mockResolvedValueOnce([testRun]);
531+
const testRunDeleteMock = jest.fn();
532+
const getDetailsMock = jest.fn().mockResolvedValueOnce(variation);
533+
const deleteBaselineMock = jest.fn().mockResolvedValueOnce(variation.baselines[0]);
534+
const service = await initModule({
535+
variationDeleteMock,
536+
testRunFindMany,
537+
testRunDeleteMock,
538+
});
539+
service.getDetails = getDetailsMock;
540+
service.deleteBaseline = deleteBaselineMock;
541+
542+
await service.delete(testVariationId);
543+
544+
expect(service.getDetails).toHaveBeenCalledWith(testVariationId);
545+
expect(testRunFindMany).toHaveBeenCalledWith({
546+
where: { testVariationId },
547+
});
548+
expect(testRunDeleteMock).toHaveBeenCalledWith(testRunId);
549+
expect(service.deleteBaseline).toHaveBeenCalledWith(variation.baselines[0]);
550+
expect(variationDeleteMock).toHaveBeenCalledWith({
551+
where: { id: testVariationId },
552+
});
553+
});
554+
555+
it('deleteBaseline', async () => {
556+
const baseline: Baseline = {
557+
id: 'baseline id 1',
558+
baselineName: 'image name 1',
559+
testVariationId: 'test variation id',
560+
testRunId: 'test run id 1',
561+
createdAt: new Date(),
562+
updatedAt: new Date(),
563+
};
564+
565+
const baselineDeleteMock = jest.fn();
566+
const imageDeleteMock = jest.fn();
567+
const service = await initModule({
568+
baselineDeleteMock,
569+
imageDeleteMock,
570+
});
571+
572+
await service.deleteBaseline(baseline);
573+
574+
expect(imageDeleteMock).toHaveBeenCalledWith(baseline.baselineName);
575+
expect(baselineDeleteMock).toHaveBeenCalledWith({
576+
where: { id: baseline.id },
577+
});
578+
});
520579
});

src/test-variations/test-variations.service.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -95,30 +95,6 @@ export class TestVariationsService {
9595
});
9696
}
9797

98-
async remove(id: string): Promise<TestVariation> {
99-
const variation = await this.getDetails(id);
100-
101-
// clear history
102-
try {
103-
await Promise.all(
104-
variation.baselines.map(baseline =>
105-
Promise.all([
106-
this.staticService.deleteImage(baseline.baselineName),
107-
this.prismaService.baseline.delete({
108-
where: { id: baseline.id },
109-
}),
110-
])
111-
)
112-
);
113-
} catch (err) {
114-
console.log(err);
115-
}
116-
117-
return this.prismaService.testVariation.delete({
118-
where: { id },
119-
});
120-
}
121-
12298
async merge(projectId: string, branchName: string): Promise<BuildDto> {
12399
const project: Project = await this.prismaService.project.findOne({ where: { id: projectId } });
124100

@@ -176,7 +152,7 @@ export class TestVariationsService {
176152

177153
// delete testRun
178154
await Promise.all(testRuns.map(item => this.testRunsService.delete(item.id)));
179-
155+
180156
// delete baseline
181157
await Promise.all(testVariation.baselines.map(baseline => this.deleteBaseline(baseline)));
182158

@@ -186,7 +162,7 @@ export class TestVariationsService {
186162
});
187163
}
188164

189-
private async deleteBaseline(baseline: Baseline): Promise<Baseline> {
165+
async deleteBaseline(baseline: Baseline): Promise<Baseline> {
190166
this.staticService.deleteImage(baseline.baselineName);
191167
return this.prismaService.baseline.delete({
192168
where: { id: baseline.id },

0 commit comments

Comments
 (0)