CreateFunctionConfigParamsSection.test.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { render, screen } from '@testing-library/react'
  2. import userEvent from '@testing-library/user-event'
  3. import { useForm } from 'react-hook-form'
  4. import { Form } from 'ui'
  5. import { describe, expect, it } from 'vitest'
  6. import { CreateFunctionConfigParamsSection } from './CreateFunctionConfigParamsSection'
  7. type ConfigParamsFormValues = {
  8. config_params: Array<{ name: string; value: string }>
  9. }
  10. const CreateFunctionConfigParamsHarness = () => {
  11. const form = useForm<ConfigParamsFormValues>({
  12. defaultValues: {
  13. config_params: [{ name: 'search_path', value: 'public' }],
  14. },
  15. })
  16. return (
  17. <Form {...form}>
  18. <CreateFunctionConfigParamsSection />
  19. </Form>
  20. )
  21. }
  22. describe('CreateFunctionConfigParamsSection', () => {
  23. it('appends a new config parameter row with name and value fields', async () => {
  24. const user = userEvent.setup()
  25. render(<CreateFunctionConfigParamsHarness />)
  26. await user.click(screen.getByRole('button', { name: 'Add a new config' }))
  27. expect(screen.getAllByPlaceholderText('parameter_name')).toHaveLength(2)
  28. expect(screen.getAllByPlaceholderText('parameter_value')).toHaveLength(2)
  29. })
  30. it('removes an existing config parameter row', async () => {
  31. const user = userEvent.setup()
  32. render(<CreateFunctionConfigParamsHarness />)
  33. expect(screen.getByDisplayValue('search_path')).toBeInTheDocument()
  34. await user.click(screen.getByRole('button', { name: 'Remove configuration parameter' }))
  35. expect(screen.queryByDisplayValue('search_path')).not.toBeInTheDocument()
  36. })
  37. })