import { describe, it, expect, beforeEach, vi } from 'vitest' import { fireEvent, render, screen } from '@testing-library/react' import { Note } from './widget-note' beforeEach(() => { // jsdom doesn't lay anything out, so scrollHeight / clientHeight default to // 0 / 0 and the overflow detector won't fire by default. Each test that // wants to exercise the overflow branch stubs these getters explicitly. }) describe('', () => { it('renders the children unclamped when no overflow is detected', () => { render(Short text) expect(screen.getByText('Short text')).toBeTruthy() expect(screen.queryByText('Show more')).toBeNull() }) it('shows the Show more toggle when content overflows', () => { // Force scrollHeight > clientHeight on the typography element. Object.defineProperty(HTMLElement.prototype, 'scrollHeight', { configurable: true, get: () => 200, }) Object.defineProperty(HTMLElement.prototype, 'clientHeight', { configurable: true, get: () => 60, }) render({'a '.repeat(400)}) expect(screen.getByText('Show more')).toBeTruthy() fireEvent.click(screen.getByText('Show more')) expect(screen.getByText('Show less')).toBeTruthy() }) it('honors custom labels', () => { Object.defineProperty(HTMLElement.prototype, 'scrollHeight', { configurable: true, get: () => 200, }) Object.defineProperty(HTMLElement.prototype, 'clientHeight', { configurable: true, get: () => 60, }) render( long text, ) expect(screen.getByText('Reveal')).toBeTruthy() fireEvent.click(screen.getByText('Reveal')) expect(screen.getByText('Hide')).toBeTruthy() }) it('renders string children verbatim (no markdown parsing on base Note)', () => { const { container } = render({'This is **literal** text'}) // Base Note no longer parses markdown — asterisks render verbatim. expect(container.querySelector('strong')).toBeNull() expect(screen.getByText('This is **literal** text')).toBeTruthy() }) it('passes through JSX children verbatim', () => { render( **not parsed** , ) // Asterisks should remain literal because the children are JSX. expect(screen.getByTestId('raw').textContent).toBe('**not parsed**') }) describe('lineClamp={0}', () => { it('does not render the Show more toggle when content overflows', () => { Object.defineProperty(HTMLElement.prototype, 'scrollHeight', { configurable: true, get: () => 200, }) Object.defineProperty(HTMLElement.prototype, 'clientHeight', { configurable: true, get: () => 60, }) render({'a '.repeat(400)}) expect(screen.queryByText('Show more')).toBeNull() expect(screen.queryByText('Show less')).toBeNull() }) it('does not apply line clamping styles', () => { const { container } = render( {'a '.repeat(400)}, ) const textBox = container.querySelector('p')?.parentElement const style = textBox ? window.getComputedStyle(textBox) : null expect(style?.getPropertyValue('-webkit-line-clamp')).toBeFalsy() }) it('default behavior (lineClamp omitted) still clamps and shows toggle on overflow', () => { Object.defineProperty(HTMLElement.prototype, 'scrollHeight', { configurable: true, get: () => 200, }) Object.defineProperty(HTMLElement.prototype, 'clientHeight', { configurable: true, get: () => 60, }) render({'a '.repeat(400)}) expect(screen.getByText('Show more')).toBeTruthy() }) }) it('does not throw when ResizeObserver is missing', () => { const original = globalThis.ResizeObserver // @ts-expect-error force-undefined for the duration of the test delete globalThis.ResizeObserver try { // We don't expect anything specific to render — just that this doesn't crash. expect(() => render(x)).not.toThrow() } finally { globalThis.ResizeObserver = original } void vi }) }) describe('', () => { it('renders bold markdown from a content string', () => { const { container } = render( , ) expect(container.querySelector('strong')?.textContent).toBe('important') }) it('renders markdown links with secure target/rel defaults', () => { const { container } = render( , ) const link = container.querySelector('a') expect(link?.getAttribute('href')).toBe('https://example.com') expect(link?.getAttribute('target')).toBe('_blank') expect(link?.getAttribute('rel')).toBe('noopener noreferrer') }) it('strips images from markdown', () => { const { container } = render( , ) expect(container.querySelector('img')).toBeNull() expect(screen.getByText(/caption text/)).toBeTruthy() }) it('clamps and shows the Show more toggle on overflow (same shell as base)', () => { Object.defineProperty(HTMLElement.prototype, 'scrollHeight', { configurable: true, get: () => 200, }) Object.defineProperty(HTMLElement.prototype, 'clientHeight', { configurable: true, get: () => 60, }) render() expect(screen.getByText('Show more')).toBeTruthy() }) })