Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.2 KB
Raw Blame
import { MOCK_DEFAULT_ORGANIZATION_MEMBER } from 'lib/api.mock'

import '@testing-library/jest-dom'

import { cleanup, fireEvent, render, RenderResult, screen } from '@testing-library/react'

import { fullName } from 'lib/utils/strings'

import { MemberSelectRow, MemberSelectRowProps } from './MemberSelectRow'

describe('MemberSelectRow', () => {
    const member = MOCK_DEFAULT_ORGANIZATION_MEMBER

    afterEach(() => {
        cleanup()
    })

    function renderRow(props: Partial<MemberSelectRowProps> = {}): RenderResult {
        return render(
            <ul>
                <MemberSelectRow member={member} isYou={false} onClick={jest.fn()} {...props} />
            </ul>
        )
    }

    it('renders the member name', () => {
        renderRow()
        expect(screen.getByText(fullName(member.user))).toBeInTheDocument()
    })

    it.each([
        ['shows', true],
        ['hides', false],
    ])('%s the "(you)" label according to isYou', (_, isYou) => {
        renderRow({ isYou })
        if (isYou) {
            expect(screen.getByText('(you)')).toBeInTheDocument()
        } else {
            expect(screen.queryByText('(you)')).not.toBeInTheDocument()
        }
    })

    it('renders no checkbox for a single-select row (`checked` omitted)', () => {
        const { container } = renderRow()
        expect(container.querySelector('input[type="checkbox"]')).toBeNull()
    })

    it.each([
        ['checked', true],
        ['unchecked', false],
    ])('renders a %s checkbox for a multi-select row', (_, checked) => {
        const { container } = renderRow({ checked })
        const checkbox = container.querySelector('input[type="checkbox"]') as HTMLInputElement | null
        expect(checkbox).not.toBeNull()
        expect(checkbox!.checked).toBe(checked)
        expect(checkbox).toHaveAttribute('tabindex', '-1')
        expect(checkbox).toHaveAttribute('aria-hidden', 'true')
    })

    it('fires onClick when the name in a multi-select row is clicked', () => {
        const onClick = jest.fn()
        renderRow({ checked: false, onClick })
        fireEvent.click(screen.getByText(fullName(member.user)))
        expect(onClick).toHaveBeenCalledTimes(1)
    })
})