|
| 1 | +import { test, expect } from '../../fixtures/superdoc.js'; |
| 2 | +import { addCommentByText, assertDocumentApiReady } from '../../helpers/document-api.js'; |
| 3 | + |
| 4 | +test.use({ config: { toolbar: 'full', comments: 'on' } }); |
| 5 | + |
| 6 | +test('scrollToComment scrolls to the comment and activates it', async ({ superdoc }) => { |
| 7 | + await assertDocumentApiReady(superdoc.page); |
| 8 | + |
| 9 | + // Create enough content so the comment is off-screen |
| 10 | + for (let i = 0; i < 30; i++) { |
| 11 | + await superdoc.type(`Line ${i}`); |
| 12 | + await superdoc.newLine(); |
| 13 | + } |
| 14 | + await superdoc.type('target text'); |
| 15 | + await superdoc.waitForStable(); |
| 16 | + |
| 17 | + const commentId = await addCommentByText(superdoc.page, { |
| 18 | + pattern: 'target text', |
| 19 | + text: 'scroll test comment', |
| 20 | + }); |
| 21 | + await superdoc.waitForStable(); |
| 22 | + await superdoc.assertCommentHighlightExists({ text: 'target text', timeoutMs: 20_000 }); |
| 23 | + |
| 24 | + // Scroll to the top so the comment is out of view |
| 25 | + await superdoc.page.evaluate(() => { |
| 26 | + document.querySelector('.superdoc')?.scrollTo({ top: 0 }); |
| 27 | + }); |
| 28 | + await superdoc.waitForStable(); |
| 29 | + |
| 30 | + // Call scrollToComment via the public API. |
| 31 | + // WebKit can lag on DOM attribute propagation, so poll until it succeeds. |
| 32 | + await expect |
| 33 | + .poll(async () => superdoc.page.evaluate((id) => (window as any).superdoc.scrollToComment(id), commentId), { |
| 34 | + timeout: 10_000, |
| 35 | + }) |
| 36 | + .toBe(true); |
| 37 | + |
| 38 | + // Verify the comment highlight is now visible in the viewport |
| 39 | + const highlight = superdoc.page.locator('.superdoc-comment-highlight').filter({ hasText: 'target text' }); |
| 40 | + await expect(highlight.first()).toBeVisible({ timeout: 5_000 }); |
| 41 | +}); |
| 42 | + |
| 43 | +test('scrollToComment returns false for a nonexistent comment', async ({ superdoc }) => { |
| 44 | + await assertDocumentApiReady(superdoc.page); |
| 45 | + |
| 46 | + const result = await superdoc.page.evaluate(() => { |
| 47 | + return (window as any).superdoc.scrollToComment('nonexistent-id'); |
| 48 | + }); |
| 49 | + |
| 50 | + expect(result).toBe(false); |
| 51 | +}); |
0 commit comments