Skip to content

Commit

Permalink
fix: reference parameters and add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Nov 8, 2024
1 parent add8c56 commit 46f41f6
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 37 deletions.
25 changes: 8 additions & 17 deletions packages/frontend/core/src/modules/peek-view/entities/peek-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,34 +119,25 @@ function resolvePeekInfoFromPeekTarget(
if (element instanceof AffineReference) {
const referenceInfo = element.referenceInfo;
if (referenceInfo) {
const { pageId: docId } = referenceInfo;
const { pageId: docId, params } = referenceInfo;
const info: DocPeekViewInfo = {
type: 'doc',
docRef: {
docId,
},
docRef: { docId, ...params },
};
Object.assign(info, referenceInfo.params);
return info;
}
} else if ('model' in element) {
const blockModel = element.model;
if (isEmbedLinkedDocModel(blockModel)) {
if (
isEmbedLinkedDocModel(blockModel) ||
isEmbedSyncedDocModel(blockModel)
) {
const { pageId: docId, params } = blockModel;

Check failure on line 135 in packages/frontend/core/src/modules/peek-view/entities/peek-view.ts

View workflow job for this annotation

GitHub Actions / Lint

Property 'params' does not exist on type 'EmbedLinkedDocModel | EmbedSyncedDocModel'.
const info: DocPeekViewInfo = {
type: 'doc',
docRef: {
docId: blockModel.pageId,
},
docRef: { docId, ...params },
};
Object.assign(info, blockModel.params);
return info;
} else if (isEmbedSyncedDocModel(blockModel)) {
return {
type: 'doc',
docRef: {
docId: blockModel.pageId,
},
};
} else if (isSurfaceRefModel(blockModel)) {
const refModel = (element as SurfaceRefBlockComponent).referenceModel;
// refModel can be null if the reference is invalid
Expand Down
246 changes: 246 additions & 0 deletions tests/affine-local/e2e/links.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
import { test } from '@affine-test/kit/playwright';
import {
pasteByKeyboard,
writeTextToClipboard,
} from '@affine-test/kit/utils/keyboard';
import { openHomePage } from '@affine-test/kit/utils/load-page';
import {
clickNewPageButton,
createLinkedPage,
waitForEmptyEditor,
} from '@affine-test/kit/utils/page-logic';
import { expect, type Locator } from '@playwright/test';

test.beforeEach(async ({ page }) => {
await openHomePage(page);
await clickNewPageButton(page);
await waitForEmptyEditor(page);
});

async function notClickable(locator: Locator) {
await expect(locator).toHaveAttribute('disabled', '');
}

async function clickable(locator: Locator) {
await expect(locator).not.toHaveAttribute('disabled', '');
}

test('not allowed to switch to embed view when linking to the same document', async ({
page,
}) => {
await page.keyboard.press('Enter');

const url0 = new URL(page.url());

await writeTextToClipboard(page, url0.toString());
await pasteByKeyboard(page);

const switchViewBtn = page.locator('Switch view');
const linkToInlineBtn = page.getByTestId('link-to-inline');
const linkToCardBtn = page.getByTestId('link-to-card');
const linkToEmbedBtn = page.getByTestId('link-to-embed');

// Inline
await page.locator('affine-reference').hover();
await page.waitForTimeout(300);
await switchViewBtn.click();

Check failure on line 46 in tests/affine-local/e2e/links.spec.ts

View workflow job for this annotation

GitHub Actions / E2E Test (3)

links.spec.ts:28:5 › not allowed to switch to embed view when linking to the same document

1) links.spec.ts:28:5 › not allowed to switch to embed view when linking to the same document ──── TimeoutError: locator.click: Timeout 5000ms exceeded. Call log: - waiting for locator('Switch view') 44 | await page.locator('affine-reference').hover(); 45 | await page.waitForTimeout(300); > 46 | await switchViewBtn.click(); | ^ 47 | 48 | await notClickable(linkToInlineBtn); 49 | await clickable(linkToCardBtn); at /home/runner/work/AFFiNE/AFFiNE/tests/affine-local/e2e/links.spec.ts:46:23

Check failure on line 46 in tests/affine-local/e2e/links.spec.ts

View workflow job for this annotation

GitHub Actions / E2E Test (3)

links.spec.ts:28:5 › not allowed to switch to embed view when linking to the same document

1) links.spec.ts:28:5 › not allowed to switch to embed view when linking to the same document ──── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── TimeoutError: locator.click: Timeout 5000ms exceeded. Call log: - waiting for locator('Switch view') 44 | await page.locator('affine-reference').hover(); 45 | await page.waitForTimeout(300); > 46 | await switchViewBtn.click(); | ^ 47 | 48 | await notClickable(linkToInlineBtn); 49 | await clickable(linkToCardBtn); at /home/runner/work/AFFiNE/AFFiNE/tests/affine-local/e2e/links.spec.ts:46:23

Check failure on line 46 in tests/affine-local/e2e/links.spec.ts

View workflow job for this annotation

GitHub Actions / E2E Test (3)

links.spec.ts:28:5 › not allowed to switch to embed view when linking to the same document

1) links.spec.ts:28:5 › not allowed to switch to embed view when linking to the same document ──── Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── TimeoutError: locator.click: Timeout 5000ms exceeded. Call log: - waiting for locator('Switch view') 44 | await page.locator('affine-reference').hover(); 45 | await page.waitForTimeout(300); > 46 | await switchViewBtn.click(); | ^ 47 | 48 | await notClickable(linkToInlineBtn); 49 | await clickable(linkToCardBtn); at /home/runner/work/AFFiNE/AFFiNE/tests/affine-local/e2e/links.spec.ts:46:23

Check failure on line 46 in tests/affine-local/e2e/links.spec.ts

View workflow job for this annotation

GitHub Actions / E2E Test (3)

links.spec.ts:28:5 › not allowed to switch to embed view when linking to the same document

1) links.spec.ts:28:5 › not allowed to switch to embed view when linking to the same document ──── Retry #3 ─────────────────────────────────────────────────────────────────────────────────────── TimeoutError: locator.click: Timeout 5000ms exceeded. Call log: - waiting for locator('Switch view') 44 | await page.locator('affine-reference').hover(); 45 | await page.waitForTimeout(300); > 46 | await switchViewBtn.click(); | ^ 47 | 48 | await notClickable(linkToInlineBtn); 49 | await clickable(linkToCardBtn); at /home/runner/work/AFFiNE/AFFiNE/tests/affine-local/e2e/links.spec.ts:46:23

await notClickable(linkToInlineBtn);
await clickable(linkToCardBtn);
await notClickable(linkToEmbedBtn);

// Switches to card view
await linkToCardBtn.click();

// Card
await page.locator('affine-embed-linked-doc-block').click();
// await switchViewBtn.click();

await clickable(linkToInlineBtn);
await notClickable(linkToCardBtn);
await notClickable(linkToEmbedBtn);
});

