lobehub/src/features/PluginsUI/Render/utils/pluginState.test.ts
Innei 3112036b38
🔧 chore: resolve all ESLint suppressions and remove suppression file (#12518)
* 🔧 chore: upgrade ESLint deps and resolve all suppressions

- Upgrade eslint 10.0.0→10.0.2, @lobehub/lint 2.1.3→2.1.5, eslint-plugin-mdx ^3.6.2→^3.7.0
- Remove eslint-suppressions.json and all suppression-related scripts/configs
- Fix 197 ESLint errors: no-console, no-unused-private-class-members, no-useless-assignment, preserve-caught-error, prefer-const, regex issues, etc.
- Remove dead rule references (sort-keys-fix, typescript-sort-keys, ban-types)
- Disable project-convention-conflicting rules globally in eslint.config.mjs
- Update test spies from console.log to console.info

* 🔧 fix: update regex for unresolved model error handling

- Modified the UNRESOLVED_MODEL_REGEXP to allow for additional valid characters in model names, enhancing error detection for missing models.

Signed-off-by: Innei <tukon479@gmail.com>

---------

Signed-off-by: Innei <tukon479@gmail.com>
2026-02-28 20:23:04 +08:00

44 lines
1.4 KiB
TypeScript

import { PluginChannel } from '@lobehub/chat-plugin-sdk/client';
import { renderHook } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { useOnPluginStateUpdate } from './pluginState';
describe('useOnPluginStateUpdate', () => {
// Mock for the callback function to be used in tests
const mockCallback = vi.fn();
afterEach(() => {
// Reset the mock callback after each test
mockCallback.mockReset();
// Ensure no event listeners are left hanging after each test
// eslint-disable-next-line unicorn/no-invalid-remove-event-listener
window.removeEventListener('message', () => {});
});
it('calls the callback when a PluginChannel update message is received', () => {
renderHook(() => useOnPluginStateUpdate(mockCallback));
const testKey = 'testKey';
const testValue = 'testValue';
const event = new MessageEvent('message', {
data: { type: PluginChannel.updatePluginState, key: testKey, value: testValue },
});
window.dispatchEvent(event);
expect(mockCallback).toHaveBeenCalledWith(testKey, testValue);
});
it('does not call the callback for non-PluginChannel messages', () => {
renderHook(() => useOnPluginStateUpdate(mockCallback));
const event = new MessageEvent('message', {
data: { type: 'nonPluginMessage', key: 'key', value: 'value' },
});
window.dispatchEvent(event);
expect(mockCallback).not.toHaveBeenCalled();
});
});