import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
import { ReturnDetailsHeaderComponent } from './return-details-header.component';
import { Receipt, ReturnDetailsStore } from '@isa/oms/data-access';
import { signal } from '@angular/core';

describe('ReturnDetailsHeaderComponent', () => {
  let spectator: Spectator<ReturnDetailsHeaderComponent>;
  const createComponent = createComponentFactory({
    component: ReturnDetailsHeaderComponent,
  });

  const getReceiptMock = signal<Receipt>({
    id: 12345,
    items: [],
    buyer: {
      reference: { id: 12345 },
      firstName: 'John',
      lastName: 'Doe',
      buyerNumber: '123456',
    },
  });

  const storeMock = {
    getReceipt: () => getReceiptMock,
  };

  beforeEach(() => {
    spectator = createComponent({
      props: {
        receiptId: 12345, // Mock receiptId for testing
      },
      providers: [
        {
          provide: ReturnDetailsStore,
          useValue: storeMock,
        },
      ],
    });
  });

  it('should create', () => {
    expect(spectator.component).toBeTruthy();
  });

  describe('[data-what="button"][data-which="customer-actions"]', () => {
    it('should be disabled if referenceId() returns undefined', () => {
      jest.spyOn(spectator.component, 'referenceId').mockReturnValue(undefined);
      spectator.detectComponentChanges();
      const button = spectator.query(
        '[data-what="button"][data-which="customer-actions"]',
      );
      expect(button).toBeTruthy();
      expect(button).toBeDisabled();
    });

    it('should be enabled if referenceId() returns a value', () => {
      jest.spyOn(spectator.component, 'referenceId').mockReturnValue(12345);
      spectator.detectComponentChanges();
      const button = spectator.query(
        '[data-what="button"][data-which="customer-actions"]',
      );
      expect(button).toBeTruthy();
      expect(button).not.toBeDisabled();
    });
  });
});
