-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: pass headers to request (#130)
* chore: pass headers to request * chore: update tests * refactor: adjust import lodash for ESM Signed-off-by: Nam Hoang <[email protected]> --------- Signed-off-by: Nam Hoang <[email protected]> Co-authored-by: Nam Hoang <[email protected]>
- Loading branch information
1 parent
3093f5e
commit 93f2076
Showing
9 changed files
with
144 additions
and
56 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
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
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,52 @@ | ||
import { request } from '../httpService'; | ||
import axios from 'axios'; | ||
|
||
jest.mock('axios'); | ||
|
||
const mockedAxios = axios as jest.Mocked<typeof axios>; | ||
|
||
describe('httpService', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
|
||
const mockRequest = jest.fn().mockResolvedValue({ data: 'mocked response' }); | ||
|
||
mockedAxios.create.mockReturnValue({ | ||
request: mockRequest, | ||
} as any); | ||
}); | ||
|
||
it('should accept a plain object for headers', async () => { | ||
const params = { headers: { test: 'test' } }; | ||
const result = await request(params); | ||
|
||
expect(result).toEqual({ data: 'mocked response' }); | ||
|
||
const mockAxiosInstance = mockedAxios.create.mock.results[0].value; | ||
expect(mockAxiosInstance.request).toHaveBeenCalledTimes(1); | ||
expect(mockAxiosInstance.request).toHaveBeenCalledWith(params); | ||
}); | ||
|
||
it('should throw an error if headers is not a plain object', async () => { | ||
await expect(request({ headers: [] as any })).rejects.toThrow( | ||
'Headers specified in the config must be a plain object with string values.', | ||
); | ||
}); | ||
|
||
it('should throw an error if headers contain non-string values', async () => { | ||
await expect(request({ headers: { test: 123 } as any })).rejects.toThrow( | ||
'Headers specified in the config must be a plain object with string values.', | ||
); | ||
}); | ||
|
||
it('should work when no headers are provided in params', async () => { | ||
const params = { url: 'test-url' }; | ||
const result = await request(params); | ||
|
||
expect(result).toEqual({ data: 'mocked response' }); | ||
|
||
const mockAxiosInstance = mockedAxios.create.mock.results[0].value; | ||
expect(mockAxiosInstance.request).toHaveBeenCalledTimes(1); | ||
expect(mockAxiosInstance.request).toHaveBeenCalledWith(params); | ||
}); | ||
}); |
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
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
Oops, something went wrong.