import { useFormikContext } from 'formik';
import type { Mock } from 'vitest';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { useTitle } from './useTitle';

vi.mock('formik', () => ({
  useFormikContext: vi.fn(),
}));

describe('useTitle', () => {
  afterEach(() => {
    vi.clearAllMocks();
  });

  it('should return default title when titleProperty is not provided', () => {
    const defaultTitle = 'Default Title';
    (useFormikContext as Mock).mockReturnValueOnce({});

    const result = useTitle(undefined, defaultTitle);

    expect(result).toBe(defaultTitle);
  });

  it('should return title from formik context when titleProperty is provided', () => {
    const titleProperty = 'title';
    const titleValue = 'Form Title';
    (useFormikContext as Mock).mockReturnValueOnce({
      values: { [titleProperty]: titleValue },
    });

    const result = useTitle(titleProperty);

    expect(result).toBe(titleValue);
  });

  it('should return default title when titleProperty is provided but not found in formik context', () => {
    const titleProperty = 'title';
    const defaultTitle = 'Default Title';
    (useFormikContext as Mock).mockReturnValueOnce({ values: {} });

    const result = useTitle(titleProperty, defaultTitle);

    expect(result).toBe(defaultTitle);
  });

  it('should prepend an * when dirty is true', () => {
    const defaultTitle = 'Default Title';
    (useFormikContext as Mock).mockReturnValueOnce({ dirty: true });

    const result = useTitle(undefined, defaultTitle);

    expect(result).toBe(`*${defaultTitle}`);
  });
});
