-
Notifications
You must be signed in to change notification settings - Fork 22
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
Kunal Nagar
authored
Dec 22, 2021
1 parent
5a22b44
commit e33610d
Showing
7 changed files
with
100 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
dist/ | ||
lib/ | ||
node_modules/ | ||
node_modules/ | ||
tsconfig.json |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 @@ | ||
export * from './request' |
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,12 @@ | ||
import fetch, { Response as FetchResponse, RequestInit } from 'node-fetch' | ||
|
||
type RequestOptions = Pick<RequestInit, 'method' | 'body' | 'headers'> | ||
type Response = Pick<FetchResponse, 'headers' | 'json' | 'ok' | 'text'> | ||
|
||
export const request = async ( | ||
url: string, | ||
options?: RequestOptions, | ||
): Promise<Response> => | ||
fetch(url, { | ||
...options, | ||
}) |
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,29 @@ | ||
import { request } from '.' | ||
import fetch from 'node-fetch' | ||
|
||
jest.mock('node-fetch') | ||
|
||
const URL = 'someUrl' | ||
|
||
describe('request', () => { | ||
it('request: should perform a GET request with empty options object', async () => { | ||
await request(URL) | ||
expect(fetch).toHaveBeenCalledTimes(1) | ||
expect(fetch).toHaveBeenCalledWith(URL, {}) | ||
}) | ||
|
||
it('request: should perform a POST request with body and headers', async () => { | ||
const options = { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
key1: 'value1', | ||
}), | ||
headers: { | ||
Authorization: 'Bearer token', | ||
}, | ||
} | ||
await request(URL, options) | ||
expect(fetch).toHaveBeenCalledTimes(1) | ||
expect(fetch).toHaveBeenCalledWith(URL, options) | ||
}) | ||
}) |