-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c33749
commit 5cc0e95
Showing
2 changed files
with
78 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { sendRequest } from '../../src/functions/request/request.functions'; | ||
import { TEST_REGTEST_ATTESTOR_APIS } from '../mocks/api.test.constants'; | ||
|
||
global.fetch = jest.fn(); | ||
|
||
describe('Request Functions', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
describe('sendRequest', () => { | ||
it('should not result in an error when the response status is ok', async () => { | ||
jest | ||
.spyOn(global, 'fetch') | ||
.mockImplementationOnce(async () => new Response(null, { status: 200 })); | ||
|
||
await expect( | ||
sendRequest(TEST_REGTEST_ATTESTOR_APIS[0], 'requestBody') | ||
).resolves.not.toThrow(); | ||
}); | ||
|
||
it('should result in an error when the response status is not ok', async () => { | ||
jest | ||
.spyOn(global, 'fetch') | ||
.mockImplementationOnce( | ||
async () => new Response(null, { status: 400, statusText: 'Bad Request' }) | ||
); | ||
|
||
await expect(sendRequest(TEST_REGTEST_ATTESTOR_APIS[0], 'requestBody')).rejects.toThrow( | ||
new Error(`Response ${TEST_REGTEST_ATTESTOR_APIS[0]} was not OK: Bad Request`) | ||
); | ||
}); | ||
|
||
it('should result in an error when the request fails', async () => { | ||
jest.spyOn(global, 'fetch').mockImplementationOnce(async () => { | ||
throw new Error('Failed to fetch'); | ||
}); | ||
|
||
await expect(sendRequest(TEST_REGTEST_ATTESTOR_APIS[0], 'requestBody')).rejects.toThrow( | ||
new Error(`Failed to fetch`) | ||
); | ||
}); | ||
}); | ||
}); |