Files
monkey-ai-web/test/unit/effect-events.test.js
T
zhanjunhao cb9d836227 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

124 lines
4.1 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { setImmediate } from 'node:timers/promises';
import React from 'react';
import { createHookRuntime } from './helpers/hookRuntime.js';
import { loadFrontendModule } from './helpers/frontendModule.js';
async function harness() {
const runtime = createHookRuntime();
const { useEffectEvent } = await loadFrontendModule(
new URL('../../src/hooks/common/useEffectEvent.js', import.meta.url),
{ react: runtime.react },
);
return { runtime, useEffectEvent };
}
test('effect events keep identity and only expose the latest committed callback', async () => {
const { runtime, useEffectEvent } = await harness();
const Component = ({ value }) => useEffectEvent((extra) => value + extra);
const first = runtime.render(Component, { value: 1 });
runtime.commit();
assert.equal(first(2), 3);
const updated = runtime.render(Component, { value: 10 });
assert.equal(updated, first);
assert.equal(
first(2),
3,
'An uncommitted render must not leak to external handlers',
);
runtime.commit();
assert.equal(first(2), 12);
});
test('explicit scope changes run once and cleanup keeps its original resource', async () => {
const { runtime, useEffectEvent } = await harness();
const calls = [];
const cleanups = [];
const Component = ({ scope, draft }) => {
const onChange = useEffectEvent(() => {
calls.push([scope, draft]);
return () => cleanups.push(scope);
});
runtime.react.useEffect(() => onChange(), [onChange, scope]);
};
runtime.render(Component, { scope: 1, draft: 'a' });
runtime.flush();
runtime.render(Component, { scope: 1, draft: 'b' });
runtime.flush();
assert.deepEqual(
calls,
[[1, 'a']],
'Editing a draft must not reinitialize the form',
);
runtime.render(Component, { scope: 2, draft: 'b' });
runtime.flush();
assert.deepEqual(calls, [
[1, 'a'],
[2, 'b'],
]);
assert.deepEqual(cleanups, [1]);
runtime.unmount();
assert.deepEqual(cleanups, [1, 2]);
});
test('event callbacks preserve return values, async results, and errors', async () => {
const { runtime, useEffectEvent } = await harness();
const AsyncComponent = () => useEffectEvent(async (value) => value * 2);
const asyncEvent = runtime.render(AsyncComponent);
runtime.commit();
assert.equal(await asyncEvent(4), 8);
const ThrowingComponent = () =>
useEffectEvent(() => {
throw new Error('expected failure');
});
runtime.render(ThrowingComponent);
runtime.commit();
assert.throws(asyncEvent, /expected failure/);
});
test('settings mount and manual refresh settle without repeated option requests', async () => {
const { runtime, useEffectEvent } = await harness();
const requests = [];
const { default: Settings } = await loadFrontendModule(
new URL('../../src/components/settings/ChatsSetting.jsx', import.meta.url),
{
react: runtime.react,
'../../hooks/common/useEffectEvent': { useEffectEvent },
'@/components/ui': { Card: 'Card', Spin: 'Spin' },
'react-i18next': { useTranslation: () => ({ t: (key) => key }) },
'../../pages/Setting/Chat/SettingsChats': {
default: 'SettingsChats',
__esModule: true,
},
'../../helpers': {
API: {
get: async (url) => {
requests.push(url);
return {
data: { success: true, data: [{ key: 'Chats', value: '[]' }] },
};
},
},
showError: assert.fail,
toBoolean: (value) => value === 'true',
},
},
);
runtime.render(Settings);
runtime.flush();
await setImmediate();
let tree = runtime.flush();
assert.deepEqual(requests, ['/api/option/']);
const find = (node) =>
React.isValidElement(node)
? node.type === 'SettingsChats'
? node
: React.Children.toArray(node.props.children).map(find).find(Boolean)
: undefined;
await find(tree).props.refresh();
tree = runtime.flush();
assert.deepEqual(requests, ['/api/option/', '/api/option/']);
assert.equal(find(tree).props.options.Chats, '[]');
});