import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { <%= classify(name) %> } from './<%= dasherize(name) %>';

@Component({
  imports: [<%= classify(name) %>],
  template: `
    <<%= selector %>
      label="Valor da transferência"
      currencyCode="BRL"
      currencyLabel="Real brasileiro"
      hint="Use o valor informado no boleto de pagamento."
      error="Informe um valor válido."
      [required]="true"
      [(value)]="amount"
    />
  `,
})
class HostComponent {
  amount = '';
}

describe('<%= classify(name) %>', () => {
  let fixture: ComponentFixture<HostComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({ imports: [HostComponent] }).compileComponents();
    fixture = TestBed.createComponent(HostComponent);
    fixture.detectChanges();
  });

  function input(): HTMLInputElement {
    return fixture.nativeElement.querySelector('input');
  }

  it('wires label, currency description, required state, hint, and error message', () => {
    const label = fixture.nativeElement.querySelector('label');
    const hint = fixture.nativeElement.querySelector('.currency-field__hint');
    const error = fixture.nativeElement.querySelector('.currency-field__error');
    const currency = fixture.nativeElement.querySelector('.visually-hidden');

    expect(label.textContent).toContain('Valor da transferência');
    expect(label.getAttribute('for')).toBe(input().id);
    expect(currency.textContent).toContain('Real brasileiro');
    expect(input().inputMode).toBe('decimal');
    expect(input().required).toBe(true);
    expect(input().getAttribute('aria-required')).toBe('true');
    expect(input().getAttribute('aria-invalid')).toBe('true');
    expect(input().getAttribute('aria-describedby')).toContain(currency.id);
    expect(input().getAttribute('aria-describedby')).toContain(hint.id);
    expect(input().getAttribute('aria-describedby')).toContain(error.id);
    expect(error.getAttribute('role')).toBe('alert');
  });

  it('updates the bound amount on input', () => {
    input().value = '1250.90';
    input().dispatchEvent(new Event('input', { bubbles: true }));
    fixture.detectChanges();

    expect(fixture.componentInstance.amount).toBe('1250.90');
  });
});
