Skip to content

Commit d51ec9b

Browse files
authored
Merge pull request #13 from Visual-Regression-Tracker/61-build-update
buildUpdated event added
2 parents bff30a4 + ccc1d58 commit d51ec9b

File tree

3 files changed

+112
-10
lines changed

3 files changed

+112
-10
lines changed

src/events/events.gateway.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export class EventsGateway {
1212
this.server.emit('build_created', build);
1313
}
1414

15+
buildUpdated(build: BuildDto) {
16+
this.server.emit('build_updated', build);
17+
}
18+
1519
newTestRun(testRun: TestRun) {
1620
this.server.emit('testRun_created', testRun);
1721
}

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

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ 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 } from '@prisma/client';
7+
import { TestStatus, Build, TestRun } from '@prisma/client';
88
import Pixelmatch from 'pixelmatch';
99
import { CreateTestRequestDto } from '../test/dto/create-test-request.dto';
1010
import { DiffResult } from './diffResult';
1111
import { IgnoreAreaDto } from '../test/dto/ignore-area.dto';
1212
import { EventsGateway } from '../events/events.gateway';
1313
import { CommentDto } from '../shared/dto/comment.dto';
14+
import { BuildDto } from '../builds/dto/build.dto';
1415

1516
jest.mock('pixelmatch');
1617

@@ -24,6 +25,8 @@ const initService = async ({
2425
saveImageMock = jest.fn(),
2526
deleteImageMock = jest.fn(),
2627
eventNewTestRunMock = jest.fn(),
28+
eventBuildUpdatedMock = jest.fn(),
29+
buildFindOneMock = jest.fn(),
2730
}) => {
2831
const module: TestingModule = await Test.createTestingModule({
2932
providers: [
@@ -38,6 +41,9 @@ const initService = async ({
3841
create: testRunCreateMock,
3942
update: testRunUpdateMock,
4043
},
44+
build: {
45+
findOne: buildFindOneMock,
46+
}
4147
},
4248
},
4349
{
@@ -52,6 +58,7 @@ const initService = async ({
5258
provide: EventsGateway,
5359
useValue: {
5460
newTestRun: eventNewTestRunMock,
61+
buildUpdated: eventBuildUpdatedMock
5562
},
5663
},
5764
],
@@ -80,20 +87,33 @@ describe('TestRunsService', () => {
8087
});
8188

8289
it('reject', async () => {
83-
const id = 'some id';
84-
const testRunUpdateMock = jest.fn();
90+
const testRun = {
91+
id: 'id',
92+
imageName: 'imageName',
93+
diffTollerancePercent: 12,
94+
status: TestStatus.new,
95+
buildId: 'buildId',
96+
testVariationId: 'testVariationId',
97+
updatedAt: new Date(),
98+
createdAt: new Date(),
99+
name: 'test run name',
100+
ignoreAreas: '[]',
101+
};
102+
const testRunUpdateMock = jest.fn().mockResolvedValueOnce(testRun);
85103
service = await initService({
86104
testRunUpdateMock,
87105
});
106+
service.emitUpdateBuildEvent = jest.fn();
88107

89-
service.reject(id);
108+
await service.reject(testRun.id);
90109

91110
expect(testRunUpdateMock).toHaveBeenCalledWith({
92-
where: { id },
111+
where: { id: testRun.id },
93112
data: {
94113
status: TestStatus.failed,
95114
},
96115
});
116+
expect(service.emitUpdateBuildEvent).toBeCalledWith(testRun.buildId)
97117
});
98118

99119
describe('approve', () => {
@@ -126,6 +146,7 @@ describe('TestRunsService', () => {
126146
getImageMock,
127147
});
128148
service.findOne = testRunFindOneMock;
149+
service.emitUpdateBuildEvent = jest.fn();
129150

130151
await service.approve(testRun.id);
131152

@@ -153,6 +174,7 @@ describe('TestRunsService', () => {
153174
},
154175
},
155176
});
177+
expect(service.emitUpdateBuildEvent).toBeCalledWith(testRun.buildId)
156178
});
157179
});
158180

