Skip to content

Commit

Permalink
fix(blocks): resolve on file input dialog cancel (#6950)
Browse files Browse the repository at this point in the history
Co-authored-by: Aadi <123532141+golok727@users.noreply.github.com>
Co-authored-by: Mirone <Saul-Mirone@outlook.com>
  • Loading branch information
3 people committed May 18, 2024
1 parent a9774c2 commit 90647ed
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/blocks/src/_common/utils/filesys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ export async function openFileOrFiles({
}
resolve(input.files[0]);
});
// The `cancel` event fires when the user cancels the dialog.
input.addEventListener('cancel', () => {
resolve(null);
});
// Show the picker.
if ('showPicker' in HTMLInputElement.prototype) {
input.showPicker();
Expand Down
38 changes: 37 additions & 1 deletion tests/attachment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ function getAttachment(page: Page) {
await type(page, 'file', 100);
await expect(slashMenu).toBeVisible();

const fileChooser = page.waitForEvent('filechooser');

Check failure on line 63 in tests/attachment.spec.ts

View workflow job for this annotation

GitHub Actions / Playground E2E test (1)

attachment.spec.ts:439:1 › support dragging attachment block directly

1) attachment.spec.ts:439:1 › support dragging attachment block directly ───────────────────────── TimeoutError: page.waitForEvent: Timeout 5000ms exceeded while waiting for event "filechooser" =========================== logs =========================== waiting for event "filechooser" ============================================================ 61 | await expect(slashMenu).toBeVisible(); 62 | > 63 | const fileChooser = page.waitForEvent('filechooser'); | ^ 64 | await pressEnter(page); 65 | await (await fileChooser).setFiles(FILE_PATH); 66 | at insertAttachment (/home/runner/work/blocksuite/blocksuite/tests/attachment.spec.ts:63:30) at /home/runner/work/blocksuite/blocksuite/tests/attachment.spec.ts:447:3

Check failure on line 63 in tests/attachment.spec.ts

View workflow job for this annotation

GitHub Actions / Playground E2E test (1)

attachment.spec.ts:608:1 › press backspace after bookmark block can select bookmark block

2) attachment.spec.ts:608:1 › press backspace after bookmark block can select bookmark block ───── TimeoutError: page.waitForEvent: Timeout 5000ms exceeded while waiting for event "filechooser" =========================== logs =========================== waiting for event "filechooser" ============================================================ 61 | await expect(slashMenu).toBeVisible(); 62 | > 63 | const fileChooser = page.waitForEvent('filechooser'); | ^ 64 | await pressEnter(page); 65 | await (await fileChooser).setFiles(FILE_PATH); 66 | at insertAttachment (/home/runner/work/blocksuite/blocksuite/tests/attachment.spec.ts:63:30) at /home/runner/work/blocksuite/blocksuite/tests/attachment.spec.ts:618:3
await pressEnter(page);
await page.setInputFiles("input[type='file']", FILE_PATH);
await (await fileChooser).setFiles(FILE_PATH);

// Try to break the undo redo test
await captureHistory(page);

Expand Down Expand Up @@ -624,3 +626,37 @@ test('press backspace after bookmark block can select bookmark block', async ({
await assertBlockSelections(page, ['4']);
await assertBlockCount(page, 'paragraph', 0);
});

test('cancel file picker with input element resolves', async ({ page }) => {
await enterPlaygroundRoom(page);
await initEmptyParagraphState(page);

const { attachment } = getAttachment(page);

await focusRichText(page);
await pressEnter(page);
await pressArrowUp(page);

await page.evaluate(() => {
// Force fallback to input[type=file]
window.showOpenFilePicker = undefined;
});

const slashMenu = page.locator(`.slash-menu`);
await waitNextFrame(page);
await type(page, '/file', 100);
await expect(slashMenu).toBeVisible();

const fileChooser = page.waitForEvent('filechooser');

Check failure on line 650 in tests/attachment.spec.ts

View workflow job for this annotation

GitHub Actions / Playground E2E test (1)

attachment.spec.ts:630:1 › cancel file picker with input element resolves

3) attachment.spec.ts:630:1 › cancel file picker with input element resolves ───────────────────── TimeoutError: page.waitForEvent: Timeout 5000ms exceeded while waiting for event "filechooser" =========================== logs =========================== waiting for event "filechooser" ============================================================ 648 | await expect(slashMenu).toBeVisible(); 649 | > 650 | const fileChooser = page.waitForEvent('filechooser'); | ^ 651 | await pressEnter(page); 652 | const inputFile = page.locator("input[type='file']"); 653 | await expect(inputFile).toHaveCount(1); at /home/runner/work/blocksuite/blocksuite/tests/attachment.spec.ts:650:28
await pressEnter(page);
const inputFile = page.locator("input[type='file']");
await expect(inputFile).toHaveCount(1);

// This does not trigger `cancel` event and,
// therefore, the test isn't representative.
// Waiting for https://github.com/microsoft/playwright/issues/27524
await (await fileChooser).setFiles([]);

await expect(attachment).toHaveCount(0);
await expect(inputFile).toHaveCount(0);
});

0 comments on commit 90647ed

Please sign in to comment.