diff --git a/nx/utils/sdk.js b/nx/utils/sdk.js index 489a43518..2eb75b2e2 100644 --- a/nx/utils/sdk.js +++ b/nx/utils/sdk.js @@ -34,6 +34,10 @@ function showPanel(name) { port2.postMessage({ action: 'showPanel', details: name }); } +function scrollTo(target) { + port2.postMessage({ action: 'scrollTo', details: target }); +} + function getSelection() { return new Promise((resolve, reject) => { const listener = (e) => { @@ -81,6 +85,7 @@ const DA_SDK = (() => new Promise((resolve) => { getSelection, setPrompt, showPanel, + scrollTo, }; resolve({ ...e.data, actions }); diff --git a/test/nx/utils/sdk.test.js b/test/nx/utils/sdk.test.js new file mode 100644 index 000000000..2d6a0def4 --- /dev/null +++ b/test/nx/utils/sdk.test.js @@ -0,0 +1,58 @@ +import { expect } from '@esm-bundle/chai'; + +describe('DA_SDK actions.scrollTo', () => { + let actions; + let received; + + before(async () => { + const { default: DA_SDK } = await import('../../../nx/utils/sdk.js'); + const channel = new MessageChannel(); + received = []; + channel.port1.onmessage = (e) => { + received.push(e.data); + }; + channel.port1.start(); + + window.postMessage( + { ready: true, project: { org: 'myorg', repo: 'mysite' } }, + window.location.origin, + [channel.port2], + ); + + ({ actions } = await DA_SDK); + }); + + beforeEach(() => { + received.length = 0; + }); + + function waitForMessage() { + return new Promise((resolve) => { + setTimeout(resolve, 0); + }); + } + + it('posts a block target', async () => { + actions.scrollTo({ type: 'block', blockIndex: 3 }); + await waitForMessage(); + expect(received).to.deep.equal([ + { action: 'scrollTo', details: { type: 'block', blockIndex: 3 } }, + ]); + }); + + it('posts a section target', async () => { + actions.scrollTo({ type: 'section', sectionIndex: 1 }); + await waitForMessage(); + expect(received).to.deep.equal([ + { action: 'scrollTo', details: { type: 'section', sectionIndex: 1 } }, + ]); + }); + + it('posts a content target', async () => { + actions.scrollTo({ type: 'content', proseIndex: 5, kind: 'heading' }); + await waitForMessage(); + expect(received).to.deep.equal([ + { action: 'scrollTo', details: { type: 'content', proseIndex: 5, kind: 'heading' } }, + ]); + }); +});