import { ComponentFixture, TestBed } from '@angular/core/testing';
import { BixiTableModule } from '../table.module';
import { Component, DebugElement } from '@angular/core';
import { By } from '@angular/platform-browser';
import { EColType, ICols } from '../table.type';
import { ColTemplateService } from '@bixi/core/table/cols/col-template.service';

describe('col template', () => {
  let fixture: ComponentFixture<TestComponent>;
  let context: TestComponent;
  let debugEle: DebugElement;
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [BixiTableModule],
      declarations: [TestComponent],
      providers: [ColTemplateService]
    });

    fixture = TestBed.createComponent(TestComponent);
    context = fixture.componentInstance;
    debugEle = fixture.debugElement;
  });

  it('should render template', () => {
    // given
    context.cols = [
      {
        name: '通过模板来显示按钮',
        key: 'name',
        type: EColType.template,
        // 指定使用哪个模版
        ref: 'basicRef'
      }
    ];

    // when
    fixture.detectChanges();

    // then
    const testEl = debugEle.query(By.css('.test-text'));
    expect(testEl).toBeDefined();
  });

  it('should render template with params', () => {
    // given
    context.cols = [
      {
        name: '通过模板来显示按钮',
        key: 'name',
        type: EColType.template,
        // 指定使用哪个模版
        ref: 'withParamsRef'
      }
    ];

    // when
    fixture.detectChanges();

    // then
    const content = debugEle.query(By.css('.test-text')).nativeElement.textContent;
    expect(content).toContain('0_阿明');
  });
});

const rows = [
  { name: '阿明' }
];

@Component({
  template: `

    <bixi-table
      [rows]="rows"
      [cols]="cols"
    >
      <ng-template bixi-row='basicRef'>
        <span class="test-text"></span>
      </ng-template>
      <ng-template bixi-row="withParamsRef" let-row let-index="index">
        <span class="test-text">{{index}}_{{ row.name }}</span>
      </ng-template>
    </bixi-table>`
})
class TestComponent {
  rows = rows;
  cols: ICols = [];
}
