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] Include user profile details in localstorage with token informa…
…tion
- Loading branch information
1 parent
a1e44aa
commit 06139d8
Showing
9 changed files
with
118 additions
and
96 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 { setItem, getItem, clearItem } from './localStorage'; | ||
|
||
/* eslint-disable camelcase */ | ||
describe('setItem', () => { | ||
test('Given a valid UserToken, sets it in LocalStorage', () => { | ||
const testToken = { access_token: 'access_token', refresh_token: 'refesh_token' }; | ||
|
||
setItem('UserProfile', testToken); | ||
|
||
expect(JSON.parse(localStorage.getItem('UserProfile') as string)).toStrictEqual(testToken); | ||
}); | ||
}); | ||
|
||
describe('getItem', () => { | ||
test('Given an existing UserToken in LocalStorage, returns the value', () => { | ||
const testToken = { access_token: 'access_token', refresh_token: 'refesh_token' }; | ||
|
||
localStorage.setItem('UserProfile', JSON.stringify(testToken)); | ||
|
||
expect(getItem('UserProfile')).toStrictEqual(testToken); | ||
|
||
localStorage.clear(); | ||
}); | ||
|
||
test('Given a NON-existing UserToken in LocalStorage, returns null', () => { | ||
expect(getItem('UserProfile')).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('clearItem', () => { | ||
test('Given an existing UserToken in LocalStorage, removes the value', () => { | ||
const testToken = { access_token: 'access_token', refresh_token: 'refesh_token' }; | ||
|
||
localStorage.setItem('UserProfile', JSON.stringify(testToken)); | ||
|
||
expect(JSON.parse(localStorage.getItem('UserProfile') as string)).toStrictEqual(testToken); | ||
|
||
clearItem('UserProfile'); | ||
|
||
expect(JSON.parse(localStorage.getItem('UserProfile') as string)).toBeNull(); | ||
}); | ||
}); | ||
/* eslint-enable camelcase */ |
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,13 @@ | ||
export const getItem = (key: string) => { | ||
const value = localStorage.getItem(key); | ||
|
||
return value && JSON.parse(value); | ||
}; | ||
|
||
export const setItem = (key: string, value: object) => { | ||
localStorage.setItem(key, JSON.stringify(value)); | ||
}; | ||
|
||
export const clearItem = (key: string) => { | ||
localStorage.removeItem(key); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
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 |
---|---|---|
|
@@ -19,21 +19,39 @@ const commonLoginParams = { | |
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', | ||
}; | ||
|
||
const commonLoginResponse = { | ||
data: { | ||
id: '18339', | ||
type: 'token', | ||
attributes: { | ||
token_type: 'Bearer', | ||
expires_in: 7200, | ||
created_at: 1677045997, | ||
...mockTokenData, | ||
}, | ||
}, | ||
}; | ||
|
||
const commonUserProfileResponse = { | ||
data: { | ||
id: '1', | ||
type: 'user', | ||
attributes: { | ||
email: '[email protected]', | ||
name: 'TestName', | ||
avatar_url: 'https://secure.gravatar.com/avatar/6733d09432e89459dba795de8312ac2d', | ||
}, | ||
}, | ||
}; | ||
|
||
const testCredentials = { | ||
email: '[email protected]', | ||
password: 'password123', | ||
|
@@ -80,14 +98,19 @@ describe('LoginScreen', () => { | |
.defaultReplyHeaders({ | ||
'access-control-allow-origin': '*', | ||
'access-control-allow-credentials': 'true', | ||
'access-control-allow-headers': 'Authorization', | ||
}) | ||
.post('/oauth/token', { | ||
...commonLoginParams, | ||
...testCredentials, | ||
}) | ||
.reply(200, { | ||
...commonLoginResponse, | ||
}); | ||
}) | ||
.options('/me') | ||
.reply(204) | ||
.get('/me') | ||
.reply(200, { ...commonUserProfileResponse }); | ||
|
||
render(<LoginScreen />, { wrapper: BrowserRouter }); | ||
|
||
|
@@ -105,7 +128,10 @@ describe('LoginScreen', () => { | |
expect(submitButton).toHaveAttribute('disabled'); | ||
|
||
await waitFor(() => { | ||
expect(localStorage.setItem).toHaveBeenLastCalledWith('UserToken', JSON.stringify({ ...mockTokenData })); | ||
expect(localStorage.setItem).toHaveBeenLastCalledWith( | ||
'UserProfile', | ||
JSON.stringify({ auth: mockTokenData, user: mockUserProfileData }) | ||
); | ||
}); | ||
|
||
await waitFor(() => { | ||
|
@@ -148,7 +174,7 @@ describe('LoginScreen', () => { | |
expect(mockLogin).toBeCalled(); | ||
|
||
await waitFor(() => { | ||
expect(localStorage.setItem).not.toHaveBeenLastCalledWith('UserToken', JSON.stringify({ ...mockTokenData })); | ||
expect(localStorage.setItem).not.toHaveBeenLastCalledWith('UserProfile', JSON.stringify({ ...mockTokenData })); | ||
}); | ||
|
||
await waitFor(() => { | ||
|
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