ResultCell.test.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { fireEvent, screen } from '@testing-library/react'
  2. import userEvent from '@testing-library/user-event'
  3. import { expect, test, vi } from 'vitest'
  4. import { ResultCell } from '@/components/interfaces/SQLEditor/UtilityPanel/ResultCell'
  5. import { customRender as render } from '@/tests/lib/custom-render'
  6. const noop = () => {}
  7. test('renders the formatted cell value', () => {
  8. render(<ResultCell column="name" value="alice" onContextMenu={noop} onExpand={noop} />)
  9. expect(screen.getByText('alice')).toBeTruthy()
  10. })
  11. test('renders NULL for null values', () => {
  12. render(<ResultCell column="name" value={null} onContextMenu={noop} onExpand={noop} />)
  13. expect(screen.getByText('NULL')).toBeTruthy()
  14. })
  15. test('does not render the expand button for short string values', () => {
  16. render(<ResultCell column="name" value="alice" onContextMenu={noop} onExpand={noop} />)
  17. expect(screen.queryByRole('button', { name: 'View full cell content' })).toBeNull()
  18. })
  19. test('renders the expand button for object values', () => {
  20. render(<ResultCell column="data" value={{ nested: true }} onContextMenu={noop} onExpand={noop} />)
  21. expect(screen.getByRole('button', { name: 'View full cell content' })).toBeTruthy()
  22. })
  23. test('renders the expand button for long string values', () => {
  24. render(<ResultCell column="bio" value={'a'.repeat(120)} onContextMenu={noop} onExpand={noop} />)
  25. expect(screen.getByRole('button', { name: 'View full cell content' })).toBeTruthy()
  26. })
  27. test('clicking the expand button calls onExpand with column and value', async () => {
  28. const onExpand = vi.fn()
  29. const value = { nested: true }
  30. render(<ResultCell column="data" value={value} onContextMenu={noop} onExpand={onExpand} />)
  31. await userEvent.click(screen.getByRole('button', { name: 'View full cell content' }))
  32. expect(onExpand).toHaveBeenCalledTimes(1)
  33. expect(onExpand).toHaveBeenCalledWith('data', value)
  34. })
  35. test('right-clicking the cell calls onContextMenu with column and value', () => {
  36. const onContextMenu = vi.fn()
  37. render(<ResultCell column="name" value="alice" onContextMenu={onContextMenu} onExpand={noop} />)
  38. fireEvent.contextMenu(screen.getByText('alice'))
  39. expect(onContextMenu).toHaveBeenCalledTimes(1)
  40. const [, column, value] = onContextMenu.mock.calls[0]
  41. expect(column).toBe('name')
  42. expect(value).toBe('alice')
  43. })