|
1 | 1 | import { Test, TestingModule } from '@nestjs/testing'; |
2 | 2 | import { TestVariationsService } from './test-variations.service'; |
3 | 3 | 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 | +} |
4 | 46 |
|
5 | 47 | describe('TestVariationsService', () => { |
6 | 48 | let service: TestVariationsService; |
7 | 49 |
|
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) }) |
15 | 96 |
|
16 | | - service = module.get<TestVariationsService>(TestVariationsService); |
17 | | - }); |
| 97 | + const result = await service.findOrCreate(data) |
18 | 98 |
|
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 | + }) |
22 | 116 | }); |
0 commit comments