fix(core): svg blob syncing issue (#4886)

This commit is contained in:
Peng Xiao
2023-11-10 13:32:51 +08:00
committed by GitHub
parent 2117d6b232
commit 5c2d958e2b
6 changed files with 120 additions and 19 deletions
@@ -6,6 +6,7 @@ import {
enableCloudWorkspaceFromShareButton,
loginUser,
} from '@affine-test/kit/utils/cloud';
import { dropFile } from '@affine-test/kit/utils/drop-file';
import {
clickNewPageButton,
getBlockSuiteEditorTitle,
@@ -284,3 +285,55 @@ test.describe('sign out', () => {
expect(page.url()).toBe(currentUrl);
});
});
test('can sync svg between different browsers', async ({ page, browser }) => {
await page.reload();
await waitForEditorLoad(page);
await createLocalWorkspace(
{
name: 'test',
},
page
);
await enableCloudWorkspace(page);
await clickNewPageButton(page);
await waitForEditorLoad(page);
// drop an svg file
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">
<rect x="0" y="0" width="200" height="200" fill="red" />
</svg>`;
await dropFile(page, 'affine-paragraph', svg, 'test.svg', 'image/svg+xml');
{
const context = await browser.newContext();
const page2 = await context.newPage();
await loginUser(page2, user.email);
await page2.goto(page.url());
// the user should see the svg
// get the image src under "affine-image img"
const src = await page2.locator('affine-image img').getAttribute('src');
expect(src).not.toBeNull();
// fetch the src resource in the browser
const svg2 = await page2.evaluate(async src => {
async function blobToString(blob: Blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsText(blob);
});
}
const blob = fetch(src!).then(res => res.blob());
return blobToString(await blob);
}, src);
// turn the blob into string and check if it contains the svg
expect(svg2).toContain(svg);
}
});
+35
View File
@@ -0,0 +1,35 @@
import type { Page } from '@playwright/test';
export const dropFile = async (
page: Page,
selector: string,
fileContent: Buffer | string,
fileName: string,
fileType = ''
) => {
const buffer =
typeof fileContent === 'string'
? Buffer.from(fileContent, 'utf-8')
: fileContent;
const dataTransfer = await page.evaluateHandle(
async ({ bufferData, localFileName, localFileType }) => {
const dt = new DataTransfer();
const blobData = await fetch(bufferData).then(res => res.blob());
const file = new File([blobData], localFileName, { type: localFileType });
dt.items.add(file);
return dt;
},
{
bufferData: `data:application/octet-stream;base64,${buffer.toString(
'base64'
)}`,
localFileName: fileName,
localFileType: fileType,
}
);
await page.dispatchEvent(selector, 'drop', { dataTransfer });
};