import { ComponentFixture, TestBed } from '@angular/core/testing';
import { Component, DebugElement } from '@angular/core';
import { BixiTableModule, EColType, ICols } from '@bixi/core/table';
import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
import { NzToolTipModule } from 'ng-zorro-antd/tooltip';
import { BixiTimeComponent, BixiTimeModule } from '@bixi/core/time';
import { By } from '@angular/platform-browser';

describe('col time', () => {
  let fixture: ComponentFixture<TestComponent>;
  let context: TestComponent;
  let debugEle: DebugElement;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [BixiTableModule, BixiTimeModule, BrowserAnimationsModule, NzToolTipModule, NoopAnimationsModule],
      declarations: [TestComponent]
    });

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

  it('should render relative time', () => {
    // given
    context.cols = [
      {
        name: '相对时间',
        key: 'updatedAt',
        type: EColType.time
      }
    ];

    // when
    fixture.detectChanges();

    // then
    const bixiTimeComponent = debugEle.query(By.directive(BixiTimeComponent));
    expect(bixiTimeComponent.componentInstance.time).toEqual(timeRows[0].updatedAt);
  });

  it('should render custom format', () => {
    // given
    context.cols = [
      {
        name: '自定义格式',
        key: 'updatedAt',
        type: EColType.time,
        relative: false,
        format: 'YYYY/MM/DD'
      }
    ];

    // when
    fixture.detectChanges();

    // then
    const content = debugEle.query(By.directive(BixiTimeComponent)).query(By.css('.bixi-time')).nativeElement.textContent;
    expect(content).toContain('2020/07/09');
  });
});

const timeRows = [{
  updatedAt: 1594281389205
}];

@Component({
  template: `
    <bixi-table
      [rows]="rows"
      [cols]="cols"
    ></bixi-table>`
})
class TestComponent {
  rows = timeRows;
  cols: ICols = [];
}
