|
| 1 | +import * as sinon from 'sinon'; |
| 2 | +import { Qminder } from '../../qminder'; |
| 3 | +import { TicketService } from './ticket.service'; |
| 4 | +import { ExternalData } from '../../model/ticket/external-data.js'; |
| 5 | + |
| 6 | +describe('Ticket.setExternalData', () => { |
| 7 | + let requestStub: sinon.SinonStub; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + Qminder.setKey('EXAMPLE_API_KEY'); |
| 11 | + Qminder.setServer('api.qminder.com'); |
| 12 | + requestStub = sinon.stub(Qminder.ApiBase, 'request'); |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + sinon.restore(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('sends POST to v1/tickets/<id>/external with stringified data and returns success', async () => { |
| 20 | + const ticketId = '123'; |
| 21 | + const provider = 'crm'; |
| 22 | + const title = 'Case #42'; |
| 23 | + const data: ExternalData = { |
| 24 | + fields: [ |
| 25 | + { type: 'message', content: 'Hello', importance: 'info' }, |
| 26 | + { type: 'text', title: 'Order', value: '42' }, |
| 27 | + { |
| 28 | + type: 'list', |
| 29 | + title: 'Links', |
| 30 | + items: [{ title: 'Home', url: 'https://example.com' }], |
| 31 | + }, |
| 32 | + ], |
| 33 | + }; |
| 34 | + |
| 35 | + requestStub.resolves({ result: 'success' }); |
| 36 | + |
| 37 | + const result = await TicketService.setExternalData( |
| 38 | + ticketId, |
| 39 | + provider, |
| 40 | + title, |
| 41 | + data, |
| 42 | + ); |
| 43 | + |
| 44 | + expect(result).toBe('success'); |
| 45 | + |
| 46 | + expect(requestStub.calledOnce).toBeTruthy(); |
| 47 | + const [url, options] = requestStub.getCall(0).args as [string, any]; |
| 48 | + expect(url).toBe(`v1/tickets/${ticketId}/external`); |
| 49 | + expect(options.method).toBe('POST'); |
| 50 | + expect(options.body).toEqual({ |
| 51 | + provider, |
| 52 | + title, |
| 53 | + data: JSON.stringify(data), |
| 54 | + }); |
| 55 | + }); |
| 56 | +}); |
0 commit comments