1
1
import { Test , TestingModule } from '@nestjs/testing' ;
2
- import { ParticipantApiController , Participant } from './participant.controller' ;
2
+ import { ParticipantApiController , Participant , Friend , Inbound , Outbound } from './participant.controller' ;
3
3
import { Friend as FriendEntity } from '../entity/friend' ;
4
4
import { Participant as ParticipantEntity } from '../entity/participant' ;
5
- import { ParticipantService , OutboundService , InboundService } from './participant.service' ;
5
+ import { ParticipantService , OutboundService , InboundService , InboundModel , OutboundModel , ParticipantModel } from './participant.service' ;
6
6
import { SearchService } from '../search.service' ;
7
7
8
8
describe ( 'ParticipantApiController' , ( ) => {
9
9
let appController : ParticipantApiController | null = null ;
10
+ let participantSaved : number = 0 ;
11
+ let friendSaved : number = 0 ;
12
+ let inboundSaved : number = 0 ;
13
+ let outboundSaved : number = 0 ;
14
+ const nowAsDate : Date = new Date ( ) ;
10
15
11
16
beforeEach ( async ( ) => {
12
17
const pe : ParticipantEntity = new ParticipantEntity ( ) ;
13
18
pe . participantId = 1 ;
14
19
pe . moniker = 'Hello World!' ;
20
+ const fe : FriendEntity = new FriendEntity ( ) ;
21
+ fe . friendsId = 1 ;
22
+ fe . fromParticipantId = 1 ;
23
+ fe . toParticipantId = 2 ;
24
+ const pm1 : ParticipantModel = new ParticipantModel ( 1 , 'Hello World!' ) ;
25
+ const pm2 : ParticipantModel = new ParticipantModel ( 2 , 'foo bar' ) ;
26
+ const im : InboundModel = new InboundModel ( pm1 , pm2 , nowAsDate , 'test subject' , 'test story' ) ;
27
+ const om : OutboundModel = new OutboundModel ( pm1 , nowAsDate , 'test subject' , 'test story' ) ;
15
28
const app : TestingModule = await Test . createTestingModule ( {
16
29
imports : [ ] ,
17
30
controllers : [ ParticipantApiController ] ,
@@ -28,35 +41,50 @@ describe('ParticipantApiController', () => {
28
41
provide : 'ParticipantRepository' ,
29
42
useValue : {
30
43
findOneBy : jest . fn ( ) . mockReturnValue ( Promise . resolve ( pe ) ) ,
31
- save : jest . fn ( ) ,
32
- } ,
44
+ save : jest . fn ( ) . mockImplementation ( ( ) => {
45
+ participantSaved ++ ;
46
+ return Promise . resolve ( pe ) ;
47
+ } )
48
+ }
33
49
} ,
34
50
{
35
51
provide : 'FriendRepository' ,
36
52
useValue : {
37
- save : jest . fn ( ) ,
53
+ save : jest . fn ( ) . mockImplementation ( ( ) => {
54
+ friendSaved ++ ;
55
+ return Promise . resolve ( fe ) ;
56
+ } ) ,
38
57
createQueryBuilder : jest . fn ( ) . mockReturnValue ( {
39
58
where : jest . fn ( ) . mockReturnThis ( ) ,
40
- getMany : jest . fn ( ) . mockReturnValue ( [ ] ) ,
59
+ getMany : jest . fn ( ) . mockReturnValue ( Promise . resolve ( [ fe ] ) ) ,
41
60
} ) ,
42
61
} ,
43
62
} ,
44
63
{
45
64
provide : OutboundService ,
46
65
useValue : {
47
- get : jest . fn ( ) ,
66
+ get : jest . fn ( ) . mockReturnValue ( Promise . resolve ( [ om ] ) ) ,
67
+ save : jest . fn ( ) . mockImplementation ( ( ) => {
68
+ outboundSaved ++ ;
69
+ return Promise . resolve ( om ) ;
70
+ } ) ,
48
71
} ,
49
72
} ,
50
73
{
51
74
provide : InboundService ,
52
75
useValue : {
53
- get : jest . fn ( ) ,
76
+ get : jest . fn ( ) . mockReturnValue ( Promise . resolve ( [ im ] ) ) ,
77
+ save : jest . fn ( ) . mockImplementation ( ( ) => {
78
+ inboundSaved ++ ; nowAsDate
79
+ return Promise . resolve ( im ) ;
80
+ } ) ,
54
81
} ,
55
82
} ,
56
83
{
57
84
provide : SearchService ,
58
85
useValue : {
59
86
search : jest . fn ( ) ,
87
+ index : jest . fn ( ) ,
60
88
} ,
61
89
} ,
62
90
ParticipantService ,
@@ -67,11 +95,63 @@ describe('ParticipantApiController', () => {
67
95
appController = app . get < ParticipantApiController > ( ParticipantApiController ) ;
68
96
} ) ;
69
97
70
- describe ( 'root ' , ( ) => {
71
- it ( 'should return "Hello World!" ' , async ( ) => {
98
+ describe ( 'participant ' , ( ) => {
99
+ it ( 'fetch should return mocked entity ' , async ( ) => {
72
100
const p : Participant = new Participant ( 1 , 'Hello World!' ) ;
73
101
const t : Participant | undefined = await appController ?. getParticipant ( 1 ) ;
74
102
expect ( t ) . toStrictEqual ( p ) ;
75
103
} ) ;
104
+ it ( 'add should call underlying repository add' , async ( ) => {
105
+ const p : Participant = new Participant ( 1 , 'Hello World!' ) ;
106
+ const t : Participant | undefined = await appController ?. addParticipant ( p ) ;
107
+ expect ( participantSaved ) . toBeGreaterThan ( 0 ) ;
108
+ } ) ;
109
+ } ) ;
110
+ describe ( 'friend' , ( ) => {
111
+ it ( 'fetch should return mocked entity' , async ( ) => {
112
+ const t : Friend [ ] | undefined = await appController ?. getFriends ( 1 ) ;
113
+ expect ( t ?. length ) . toBe ( 1 ) ;
114
+ expect ( t ?. [ 0 ] . from ) . toBe ( '/participant/1' ) ;
115
+ expect ( t ?. [ 0 ] . to ) . toBe ( '/participant/2' ) ;
116
+ } ) ;
117
+ it ( 'add should call underlying repository add' , async ( ) => {
118
+ const p1 : Participant = new Participant ( 1 , 'Hello World!' ) ;
119
+ const p2 : Participant = new Participant ( 2 , 'foo bar' ) ;
120
+ const f : Friend = new Friend ( 1 , p1 . link , p2 . link ) ;
121
+ const t : Friend | undefined = await appController ?. addFriend ( 1 , f ) ;
122
+ expect ( friendSaved ) . toBeGreaterThan ( 0 ) ;
123
+ expect ( t ) . toStrictEqual ( f ) ;
124
+ } ) ;
125
+ } ) ;
126
+ describe ( 'inbound' , ( ) => {
127
+ it ( 'fetch should return mocked entity' , async ( ) => {
128
+ const t : Inbound [ ] | undefined = await appController ?. getInbound ( 1 ) ;
129
+ expect ( t ?. length ) . toBe ( 1 ) ;
130
+ expect ( t ?. [ 0 ] . from ) . toBe ( '/participant/1' ) ;
131
+ expect ( t ?. [ 0 ] . to ) . toBe ( '/participant/2' ) ;
132
+ expect ( t ?. [ 0 ] . occurred ) . toStrictEqual ( nowAsDate ) ;
133
+ expect ( t ?. [ 0 ] . subject ) . toBe ( 'test subject' ) ;
134
+ expect ( t ?. [ 0 ] . story ) . toBe ( 'test story' ) ;
135
+ } ) ;
136
+ } ) ;
137
+ describe ( 'outbound' , ( ) => {
138
+ it ( 'fetch should return mocked entity' , async ( ) => {
139
+ const t : Outbound [ ] | undefined = await appController ?. getOutbound ( 1 ) ;
140
+ expect ( t ?. length ) . toBe ( 1 ) ;
141
+ expect ( t ?. [ 0 ] . from ) . toBe ( '/participant/1' ) ;
142
+ expect ( t ?. [ 0 ] . occurred ) . toStrictEqual ( nowAsDate ) ;
143
+ expect ( t ?. [ 0 ] . subject ) . toBe ( 'test subject' ) ;
144
+ expect ( t ?. [ 0 ] . story ) . toBe ( 'test story' ) ;
145
+ } ) ;
146
+ it ( 'add should call underlying repository add' , async ( ) => {
147
+ const p1 : Participant = new Participant ( 1 , 'Hello World!' ) ;
148
+ const p2 : Participant = new Participant ( 2 , 'foo bar' ) ;
149
+ const f : Friend = new Friend ( 1 , p1 . link , p2 . link ) ;
150
+ const o : Outbound = new Outbound ( p1 . link , nowAsDate , 'test subject' , 'test story' ) ;
151
+ const t : Outbound | undefined = await appController ?. addOutbound ( 1 , o ) ;
152
+ expect ( outboundSaved ) . toBeGreaterThan ( 0 ) ;
153
+ expect ( inboundSaved ) . toBeGreaterThan ( 0 ) ;
154
+ expect ( t ) . toStrictEqual ( o ) ;
155
+ } ) ;
76
156
} ) ;
77
157
} ) ;
0 commit comments