test('not allowed to switch to embed view when linking to block', async ({
page,
}) => {
await page.keyboard.press('Enter');
await createLinkedPage(page, 'Test Page');

await page.locator('affine-reference').hover();
});

test('allow switching to embed view when linking to the other document without mode', async ({
page,
}) => {
await page.keyboard.press('Enter');
await createLinkedPage(page, 'Test Page');

const switchViewBtn = page.locator('Switch view');
const linkToInlineBtn = page.getByTestId('link-to-inline');
const linkToCardBtn = page.getByTestId('link-to-card');
const linkToEmbedBtn = page.getByTestId('link-to-embed');

// Inline
await page.locator('affine-reference').hover();
await switchViewBtn.click();

Check failure on line 86 in tests/affine-local/e2e/links.spec.ts

View workflow job for this annotation

GitHub Actions / E2E Test (3)

links.spec.ts:73:5 › allow switching to embed view when linking to the other document without mode

2) links.spec.ts:73:5 › allow switching to embed view when linking to the other document without mode TimeoutError: locator.click: Timeout 5000ms exceeded. Call log: - waiting for locator('Switch view') 84 | // Inline 85 | await page.locator('affine-reference').hover(); > 86 | await switchViewBtn.click(); | ^ 87 | 88 | await notClickable(linkToInlineBtn); 89 | await clickable(linkToCardBtn); at /home/runner/work/AFFiNE/AFFiNE/tests/affine-local/e2e/links.spec.ts:86:23

Check failure on line 86 in tests/affine-local/e2e/links.spec.ts

View workflow job for this annotation

GitHub Actions / E2E Test (3)

links.spec.ts:73:5 › allow switching to embed view when linking to the other document without mode

2) links.spec.ts:73:5 › allow switching to embed view when linking to the other document without mode Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── TimeoutError: locator.click: Timeout 5000ms exceeded. Call log: - waiting for locator('Switch view') 84 | // Inline 85 | await page.locator('affine-reference').hover(); > 86 | await switchViewBtn.click(); | ^ 87 | 88 | await notClickable(linkToInlineBtn); 89 | await clickable(linkToCardBtn); at /home/runner/work/AFFiNE/AFFiNE/tests/affine-local/e2e/links.spec.ts:86:23

Check failure on line 86 in tests/affine-local/e2e/links.spec.ts

View workflow job for this annotation

GitHub Actions / E2E Test (3)

links.spec.ts:73:5 › allow switching to embed view when linking to the other document without mode

