Skip to content

Commit 9bbd3bf

Browse files
committed
unit tests for all participant endpoints coded and passing
1 parent 703cc9c commit 9bbd3bf

File tree

2 files changed

+91
-11
lines changed

2 files changed

+91
-11
lines changed

server/feed16/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"start:debug": "nest start --debug --watch",
1414
"start:prod": "node dist/main",
1515
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
16-
"test": "jest --config jest.config.ts --forceExit",
16+
"test": "jest --config jest.config.ts --detectOpenHandles --forceExit",
1717
"test:watch": "jest --watch",
1818
"test:cov": "jest --coverage",
1919
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",

server/feed16/src/participant/participant.controller.spec.ts

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
import { Test, TestingModule } from '@nestjs/testing';
2-
import { ParticipantApiController, Participant } from './participant.controller';
2+
import { ParticipantApiController, Participant, Friend, Inbound, Outbound } from './participant.controller';
33
import { Friend as FriendEntity } from '../entity/friend';
44
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';
66
import { SearchService } from '../search.service';
77

88
describe('ParticipantApiController', () => {
99
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();
1015

1116
beforeEach(async () => {
1217
const pe: ParticipantEntity = new ParticipantEntity();
1318
pe.participantId = 1;
1419
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');
1528
const app: TestingModule = await Test.createTestingModule({
1629
imports: [],
1730
controllers: [ParticipantApiController],
@@ -28,35 +41,50 @@ describe('ParticipantApiController', () => {
2841
provide: 'ParticipantRepository',
2942
useValue: {
3043
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+
}
3349
},
3450
{
3551
provide: 'FriendRepository',
3652
useValue: {
37-
save: jest.fn(),
53+
save: jest.fn().mockImplementation(() => {
54+
friendSaved++;
55+
return Promise.resolve(fe);
56+
}),
3857
createQueryBuilder: jest.fn().mockReturnValue({
3958
where: jest.fn().mockReturnThis(),
40-
getMany: jest.fn().mockReturnValue([]),
59+
getMany: jest.fn().mockReturnValue(Promise.resolve([fe])),
4160
}),
4261
},
4362
},
4463
{
4564
provide: OutboundService,
4665
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+
}),
4871
},
4972
},
5073
{
5174
provide: InboundService,
5275
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+
}),
5481
},
5582
},
5683
{
5784
provide: SearchService,
5885
useValue: {
5986
search: jest.fn(),
87+
index: jest.fn(),
6088
},
6189
},
6290
ParticipantService,
@@ -67,11 +95,63 @@ describe('ParticipantApiController', () => {
6795
appController = app.get<ParticipantApiController>(ParticipantApiController);
6896
});
6997

70-
describe('root', () => {
71-
it('should return "Hello World!"', async () => {
98+
describe('participant', () => {
99+
it('fetch should return mocked entity', async () => {
72100
const p: Participant = new Participant(1, 'Hello World!');
73101
const t: Participant | undefined = await appController?.getParticipant(1);
74102
expect(t).toStrictEqual(p);
75103
});
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+
});
76156
});
77157
});

0 commit comments

Comments
 (0)