Skip to content

Commit 0f471ea

Browse files
committed
TestVariationsService. findOrCreate covered
1 parent 4e2828f commit 0f471ea

File tree

3 files changed

+112
-18
lines changed

3 files changed

+112
-18
lines changed
Lines changed: 106 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,116 @@
11
import { Test, TestingModule } from '@nestjs/testing';
22
import { TestVariationsService } from './test-variations.service';
33
import { PrismaService } from '../prisma/prisma.service';
4+
import { CreateTestRequestDto } from '../test/dto/create-test-request.dto';
5+
import { StaticService } from '../shared/static/static.service';
6+
7+
const initModule = async ({
8+
findManyMock = jest.fn().mockReturnValue([]),
9+
createMock = jest.fn(),
10+
}) => {
11+
const module: TestingModule = await Test.createTestingModule({
12+
providers: [
13+
TestVariationsService,
14+
{ provide: StaticService, useValue: {} },
15+
{
16+
provide: PrismaService, useValue: {
17+
testVariation: {
18+
findMany: findManyMock,
19+
create: createMock
20+
}
21+
}
22+
},
23+
],
24+
}).compile();
25+
26+
return module.get<TestVariationsService>(TestVariationsService);
27+
}
28+
29+
const dataRequiredFields: CreateTestRequestDto = {
30+
buildId: 'buildId',
31+
projectId: 'projectId',
32+
name: 'Test name',
33+
imageBase64: 'Image'
34+
}
35+
36+
const dataAllFields: CreateTestRequestDto = {
37+
buildId: 'buildId',
38+
projectId: 'projectId',
39+
name: 'Test name',
40+
imageBase64: 'Image',
41+
os: 'OS',
42+
browser: 'browser',
43+
viewport: 'viewport',
44+
device: 'device',
45+
}
446

547
describe('TestVariationsService', () => {
648
let service: TestVariationsService;
749

8-
beforeEach(async () => {
9-
const module: TestingModule = await Test.createTestingModule({
10-
providers: [
11-
TestVariationsService,
12-
{ provide: PrismaService, useValue: {} },
13-
],
14-
}).compile();
50+
describe('findOrCreate', () => {
51+
52+
it('can find by required fields', async () => {
53+
const data = dataRequiredFields
54+
const findManyMock = jest.fn()
55+
service = await initModule({ findManyMock: findManyMock.mockResolvedValueOnce([data]) })
56+
57+
const result = await service.findOrCreate(data)
58+
59+
expect(findManyMock).toHaveBeenCalledWith({
60+
where: {
61+
name: data.name,
62+
projectId: data.projectId,
63+
os: null,
64+
browser: null,
65+
viewport: null,
66+
device: null,
67+
},
68+
})
69+
expect(result).toBe(data)
70+
})
71+
72+
it('can find by all fields', async () => {
73+
const data = dataAllFields
74+
const findManyMock = jest.fn()
75+
service = await initModule({ findManyMock: findManyMock.mockResolvedValueOnce([data]) })
76+
77+
const result = await service.findOrCreate(data)
78+
79+
expect(findManyMock).toHaveBeenCalledWith({
80+
where: {
81+
name: data.name,
82+
projectId: data.projectId,
83+
os: data.os,
84+
browser: data.browser,
85+
viewport: data.viewport,
86+
device: data.device,
87+
},
88+
})
89+
expect(result).toBe(data)
90+
})
91+
92+
it('can create if not found', async () => {
93+
const data = dataAllFields
94+
const createMock = jest.fn()
95+
service = await initModule({ createMock: createMock.mockResolvedValueOnce(data) })
1596

16-
service = module.get<TestVariationsService>(TestVariationsService);
17-
});
97+
const result = await service.findOrCreate(data)
1898

19-
it('should be defined', () => {
20-
expect(service).toBeDefined();
21-
});
99+
expect(createMock).toHaveBeenCalledWith({
100+
data: {
101+
name: data.name,
102+
os: data.os,
103+
browser: data.browser,
104+
viewport: data.viewport,
105+
device: data.device,
106+
project: {
107+
connect: {
108+
id: data.projectId,
109+
}
110+
}
111+
},
112+
})
113+
expect(result).toBe(data)
114+
})
115+
})
22116
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { CreateTestRequestDto } from '../test/dto/create-test-request.dto';
33
import { IgnoreAreaDto } from '../test/dto/ignore-area.dto';
44
import { PrismaService } from '../prisma/prisma.service';
55
import { TestVariation, Baseline } from '@prisma/client';
6-
import { StaticService } from 'src/shared/static/static.service';
6+
import { StaticService } from '../shared/static/static.service';
77

88
@Injectable()
99
export class TestVariationsService {

src/test/dto/create-test-request.dto.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ export class CreateTestRequestDto {
1313
@ApiProperty()
1414
@IsOptional()
1515
@IsString()
16-
os: string;
16+
os?: string;
1717

1818
@ApiProperty()
1919
@IsOptional()
2020
@IsString()
21-
browser: string;
21+
browser?: string;
2222

2323
@ApiProperty()
2424
@IsOptional()
2525
@IsString()
26-
viewport: string;
26+
viewport?: string;
2727

2828
@ApiProperty()
2929
@IsOptional()
3030
@IsString()
31-
device: string;
31+
device?: string;
3232

3333
@ApiProperty()
3434
@IsUUID()
@@ -41,5 +41,5 @@ export class CreateTestRequestDto {
4141
@ApiProperty()
4242
@IsOptional()
4343
@IsNumber()
44-
diffTollerancePercent: number;
44+
diffTollerancePercent?: number;
4545
}

0 commit comments

Comments
 (0)