Skip to content

Commit

Permalink
FIX Show correct error message instead of successful message if file …
Browse files Browse the repository at this point in the history
…exceeding maximum file size
  • Loading branch information
Sabina Talipova committed May 30, 2022
1 parent 21ff84c commit d54ae07
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 3 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/dist/styles/bundle.css

Large diffs are not rendered by default.

15 changes: 14 additions & 1 deletion client/src/components/PreviewImageField/PreviewImageField.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ class PreviewImageField extends Component {
* @param {object} progress
*/
handleUploadProgress(file, progress) {
let timer;
clearTimeout(timer);
if (file && file.status === 'uploading') {
timer = setTimeout(() => this.handleUploadProgress(file, progress), 1000);
return;
}
this.props.actions.previewField.updateFile(this.props.id, { progress });
}

Expand Down Expand Up @@ -277,6 +283,7 @@ class PreviewImageField extends Component {
}

const { category, progress, message } = upload;
const errors = upload.errors ? upload.errors[0] : null;
const preview = this.preview(category, upload, data);
const image = <img alt="preview" src={preview} className="editor__thumbnail" />;
const linkedImage = (data.url && !progress) ? (
Expand All @@ -302,7 +309,13 @@ class PreviewImageField extends Component {
{message.value}
</div>
);
} else if (progress === 100) {
} else if (errors) {
messageBox = (
<div className={`preview-image-field__message preview-image-field__message--${errors.type}`}>
{errors.value}
</div>
);
} else if (progress === 100 && !errors) {
messageBox = (
<div className="preview-image-field__message preview-image-field__message--success">
{i18n._t(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,9 @@
color: $white;
background: $brand-success;
}

.preview-image-field__message--info {
color: $white;
background: $brand-primary;
}

Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,87 @@ describe('PreviewImageField', () => {
expect(url).toBe('/logo.jpg?vid=456');
});
});

describe('handleUploadProgress()', () => {
it.each(
[
{
status: 'uploading',
resultStatus: 'success',
upload: {
category: 'image',
extension: 'png',
filename: 'test.png',
},

},
{
status: 'uploading',
resultStatus: 'error',
upload: {
category: 'text',
extension: 'txt',
filename: 'test.txt',
errors: [
{
code: 400,
type: 'error',
value: 'Filesize is too large, maximum 100 KB allowed',
}
]
},
message: 'Filesize is too large, maximum 100 KB allowed',
},
]
)('should wait before upload process will finished', ({ status, upload, resultStatus, message }, done) => {
const file = {
status
};
props.actions = {
previewField: {
updateFile: jest.fn((id, { progress }) => {
props.upload.progress = progress;
props.id = id;
})
},
};
props.upload = upload;
props.upload.progress = 0;

const item = ReactTestUtils.renderIntoDocument(
<PreviewImageField {...props} />
);

jest.useFakeTimers();

item.handleUploadProgress(file, 100);
file.status = resultStatus;

const asyncFunction = (callback) => {
setTimeout(() => {
callback();
}, 1001);
};

const callback = () => {
if (resultStatus === 'error') {
expect(props.id).toBe('Form_Test_Field');
expect(props.upload.progress).toBe(100);
expect(props.upload.category).toBe('text');
expect(props.upload.errors[0].code).toBe(400);
expect(props.upload.errors[0].type).toBe(resultStatus);
expect(props.upload.errors[0].value).toBe(message);
done();
} else {
expect(props.id).toBe('Form_Test_Field');
expect(props.upload.errors).toBe(undefined);
expect(props.upload.category).toBe('image');
expect(props.upload.progress).toBe(100);
done();
}
};
asyncFunction(callback);
jest.advanceTimersByTime(1001);
});
});
});
11 changes: 11 additions & 0 deletions tests/behat/features/replace-file.feature
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,14 @@ Feature: Replace a file with a new file
When I press the "Save" button
Then the "Name" field should contain "document-v2.pdf"
And I should not see a ".preview-image-field__message--success" element

@javascript
Scenario: I upload an image file exceeding maximum file size to replace a file
Given a maximum file size is "5k"
When I attach the file "file1.jpg" to dropzone "PreviewImage"
Then I should see a ".preview-image-field__message-button" element
And I should see a ".preview-image-field__message--success" element
And the "Name" field should contain "file1.jpg"
When I attach the file "file3.jpg" to dropzone "PreviewImage"
Then I should see a ".preview-image-field__message--error" element
And the "Name" field should contain "file1.jpg"
Binary file added tests/behat/files/file3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions tests/behat/src/FixtureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Page;
use PHPUnit\Framework\Assert;
use SilverStripe\Assets\Image;
use SilverStripe\Assets\Upload_Validator;
use SilverStripe\BehatExtension\Context\FixtureContext as BaseFixtureContext;
use SilverStripe\BehatExtension\Utility\StepHelper;

Expand Down Expand Up @@ -508,4 +509,15 @@ public function iScrollTheEditorDetailsPanelToTheTop()
$script = "document.querySelector('.editor__details fieldset').scrollTo(0, 0);";
$this->getMainContext()->getSession()->executeScript($script);
}

/**
* Example: Given a maximum file size is 5k
*
* @Given /^a maximum file size is "([^"]+)"$/
* @param string $size Max file size
*/
public function stepCreateMaximumFileSizeStep($size): void
{
Upload_Validator::config()->set('default_max_file_size', ['*' => $size]);
}
}

0 comments on commit d54ae07

Please sign in to comment.