|
| 1 | +import { module, test } from 'qunit'; |
| 2 | +import { setupRenderingTest } from 'website-www/tests/helpers'; |
| 3 | +import { render, triggerEvent, waitFor } from '@ember/test-helpers'; |
| 4 | +import { hbs } from 'ember-cli-htmlbars'; |
| 5 | +import sinon from 'sinon'; |
| 6 | + |
| 7 | +module( |
| 8 | + 'Integration | Component | new-join-steps/new-step-one', |
| 9 | + function (hooks) { |
| 10 | + setupRenderingTest(hooks); |
| 11 | + |
| 12 | + hooks.beforeEach(function () { |
| 13 | + localStorage.removeItem('newStepOneData'); |
| 14 | + |
| 15 | + this.toast = this.owner.lookup('service:toast'); |
| 16 | + sinon.stub(this.toast, 'success'); |
| 17 | + sinon.stub(this.toast, 'error'); |
| 18 | + |
| 19 | + this.setIsPreValid = sinon.stub(); |
| 20 | + this.setIsValid = sinon.stub(); |
| 21 | + }); |
| 22 | + |
| 23 | + hooks.afterEach(function () { |
| 24 | + sinon.restore(); |
| 25 | + }); |
| 26 | + |
| 27 | + test('handleImageSelect rejects non-image files', async function (assert) { |
| 28 | + await render( |
| 29 | + hbs`<NewJoinSteps::NewStepOne @setIsPreValid={{this.setIsPreValid}} @setIsValid={{this.setIsValid}} />`, |
| 30 | + ); |
| 31 | + |
| 32 | + const file = new File(['pdf content'], 'document.pdf', { |
| 33 | + type: 'application/pdf', |
| 34 | + }); |
| 35 | + |
| 36 | + await triggerEvent('input[type="file"]', 'change', { |
| 37 | + files: [file], |
| 38 | + }); |
| 39 | + |
| 40 | + assert.ok( |
| 41 | + this.toast.error.calledWith( |
| 42 | + 'Invalid file type. Please upload an image file.', |
| 43 | + ), |
| 44 | + 'Shows error for non-image file', |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + test('handleImageSelect rejects files larger than 2MB', async function (assert) { |
| 49 | + await render( |
| 50 | + hbs`<NewJoinSteps::NewStepOne @setIsPreValid={{this.setIsPreValid}} @setIsValid={{this.setIsValid}} />`, |
| 51 | + ); |
| 52 | + |
| 53 | + const largeFile = new File(['x'.repeat(3 * 1024 * 1024)], 'large.jpg', { |
| 54 | + type: 'image/jpeg', |
| 55 | + }); |
| 56 | + |
| 57 | + await triggerEvent('input[type="file"]', 'change', { |
| 58 | + files: [largeFile], |
| 59 | + }); |
| 60 | + |
| 61 | + assert.ok( |
| 62 | + this.toast.error.calledWith('Image size must be less than 2MB'), |
| 63 | + 'Shows error for oversized file', |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + test('imagePreview and imageUrl are updated on successful upload', async function (assert) { |
| 68 | + const fetchStub = sinon.stub(window, 'fetch').resolves({ |
| 69 | + ok: true, |
| 70 | + json: async () => ({ url: 'https://example.com/photo.jpg' }), |
| 71 | + }); |
| 72 | + |
| 73 | + await render( |
| 74 | + hbs`<NewJoinSteps::NewStepOne @setIsPreValid={{this.setIsPreValid}} @setIsValid={{this.setIsValid}} />`, |
| 75 | + ); |
| 76 | + |
| 77 | + const file = new File(['image'], 'photo.jpg', { type: 'image/jpeg' }); |
| 78 | + |
| 79 | + await triggerEvent('input[type="file"]', 'change', { |
| 80 | + files: [file], |
| 81 | + }); |
| 82 | + |
| 83 | + await waitFor('[data-test-image-preview]'); |
| 84 | + |
| 85 | + assert.dom('[data-test-image-preview]').exists(); |
| 86 | + assert.dom('[data-test-image-preview]').hasAttribute('src'); |
| 87 | + assert.ok( |
| 88 | + this.toast.success.calledWith('Profile image uploaded successfully!'), |
| 89 | + 'Shows success toast', |
| 90 | + ); |
| 91 | + |
| 92 | + fetchStub.restore(); |
| 93 | + }); |
| 94 | + |
| 95 | + test('shows error toast on image upload API failure', async function (assert) { |
| 96 | + const fetchStub = sinon.stub(window, 'fetch').resolves({ |
| 97 | + ok: false, |
| 98 | + json: async () => ({ message: 'Server error' }), |
| 99 | + }); |
| 100 | + |
| 101 | + await render( |
| 102 | + hbs`<NewJoinSteps::NewStepOne @setIsPreValid={{this.setIsPreValid}} @setIsValid={{this.setIsValid}} />`, |
| 103 | + ); |
| 104 | + |
| 105 | + const file = new File(['image'], 'photo.jpg', { type: 'image/jpeg' }); |
| 106 | + |
| 107 | + await triggerEvent('input[type="file"]', 'change', { |
| 108 | + files: [file], |
| 109 | + }); |
| 110 | + |
| 111 | + assert.dom('[data-test-image-preview]').doesNotExist(); |
| 112 | + assert.ok( |
| 113 | + this.toast.error.calledWith('Server error'), |
| 114 | + 'Shows error toast with API message', |
| 115 | + ); |
| 116 | + |
| 117 | + fetchStub.restore(); |
| 118 | + }); |
| 119 | + }, |
| 120 | +); |
0 commit comments