Skip to content

Commit 92d41d4

Browse files
authored
fix: listen to file-reject instead of file-rejected (#4732)
* fix: fix event name typo * fix: remove default createTempFile and add parameters for the usages
1 parent 9ff0890 commit 92d41d4

6 files changed

Lines changed: 38 additions & 30 deletions

File tree

‎vaadin-upload-flow-parent/vaadin-upload-flow-integration-tests/src/main/java/com/vaadin/flow/component/upload/tests/UploadView.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ private void createSimpleUpload() {
4747

4848
MultiFileMemoryBuffer buffer = new MultiFileMemoryBuffer();
4949
Upload upload = new Upload(buffer);
50+
upload.setAcceptedFileTypes(".txt");
5051

5152
upload.addSucceededListener(event -> {
5253
try {
@@ -59,6 +60,7 @@ private void createSimpleUpload() {
5960
eventsOutput.add("-succeeded");
6061
});
6162
upload.addAllFinishedListener(event -> eventsOutput.add("-finished"));
63+
upload.addFileRejectedListener(event -> eventsOutput.add("-rejected"));
6264

6365
NativeButton clearFileListBtn = new NativeButton("Clear file list",
6466
e -> upload.clearFileList());

‎vaadin-upload-flow-parent/vaadin-upload-flow-integration-tests/src/test/java/com/vaadin/flow/component/upload/tests/AbstractUploadIT.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
import com.vaadin.tests.AbstractComponentIT;
99

1010
public abstract class AbstractUploadIT extends AbstractComponentIT {
11+
1112
/**
13+
* Creates a temp file with the provided extension for testing purposes.
14+
*
15+
* @param extension
16+
* the temp file extension without leading dot
1217
* @return The generated temp file handle
1318
* @throws IOException
1419
*/
15-
File createTempFile() throws IOException {
16-
File tempFile = File.createTempFile("TestFileUpload", ".txt");
20+
File createTempFile(String extension) throws IOException {
21+
File tempFile = File.createTempFile("TestFileUpload", "." + extension);
1722
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
1823
writer.write(getTempFileContents());
1924
writer.close();

‎vaadin-upload-flow-parent/vaadin-upload-flow-integration-tests/src/test/java/com/vaadin/flow/component/upload/tests/FileBufferIT.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void testUploadAnyFile() throws Exception {
2121
final UploadElement upload = $(UploadElement.class).id("single-upload");
2222
waitUntil(driver -> upload.isDisplayed());
2323

24-
File tempFile = createTempFile();
24+
File tempFile = createTempFile("txt");
2525
upload.upload(tempFile);
2626

2727
WebElement uploadOutput = getDriver()
@@ -42,7 +42,7 @@ public void testUploadMultipleEventOrder() throws Exception {
4242
final UploadElement upload = $(UploadElement.class).id("multi-upload");
4343
waitUntil(driver -> upload.isDisplayed());
4444

45-
File tempFile = createTempFile();
45+
File tempFile = createTempFile("txt");
4646

4747
upload.uploadMultiple(List.of(tempFile, tempFile, tempFile), 10);
4848

‎vaadin-upload-flow-parent/vaadin-upload-flow-integration-tests/src/test/java/com/vaadin/flow/component/upload/tests/NonImmediateUploadIT.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public void uploadMultipleFiles_shouldNotThrowException_onRemove()
2828
private void uploadMultipleFiles_shouldNotThrowException(String buttonType)
2929
throws Exception {
3030
open();
31-
File file1 = createTempFile();
32-
File file2 = createTempFile();
31+
File file1 = createTempFile("txt");
32+
File file2 = createTempFile("txt");
3333
UploadElement upload = $(UploadElement.class).waitForFirst();
3434
upload.upload(file1);
3535
upload.upload(file2);

‎vaadin-upload-flow-parent/vaadin-upload-flow-integration-tests/src/test/java/com/vaadin/flow/component/upload/tests/UploadIT.java‎

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.hamcrest.CoreMatchers;
2323
import org.junit.Assert;
24+
import org.junit.Before;
2425
import org.junit.Test;
2526
import org.openqa.selenium.By;
2627
import org.openqa.selenium.WebElement;
@@ -37,13 +38,15 @@
3738
@TestPath("vaadin-upload")
3839
public class UploadIT extends AbstractUploadIT {
3940

40-
@Test
41-
public void testUploadAnyFile() throws Exception {
41+
@Before
42+
public void init() {
4243
open();
43-
4444
waitUntil(driver -> getUpload().isDisplayed());
45+
}
4546

46-
File tempFile = createTempFile();
47+
@Test
48+
public void testUploadAnyFile() throws Exception {
49+
File tempFile = createTempFile("txt");
4750
getUpload().upload(tempFile);
4851

4952
WebElement uploadOutput = getDriver().findElement(By.id("test-output"));
@@ -58,11 +61,7 @@ public void testUploadAnyFile() throws Exception {
5861

5962
@Test
6063
public void testClearFileList() throws Exception {
61-
open();
62-
63-
waitUntil(driver -> getUpload().isDisplayed());
64-
65-
File tempFile = createTempFile();
64+
File tempFile = createTempFile("txt");
6665

6766
getUpload().upload(tempFile);
6867
getUpload().upload(tempFile);
@@ -82,11 +81,7 @@ public void testClearFileList() throws Exception {
8281

8382
@Test
8483
public void testUploadMultipleEventOrder() throws Exception {
85-
open();
86-
87-
waitUntil(driver -> getUpload().isDisplayed());
88-
89-
File tempFile = createTempFile();
84+
File tempFile = createTempFile("txt");
9085

9186
getUpload().uploadMultiple(List.of(tempFile, tempFile, tempFile), 10);
9287

@@ -100,11 +95,7 @@ public void testUploadMultipleEventOrder() throws Exception {
10095

10196
@Test
10297
public void testUploadEventOrder() throws Exception {
103-
open();
104-
105-
waitUntil(driver -> getUpload().isDisplayed());
106-
107-
File tempFile = createTempFile();
98+
File tempFile = createTempFile("txt");
10899

109100
getUpload().upload(tempFile);
110101

@@ -116,11 +107,21 @@ public void testUploadEventOrder() throws Exception {
116107
}
117108

118109
@Test
119-
public void uploadFileAndNoErrorThrown() throws Exception {
120-
open();
121-
waitUntil(driver -> getUpload().isDisplayed());
110+
public void uploadInvalidFile_fileIsRejected() throws Exception {
111+
File invalidFile = createTempFile("pdf");
122112

123-
File tempFile = createTempFile();
113+
getUpload().upload(invalidFile);
114+
115+
WebElement eventsOutput = getDriver()
116+
.findElement(By.id("test-events-output"));
117+
118+
Assert.assertEquals("Invalid file was not rejected", "-rejected",
119+
eventsOutput.getText());
120+
}
121+
122+
@Test
123+
public void uploadFileAndNoErrorThrown() throws Exception {
124+
File tempFile = createTempFile("txt");
124125
getUpload().upload(tempFile);
125126

126127
List<LogEntry> logList1 = getLogEntries(Level.SEVERE);

‎vaadin-upload-flow-parent/vaadin-upload-flow/src/main/java/com/vaadin/flow/component/upload/Upload.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public UploadIcon() {
108108
*/
109109
public Upload() {
110110
final String eventDetailError = "event.detail.error";
111-
getElement().addEventListener("file-rejected", event -> {
111+
getElement().addEventListener("file-reject", event -> {
112112
String detailError = event.getEventData()
113113
.getString(eventDetailError);
114114
fireEvent(new FileRejectedEvent(this, detailError));

0 commit comments

Comments
 (0)