Skip to content

Commit 2013876

Browse files
committed
test: more extractions for helpers in tests
1 parent ade952e commit 2013876

5 files changed

Lines changed: 79 additions & 145 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { Page } from '@playwright/test';
2+
3+
/**
4+
* Right-clicks at the screen location corresponding to a document position in the SuperDoc editor.
5+
*
6+
* This helper queries the editor's coordinates for the given logical document position, calculates a suitable
7+
* (x, y) point within the bounding rectangle, and dispatches a mouse right-click at that spot.
8+
*
9+
* Throws if coordinates cannot be resolved for the given position.
10+
*
11+
* @param {Page} page - The Playwright test page instance.
12+
* @param {number} pos - The logical document position (character offset) at which to right-click.
13+
* @returns {Promise<void>} Resolves when the click has been dispatched.
14+
*/
15+
export async function rightClickAtDocPos(page: Page, pos: number): Promise<void> {
16+
const coords = await page.evaluate((targetPos) => {
17+
const editor = (window as any).editor;
18+
const rect = editor?.coordsAtPos?.(targetPos);
19+
if (!rect) return null;
20+
return {
21+
left: Number(rect.left),
22+
right: Number(rect.right),
23+
top: Number(rect.top),
24+
bottom: Number(rect.bottom),
25+
};
26+
}, pos);
27+
28+
if (!coords) {
29+
throw new Error(`Could not resolve coordinates for document position ${pos}`);
30+
}
31+
32+
const x = Math.min(Math.max(coords.left + 1, coords.left), Math.max(coords.right - 1, coords.left + 1));
33+
const y = (coords.top + coords.bottom) / 2;
34+
await page.mouse.click(x, y, { button: 'right' });
35+
}

tests/behavior/helpers/tracked-changes.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import type { Page } from '@playwright/test';
22

3+
type InsertTrackedChangeOptions = {
4+
from: number;
5+
to: number;
6+
text: string;
7+
};
8+
39
/**
410
* Reject all tracked changes in the document via document-api.
511
*/
@@ -12,3 +18,36 @@ export async function rejectAllTrackedChanges(page: Page): Promise<void> {
1218
decide({ decision: 'reject', target: { scope: 'all' } });
1319
});
1420
}
21+
22+
export async function insertTrackedChange(page: Page, options: InsertTrackedChangeOptions): Promise<void> {
23+
await page.evaluate((payload) => {
24+
(window as any).editor.commands.insertTrackedChange({
25+
...payload,
26+
user: { name: 'Track Tester', email: 'track@example.com' },
27+
});
28+
}, options);
29+
}
30+
31+
export async function getMarkedText(page: Page, markName: string): Promise<string> {
32+
return page.evaluate((name) => {
33+
let text = '';
34+
const doc = (window as any).editor.state.doc;
35+
36+
doc.descendants((node: any) => {
37+
if (!node.isText) return;
38+
if (node.marks.some((mark: any) => mark.type.name === name)) {
39+
text += node.text ?? '';
40+
}
41+
});
42+
43+
return text;
44+
}, markName);
45+
}
46+
47+
export async function getSelectedText(page: Page): Promise<string> {
48+
return page.evaluate(() => {
49+
const { from, to, empty } = (window as any).editor.state.selection;
50+
if (empty) return '';
51+
return (window as any).editor.state.doc.textBetween(from, to);
52+
});
53+
}

tests/behavior/tests/comments/tracked-change-partial-resolution.spec.ts

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,14 @@
11
import { test, expect } from '../../fixtures/superdoc.js';
22
import { getDocumentText } from '../../helpers/document-api.js';
3+
import { rightClickAtDocPos } from '../../helpers/editor-interactions.js';
4+
import { getMarkedText, getSelectedText, insertTrackedChange } from '../../helpers/tracked-changes.js';
35

46
test.use({ config: { toolbar: 'full', comments: 'panel', trackChanges: true, showSelection: true } });
57

68
const TRACK_TEXT = 'ABCDE';
79
const PARTIAL_TEXT = 'BC';
810
const ACCEPT_TRACKED_CHANGES_BUTTON = 'Accept tracked changes';
911

