generated from nimblehq/git-template
-
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.
[#25] Add tests for Axios interceptors wip
- Loading branch information
1 parent
1ca5c97
commit e1cfc27
Showing
3 changed files
with
146 additions
and
50 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,12 +1,37 @@ | ||
import axios from 'axios'; | ||
|
||
import requestManager, { defaultOptions } from './requestManager'; | ||
import requestManager, { | ||
createRequestSuccessInterceptor, | ||
createResponseErrorInterceptor, | ||
defaultOptions, | ||
} from './requestManager'; | ||
import { LOGIN_URL } from '../constants'; | ||
import { setItem, clearItem } from '../helpers/localStorage'; | ||
|
||
const windowHref = window.location.href; | ||
const endPoint = 'https://sample-endpoint.com/api/'; | ||
|
||
jest.mock('axios'); | ||
|
||
describe('requestManager', () => { | ||
const endPoint = 'https://sample-endpoint.com/api/'; | ||
/* eslint-disable camelcase */ | ||
|
||
const mockTokenData = { | ||
access_token: 'test_access_token', | ||
refresh_token: 'test_refresh_token', | ||
token_type: 'Bearer', | ||
expires_in: 7200, | ||
created_at: 1677045997, | ||
}; | ||
|
||
const mockUserProfileData = { | ||
email: '[email protected]', | ||
name: 'TestName', | ||
avatar_url: 'https://secure.gravatar.com/avatar/6733d09432e89459dba795de8312ac2d', | ||
}; | ||
|
||
/* eslint-enable camelcase */ | ||
|
||
describe('requestManager', () => { | ||
it('fetches successfully data from an API', async () => { | ||
const responseData = { | ||
data: [ | ||
|
@@ -44,3 +69,66 @@ describe('requestManager', () => { | |
requestSpy.mockRestore(); | ||
}); | ||
}); | ||
|
||
describe('createRequestSuccessInterceptor', () => { | ||
beforeEach(() => { | ||
clearItem('UserProfile'); | ||
}); | ||
|
||
it('given an existing access_token, sets Authorization header in Axios config', async () => { | ||
setItem('UserProfile', { auth: mockTokenData, user: mockUserProfileData }); | ||
|
||
const interceptor = createRequestSuccessInterceptor(); | ||
const config = interceptor({ headers: {}, url: endPoint }); | ||
|
||
expect(config.headers.Authorization).toBe(`Bearer ${mockTokenData.access_token}`); | ||
}); | ||
|
||
it('given a non-existing access_token, does NOT set Authorization header in Axios config', async () => { | ||
const interceptor = createRequestSuccessInterceptor(); | ||
const config = interceptor({ headers: {}, url: endPoint }); | ||
|
||
expect(config.headers.Authorization).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('createResponseErrorInterceptor', () => { | ||
beforeEach(() => { | ||
clearItem('UserProfile'); | ||
|
||
delete global.window.location; | ||
window.location = {}; | ||
Object.defineProperty(window, 'location', { | ||
value: { | ||
href: windowHref, | ||
}, | ||
}); | ||
}); | ||
|
||
it('given NON existing tokens and 401 error, redirects to login page', async () => { | ||
const errorResponse = { | ||
name: '', | ||
message: '', | ||
isAxiosError: true, | ||
toJSON: () => ({}), | ||
config: {}, | ||
code: '', | ||
response: { | ||
data: {}, | ||
status: 401, | ||
statusText: '', | ||
headers: {}, | ||
config: {}, | ||
}, | ||
}; | ||
|
||
try { | ||
const interceptor = createResponseErrorInterceptor(); | ||
await interceptor(errorResponse); | ||
} catch (err) { | ||
console.log(err); | ||
expect(err).toBe(errorResponse); | ||
expect(window.location.href).toEqual(LOGIN_URL); | ||
} | ||
}); | ||
}); |
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