import { By } from '@angular/platform-browser';
import { DomainDashboardService } from '@domain/isa';
import { Spectator, createComponentFactory, SpyObject, createSpyObject } from '@ngneat/spectator';
import { MockComponent } from 'ng-mocks';
import { of } from 'rxjs';
import { DashboardComponent } from './dashboard.component';
import { KpiCardComponent } from './kpi-card/kpi-card.component';
import { ProductsCardComponent } from './products-card/product-card.component';

describe('DashboardComponent', () => {
  let spectator: Spectator<DashboardComponent>;
  let domainIsaDashboardService: SpyObject<DomainDashboardService>;

  const createComponent = createComponentFactory({
    component: DashboardComponent,
    declarations: [MockComponent(KpiCardComponent), MockComponent(ProductsCardComponent)],
    componentMocks: [KpiCardComponent, ProductsCardComponent],
  });

  beforeEach(() => {
    domainIsaDashboardService = createSpyObject(DomainDashboardService);

    domainIsaDashboardService.feed.andReturn(of({ result: [] }));

    spectator = createComponent({
      providers: [{ provide: DomainDashboardService, useValue: domainIsaDashboardService }],
    });
  });

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

  describe('page-kpi-card element', () => {
    it('should be created', () => {
      spectator.component.feeds$ = of([
        {
          type: 'kpi',
        },
      ]);

      spectator.detectChanges();
      expect(spectator.query('page-kpi-card')).toExist();
    });

    it('should have the feed as [feed] input', () => {
      spectator.component.feeds$ = of([
        {
          type: 'kpi',
        },
      ]);

      spectator.detectChanges();

      expect(spectator.debugElement.query(By.css('page-kpi-card')).componentInstance.feed).toEqual({
        type: 'kpi',
      });
    });

    it('should render two page-kpi-card elements', () => {
      spectator.component.feeds$ = of([
        {
          type: 'kpi',
        },
        {
          type: 'kpi',
        },
      ]);

      spectator.detectChanges();

      expect(spectator.queryAll('page-kpi-card').length).toEqual(2);
    });
  });

  describe('page-products-card element', () => {
    it('should be created', () => {
      spectator.component.feeds$ = of([
        {
          type: 'products',
        },
      ]);

      spectator.detectChanges();
      expect(spectator.query('page-products-card')).toExist();
    });

    it('should have the feed as [feed] input', () => {
      spectator.component.feeds$ = of([
        {
          type: 'products',
        },
      ]);

      spectator.detectChanges();

      expect(spectator.debugElement.query(By.css('page-products-card')).componentInstance.feed).toEqual({
        type: 'products',
      });
    });

    it('should render two page-products-card elements', () => {
      spectator.component.feeds$ = of([
        {
          type: 'products',
        },
        {
          type: 'products',
        },
      ]);

      spectator.detectChanges();

      expect(spectator.queryAll('page-products-card').length).toEqual(2);
    });
  });
});
