import { createComponentFactory, mockProvider, Spectator } from '@ngneat/spectator';
import { SharedHistoryListComponent, SharedHistoryListModule } from '@shared/components/history';
import { HistoryDTO } from '@generated/swagger/crm-api';
import { UiModalRef } from '@ui/modal';
import { HistoryData } from './history-data';
import { HistoryModalComponent } from './history.component';

describe('HistoryModalComponent', () => {
  let spectator: Spectator<HistoryModalComponent>;
  let modalRefMock: jasmine.SpyObj<UiModalRef<void, HistoryData>>;

  const createComponent = createComponentFactory({
    component: HistoryModalComponent,
    imports: [SharedHistoryListModule],
    declarations: [],
    providers: [mockProvider(UiModalRef, { data: {} as HistoryData })],
  });

  beforeEach(() => {
    spectator = createComponent();
    modalRefMock = spectator.inject(UiModalRef);
  });

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

  describe('customerName', () => {
    it('should get the customer name from ref.data', () => {
      const customerName = 'Test';
      spectator.component.ref.data.customerName = customerName;
      expect(spectator.component.customerName).toBe(customerName);
    });
  });

  describe('customerNumber', () => {
    it('should get the customer number from ref.data', () => {
      const customerNumber = '12345';
      spectator.component.ref.data.customerNumber = customerNumber;
      expect(spectator.component.customerNumber).toBe(customerNumber);
    });
  });

  describe('history', () => {
    it('should get the history from ref.data', () => {
      const history: HistoryDTO[] = [{ id: 1 }];
      spectator.component.ref.data.history = history;
      expect(spectator.component.history).toBe(history);
    });
  });

  describe('template', () => {
    it('should render shared-history-list and set Input History', () => {
      const history: HistoryDTO[] = [{ id: 1 }, { id: 2 }];
      spyOnProperty(spectator.component, 'history', 'get').and.returnValue(history);
      spectator.detectComponentChanges();
      expect(spectator.query(SharedHistoryListComponent).history).toEqual(history);
    });
  });
});
