import { createComponentFactory, Spectator } from '@ngneat/spectator';
import {
  SharedHistoryListComponent,
  SharedHistoryLogEntryComponent,
  SharedHistoryLogEntryModule,
} from '@shared/components/history';
import { HistoryDTO } from '@generated/swagger/crm-api';

describe('SharedHistoryListComponent', () => {
  let spectator: Spectator<SharedHistoryListComponent>;

  const createComponent = createComponentFactory({
    component: SharedHistoryListComponent,
    imports: [SharedHistoryLogEntryModule],
    declarations: [],
  });

  beforeEach(() => {
    spectator = createComponent();
  });

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

  describe('history', () => {
    it('should render log-entry if history length <= 0', () => {
      const history = [];
      spectator.setInput('history', history);
      expect(spectator.query('.log-entry')).not.toBeVisible();
    });

    it('should render log-entry if history length > 0', () => {
      const history = [{}, {}];
      spectator.setInput('history', history);
      expect(spectator.query('.log-entry')).toBeVisible();
    });

    it('should add class last to last log-entry', () => {
      const history = [{}, {}];
      spectator.setInput('history', history);
      expect(spectator.queryLast('.log-entry')).toHaveClass('last');
    });

    it('should add class first to first log-entry', () => {
      const history = [{}, {}];
      spectator.setInput('history', history);
      expect(spectator.queryAll('.log-entry')[0]).toHaveClass('first');
    });

    it('should add class last to timeline-dot if it is the last log-entry and not the first', () => {
      const history = [{}, {}];
      spectator.setInput('history', history);
      expect(spectator.queryLast('.log-entry').querySelector('.timeline-dot')).toHaveClass('last');
    });

    it('should add class bottom-square if it is the last and first log-entry at the same time', () => {
      const history = [{}];
      spectator.setInput('history', history);
      expect(spectator.queryLast('.timeline-dot')).toHaveClass('bottom-square');
    });
  });

  describe('template', () => {
    it('should render shared-history-log-entry and set Input historyLogEntryData', () => {
      const history: HistoryDTO[] = [];
      const historyLogEntryData: HistoryDTO = { id: 1 };
      history.push(historyLogEntryData);
      spectator.setInput('history', history);
      spectator.detectComponentChanges();
      expect(spectator.query(SharedHistoryLogEntryComponent).historyLogEntryData).toEqual(historyLogEntryData);
    });
  });
});