2) links.spec.ts:73:5 › allow switching to embed view when linking to the other document without mode Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── TimeoutError: locator.click: Timeout 5000ms exceeded. Call log: - waiting for locator('Switch view') 84 | // Inline 85 | await page.locator('affine-reference').hover(); > 86 | await switchViewBtn.click(); | ^ 87 | 88 | await notClickable(linkToInlineBtn); 89 | await clickable(linkToCardBtn); at /home/runner/work/AFFiNE/AFFiNE/tests/affine-local/e2e/links.spec.ts:86:23

Check failure on line 86 in tests/affine-local/e2e/links.spec.ts

View workflow job for this annotation

GitHub Actions / E2E Test (3)

links.spec.ts:73:5 › allow switching to embed view when linking to the other document without mode

2) links.spec.ts:73:5 › allow switching to embed view when linking to the other document without mode Retry #3 ─────────────────────────────────────────────────────────────────────────────────────── TimeoutError: locator.click: Timeout 5000ms exceeded. Call log: - waiting for locator('Switch view') 84 | // Inline 85 | await page.locator('affine-reference').hover(); > 86 | await switchViewBtn.click(); | ^ 87 | 88 | await notClickable(linkToInlineBtn); 89 | await clickable(linkToCardBtn); at /home/runner/work/AFFiNE/AFFiNE/tests/affine-local/e2e/links.spec.ts:86:23

await notClickable(linkToInlineBtn);
await clickable(linkToCardBtn);
await clickable(linkToEmbedBtn);

// Switches to card view
await linkToCardBtn.click();

// Card
await page.locator('affine-embed-linked-doc-block').click();
await switchViewBtn.click();

await clickable(linkToInlineBtn);
await notClickable(linkToCardBtn);
await clickable(linkToEmbedBtn);

// Switches to embed view
await linkToEmbedBtn.click();

// Embed
await page.locator('affine-embed-synced-doc-block').click();
await switchViewBtn.click();

await clickable(linkToInlineBtn);
await clickable(linkToCardBtn);
await notClickable(linkToEmbedBtn);

// Closes
await switchViewBtn.click();
await expect(
page.locator('.affine-embed-synced-doc-container.page')
).toBeVisible();

// Opens in peek view
await page.locator('affine-embed-synced-doc-block').dblclick();

const peekViewModel = page.getByTestId('peek-view-modal');
await expect(peekViewModel).toBeVisible();
await expect(peekViewModel.locator('page-editor')).toBeVisible();
await page.keyboard.press('Escape');
await expect(peekViewModel).not.toBeVisible();
await page.waitForTimeout(300);

await page.locator('affine-embed-synced-doc-block').click();
await switchViewBtn.click();

await clickable(linkToInlineBtn);
await clickable(linkToCardBtn);
await notClickable(linkToEmbedBtn);

// Switches to card view
await linkToCardBtn.click();

await page.locator('affine-embed-linked-doc-block').click();
await switchViewBtn.click();

await clickable(linkToInlineBtn);
await notClickable(linkToCardBtn);
await clickable(linkToEmbedBtn);

// Switches to inline view
await linkToInlineBtn.click();

await expect(page.locator('affine-reference')).toBeVisible();
});

test('allow switching to embed view when linking to the other document with mode', async ({
page,
}) => {
await page.keyboard.press('Enter');
await createLinkedPage(page, 'Test Page');

const switchViewBtn = page.locator('Switch view');
const linkToInlineBtn = page.getByTestId('link-to-inline');
const linkToCardBtn = page.getByTestId('link-to-card');
const linkToEmbedBtn = page.getByTestId('link-to-embed');

const url = new URL(page.url());
url.searchParams.append('mode', 'edgeless');

await page.locator('affine-reference').click();
await page.waitForTimeout(300);
await page.keyboard.press('Enter');

await writeTextToClipboard(page, url.toString());
await pasteByKeyboard(page);

// Inline
await page.locator('affine-reference').hover();
await switchViewBtn.click();

Check failure on line 176 in tests/affine-local/e2e/links.spec.ts

View workflow job for this annotation

GitHub Actions / E2E Test (3)

links.spec.ts:153:5 › allow switching to embed view when linking to the other document with mode

3) links.spec.ts:153:5 › allow switching to embed view when linking to the other document with mode TimeoutError: locator.click: Timeout 5000ms exceeded. Call log: - waiting for locator('Switch view') 174 | // Inline 175 | await page.locator('affine-reference').hover(); > 176 | await switchViewBtn.click(); | ^ 177 | 178 | await notClickable(linkToInlineBtn); 179 | await clickable(linkToCardBtn); at /home/runner/work/AFFiNE/AFFiNE/tests/affine-local/e2e/links.spec.ts:176:23

Check failure on line 176 in tests/affine-local/e2e/links.spec.ts

View workflow job for this annotation

GitHub Actions / E2E Test (3)

links.spec.ts:153:5 › allow switching to embed view when linking to the other document with mode

3) links.spec.ts:153:5 › allow switching to embed view when linking to the other document with mode Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── TimeoutError: locator.click: Timeout 5000ms exceeded. Call log: - waiting for locator('Switch view') 174 | // Inline 175 | await page.locator('affine-reference').hover(); > 176 | await switchViewBtn.click(); | ^ 177 | 178 | await notClickable(linkToInlineBtn); 179 | await clickable(linkToCardBtn); at /home/runner/work/AFFiNE/AFFiNE/tests/affine-local/e2e/links.spec.ts:176:23

