/*
 * Copyright (c) 2019 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { TableContainerComponent } from './table-container.component';

describe('TableContainerComponent', () => {
  let component: TableContainerComponent;
  let fixture: ComponentFixture<TableContainerComponent>;

  beforeEach(
    waitForAsync(() => {
      TestBed.configureTestingModule({
        declarations: [TableContainerComponent],
      }).compileComponents();
    })
  );

  beforeEach(() => {
    fixture = TestBed.createComponent(TableContainerComponent);
    component = fixture.componentInstance;
  });

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

  it('should hide the element when no table data source is available', () => {
    fixture.detectChanges();
    const emptyTableInfo = getEmptyTableInfoElement();
    expect(emptyTableInfo).toBeNull();
  });

  describe('filled table', () => {
    beforeEach(() => {
      component.showEmptyState = false;
      fixture.detectChanges();
    });

    it('should hide the element when showEmptyState is set to false', () => {
      const emptyTableInfo = getEmptyTableInfoElement();
      expect(emptyTableInfo).toBeNull();
    });

    it('should show the element when showEmptyState is set to true', () => {
      component.showEmptyState = true;
      fixture.detectChanges();
      const emptyTableInfo = getEmptyTableInfoElement();
      expect(emptyTableInfo).not.toBeNull();
    });
  });

  describe('empty table', () => {
    let emptyTableInfo;

    beforeEach(() => {
      component.showEmptyState = true;
      fixture.detectChanges();
      emptyTableInfo = getEmptyTableInfoElement();
    });

    it('should show the element when showEmptyState is set to true', () => {
      expect(emptyTableInfo).not.toBeNull();
    });

    it('should hide the element when showEmptyState is set to false', () => {
      component.showEmptyState = false;
      fixture.detectChanges();
      emptyTableInfo = getEmptyTableInfoElement();
      expect(emptyTableInfo).toBeNull();
    });
  });

  function getEmptyTableInfoElement() {
    return fixture.debugElement.nativeElement.querySelector('bci-empty-state');
  }
});
