|
| 1 | +import { IssueApiClient } from '@issue'; |
| 2 | +import { createFakeBugSplatApiClient } from '@spec/fakes/common/bugsplat-api-client'; |
| 3 | +import { createFakeFormData } from '@spec/fakes/common/form-data'; |
| 4 | +import { createFakeResponseBody } from '@spec/fakes/common/response'; |
| 5 | +import { fakeEventsApiResponse } from '@spec/fakes/events/events-api-response'; |
| 6 | + |
| 7 | +describe('IssueApiClient', () => { |
| 8 | + const database = 'fred'; |
| 9 | + const id = 100000; |
| 10 | + const notes = 'hello world!'; |
| 11 | + let client: IssueApiClient; |
| 12 | + let fakeBugSplatApiClient; |
| 13 | + let fakeFormData; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + const fakeResponse = createFakeResponseBody(200, fakeEventsApiResponse); |
| 17 | + fakeFormData = createFakeFormData(); |
| 18 | + fakeBugSplatApiClient = createFakeBugSplatApiClient(fakeFormData, fakeResponse); |
| 19 | + client = new IssueApiClient(fakeBugSplatApiClient); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('postStackKeyIssue', () => { |
| 23 | + beforeEach(async () => await client.postStackKeyIssue(database, id, notes)); |
| 24 | + |
| 25 | + it('should throw if database is falsy', async () => { |
| 26 | + try { |
| 27 | + await client.postStackKeyIssue('', id, notes); |
| 28 | + fail('postStackKeyIssue was supposed to throw!'); |
| 29 | + } catch (error: any) { |
| 30 | + expect(error.message).toMatch(/to be a non white space string/); |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + it('should throw if id is less than or equal to 0', async () => { |
| 35 | + try { |
| 36 | + await client.postStackKeyIssue(database, -1, notes); |
| 37 | + fail('postStackKeyIssue was supposed to throw!'); |
| 38 | + } catch (error: any) { |
| 39 | + expect(error.message).toMatch(/to be a positive non-zero number/); |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + it('should call fetch with route and formData', () => { |
| 44 | + expect(fakeFormData.append).toHaveBeenCalledWith('database', database); |
| 45 | + expect(fakeFormData.append).toHaveBeenCalledWith('stackKeyId', id.toString()); |
| 46 | + expect(fakeFormData.append).toHaveBeenCalledWith('notes', notes); |
| 47 | + expect(fakeFormData.append).not.toHaveBeenCalledWith('linkDefectId'); |
| 48 | + expect(fakeBugSplatApiClient.fetch).toHaveBeenCalledWith( |
| 49 | + '/api/logDefect', |
| 50 | + jasmine.objectContaining({ |
| 51 | + method: 'POST', |
| 52 | + body: fakeFormData, |
| 53 | + duplex: 'half', |
| 54 | + }) |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should call fetch with linkDefectId if provided', () => { |
| 59 | + const linkDefectId = '123'; |
| 60 | + client.postStackKeyIssue(database, id, notes, linkDefectId); |
| 61 | + |
| 62 | + expect(fakeFormData.append).toHaveBeenCalledWith('linkDefectId', linkDefectId); |
| 63 | + }); |
| 64 | + }); |
| 65 | +}); |
0 commit comments