test(shortcuts-popup-plugin): add JSDOM mocks for Range methods to improve test reliability

This commit is contained in:
twwu 2026-01-28 13:24:55 +08:00
parent b3be035c64
commit fe5a65b21c
5 changed files with 36 additions and 0 deletions

View File

@ -8,6 +8,32 @@ import { useState } from 'react'
import ShortcutsPopupPlugin, { SHORTCUTS_EMPTY_CONTENT } from './index'
import '@testing-library/jest-dom'
// Mock Range.getClientRects and getBoundingClientRect for JSDOM
const mockDOMRect = {
x: 100,
y: 100,
width: 100,
height: 20,
top: 100,
right: 200,
bottom: 120,
left: 100,
toJSON: () => ({}),
}
beforeAll(() => {
// Mock getClientRects on Range prototype
Range.prototype.getClientRects = vi.fn(() => {
const rectList = [mockDOMRect] as unknown as DOMRectList
Object.defineProperty(rectList, 'length', { value: 1 })
Object.defineProperty(rectList, 'item', { value: (index: number) => index === 0 ? mockDOMRect : null })
return rectList
})
// Mock getBoundingClientRect on Range prototype
Range.prototype.getBoundingClientRect = vi.fn(() => mockDOMRect as DOMRect)
})
const CONTAINER_ID = 'host'
const CONTENT_EDITABLE_ID = 'ce'

View File

@ -1247,6 +1247,10 @@ describe('CommonCreateModal', () => {
const input = screen.getByTestId('form-field-webhook_url')
fireEvent.change(input, { target: { value: 'https://example.com/webhook' } })
await waitFor(() => {
expect(mockUpdateBuilder).toHaveBeenCalled()
})
await waitFor(() => {
expect(mockToastNotify).toHaveBeenCalledWith({
type: 'error',

View File

@ -295,6 +295,8 @@ vi.mock('@/utils/var', () => ({
// Mock provider context
vi.mock('@/context/provider-context', () => ({
useProviderContext: () => createMockProviderContextValue(),
useProviderContextSelector: <T,>(selector: (state: ReturnType<typeof createMockProviderContextValue>) => T): T =>
selector(createMockProviderContextValue()),
}))
// Mock WorkflowWithInnerContext

View File

@ -141,6 +141,8 @@ vi.mock('@/context/modal-context', () => ({
let mockProviderContextValue = createMockProviderContextValue()
vi.mock('@/context/provider-context', () => ({
useProviderContext: () => mockProviderContextValue,
useProviderContextSelector: <T,>(selector: (s: ReturnType<typeof createMockProviderContextValue>) => T): T =>
selector(mockProviderContextValue),
}))
// Mock event emitter context

View File

@ -131,6 +131,8 @@ vi.mock('@/context/provider-context', () => ({
useProviderContext: () => ({
isAllowPublishAsCustomKnowledgePipelineTemplate: mockIsAllowPublishAsCustomKnowledgePipelineTemplate(),
}),
useProviderContextSelector: <T,>(selector: (s: { isAllowPublishAsCustomKnowledgePipelineTemplate: boolean }) => T): T =>
selector({ isAllowPublishAsCustomKnowledgePipelineTemplate: mockIsAllowPublishAsCustomKnowledgePipelineTemplate() }),
}))
// Mock toast context