import '@testing-library/jest-dom'

import { fireEvent, render, screen } from '@testing-library/react'
import { axe, toHaveNoViolations } from 'jest-axe'

import { PktTextarea } from './Textarea'

expect.extend(toHaveNoViolations)

describe('PktTextarea', () => {
  test('renders input field with correct props', async () => {
    const { getByLabelText } = render(<PktTextarea label="Input Label" id="inputId" />)

    const inputElement = getByLabelText('Input Label')
    expect(inputElement).toBeInTheDocument()
    expect(inputElement.tagName).toBe('TEXTAREA')
    expect(inputElement.id).toBe('inputId-input')
    expect(inputElement).toHaveClass('pkt-input--medium')
  })

  test('applies inputSize to textarea and wrapper', async () => {
    const { container, getByLabelText } = render(
      <PktTextarea label="Small textarea" id="smallTextarea" inputSize="small" />,
    )

    expect(getByLabelText('Small textarea')).toHaveClass('pkt-input--small')
    expect(container.querySelector('.pkt-inputwrapper--small')).toBeInTheDocument()
  })

  test('renders error message when hasError prop is true', async () => {
    render(<PktTextarea label="Input Label" id="inputId" hasError errorMessage="Input error" />)
    const errorMessageId = 'inputId-input-error' // når https://github.com/oslokommune/punkt/issues/3093 er fiksa:  screen.getByRole('textbox').getAttribute('aria-errormessage')
    const alertContainer = screen.getByRole('alert')
    expect(alertContainer).toHaveAttribute('id', errorMessageId)
    expect(alertContainer).toHaveTextContent('Input error')
  })

  test('should display a correct initial counter (#3466)', async () => {
    const variants = [
      <PktTextarea key={'controlled'} label="Input Label" id="controlledInputId" counter value={'abc'} />,
      <PktTextarea key={'uncontrolled'} label="Input Label" id="uncontrolledInputId" counter defaultValue={'abc'} />,
    ]
    variants.forEach((variant) => {
      const { container } = render(variant)
      const counter = container.querySelector('.pkt-input__counter')
      expect(counter).toHaveTextContent('3')
    })
  })

  describe('PktTextarea', () => {
    test('toggles helptext class', async () => {
      const { getByText, container } = render(
        <PktTextarea label="Input Label" id="inputId" helptext="Help Text" helptextDropdown="Help Text" />,
      )

      const expandButton = getByText('Les mer').closest('button') as HTMLButtonElement
      const helptextElement = container.querySelector('.pkt-inputwrapper__helptext-expandable-closed')

      await fireEvent.click(expandButton)
      expect(helptextElement).toHaveClass('pkt-inputwrapper__helptext-expandable-open')

      await fireEvent.click(expandButton)
      expect(helptextElement).toHaveClass('pkt-inputwrapper__helptext-expandable-closed')
    })
  })

  describe('accessibility', () => {
    it('renders with no wcag errors with axe', async () => {
      const { container } = render(<PktTextarea label="Input Label" id="inputId" />)
      const results = await axe(container)
      expect(results).toHaveNoViolations()
    })
  })
})