10-
async function insertTrackedChange(
11-
page: import('@playwright/test').Page,
12-
options: {
13-
from: number;
14-
to: number;
15-
text: string;
16-
},
17-
) {
18-
await page.evaluate((payload) => {
19-
(window as any).editor.commands.insertTrackedChange({
20-
...payload,
21-
user: { name: 'Track Tester', email: 'track@example.com' },
22-
});
23-
}, options);
24-
}
25-
26-
async function getMarkedText(page: import('@playwright/test').Page, markName: string): Promise<string> {
27-
return page.evaluate((name) => {
28-
let text = '';
29-
const doc = (window as any).editor.state.doc;
30-
31-
doc.descendants((node: any) => {
32-
if (!node.isText) return;
33-
if (node.marks.some((mark: any) => mark.type.name === name)) {
34-
text += node.text ?? '';
35-
}
36-
});
37-
38-
return text;
39-
}, markName);
40-
}
41-
42-
async function getSelectedText(page: import('@playwright/test').Page): Promise<string> {
43-
return page.evaluate(() => {
44-
const { from, to, empty } = (window as any).editor.state.selection;
45-
if (empty) return '';
46-
return (window as any).editor.state.doc.textBetween(from, to);
47-
});
48-
}
49-
50-
async function rightClickAtDocPos(page: import('@playwright/test').Page, pos: number) {
51-
const coords = await page.evaluate((p) => {
52-
const editor = (window as any).editor;
53-
const rect = editor?.coordsAtPos?.(p);
54-
if (!rect) return null;
55-
return {
56-
left: Number(rect.left),
57-
right: Number(rect.right),
58-
top: Number(rect.top),
59-
bottom: Number(rect.bottom),
60-
};
61-
}, pos);
62-
63-
if (!coords) {
64-
throw new Error(`Could not resolve coordinates for document position ${pos}`);
65-
}
66-
67-
const x = Math.min(Math.max(coords.left + 1, coords.left), Math.max(coords.right - 1, coords.left + 1));
68-
const y = (coords.top + coords.bottom) / 2;
69-
await page.mouse.click(x, y, { button: 'right' });
70-
}
71-
7212
test('toolbar accept partially resolves a tracked insertion and updates the bubble text', async ({ superdoc }) => {
7313
await insertTrackedChange(superdoc.page, { from: 1, to: 1, text: TRACK_TEXT });
7414
await superdoc.waitForStable();

tests/behavior/tests/slash-menu/paste.spec.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { test, expect } from '../../fixtures/superdoc.js';
2+
import { rightClickAtDocPos } from '../../helpers/editor-interactions.js';
23

34
test.use({ config: { toolbar: 'full' } });
45

@@ -16,28 +17,6 @@ async function writeToClipboard(page: import('@playwright/test').Page, text: str
1617
await page.evaluate((t) => navigator.clipboard.writeText(t), text);
1718
}
1819

19-
async function rightClickAtDocPos(page: import('@playwright/test').Page, pos: number) {
20-
const coords = await page.evaluate((p) => {
21-
const editor = (window as any).editor;
22-
const rect = editor?.coordsAtPos?.(p);
23-
if (!rect) return null;
24-
return {
25-
left: Number(rect.left),
26-
right: Number(rect.right),
27-
top: Number(rect.top),
28-
bottom: Number(rect.bottom),
29-
};
30-
}, pos);
31-
32-
if (!coords) {
33-
throw new Error(`Could not resolve coordinates for document position ${pos}`);
34-
}
35-
36-
const x = Math.min(Math.max(coords.left + 1, coords.left), Math.max(coords.right - 1, coords.left + 1));
37-
const y = (coords.top + coords.bottom) / 2;
38-
await page.mouse.click(x, y, { button: 'right' });
39-
}
40-
4120
test('right-click opens context menu and paste inserts clipboard text', async ({ superdoc }) => {
4221
await superdoc.type('Hello world');
4322
await superdoc.newLine();

tests/visual/tests/behavior/tracked-change-partial-resolution.spec.ts

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { test, expect } from '../fixtures/superdoc.ts';
2+
import { rightClickAtDocPos } from '../../../behavior/helpers/editor-interactions.js';
3+
import { getMarkedText, getSelectedText, insertTrackedChange } from '../../../behavior/helpers/tracked-changes.js';
24

35
test.use({
46
config: {
@@ -11,72 +13,11 @@ test.use({
1113

1214
const TRACK_TEXT = 'ABCDE';
1315
const PARTIAL_TEXT = 'BC';
14-
async function insertTrackedChange(
15-
page: import('@playwright/test').Page,
16-
options: {
17-
from: number;
18-
to: number;
19-
text: string;
20-
},
21-
) {
22-
await page.evaluate((payload) => {
23-
(window as any).editor.commands.insertTrackedChange({
24-
...payload,
25-
user: { name: 'Track Tester', email: 'track@example.com' },
26-
});
27-
}, options);
28-
}
29-
30-
async function getMarkedText(page: import('@playwright/test').Page, markName: string): Promise<string> {
31-
return page.evaluate((name) => {
32-
let text = '';
33-
const doc = (window as any).editor.state.doc;
34-
35-
doc.descendants((node: any) => {
36-
if (!node.isText) return;
37-
if (node.marks.some((mark: any) => mark.type.name === name)) {
38-
text += node.text ?? '';
39-
}
40-
});
41-
42-
return text;
43-
}, markName);
44-
}
4516

4617
async function getDocumentText(page: import('@playwright/test').Page): Promise<string> {
4718
return page.evaluate(() => (window as any).editor.doc.getText({}));
4819
}
4920

50-
async function getSelectedText(page: import('@playwright/test').Page): Promise<string> {
51-
return page.evaluate(() => {
52-
const { from, to, empty } = (window as any).editor.state.selection;
53-
if (empty) return '';
54-
return (window as any).editor.state.doc.textBetween(from, to);
55-
});
56-
}
57-
58-
async function rightClickAtDocPos(page: import('@playwright/test').Page, pos: number) {
59-
const coords = await page.evaluate((p) => {
60-
const editor = (window as any).editor;
61-
const rect = editor?.coordsAtPos?.(p);
62-
if (!rect) return null;
63-
return {
64-
left: Number(rect.left),
65-
right: Number(rect.right),
66-
top: Number(rect.top),
67-
bottom: Number(rect.bottom),
68-
};
69-
}, pos);
70-
71-
if (!coords) {
72-
throw new Error(`Could not resolve coordinates for document position ${pos}`);
73-
}
74-
75-
const x = Math.min(Math.max(coords.left + 1, coords.left), Math.max(coords.right - 1, coords.left + 1));
76-
const y = (coords.top + coords.bottom) / 2;
77-
await page.mouse.click(x, y, { button: 'right' });
78-
}
79-
8021
test('@behavior toolbar accept partially resolves a tracked insertion', async ({ superdoc }) => {
8122
await insertTrackedChange(superdoc.page, { from: 1, to: 1, text: TRACK_TEXT });
8223
await superdoc.waitForStable();

0 commit comments

Comments
 (0)