@@ -556,4 +578,60 @@ describe('TestRunsService', () => {
556578
},
557579
});
558580
});
581+
582+
it('emitUpdateBuildEvent', async () => {
583+
const build: Build & {
584+
testRuns: TestRun[];
585+
} = {
586+
id: 'a9385fc1-884d-4f9f-915e-40da0e7773d5',
587+
number: null,
588+
branchName: 'develop',
589+
status: null,
590+
projectId: 'e0a37894-6f29-478d-b13e-6182fecc715e',
591+
updatedAt: new Date(),
592+
createdAt: new Date(),
593+
userId: null,
594+
testRuns: [
595+
{
596+
id: '10fb5e02-64e0-4cf5-9f17-c00ab3c96658',
597+
imageName: '1592423768112.screenshot.png',
598+
diffName: null,
599+
diffPercent: null,
600+
diffTollerancePercent: 1,
601+
pixelMisMatchCount: null,
602+
status: 'new',
603+
buildId: '146e7a8d-89f0-4565-aa2c-e61efabb0afd',
604+
testVariationId: '3bc4a5bc-006e-4d43-8e4e-eaa132627fca',
605+
updatedAt: new Date(),
606+
createdAt: new Date(),
607+
name: 'ss2f77',
608+
browser: 'chromium',
609+
device: null,
610+
os: null,
611+
viewport: '1800x1600',
612+
baselineName: null,
613+
ignoreAreas: '[]',
614+
comment: 'some comment',
615+
},
616+
],
617+
};
618+
const buildFindOneMock = jest.fn().mockResolvedValueOnce(build)
619+
const eventBuildUpdatedMock = jest.fn()
620+
service = await initService({
621+
buildFindOneMock,
622+
eventBuildUpdatedMock,
623+
});
624+
625+
await service.emitUpdateBuildEvent(build.id)
626+
627+
expect(buildFindOneMock).toHaveBeenCalledWith({
628+
where: {
629+
id: build.id
630+
},
631+
include: {
632+
testRuns: true
633+
}
634+
})
635+
expect(eventBuildUpdatedMock).toHaveBeenCalledWith(new BuildDto(build))
636+
})
559637
});

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ import { PrismaService } from '../prisma/prisma.service';
88
import { TestRun, TestStatus, TestVariation } from '@prisma/client';
99
import { DiffResult } from './diffResult';
1010
import { EventsGateway } from '../events/events.gateway';
11-
import { CommentDto } from 'src/shared/dto/comment.dto';
11+
import { CommentDto } from '../shared/dto/comment.dto';
12+
import { BuildDto } from '../builds/dto/build.dto';
1213

1314
@Injectable()
1415
export class TestRunsService {
1516
constructor(
1617
private prismaService: PrismaService,
1718
private staticService: StaticService,
18-
private eventsGateway: EventsGateway
19-
) {}
19+
private eventsGateway: EventsGateway,
20+
) { }
2021

2122
async findMany(buildId: string): Promise<TestRun[]> {
2223
return this.prismaService.testRun.findMany({
@@ -39,14 +40,27 @@ export class TestRunsService {
3940
});
4041
}
4142

43+
async emitUpdateBuildEvent(buildId: string) {
44+
const build = await this.prismaService.build.findOne({
45+
where: {
46+
id: buildId
47+
},
48+
include: {
49+
testRuns: true
50+
}
51+
})
52+
const buildDto = new BuildDto(build)
53+
this.eventsGateway.buildUpdated(buildDto)
54+
}
55+
4256
async approve(id: string): Promise<TestRun> {
4357
const testRun = await this.findOne(id);
4458

4559
// save new baseline
4660
const baseline = this.staticService.getImage(testRun.imageName);
4761
const baselineName = this.staticService.saveImage('baseline', PNG.sync.write(baseline));
4862

49-
return this.prismaService.testRun.update({
63+
const testRunUpdated = await this.prismaService.testRun.update({
5064
where: { id },
5165
data: {
5266
status: TestStatus.approved,
@@ -67,15 +81,21 @@ export class TestRunsService {
6781
},
6882
},
6983
});
84+
85+
this.emitUpdateBuildEvent(testRun.buildId)
86+
return testRunUpdated;
7087
}
7188

7289
async reject(id: string): Promise<TestRun> {
73-
return this.prismaService.testRun.update({
90+
const testRun = await this.prismaService.testRun.update({
7491
where: { id },
7592
data: {
7693
status: TestStatus.failed,
7794
},
7895
});
96+
97+
this.emitUpdateBuildEvent(testRun.buildId)
98+
return testRun;
7999
}
80100

81101
async saveDiffResult(id: string, diffResult: DiffResult): Promise<TestRun> {

0 commit comments

Comments
 (0)