2023-11-22 09:06:21 +00:00
|
|
|
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
|
🔧 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 12:23:04 +00:00
|
|
|
// eslint-disable-next-line unicorn/no-invalid-remove-event-listener
|
2025-06-02 16:22:14 +00:00
|
|
|
window.removeEventListener('message', () => {});
|
2023-11-22 09:06:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
});
|
|
|
|
|
});
|