-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENG-5114 integration API /userProfiles/avatar to upload the avatar #1582
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,39 @@ | ||
import { makeRequest, METHODS } from '@entando/apimanager'; | ||
import { FILE_BROWSER_FILE } from 'test/mocks/fileBrowser'; | ||
|
||
const AVATAR_ENDPOINT = '/api/userProfiles/avatar'; | ||
|
||
export const getAvatar = () => | ||
makeRequest({ | ||
uri: AVATAR_ENDPOINT, | ||
method: METHODS.GET, | ||
mockResponse: FILE_BROWSER_FILE, | ||
useAuthentication: true, | ||
}); | ||
|
||
export const postAvatar = avatar => | ||
makeRequest({ | ||
uri: AVATAR_ENDPOINT, | ||
method: METHODS.POST, | ||
body: avatar, | ||
mockResponse: FILE_BROWSER_FILE, | ||
useAuthentication: true, | ||
}); | ||
|
||
export const updateAvatar = avatar => | ||
makeRequest({ | ||
uri: AVATAR_ENDPOINT, | ||
method: METHODS.PUT, | ||
body: avatar, | ||
mockResponse: FILE_BROWSER_FILE, | ||
useAuthentication: true, | ||
}); | ||
|
||
export const deleteAvatar = () => | ||
makeRequest({ | ||
uri: AVATAR_ENDPOINT, | ||
method: METHODS.DELETE, | ||
mockResponse: FILE_BROWSER_FILE, | ||
useAuthentication: true, | ||
}); | ||
|
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,27 @@ | ||
import { postAvatar } from 'api/avatar'; | ||
import { getBase64 } from 'state/file-browser/actions'; | ||
import { toggleLoading } from 'state/loading/actions'; | ||
import { addToast, addErrors, TOAST_SUCCESS, TOAST_ERROR } from '@entando/messages'; | ||
|
||
export const createFileObject = async (avatar) => { | ||
const base64 = await getBase64(avatar); | ||
return { fileName: avatar.name, base64 }; | ||
}; | ||
|
||
export const uploadAvatar = | ||
(avatar, loader = 'uploadAvatar') => | ||
async (dispatch) => { | ||
try { | ||
dispatch(toggleLoading(loader)); | ||
const requestObject = await createFileObject(avatar); | ||
await postAvatar(requestObject); | ||
dispatch(toggleLoading(loader)); | ||
dispatch(addToast({ id: 'fileBrowser.uploadFileComplete' }, TOAST_SUCCESS)); | ||
} catch (error) { | ||
dispatch(toggleLoading(loader)); | ||
const message = { id: 'fileBrowser.uploadFileError', values: { errmsg: error } }; | ||
dispatch(addErrors(error)); | ||
dispatch(addToast(message, TOAST_ERROR)); | ||
} | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a new line at the end of the file |
||
|
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,44 @@ | ||
import { createFileObject, uploadAvatar } from 'state/avatar/actions'; | ||
import * as actionsHelper from 'state/avatar/actions'; | ||
import * as apiHelper from 'api/avatar'; | ||
|
||
describe('createFileObject', () => { | ||
it('should create a file object', async () => { | ||
const avatar = new File(['test data'], 'test.png', { type: 'image/png' }); | ||
const result = await createFileObject(avatar); | ||
expect(result).toEqual({ fileName: 'test.png', base64: result.base64 }); | ||
}); | ||
}); | ||
|
||
describe('uploadAvatar', () => { | ||
it('should upload avatar successfully', async () => { | ||
const avatar = new File(['test data'], 'test.png', { type: 'image/png' }); | ||
const dispatch = jest.fn(); | ||
jest.spyOn(actionsHelper, 'createFileObject').mockResolvedValue({ fileName: 'test.png', base64: 'base64encodedstring' }); | ||
jest.spyOn(apiHelper, 'postAvatar').mockResolvedValue(); | ||
|
||
await uploadAvatar(avatar)(dispatch); | ||
|
||
expect(dispatch).toHaveBeenCalledWith({ payload: { id: 'uploadAvatar' }, type: 'loading/toggle-loading' }); | ||
expect(dispatch).toHaveBeenCalledWith({ payload: { id: 'uploadAvatar' }, type: 'loading/toggle-loading' }); | ||
expect(dispatch).toHaveBeenCalledWith({ payload: { message: { id: 'fileBrowser.uploadFileComplete' }, type: 'success' }, type: 'toasts/add-toast' }); | ||
expect(dispatch).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it('should handle avatar upload error', async () => { | ||
const avatar = { name: 'test.jpg' }; | ||
const dispatch = jest.fn(); | ||
const error = new Error('Upload failed'); | ||
jest.spyOn(actionsHelper, 'createFileObject').mockResolvedValue({ fileName: avatar.name, base64: 'base64encodedstring' }); | ||
jest.spyOn(apiHelper, 'postAvatar').mockRejectedValue(error); | ||
|
||
await uploadAvatar(avatar)(dispatch); | ||
|
||
expect(dispatch).toHaveBeenCalledWith({ payload: { id: 'uploadAvatar' }, type: 'loading/toggle-loading' }); | ||
expect(dispatch).toHaveBeenCalledWith({ payload: { id: 'uploadAvatar' }, type: 'loading/toggle-loading' }); | ||
expect(dispatch).toHaveBeenCalledWith({ payload: { message: { id: 'fileBrowser.uploadFileError', values: { errmsg: TypeError('[TypeError: Failed to execute \'readAsDataURL\' on \'FileReader\': parameter 1 is not of type \'Blob\'.]') } }, type: 'error' }, type: 'toasts/add-toast' }); | ||
expect(dispatch).toHaveBeenCalledWith({ type: 'errors/add-errors', payload: { errors: TypeError('[TypeError: Failed to execute \'readAsDataURL\' on \'FileReader\': parameter 1 is not of type \'Blob\'.]') } }); | ||
expect(dispatch).toHaveBeenCalledTimes(4); | ||
}); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a new line at the end of the file |
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a new line at the end of the file