@@ -4,25 +4,100 @@ import { BuildsService } from './builds.service';
44import { PrismaService } from '../prisma/prisma.service' ;
55import { ApiGuard } from '../auth/guards/api.guard' ;
66import { JwtAuthGuard } from '../auth/guards/auth.guard' ;
7+ import { ProjectsService } from '../projects/projects.service' ;
8+ import { TEST_BUILD , TEST_PROJECT } from '../_data_' ;
9+ import { Build } from '.prisma/client' ;
10+ import { BuildDto } from './dto/build.dto' ;
11+ import { CreateBuildDto } from './dto/build-create.dto' ;
12+ import { EventsGateway } from '../shared/events/events.gateway' ;
713
14+ const initController = async ( {
15+ projectFindOneMock = jest . fn ( ) ,
16+ buildFindOrCreateMock = jest . fn ( ) ,
17+ buildIncrementBuildNumberMock = jest . fn ( ) ,
18+ eventBuildCreatedMock = jest . fn ( ) ,
19+ } ) => {
20+ const module : TestingModule = await Test . createTestingModule ( {
21+ controllers : [ BuildsController ] ,
22+ providers : [
23+ {
24+ provide : ProjectsService ,
25+ useValue : {
26+ findOne : projectFindOneMock ,
27+ } ,
28+ } ,
29+ {
30+ provide : BuildsService ,
31+ useValue : {
32+ findOrCreate : buildFindOrCreateMock ,
33+ incrementBuildNumber : buildIncrementBuildNumberMock ,
34+ } ,
35+ } ,
36+ { provide : EventsGateway , useValue : { buildCreated : eventBuildCreatedMock } } ,
37+ { provide : PrismaService , useValue : { } } ,
38+ { provide : ApiGuard , useValue : { } } ,
39+ { provide : JwtAuthGuard , useValue : { } } ,
40+ ] ,
41+ } ) . compile ( ) ;
42+
43+ return module . get < BuildsController > ( BuildsController ) ;
44+ } ;
845describe ( 'Builds Controller' , ( ) => {
946 let controller : BuildsController ;
1047
11- beforeEach ( async ( ) => {
12- const module : TestingModule = await Test . createTestingModule ( {
13- controllers : [ BuildsController ] ,
14- providers : [
15- { provide : BuildsService , useValue : { } } ,
16- { provide : PrismaService , useValue : { } } ,
17- { provide : ApiGuard , useValue : { } } ,
18- { provide : JwtAuthGuard , useValue : { } } ,
19- ] ,
20- } ) . compile ( ) ;
21-
22- controller = module . get < BuildsController > ( BuildsController ) ;
23- } ) ;
48+ const createBuildDto : CreateBuildDto = {
49+ ciBuildId : 'ciBuildId' ,
50+ branchName : 'branchName' ,
51+ project : 'name' ,
52+ } ;
53+ const project = TEST_PROJECT ;
54+ const newBuild : Build = {
55+ ...TEST_BUILD ,
56+ number : null ,
57+ } ;
58+ const buildWithNumber : Build = {
59+ ...TEST_BUILD ,
60+ number : 12 ,
61+ } ;
62+
63+ beforeEach ( async ( ) => { } ) ;
2464
25- it ( 'should be defined' , ( ) => {
65+ it ( 'should be defined' , async ( ) => {
66+ controller = await initController ( { } ) ;
2667 expect ( controller ) . toBeDefined ( ) ;
2768 } ) ;
69+
70+ it ( 'should create new build' , async ( ) => {
71+ const eventBuildCreatedMock = jest . fn ( ) ;
72+ const projectFindOneMock = jest . fn ( ) . mockResolvedValueOnce ( project ) ;
73+ const buildFindOrCreateMock = jest . fn ( ) . mockResolvedValueOnce ( newBuild ) ;
74+ const buildIncrementBuildNumberMock = jest . fn ( ) . mockResolvedValueOnce ( buildWithNumber ) ;
75+ controller = await initController ( {
76+ projectFindOneMock,
77+ buildFindOrCreateMock,
78+ buildIncrementBuildNumberMock,
79+ eventBuildCreatedMock,
80+ } ) ;
81+
82+ const result = await controller . create ( createBuildDto ) ;
83+
84+ expect ( result ) . toStrictEqual ( new BuildDto ( buildWithNumber ) ) ;
85+ expect ( buildIncrementBuildNumberMock ) . toHaveBeenCalledWith ( newBuild . id , project . id ) ;
86+ expect ( eventBuildCreatedMock ) . toHaveBeenCalledWith ( new BuildDto ( buildWithNumber ) ) ;
87+ } ) ;
88+
89+ it ( 'should reuse build' , async ( ) => {
90+ const eventBuildCreatedMock = jest . fn ( ) ;
91+ const projectFindOneMock = jest . fn ( ) . mockResolvedValueOnce ( project ) ;
92+ const buildFindOrCreateMock = jest . fn ( ) . mockResolvedValueOnce ( buildWithNumber ) ;
93+ controller = await initController ( {
94+ projectFindOneMock,
95+ buildFindOrCreateMock,
96+ eventBuildCreatedMock,
97+ } ) ;
98+
99+ const result = await controller . create ( createBuildDto ) ;
100+
101+ expect ( result ) . toStrictEqual ( new BuildDto ( buildWithNumber ) ) ;
102+ } ) ;
28103} ) ;
0 commit comments