Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/shared/dto/baseline-data.dto.ts
Original file line number Diff line number Diff line change
@@ -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,
};
};
29 changes: 3 additions & 26 deletions src/test-runs/dto/create-test-request.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
39 changes: 28 additions & 11 deletions src/test-runs/test-runs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ 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 {
constructor(
private testVariationService: TestVariationsService,
private prismaService: PrismaService,
private staticService: StaticService,
private eventsGateway: EventsGateway,
) { }
private eventsGateway: EventsGateway
) {}

async findMany(buildId: string): Promise<TestRun[]> {
return this.prismaService.testRun.findMany({
Expand All @@ -44,8 +45,24 @@ export class TestRunsService {
}

async postTestRun(createTestRequestDto: CreateTestRequestDto): Promise<TestRunResultDto> {
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);
Expand All @@ -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<TestRun> {
Expand Down Expand Up @@ -93,7 +110,7 @@ export class TestRunsService {
},
});

this.emitUpdateBuildEvent(testRun.buildId)
this.emitUpdateBuildEvent(testRun.buildId);
return testRunUpdated;
}

Expand All @@ -105,7 +122,7 @@ export class TestRunsService {
},
});

this.emitUpdateBuildEvent(testRun.buildId)
this.emitUpdateBuildEvent(testRun.buildId);
return testRun;
}

Expand Down
7 changes: 4 additions & 3 deletions src/test-variations/test-variations.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand Down
19 changes: 6 additions & 13 deletions src/test-variations/test-variations.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -26,26 +26,19 @@ export class TestVariationsService {
});
}

async findOrCreate(createTestDto: CreateTestRequestDto): Promise<TestVariation> {
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<TestVariation> {
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,
},
});
}
Expand Down