import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { CustomerSearchType } from '../../defs';
import { CustomerResultCardComponent } from './customer-result-card.component';

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

  const mockCustomer: CustomerSearchType = {
    loaded: true,
    created: new Date().toISOString(),
    customerType: 8,
    firstName: 'Unit',
    lastName: 'Test',
    communicationDetails: {
      email: 'unit@test.de',
    },
    features: [{ enabled: true, description: 'Great Feature' }],
  };

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

    fixture = TestBed.createComponent(CustomerResultCardComponent);
    component = fixture.componentInstance;

    component.customer = mockCustomer;
  });

  it('should be created', () => {
    expect(component instanceof CustomerResultCardComponent).toBeTruthy();
  });

  it('should show the customers name', () => {
    fixture.detectChanges();

    const name: HTMLSpanElement = fixture.debugElement.query(By.css('.heading > span')).nativeElement;

    expect(name.textContent).toContain(mockCustomer.firstName);
    expect(name.textContent).toContain(mockCustomer.lastName);
  });

  it('should show the customers created date', () => {
    fixture.detectChanges();

    const createdDate: HTMLSpanElement = fixture.debugElement.query(By.css('.date')).nativeElement;

    expect(createdDate.textContent).toContain(String(new Date(mockCustomer.created).getMonth() + 1));
    expect(createdDate.textContent).toContain(String(new Date(mockCustomer.created).getDate()));
  });

  it('should show the customers features', () => {
    fixture.detectChanges();

    const features: HTMLDivElement[] = fixture.debugElement.queryAll(By.css('.feature')).map((db) => db.nativeElement);

    const firstFeatures: HTMLSpanElement = fixture.debugElement.query(By.css('.feature > span ')).nativeElement;

    expect(features.length).toEqual(mockCustomer.features.length);
    expect(firstFeatures.textContent).toContain(mockCustomer.features[0].description);
  });
});
