Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions nx/utils/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -81,6 +85,7 @@ const DA_SDK = (() => new Promise((resolve) => {
getSelection,
setPrompt,
showPanel,
scrollTo,
};

resolve({ ...e.data, actions });
Expand Down
58 changes: 58 additions & 0 deletions test/nx/utils/sdk.test.js
Original file line number Diff line number Diff line change
@@ -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' } },
]);
});
});
Loading