Skip to content
Open
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
33 changes: 33 additions & 0 deletions src/components/super/i-block/event/test/unit/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import test from 'tests/config/unit/test';
import { renderDummy } from 'components/super/i-block/event/test/helpers';

import type bDummy from 'components/dummies/b-dummy/b-dummy';
import { BOM, Component } from 'tests/helpers';

test.describe('<i-block> event API', () => {
const componentName = 'b-dummy';
Expand All @@ -22,6 +23,38 @@ test.describe('<i-block> event API', () => {
}, componentName);
});

test('the event handler should be removed by using off and providing cb', async ({page}) => {
const dummy = await Component.createComponent<bDummy>(page, 'b-dummy');

await dummy.evaluate((ctx) => {
ctx.testComponent = 'b-button-functional';
});

const childComponent = await Component.waitForComponentByQuery(page, '.b-button');

await childComponent.evaluate((ctx) => {
const dummy = ctx.$normalParent;

const handler = () => {
globalThis.testResult = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше юзать JSHandle для этих целей:

const testHandle = page.evaluateHandle(() => ({value: false}));

await childComponent.evaluate((ctx, testHandle) => {
  const handler = () => {
    testHandle.value = true;
  };

  dummy?.once('hook:deactivated', handler);

	ctx.unsafe.async.worker(() => {
		dummy?.off('hook:deactivated', handler);
	});
}, testHandle);

await test.expect(testHandle.evaluate((ctx) => ctx.value)).resolves.toBeFalsy();

};

dummy?.once('hook:deactivated', handler);

ctx.unsafe.async.worker(() => {
dummy?.off('hook:deactivated', handler);
});
});

await BOM.waitForIdleCallback(page);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Имхо тест-кейс очень специфичный надо его в test-suite с активацией/деактивацией

Либо сделать простой тест с явным вызовом emit произвольного события

await childComponent.evaluate((ctx) => ctx.unsafe.$destroy());
await BOM.waitForIdleCallback(page);
await dummy.evaluate((ctx) => ctx.deactivate());
await BOM.waitForIdleCallback(page);

await test.expect(page.evaluate(() => globalThis.testResult)).resolves.toBeUndefined();
});

test('the event passed to `emit` should be normalized to camelCase', async ({page}) => {
const target = await renderDummy(page);

Expand Down