Skip to content

Commit 5e1e4d7

Browse files
committed
conversationsPostEvent controller tests
1 parent 3e675dd commit 5e1e4d7

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

src/controllers/conversationsPostEvent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ export const post = async (
1515
if (eventType === 'onConversationUpdated' && state === 'closed') {
1616
try {
1717
await deleteConversation(conversationSid)
18+
return res.status(200).send(`${conversationSid} deleted`)
1819
} catch (err) {
1920
return res.status(500).send(`${conversationSid} failed to delete: ${err}`)
2021
}
21-
return res.status(200).send(`${conversationSid} deleted`)
2222
}
2323
return res.status(200).send('not processed')
2424
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import request from 'supertest'
2+
import {app} from '../../src/app/app'
3+
import * as DeleteConversation from '../../src/utils/deleteConversation.util'
4+
5+
describe('conversations post event controller', () => {
6+
jest.setTimeout(3600000)
7+
8+
beforeEach(() => {
9+
jest.resetAllMocks()
10+
})
11+
12+
// Test parameters
13+
const eventTypeOnConversationUpdated = 'onConversationUpdated'
14+
const eventTypeOther = 'otherEvent123'
15+
const closedState = 'closed'
16+
const otherState = 'state123'
17+
const conversationSid = 'CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
18+
19+
it('should delete conversation', async () => {
20+
const deleteConversationSpy = jest
21+
.spyOn(DeleteConversation, 'deleteConversation')
22+
.mockResolvedValue(true)
23+
24+
const res = await request(app)
25+
.post('/conversations-post-event')
26+
.set('content-type', 'application/json')
27+
.send({
28+
EventType: eventTypeOnConversationUpdated,
29+
State: closedState,
30+
ConversationSid: conversationSid,
31+
})
32+
33+
expect(res.status).toEqual(200)
34+
expect(res.text).toEqual(`${conversationSid} deleted`)
35+
expect(deleteConversationSpy).toBeCalledWith(conversationSid)
36+
})
37+
38+
it('should ignore if status != closed', async () => {
39+
const deleteConversationSpy = jest
40+
.spyOn(DeleteConversation, 'deleteConversation')
41+
.mockResolvedValue(true)
42+
43+
const res = await request(app)
44+
.post('/conversations-post-event')
45+
.set('content-type', 'application/json')
46+
.send({
47+
EventType: eventTypeOnConversationUpdated,
48+
State: otherState,
49+
ConversationSid: conversationSid,
50+
})
51+
52+
expect(res.status).toEqual(200)
53+
expect(res.text).toEqual('not processed')
54+
expect(deleteConversationSpy).toBeCalledTimes(0)
55+
})
56+
57+
it('should ignore if eventType != onConversationUpdated', async () => {
58+
const deleteConversationSpy = jest
59+
.spyOn(DeleteConversation, 'deleteConversation')
60+
.mockResolvedValue(true)
61+
62+
const res = await request(app)
63+
.post('/conversations-post-event')
64+
.set('content-type', 'application/json')
65+
.send({
66+
EventType: eventTypeOther,
67+
State: closedState,
68+
ConversationSid: conversationSid,
69+
})
70+
71+
expect(res.status).toEqual(200)
72+
expect(res.text).toEqual('not processed')
73+
expect(deleteConversationSpy).toBeCalledTimes(0)
74+
})
75+
76+
it('should return 500 if throws', async () => {
77+
const errorCode = 'ErrorCode123'
78+
79+
const deleteConversationSpy = jest
80+
.spyOn(DeleteConversation, 'deleteConversation')
81+
.mockRejectedValue(new Error(errorCode))
82+
83+
const res = await request(app)
84+
.post('/conversations-post-event')
85+
.set('content-type', 'application/json')
86+
.send({
87+
EventType: eventTypeOnConversationUpdated,
88+
State: closedState,
89+
ConversationSid: conversationSid,
90+
})
91+
92+
expect(res.status).toEqual(500)
93+
expect(res.text).toEqual(`${conversationSid} failed to delete: Error: ${errorCode}`)
94+
expect(deleteConversationSpy).toBeCalledWith(conversationSid)
95+
})
96+
})

0 commit comments

Comments
 (0)