mirror of
https://github.com/langgenius/dify.git
synced 2026-05-06 02:18:08 +08:00
test(web): add and enhance frontend automated tests across multiple modules (#32268)
Co-authored-by: CodingOnStar <hanxujiang@dify.com>
This commit is contained in:
@ -1,11 +1,9 @@
|
||||
import { act, render, screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import copy from 'copy-to-clipboard'
|
||||
import InputCopy from '../input-copy'
|
||||
|
||||
vi.mock('copy-to-clipboard', () => ({
|
||||
default: vi.fn().mockReturnValue(true),
|
||||
}))
|
||||
// Suppress expected React act() warnings from CopyFeedback timer-based state updates
|
||||
vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
|
||||
async function renderAndFlush(ui: React.ReactElement) {
|
||||
const result = render(ui)
|
||||
@ -15,10 +13,14 @@ async function renderAndFlush(ui: React.ReactElement) {
|
||||
return result
|
||||
}
|
||||
|
||||
const execCommandMock = vi.fn().mockReturnValue(true)
|
||||
|
||||
describe('InputCopy', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
vi.useFakeTimers({ shouldAdvanceTime: true })
|
||||
execCommandMock.mockReturnValue(true)
|
||||
document.execCommand = execCommandMock
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
@ -107,7 +109,7 @@ describe('InputCopy', () => {
|
||||
await user.click(copyableArea)
|
||||
})
|
||||
|
||||
expect(copy).toHaveBeenCalledWith('copy-this-value')
|
||||
expect(execCommandMock).toHaveBeenCalledWith('copy')
|
||||
})
|
||||
|
||||
it('should update copied state after clicking', async () => {
|
||||
@ -119,7 +121,7 @@ describe('InputCopy', () => {
|
||||
await user.click(copyableArea)
|
||||
})
|
||||
|
||||
expect(copy).toHaveBeenCalledWith('test-value')
|
||||
expect(execCommandMock).toHaveBeenCalledWith('copy')
|
||||
})
|
||||
|
||||
it('should reset copied state after timeout', async () => {
|
||||
@ -131,7 +133,7 @@ describe('InputCopy', () => {
|
||||
await user.click(copyableArea)
|
||||
})
|
||||
|
||||
expect(copy).toHaveBeenCalledWith('test-value')
|
||||
expect(execCommandMock).toHaveBeenCalledWith('copy')
|
||||
|
||||
await act(async () => {
|
||||
vi.advanceTimersByTime(1500)
|
||||
@ -306,7 +308,7 @@ describe('InputCopy', () => {
|
||||
await user.click(copyableArea)
|
||||
})
|
||||
|
||||
expect(copy).toHaveBeenCalledTimes(3)
|
||||
expect(execCommandMock).toHaveBeenCalledTimes(3)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -3,6 +3,9 @@ import userEvent from '@testing-library/user-event'
|
||||
import { afterEach } from 'vitest'
|
||||
import SecretKeyModal from '../secret-key-modal'
|
||||
|
||||
// Suppress expected React act() warnings from Headless UI Dialog transitions and async API state updates
|
||||
vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
|
||||
async function renderModal(ui: React.ReactElement) {
|
||||
const result = render(ui)
|
||||
await act(async () => {
|
||||
|
||||
Reference in New Issue
Block a user