test: refactor GenericTable tests to use a helper function for option selection

This commit is contained in:
CodingOnStar 2026-03-25 18:09:38 +08:00
parent 8e093a2b25
commit f7ae14d50e

View File

@ -1,4 +1,4 @@
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { useState } from 'react'
import GenericTable from '../generic-table'
@ -53,6 +53,16 @@ describe('GenericTable', () => {
vi.useRealTimers()
})
const selectOption = async (triggerName: string, optionName: string) => {
await act(async () => {
fireEvent.click(screen.getByRole('button', { name: triggerName }))
})
await act(async () => {
fireEvent.click(await screen.findByRole('option', { name: optionName }))
})
}
it('should render an empty editable row and append a configured row when typing into the virtual row', async () => {
const onChange = vi.fn()
@ -144,8 +154,7 @@ describe('GenericTable', () => {
<ControlledTable />,
)
await user.click(screen.getByRole('button', { name: 'Choose method' }))
await user.click(await screen.findByRole('option', { name: 'POST' }))
await selectOption('Choose method', 'POST')
await waitFor(() => {
expect(onChange).toHaveBeenCalledWith([{ method: 'post', preview: '' }])