import { render } from '@testing-library/react'; import { SECRET_MAX_LABELS } from '~/consts/secrets'; import { SecretLabels } from '../SecretsListView/SecretLabels'; describe('SecretLabels', () => { it('should render the labels', () => { const r = render( {}} />, ); expect(r.getByText('label1')).toBeInTheDocument(); expect(r.getByText('label2')).toBeInTheDocument(); }); it('should show toggle button when labels are more than maxLabels', () => { const r = render( {}} />, ); expect(r.getByText('label1')).toBeInTheDocument(); expect(r.getByText('label2')).toBeInTheDocument(); expect(r.getByText('label3')).toBeInTheDocument(); expect(r.queryByText('label4')).not.toBeInTheDocument(); expect(r.getByText('Show more')).toBeInTheDocument(); }); it('should hide the label at maxLabels index when collapsed', () => { const labels = Array.from({ length: SECRET_MAX_LABELS + 1 }, (_, i) => `label${i + 1}`); const r = render( {}} />, ); labels.slice(0, SECRET_MAX_LABELS).forEach((label) => { expect(r.getByText(label)).toBeInTheDocument(); }); expect(r.queryByText(labels[SECRET_MAX_LABELS])).not.toBeInTheDocument(); expect(r.getByText('Show more')).toBeInTheDocument(); }); it('should show less button when labels are more than maxLabels and expanded is true', () => { const r = render( {}} />, ); expect(r.getByText('label1')).toBeInTheDocument(); expect(r.getByText('label2')).toBeInTheDocument(); expect(r.getByText('label4')).toBeInTheDocument(); expect(r.getByText('Show less')).toBeInTheDocument(); }); it('should call handleToggle when toggle button is clicked', () => { const handleToggle = jest.fn(); const r = render( , ); r.getByText('Show more').click(); expect(handleToggle).toHaveBeenCalledWith(0); }); });