import { TestBed } from '@angular/core/testing';
import { TransactionAmount } from './transaction-amount';

describe('TransactionAmount', () => {
  it.each([
    {
      amount: 12.5,
      type: 'payment',
      context: 'player' as const,
      sign: '+',
      semanticClass: 'inflow',
      direction: 'Einzahlung',
      text: 'Einzahlung 12,50 \u20ac',
    },
    {
      amount: 12.5,
      type: 'expense',
      context: 'team' as const,
      sign: '−',
      semanticClass: 'outflow',
      direction: 'Auszahlung',
      text: 'Auszahlung 12,50 \u20ac',
    },
    {
      amount: 12.5,
      type: 'fine',
      context: 'player' as const,
      sign: '',
      semanticClass: 'neutral',
      direction: 'Keine Kassenbewegung',
      text: 'Keine Kassenbewegung 12,50 \u20ac',
    },
  ])(
    'renders the $semanticClass cash-flow meaning accessibly',
    async ({ amount, type, context, sign, semanticClass, direction, text }) => {
      await TestBed.configureTestingModule({ imports: [TransactionAmount] }).compileComponents();
      const fixture = TestBed.createComponent(TransactionAmount);
      fixture.componentRef.setInput('amount', amount);
      fixture.componentRef.setInput('type', type);
      fixture.componentRef.setInput('context', context);
      fixture.detectChanges();

      const element = fixture.nativeElement.querySelector('[data-testid="transaction-amount"]');
      const signElement = element.querySelector('.transaction-amount__sign');
      const directionElement = element.querySelector('.transaction-amount__direction');
      const accessibleElement = element.cloneNode(true);
      accessibleElement
        .querySelectorAll('[aria-hidden=true]')
        .forEach((node: Element) => node.remove());
      expect(element.classList).toContain(semanticClass);
      expect(signElement.textContent).toBe(sign);
      expect(signElement.getAttribute('aria-hidden')).toBe('true');
      expect(directionElement.textContent).toBe(direction);
      expect(accessibleElement.textContent.replace(/\s+/g, ' ').trim()).toBe(text);
      expect(element.getAttribute('aria-label')).toBeNull();
      expect(getComputedStyle(element).whiteSpace).toBe('nowrap');
      expect(getComputedStyle(directionElement).position).toBe('absolute');
    },
  );
});
