23824dad32
- 冲突以 weiyuan 功能(小程序登录、i18n 文案)为准,并保留远端 lint 重构与依赖收敛 - 删除 weiyuan 已移除、远端仅做 lint 修正的 3 个图片相关文件 - 适配远端零警告 ESLint:Monitoring/Subscription 改用 useEffectEvent,EditChannelModal 注明 t 依赖 - effect-events 单测补充 react-i18next mock - 对合并后仍不符合 Prettier 的文件做纯格式化
143 lines
4.8 KiB
JavaScript
143 lines
4.8 KiB
JavaScript
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
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, '[]');
|
|
});
|