|
| 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(60000) |
| 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