-
Notifications
You must be signed in to change notification settings - Fork 153
feat: integrate image upload API for application #1142
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a4c2ad4
feat: integrate image upload API
MayankBansal12 aa60b8f
fix: modify constant for application image upload
MayankBansal12 5624407
test: add test for image upload for applications
MayankBansal12 10236e5
fix: remove fileReader and use imageUrl from response
MayankBansal12 1709989
fix: update api and response for application profile upload
MayankBansal12 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
141 changes: 141 additions & 0 deletions
141
tests/integration/components/new-join-steps/new-step-one-test.js
This file contains hidden or 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,141 @@ | ||
| import { module, test } from 'qunit'; | ||
| import { setupRenderingTest } from 'website-www/tests/helpers'; | ||
| import { render, triggerEvent, waitFor } from '@ember/test-helpers'; | ||
| import { hbs } from 'ember-cli-htmlbars'; | ||
| import sinon from 'sinon'; | ||
|
|
||
| module( | ||
| 'Integration | Component | new-join-steps/new-step-one', | ||
| function (hooks) { | ||
| setupRenderingTest(hooks); | ||
|
|
||
| hooks.beforeEach(function () { | ||
| localStorage.removeItem('newStepOneData'); | ||
|
|
||
| this.toast = this.owner.lookup('service:toast'); | ||
| sinon.stub(this.toast, 'success'); | ||
| sinon.stub(this.toast, 'error'); | ||
|
|
||
| this.setIsPreValid = sinon.stub(); | ||
| this.setIsValid = sinon.stub(); | ||
| }); | ||
|
|
||
| hooks.afterEach(function () { | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| test('handleImageSelect rejects non-image files', async function (assert) { | ||
| await render( | ||
| hbs`<NewJoinSteps::NewStepOne @setIsPreValid={{this.setIsPreValid}} @setIsValid={{this.setIsValid}} />`, | ||
| ); | ||
|
|
||
| const file = new File(['pdf content'], 'document.pdf', { | ||
| type: 'application/pdf', | ||
| }); | ||
|
|
||
| await triggerEvent('input[type="file"]', 'change', { | ||
| files: [file], | ||
| }); | ||
|
|
||
| assert.ok( | ||
| this.toast.error.calledWithExactly( | ||
| 'Invalid file type. Please upload an image file.', | ||
| 'Error!', | ||
| ), | ||
| 'Shows error for non-image file', | ||
| ); | ||
| }); | ||
|
|
||
| test('handleImageSelect rejects files larger than 2MB', async function (assert) { | ||
| await render( | ||
| hbs`<NewJoinSteps::NewStepOne @setIsPreValid={{this.setIsPreValid}} @setIsValid={{this.setIsValid}} />`, | ||
| ); | ||
|
|
||
| const largeFile = new File(['x'.repeat(3 * 1024 * 1024)], 'large.jpg', { | ||
| type: 'image/jpeg', | ||
| }); | ||
|
|
||
| await triggerEvent('input[type="file"]', 'change', { | ||
| files: [largeFile], | ||
| }); | ||
|
|
||
| assert.ok( | ||
| this.toast.error.calledWithExactly( | ||
| 'Image size must be less than 2MB', | ||
| 'Error!', | ||
| ), | ||
| 'Shows error for oversized file', | ||
| ); | ||
| }); | ||
|
|
||
| test('imagePreview and imageUrl are updated on successful upload', async function (assert) { | ||
| sinon.stub(window, 'fetch').resolves({ | ||
| ok: true, | ||
| json: async () => ({ | ||
| image: { | ||
| url: 'https://example.com/photo.jpg', | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| await render( | ||
| hbs`<NewJoinSteps::NewStepOne @setIsPreValid={{this.setIsPreValid}} @setIsValid={{this.setIsValid}} />`, | ||
| ); | ||
|
|
||
| const file = new File(['image'], 'photo.jpg', { type: 'image/jpeg' }); | ||
|
|
||
| await triggerEvent('input[type="file"]', 'change', { | ||
| files: [file], | ||
| }); | ||
|
|
||
| await waitFor('[data-test-image-preview]'); | ||
|
|
||
| assert.dom('[data-test-image-preview]').exists(); | ||
| assert.dom('[data-test-image-preview]').hasAttribute('src'); | ||
|
|
||
| const storedData = JSON.parse( | ||
| localStorage.getItem('newStepOneData') || '{}', | ||
| ); | ||
| assert.strictEqual( | ||
| storedData.imageUrl, | ||
| 'https://example.com/photo.jpg', | ||
| 'Persists returned image URL to localStorage', | ||
| ); | ||
| assert.ok( | ||
| this.toast.success.calledWithExactly( | ||
| 'Profile image uploaded successfully!', | ||
| 'Success!', | ||
| sinon.match.object, | ||
| ), | ||
| 'Shows success toast', | ||
| ); | ||
| }); | ||
|
|
||
| test('shows error toast on image upload API failure', async function (assert) { | ||
| sinon.stub(window, 'fetch').resolves({ | ||
| ok: false, | ||
| json: async () => ({ message: 'Server error' }), | ||
| }); | ||
|
|
||
| await render( | ||
| hbs`<NewJoinSteps::NewStepOne @setIsPreValid={{this.setIsPreValid}} @setIsValid={{this.setIsValid}} />`, | ||
| ); | ||
|
|
||
| const file = new File(['image'], 'photo.jpg', { type: 'image/jpeg' }); | ||
|
|
||
| await triggerEvent('input[type="file"]', 'change', { | ||
| files: [file], | ||
| }); | ||
|
|
||
| assert.dom('[data-test-image-preview]').doesNotExist(); | ||
| assert.ok( | ||
| this.toast.error.calledWithExactly( | ||
| 'Server error', | ||
| 'Error!', | ||
| sinon.match.object, | ||
| ), | ||
| 'Shows error toast with API message', | ||
| ); | ||
| }); | ||
| }, | ||
| ); |
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.
at response.json()
If the server returns a non-JSON error response (e.g., a 502 Bad Gateway with an HTML body), await response.json() will throw, get swallowed by the catch, and display a cryptic message like "Unexpected token '<'" instead of a meaningful error.