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

import { Component, QueryList, ViewChild, ViewChildren } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { ColumnItemDirective } from './column-item.directive';
import { ColumnDirective } from './column.directive';

@Component({
  selector: 'bci-scroll-list-item',
  template: ` <div bciColumn>
    <div bciColumnItem>dummy1</div>
    <div bciColumnItem>dummy2</div>
    <div bciColumnItem>dummy3</div>
  </div>`,
})
class ScrollListItemComponent {
  @ViewChild(ColumnDirective) directive: ColumnDirective;

  @ViewChildren(ColumnItemDirective) itemDirectives: QueryList<ColumnItemDirective>;
}

describe('ScrollListItemDirective', () => {
  let host: ScrollListItemComponent;
  let fixture: ComponentFixture<ScrollListItemComponent>;

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

  beforeEach(() => {
    fixture = TestBed.createComponent(ScrollListItemComponent);
    host = fixture.componentInstance;
    fixture.detectChanges();
  });

  [
    { description: 'scroll first item index = 0', itemIndex: 0, result: 0 },
    { description: 'scroll first item index = 1', itemIndex: 1, result: 0 },
    { description: 'scroll first item index = 2', itemIndex: 2, result: 0 },
  ].forEach((config) => {
    // FIXME: scrollTop not working in jest
    xit(`scrollToItem - ${config.description}`, () => {
      fixture.detectChanges();
      const dirs = fixture.debugElement.queryAll(By.directive(ColumnItemDirective));
      host.directive.scrollToItem(
        dirs[config.itemIndex].injector.get(ColumnItemDirective) as ColumnItemDirective,
        config.itemIndex
      );
      expect(host.directive.elem.nativeElement.scrollTop).toBe(config.result);
    });
  });
});
