-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(form): prevent drop event propagating outside of EditPortal compo…
…nent (#5813) * feat(core): allow `onDrop` prop in `Dialog` component * fix(form): prevent `drop` event propagating outside of `EditPortal` component * feat(e2e): add e2e helper for creating `DataTransfer` instances for buffers * test(core): ensure `drop` event does not propagate outside of `EditPortal` component * test(core): fix typo * test(core): reorder assertions for clarity
- Loading branch information
Showing
6 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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,33 @@ | ||
import {type JSHandle, type Page} from '@playwright/test' | ||
|
||
interface Context { | ||
page: Page | ||
} | ||
|
||
interface Options { | ||
buffer: Buffer | ||
fileName: string | ||
fileOptions: FilePropertyBag | ||
} | ||
|
||
/** | ||
* Create a `DataTransfer` handle containing the provided buffer. | ||
* | ||
* @internal | ||
**/ | ||
export function createFileDataTransferHandle( | ||
{page}: Context, | ||
options: Options, | ||
): Promise<JSHandle<DataTransfer>> { | ||
return page.evaluateHandle( | ||
({fileData, fileName, fileOptions}) => { | ||
const dataTransfer = new DataTransfer() | ||
dataTransfer.items.add(new File([new Uint8Array(fileData)], fileName, fileOptions)) | ||
return dataTransfer | ||
}, | ||
{ | ||
...options, | ||
fileData: options.buffer.toJSON().data, | ||
}, | ||
) | ||
} |
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './constants' | ||
export * from './createFileDataTransferHandle' | ||
export * from './createUniqueDocument' | ||
export * from './sanityClient' |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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,54 @@ | ||
import {readFileSync} from 'node:fs' | ||
import path from 'node:path' | ||
|
||
import {expect} from '@playwright/test' | ||
import {test} from '@sanity/test' | ||
|
||
import {createFileDataTransferHandle} from '../../helpers' | ||
|
||
const fileName = 'capybara.jpg' | ||
const image = readFileSync(path.join(__dirname, '..', '..', 'resources', fileName)) | ||
|
||
test(`file drop event should not propagate to dialog parent`, async ({ | ||
page, | ||
createDraftDocument, | ||
}) => { | ||
await createDraftDocument('/test/content/input-standard;arraysTest') | ||
|
||
const list = page.getByTestId('field-arrayOfMultipleTypes').locator('#arrayOfMultipleTypes') | ||
const item = list.locator('[data-ui="Grid"] > div') | ||
|
||
const dataTransfer = await createFileDataTransferHandle( | ||
{page}, | ||
{ | ||
buffer: image, | ||
fileName, | ||
fileOptions: { | ||
type: 'image/jpeg', | ||
}, | ||
}, | ||
) | ||
|
||
// Drop the file. | ||
await list.dispatchEvent('drop', {dataTransfer}) | ||
|
||
// Ensure the list contains one item. | ||
expect(item).toHaveCount(1) | ||
|
||
// Open the dialog. | ||
await page.getByRole('button', {name: fileName}).click() | ||
await expect(page.getByRole('dialog')).toBeVisible() | ||
|
||
// Drop the file again; this time, while the dialog is open. | ||
// | ||
// - The drop event should not propagate to the parent. | ||
// - Therefore, the drop event should not cause the image to be added to the list again. | ||
await page.getByRole('dialog').dispatchEvent('drop', {dataTransfer}) | ||
|
||
// Close the dialog. | ||
await page.keyboard.press('Escape') | ||
await expect(page.getByRole('dialog')).not.toBeVisible() | ||
|
||
// Ensure the list still contains one item. | ||
expect(item).toHaveCount(1) | ||
}) |