HTTPParameters.test.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 { type WebhookFormValues } from './EditHookPanel.constants'
  7. import { HTTPParameters } from './HTTPParameters'
  8. const HTTPParametersHarness = () => {
  9. const form = useForm<WebhookFormValues>({
  10. defaultValues: {
  11. name: 'test-hook',
  12. table_id: 'public.messages',
  13. http_method: 'POST',
  14. timeout_ms: 1000,
  15. events: ['INSERT'],
  16. function_type: 'http_request',
  17. http_url: 'https://hooks.example.com/webhook',
  18. httpHeaders: [],
  19. httpParameters: [{ id: 'param-1', name: 'tenant', value: 'prod' }],
  20. },
  21. })
  22. return (
  23. <Form {...form}>
  24. <HTTPParameters form={form} />
  25. </Form>
  26. )
  27. }
  28. describe('HTTPParameters', () => {
  29. it('appends a new parameter row', async () => {
  30. const user = userEvent.setup()
  31. render(<HTTPParametersHarness />)
  32. await user.click(screen.getByRole('button', { name: 'Add a new parameter' }))
  33. expect(screen.getAllByPlaceholderText('Parameter name')).toHaveLength(2)
  34. expect(screen.getAllByPlaceholderText('Parameter value')).toHaveLength(2)
  35. })
  36. it('removes an existing parameter row', async () => {
  37. const user = userEvent.setup()
  38. render(<HTTPParametersHarness />)
  39. expect(screen.getByDisplayValue('tenant')).toBeInTheDocument()
  40. await user.click(screen.getByRole('button', { name: 'Remove parameter' }))
  41. expect(screen.queryByDisplayValue('tenant')).not.toBeInTheDocument()
  42. })
  43. })