import * as React from 'react'
import { fireEvent, render } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import Label from './'
import { Comment } from '@planview/pv-icons'
describe('Label', () => {
it('should render label', () => {
const { getByText } = render()
expect(getByText('Label text')).toBeInTheDocument()
})
it('should render label as a "label" when useLabelElement is true', () => {
const { getByText } = render(
)
expect(getByText('Label text').tagName).toBe('LABEL')
})
it('should render label as a "span" when useLabelElement is false', () => {
const { getByText } = render(
)
expect(getByText('Label text').tagName).toBe('SPAN')
})
it('should render an asterisk when required is true', () => {
const { getByLabelText } = render()
expect(getByLabelText('*')).toBeInTheDocument()
})
it('should render an asterisk with a tooltip when requiredTooltip is provided', async () => {
const { getByLabelText, findByText } = render(
)
const requiredEl = getByLabelText('*')
expect(requiredEl).toBeInTheDocument()
fireEvent.mouseEnter(requiredEl)
const tooltip = await findByText('Required')
expect(tooltip).toBeInTheDocument()
})
it('should render an icon when provided', () => {
const { getByLabelText } = render(
}>
Label text
)
expect(getByLabelText('Used by comments')).toBeInTheDocument()
})
it('should render a help icon with a tooltip when only iconTooltip is provided', async () => {
const { findByRole, container } = render(
)
/* There is no accessible way of finding this element to start */
const requiredEl = container.querySelector('svg')?.closest('span')
expect(requiredEl).toBeInTheDocument()
fireEvent.mouseEnter(requiredEl!)
const tooltip = await findByRole('tooltip', {
name: 'Icon tooltip text',
})
expect(tooltip).toBeInTheDocument()
})
it('should render a custom icon with a tooltip when both icon and iconTooltip are provided', async () => {
userEvent.setup()
const { getByLabelText, findByRole } = render(
}
iconTooltip="Icon tooltip text"
>
Label text
)
const requiredEl = getByLabelText('Used by comments').parentElement
expect(requiredEl).toBeInTheDocument()
fireEvent.mouseEnter(requiredEl as HTMLSpanElement)
const tooltip = await findByRole('tooltip', {
name: 'Icon tooltip text',
})
expect(tooltip).toBeInTheDocument()
})
})