Alonso Torres 7655f854a4
Improved api test suite (#10533)
* 🐛 Fix plugins api issues

* 🐛 Fix problem with text weight

* 🐛 Fix api problems

*  Updated plugins test suite

* 🐛 Referential integrity tests

*  Add resetOverrides method

* 🐛 Fix plugins components structure poluting
2026-07-07 16:12:12 +02:00

71 lines
1.9 KiB
TypeScript

import { expect } from '../framework/expect';
import { describe, test } from '../framework/registry';
import { waitFor } from './wait';
// Events.
// Listeners are registered with `on`, triggered by mutating state, and removed
// with `off`. Callbacks are debounced (~10ms), so the tests wait before asserting.
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
describe('Events', () => {
test('selectionchange fires with the selected ids', async (ctx) => {
const rect = ctx.penpot.createRectangle();
ctx.board.appendChild(rect);
let received: string[] | null = null;
const listenerId = ctx.penpot.on('selectionchange', (ids) => {
received = ids;
});
ctx.penpot.selection = [rect];
await waitFor(() => received !== null);
ctx.penpot.off(listenerId);
expect(received).not.toBeNull();
if (received) {
expect((received as string[]).includes(rect.id)).toBe(true);
}
});
test('shapechange fires when the observed shape changes', async (ctx) => {
const rect = ctx.penpot.createRectangle();
ctx.board.appendChild(rect);
let fired = false;
const listenerId = ctx.penpot.on(
'shapechange',
() => {
fired = true;
},
{ shapeId: rect.id },
);
rect.name = 'changed-name';
await waitFor(() => fired);
ctx.penpot.off(listenerId);
expect(fired).toBe(true);
});
test('off stops further notifications', async (ctx) => {
const rect = ctx.penpot.createRectangle();
ctx.board.appendChild(rect);
let count = 0;
const listenerId = ctx.penpot.on('selectionchange', () => {
count += 1;
});
ctx.penpot.off(listenerId);
ctx.penpot.selection = [rect];
// Asserting a non-event: there is no positive condition to poll for, so
// give the debounce a fixed window to (not) fire.
await sleep(150);
expect(count).toBe(0);
});
});