import React, { useState } from 'react'
import { render, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { Checkbox, type CheckedStatus } from './index'
const user = userEvent.setup()
describe('', () => {
describe('unchecked', () => {
it('has correct unchecked attributes', () => {
const { getByRole } = render()
const checkbox = getByRole('checkbox') as HTMLInputElement
expect(checkbox.checked).toBe(false)
expect(checkbox.indeterminate).toBe(false)
})
})
describe('checked', () => {
it('has correct checked attributes', () => {
const { getByRole } = render()
const checkbox = getByRole('checkbox') as HTMLInputElement
expect(checkbox.checked).toBe(true)
expect(checkbox.indeterminate).toBe(false)
})
})
describe('indeterminate', () => {
it('has correct indeterminate attributes', () => {
const { getByRole } = render()
const checkbox = getByRole('checkbox') as HTMLInputElement
expect(checkbox.checked).toBe(false)
expect(checkbox.indeterminate).toBe(true)
})
})
it('correctly changes the checked attributes according to status', async () => {
const Wrapper = (): JSX.Element => {
const [status, setStatus] = useState('unchecked')
const handleChange: React.ChangeEventHandler = () => {
if (status === 'unchecked') {
setStatus('indeterminate')
} else if (status === 'indeterminate') {
setStatus('checked')
} else if (status === 'checked') {
setStatus('unchecked')
}
}
return
}
const { getByRole } = render()
const checkbox = getByRole('checkbox') as HTMLInputElement
expect(checkbox.checked).toBe(false)
expect(checkbox.indeterminate).toBe(false)
await user.click(checkbox)
await waitFor(() => {
expect(checkbox.checked).toBe(false)
expect(checkbox.indeterminate).toBe(true)
})
await user.click(checkbox)
await waitFor(() => {
expect(checkbox.checked).toBe(true)
expect(checkbox.indeterminate).toBe(false)
})
})
})