await notClickable(linkToInlineBtn);
await clickable(linkToCardBtn);
await clickable(linkToEmbedBtn);

// Switches to card view
await linkToCardBtn.click();

// Card
await page.locator('affine-embed-linked-doc-block').click();
await switchViewBtn.click();

await clickable(linkToInlineBtn);
await notClickable(linkToCardBtn);
await clickable(linkToEmbedBtn);

// Switches to embed view
await linkToEmbedBtn.click();

// Embed
await page.locator('affine-embed-synced-doc-block').click();
await switchViewBtn.click();

await clickable(linkToInlineBtn);
await clickable(linkToCardBtn);
await notClickable(linkToEmbedBtn);

// Closes
await switchViewBtn.click();
await expect(
page.locator('.affine-embed-synced-doc-container.edgeless')
).toBeVisible();

// Opens in peek view
await page.locator('affine-embed-synced-doc-block').dblclick();

const peekViewModel = page.getByTestId('peek-view-modal');
await expect(peekViewModel).toBeVisible();
await expect(peekViewModel.locator('edgeless-editor')).toBeVisible();
await page.keyboard.press('Escape');
await expect(peekViewModel).not.toBeVisible();
await page.waitForTimeout(300);

await page.locator('affine-embed-synced-doc-block').click();
await switchViewBtn.click();

await clickable(linkToInlineBtn);
await clickable(linkToCardBtn);
await notClickable(linkToEmbedBtn);

// Switches to card view
await linkToCardBtn.click();

await page.locator('affine-embed-linked-doc-block').click();
await switchViewBtn.click();

await clickable(linkToInlineBtn);
await notClickable(linkToCardBtn);
await clickable(linkToEmbedBtn);

// Switches to inline view
await linkToInlineBtn.click();

await page.locator('affine-reference').click();

// Checks the url
const url2 = new URL(page.url());
url2.searchParams.delete('refreshKey');
expect(url.toJSON()).toStrictEqual(url2.toJSON());
});
25 changes: 5 additions & 20 deletions tests/affine-local/e2e/quick-search.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { test } from '@affine-test/kit/playwright';
import { clickEdgelessModeButton } from '@affine-test/kit/utils/editor';
import { withCtrlOrMeta } from '@affine-test/kit/utils/keyboard';
import {
withCtrlOrMeta,
writeTextToClipboard,
} from '@affine-test/kit/utils/keyboard';
import { openHomePage } from '@affine-test/kit/utils/load-page';
import {
clickNewPageButton,
Expand Down Expand Up @@ -510,25 +513,7 @@ test('can paste a doc link to create link reference', async ({ page }) => {
await page.keyboard.press('Enter');

// paste the url
await page.evaluate(
async ([url]) => {
const clipData = {
'text/plain': url,
};
const e = new ClipboardEvent('paste', {
clipboardData: new DataTransfer(),
});
Object.defineProperty(e, 'target', {
writable: false,
value: document,
});
Object.entries(clipData).forEach(([key, value]) => {
e.clipboardData?.setData(key, value);
});
document.dispatchEvent(e);
},
[url]
);
await writeTextToClipboard(page, url);

// check the link reference
await page.waitForTimeout(500);
Expand Down
23 changes: 23 additions & 0 deletions tests/kit/utils/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,26 @@ export async function pasteByKeyboard(page: Page) {
await page.keyboard.press('v', { delay: 50 });
await keyUpCtrlOrMeta(page);
}

export async function writeTextToClipboard(page: Page, text: string) {
// paste the url
await page.evaluate(
async ([text]) => {
const clipData = {
'text/plain': text,
};
const e = new ClipboardEvent('paste', {
clipboardData: new DataTransfer(),
});
Object.defineProperty(e, 'target', {
writable: false,
value: document,
});
Object.entries(clipData).forEach(([key, value]) => {
e.clipboardData?.setData(key, value);
});
document.dispatchEvent(e);
},
[text]
);
}

0 comments on commit 46f41f6

Please sign in to comment.