import { TestBed, getTestBed } from '@angular/core/testing';
import { BrowserTestingModule, platformBrowserTesting } from '@angular/platform-browser/testing';
import { afterEach, beforeAll, describe, expect, it } from 'vitest';

import { TooltipComponent } from './tooltip.component';

describe('TooltipComponent', () => {
  beforeAll(() => {
    try {
      getTestBed().initTestEnvironment(BrowserTestingModule, platformBrowserTesting());
    } catch {
      // Test environment may already be initialized by another setup entrypoint.
    }
  });

  afterEach(() => TestBed.resetTestingModule());

  it('renders the reusable danger information tooltip', async () => {
    await TestBed.configureTestingModule({ imports: [TooltipComponent] }).compileComponents();
    const fixture = TestBed.createComponent(TooltipComponent);
    fixture.componentRef.setInput('message', 'Este producto no tiene stock disponible.');
    fixture.detectChanges();

    const element = fixture.nativeElement as HTMLElement;
    const trigger = element.querySelector('.tooltip-trigger');
    const message = element.querySelector('.tooltip-message');

    expect(trigger?.querySelector('i.fa-solid.fa-circle-info')).not.toBeNull();
    expect(message?.textContent?.trim()).toBe('Este producto no tiene stock disponible.');
    expect(trigger?.getAttribute('aria-describedby')).toBe(message?.id);
  });
});
