import { describe, expect, it } from 'vitest';
import EditorialSearchBody from './EditorialSearchBody.astro';
import {
  SEARCH_HIT,
  assertSearchInputHasNoPlaceholder,
  renderBodyComponent,
} from '../../test/body-component-render';

describe('EditorialSearchBody', () => {
  it('renders results when hits are present', async () => {
    const html = await renderBodyComponent(EditorialSearchBody, {
      q: 'alpha',
      hasQuery: true,
      hits: [SEARCH_HIT],
      nextHref: null,
    });
    assertSearchInputHasNoPlaceholder(html);
    expect(html).toContain('class="editorial-search__results"');
    expect(html).toContain('Search hit title');
    expect(html).toContain('Result snippet');
    expect(html).not.toMatch(/<script\b/i);
  });

  it('renders empty state when query has no hits', async () => {
    const html = await renderBodyComponent(EditorialSearchBody, {
      q: 'missing',
      hasQuery: true,
      hits: [],
      nextHref: null,
    });
    expect(html).toContain('No results for that query.');
    expect(html).not.toContain('class="editorial-search__results"');
  });

  it('omits results and empty state before a query is submitted', async () => {
    const html = await renderBodyComponent(EditorialSearchBody, {
      q: '',
      hasQuery: false,
      hits: [],
      nextHref: null,
    });
    expect(html).not.toContain('No results for that query.');
    expect(html).not.toContain('class="editorial-search__results"');
    assertSearchInputHasNoPlaceholder(html);
  });
});
