diff --git a/src/shared/dto/baseline-data.dto.ts b/src/shared/dto/baseline-data.dto.ts new file mode 100644 index 00000000..7c030986 --- /dev/null +++ b/src/shared/dto/baseline-data.dto.ts @@ -0,0 +1,38 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsString, IsOptional } from 'class-validator'; + +export class BaselineDataDto { + @ApiProperty() + @IsString() + name: string; + + @ApiProperty() + @IsOptional() + @IsString() + os?: string; + + @ApiProperty() + @IsOptional() + @IsString() + browser?: string; + + @ApiProperty() + @IsOptional() + @IsString() + viewport?: string; + + @ApiProperty() + @IsOptional() + @IsString() + device?: string; +} + +export const convertBaselineDataToQuery = (data: BaselineDataDto) => { + return { + name: data.name, + os: data.os ? data.os : null, + browser: data.browser ? data.browser : null, + device: data.device ? data.device : null, + viewport: data.viewport ? data.viewport : null, + }; +}; diff --git a/src/test-runs/dto/create-test-request.dto.ts b/src/test-runs/dto/create-test-request.dto.ts index b7ab1c18..44c0c0d2 100644 --- a/src/test-runs/dto/create-test-request.dto.ts +++ b/src/test-runs/dto/create-test-request.dto.ts @@ -1,35 +1,12 @@ import { ApiProperty } from '@nestjs/swagger'; -import { IsString, IsOptional, IsBase64, IsUUID, IsNumber } from 'class-validator'; - -export class CreateTestRequestDto { - @ApiProperty() - @IsString() - name: string; +import { IsOptional, IsBase64, IsUUID, IsNumber } from 'class-validator'; +import { BaselineDataDto } from '../../shared/dto/baseline-data.dto'; +export class CreateTestRequestDto extends BaselineDataDto { @ApiProperty() @IsBase64() imageBase64: string; - @ApiProperty() - @IsOptional() - @IsString() - os?: string; - - @ApiProperty() - @IsOptional() - @IsString() - browser?: string; - - @ApiProperty() - @IsOptional() - @IsString() - viewport?: string; - - @ApiProperty() - @IsOptional() - @IsString() - device?: string; - @ApiProperty() @IsUUID() buildId: string; diff --git a/src/test-runs/test-runs.service.ts b/src/test-runs/test-runs.service.ts index d7991fca..3eccd8d5 100644 --- a/src/test-runs/test-runs.service.ts +++ b/src/test-runs/test-runs.service.ts @@ -12,6 +12,7 @@ import { CommentDto } from '../shared/dto/comment.dto'; import { BuildDto } from '../builds/dto/build.dto'; import { TestRunResultDto } from '../test-runs/dto/testRunResult.dto'; import { TestVariationsService } from '../test-variations/test-variations.service'; +import { convertBaselineDataToQuery } from '../shared/dto/baseline-data.dto'; @Injectable() export class TestRunsService { @@ -19,8 +20,8 @@ export class TestRunsService { private testVariationService: TestVariationsService, private prismaService: PrismaService, private staticService: StaticService, - private eventsGateway: EventsGateway, - ) { } + private eventsGateway: EventsGateway + ) {} async findMany(buildId: string): Promise { return this.prismaService.testRun.findMany({ @@ -44,8 +45,24 @@ export class TestRunsService { } async postTestRun(createTestRequestDto: CreateTestRequestDto): Promise { - const testVariation = await this.testVariationService.findOrCreate(createTestRequestDto); + const baselineData = convertBaselineDataToQuery(createTestRequestDto); + // creates variatioin if does not exist + const testVariation = await this.testVariationService.findOrCreate(createTestRequestDto.projectId, baselineData); + + // delete previous test run if exists + let [previousTestRun] = await this.prismaService.testRun.findMany({ + where: { + buildId: createTestRequestDto.buildId, + ...baselineData, + }, + }); + + if (!!previousTestRun) { + await this.delete(previousTestRun.id); + } + + // create test run result const testRun = await this.create(testVariation, createTestRequestDto); return new TestRunResultDto(testRun, testVariation); @@ -54,14 +71,14 @@ export class TestRunsService { async emitUpdateBuildEvent(buildId: string) { const build = await this.prismaService.build.findOne({ where: { - id: buildId + id: buildId, }, include: { - testRuns: true - } - }) - const buildDto = new BuildDto(build) - this.eventsGateway.buildUpdated(buildDto) + testRuns: true, + }, + }); + const buildDto = new BuildDto(build); + this.eventsGateway.buildUpdated(buildDto); } async approve(id: string): Promise { @@ -93,7 +110,7 @@ export class TestRunsService { }, }); - this.emitUpdateBuildEvent(testRun.buildId) + this.emitUpdateBuildEvent(testRun.buildId); return testRunUpdated; } @@ -105,7 +122,7 @@ export class TestRunsService { }, }); - this.emitUpdateBuildEvent(testRun.buildId) + this.emitUpdateBuildEvent(testRun.buildId); return testRun; } diff --git a/src/test-variations/test-variations.service.spec.ts b/src/test-variations/test-variations.service.spec.ts index e16fd16b..05e7c67c 100644 --- a/src/test-variations/test-variations.service.spec.ts +++ b/src/test-variations/test-variations.service.spec.ts @@ -6,6 +6,7 @@ import { StaticService } from '../shared/static/static.service'; import { IgnoreAreaDto } from '../test-runs/dto/ignore-area.dto'; import { TestVariation, Baseline } from '@prisma/client'; import { CommentDto } from '../shared/dto/comment.dto'; +import { convertBaselineDataToQuery } from '../shared/dto/baseline-data.dto'; const initModule = async ({ imageDeleteMock = jest.fn(), @@ -97,7 +98,7 @@ describe('TestVariationsService', () => { const variationFindManyMock = jest.fn(); service = await initModule({ variationFindManyMock: variationFindManyMock.mockResolvedValueOnce([data]) }); - const result = await service.findOrCreate(data); + const result = await service.findOrCreate(data.projectId, convertBaselineDataToQuery(data)); expect(variationFindManyMock).toHaveBeenCalledWith({ where: { @@ -117,7 +118,7 @@ describe('TestVariationsService', () => { const variationFindManyMock = jest.fn(); service = await initModule({ variationFindManyMock: variationFindManyMock.mockResolvedValueOnce([data]) }); - const result = await service.findOrCreate(data); + const result = await service.findOrCreate(data.projectId, convertBaselineDataToQuery(data)); expect(variationFindManyMock).toHaveBeenCalledWith({ where: { @@ -137,7 +138,7 @@ describe('TestVariationsService', () => { const variationCreateMock = jest.fn(); service = await initModule({ variationCreateMock: variationCreateMock.mockResolvedValueOnce(data) }); - const result = await service.findOrCreate(data); + const result = await service.findOrCreate(data.projectId, convertBaselineDataToQuery(data)); expect(variationCreateMock).toHaveBeenCalledWith({ data: { diff --git a/src/test-variations/test-variations.service.ts b/src/test-variations/test-variations.service.ts index 3900c78f..a03e8291 100644 --- a/src/test-variations/test-variations.service.ts +++ b/src/test-variations/test-variations.service.ts @@ -1,10 +1,10 @@ import { Injectable } from '@nestjs/common'; -import { CreateTestRequestDto } from '../test-runs/dto/create-test-request.dto'; import { IgnoreAreaDto } from '../test-runs/dto/ignore-area.dto'; import { PrismaService } from '../prisma/prisma.service'; import { TestVariation, Baseline } from '@prisma/client'; import { StaticService } from '../shared/static/static.service'; import { CommentDto } from '../shared/dto/comment.dto'; +import { BaselineDataDto } from '../shared/dto/baseline-data.dto'; @Injectable() export class TestVariationsService { @@ -26,26 +26,19 @@ export class TestVariationsService { }); } - async findOrCreate(createTestDto: CreateTestRequestDto): Promise { - const data = { - name: createTestDto.name, - os: createTestDto.os ? createTestDto.os : null, - browser: createTestDto.browser ? createTestDto.browser : null, - viewport: createTestDto.viewport ? createTestDto.viewport : null, - device: createTestDto.device ? createTestDto.device : null, - }; + async findOrCreate(projectId: string, baselineData: BaselineDataDto): Promise { let [testVariation] = await this.prismaService.testVariation.findMany({ where: { - projectId: createTestDto.projectId, - ...data, + projectId, + ...baselineData, }, }); if (!testVariation) { testVariation = await this.prismaService.testVariation.create({ data: { - project: { connect: { id: createTestDto.projectId } }, - ...data, + project: { connect: { id: projectId } }, + ...baselineData, }, }); }