/* eslint-disable no-console */
import { render, screen } from '@testing-library/react';

import { RichTextEditor } from '../rich-text-editor';
import type { CrystallizeRichText } from '../types/crystallize-rich-text-types';
import { sleep } from './utils';

const emptyParagraph: CrystallizeRichText = [{ kind: 'block', type: 'paragraph', textContent: 'rabbit' }];

describe('RichTextEditor basic rendering', () => {
  it('shows placeholder when no initial data is passed', async () => {
    const onChange = vi.fn();
    await render(<RichTextEditor placeholder="Get your content in here" />);
    await sleep(1);

    expect(screen.getByText('Get your content in here')).toBeInTheDocument();

    expect(onChange).not.toHaveBeenCalled();
  });

  it('should not trigger onChange on mount when no initialData is passed', async () => {
    const onChange = vi.fn();
    await render(<RichTextEditor onChange={onChange} />);
    await sleep(1);

    expect(onChange).not.toHaveBeenCalled();
  });

  it('should not trigger onChange on mount when initialData is passed', async () => {
    const onChange = vi.fn();
    await render(<RichTextEditor initialData={emptyParagraph} onChange={onChange} />);
    await sleep(1);

    expect(onChange).not.toHaveBeenCalled();
  });

  it('should trigger onChange on mount if triggerOnChangeOnAutoFocus and autoFocus is passed', async () => {
    const onChange = vi.fn();
    await render(
      <RichTextEditor initialData={emptyParagraph} onChange={onChange} triggerOnChangeOnAutoFocus autoFocus />,
    );
    await sleep(1);

    expect(onChange).toHaveBeenCalledTimes(1);
    expect(onChange).toHaveBeenCalledWith(emptyParagraph);
  });
});
