<commit_before>import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { DebugElement } from '@angular/core';
import { By } from '@angular/platform-browser';

import ProjectListComponent from './project-list.component';
import { ProjectSearchPipe } from '../../common/pipes';
import { PROJECTS, FakeProjectService } from '../../common/testing/fake-project.service.spec';
import { ProjectService } from '../../common/services';
import PageHeaderComponent from "../../common/components/page-header/page-header.component";
import { RouterLinkStubDirective, RouterOutletStubComponent, RouterStub } from "../../common/testing/router-stubs.spec";
import { Router } from "@angular/router";
import {FormsModule, ReactiveFormsModule} from "@angular/forms";

let comp: ProjectListComponent;
let fixture: ComponentFixture<ProjectListComponent>;
let router: RouterStub;
let links: RouterLinkStubDirective[];
let linkDes: DebugElement[];

function initTestBed() {
  router = new RouterStub();

  TestBed.configureTestingModule({
    imports: [FormsModule, ReactiveFormsModule],
    declarations: [ ProjectListComponent, ProjectSearchPipe, PageHeaderComponent, RouterLinkStubDirective, RouterOutletStubComponent  ],
    // schemas:      [ NO_ERRORS_SCHEMA ]
  }).overrideComponent(ProjectListComponent, {
    set: {
      providers: [
        { provide: Router, useValue: router },
        { provide: ProjectService, useClass: FakeProjectService }
      ]
    }
  }); // not necessary with webpack .compileComponents();

  fixture = TestBed.createComponent(ProjectListComponent);
  comp = fixture.componentInstance; // ProjectListComponent test instance

  fixture.detectChanges();
  return fixture.whenStable().then(() => fixture.detectChanges());
}

describe('ProjectListComponent', () => {
  beforeEach(async(() => initTestBed()));

  it('can instantiate it', () => expect(comp).not.toBeNull());

  describe('---YES---', () => {

    beforeEach(() => {
      fixture.detectChanges();
      linkDes = fixture.debugElement.queryAll(By.directive(RouterLinkStubDirective));
      links = linkDes.map(de => de.injector.get(RouterLinkStubDirective) as RouterLinkStubDirective);
    });

    it('can display the projectlist page', () => {
      const element: DebugElement = fixture.debugElement;

      const title: DebugElement[] = element.queryAll(By.css('h1'));
      expect(title.length).toBe(1);
      expect(title[0].nativeElement.textContent.trim()).toBe('Projects');

      fixture.detectChanges();

      const message: DebugElement[] = element.queryAll(By.css('small'));
      expect(message.length).toBe(1); //because pageHeader has a <small> tag in its template
      expect(message[0].nativeElement.textContent.trim()).toBe('');
    });

    it('should check if projects are displayed correctly', () => {
      fixture.detectChanges(); // trigger data binding
      const element: DebugElement = fixture.debugElement;

      const values: DebugElement[] = element.queryAll(By.css('a'));
      expect(values.length).toBe(9);

      const projectHeaders: DebugElement[] = element.queryAll(By.css('h4.media-heading a'));
      expect(projectHeaders.length).toBe(6);
      expect(projectHeaders[1].nativeElement.textContent).toBe(PROJECTS[0].name);
      expect(projectHeaders[3].nativeElement.textContent).toBe(PROJECTS[1].name);
      expect(projectHeaders[5].nativeElement.textContent).toBe(PROJECTS[2].name);
      // expect(projectHeaders[1].nativeElement.href).toBe(baseUrl + PROJECTS[0]._id);
      // expect(projectHeaders[3].nativeElement.href).toBe(baseUrl + PROJECTS[1]._id);
      // expect(projectHeaders[5].nativeElement.href).toBe(baseUrl + PROJECTS[2]._id);
      //because there are also github icons
      expect(projectHeaders[0].nativeElement.href).toBe(PROJECTS[0].url);
      expect(projectHeaders[2].nativeElement.href).toBe(PROJECTS[1].url);
      expect(projectHeaders[4].nativeElement.href).toBe(PROJECTS[2].url);

      const shortDescriptions: DebugElement[] = element.queryAll(By.css('p.name'));
      expect(shortDescriptions.length).toBe(3);
      expect(shortDescriptions[0].nativeElement.textContent).toBe(PROJECTS[0].shortDescription);
      expect(shortDescriptions[1].nativeElement.textContent).toBe(PROJECTS[1].shortDescription);
      expect(shortDescriptions[2].nativeElement.textContent).toBe(PROJECTS[2].shortDescription);

      const tags: DebugElement[] = element.queryAll(By.css('span.tag.tag-pill.tag-warning'));
      expect(tags.length).toBe(9);
      expect(tags[0].nativeElement.textContent.trim()).toBe(PROJECTS[0].tags[0]);
      expect(tags[1].nativeElement.textContent.trim()).toBe(PROJECTS[0].tags[1]);
      expect(tags[2].nativeElement.textContent.trim()).toBe(PROJECTS[0].tags[2]);
      expect(tags[3].nativeElement.textContent.trim()).toBe(PROJECTS[1].tags[0]);
      expect(tags[4].nativeElement.textContent.trim()).toBe(PROJECTS[1].tags[1]);
      expect(tags[5].nativeElement.textContent.trim()).toBe(PROJECTS[1].tags[2]);
      expect(tags[6].nativeElement.textContent.trim()).toBe(PROJECTS[2].tags[0]);
      expect(tags[7].nativeElement.textContent.trim()).toBe(PROJECTS[2].tags[1]);
      expect(tags[8].nativeElement.textContent.trim()).toBe(PROJECTS[2].tags[2]);

      const timelineHeader: DebugElement = element.query(By.css('h3#timeline'));
      expect(timelineHeader.nativeElement.textContent).toBe(comp.sidebarTitle);

      const timelineTitles: DebugElement[] = element.queryAll(By.css('h4.timeline-title'));
      expect(timelineTitles.length).toBe(5);
      expect(timelineTitles[0].nativeElement.textContent).toBe(comp.sidebar.timeline[0].title);
      expect(timelineTitles[1].nativeElement.textContent).toBe(comp.sidebar.timeline[1].title);
      expect(timelineTitles[2].nativeElement.textContent).toBe(comp.sidebar.timeline[2].title);
      expect(timelineTitles[3].nativeElement.textContent).toBe(comp.sidebar.timeline[3].title);
      expect(timelineTitles[4].nativeElement.textContent).toBe(comp.sidebar.timeline[4].title);

      const timelineTexts: DebugElement[] = element.queryAll(By.css('div.timeline-body p'));
      expect(timelineTexts.length).toBe(5);
      expect(timelineTexts[0].nativeElement.textContent).toBe(comp.sidebar.timeline[0].body);
      expect(timelineTexts[1].nativeElement.textContent).toBe(comp.sidebar.timeline[1].body);
      expect(timelineTexts[2].nativeElement.textContent).toBe(comp.sidebar.timeline[2].body);
      expect(timelineTexts[3].nativeElement.textContent).toBe(comp.sidebar.timeline[3].body);
      expect(timelineTexts[4].nativeElement.textContent).toBe(comp.sidebar.timeline[4].body);

      // const timelineIconBadge = element.query(By.css('div.timeline-badge.badge i'));
      // expect(timelineIconBadge.nativeElement.classes['fa fa-' + comp.sidebar.timeline[0].icon]).toBe(true, 'it is ok');
      // const timelineIconDanger = element.query(By.css('div.timeline-badge.danger i'));
      // expect(timelineIconDanger.nativeElement.css).toBe('fa fa-' + comp.sidebar.timeline[1].icon);
      // const timelineIconWarning = element.query(By.css('div.timeline-badge.warning i'));
      // expect(timelineIconWarning.nativeElement.css).toBe('fa fa-' + comp.sidebar.timeline[2].icon);
      // const timelineIconInfo = element.query(By.css('div.timeline-badge.info i'));
      // expect(timelineIconInfo.nativeElement.css).toBe('fa fa-' + comp.sidebar.timeline[3].icon);
      // const timelineIconSuccess = element.query(By.css('div.timeline-badge.success i'));
      // expect(timelineIconSuccess.nativeElement.css).toBe('fa fa-' + comp.sidebar.timeline[4].icon);
    });
  });
});<commit_msg>project list unit test improvements #3<commit_after>import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { DebugElement } from '@angular/core';
import { By } from '@angular/platform-browser';

import ProjectListComponent from './project-list.component';
import { ProjectSearchPipe } from '../../common/pipes';
import { PROJECTS, FakeProjectService } from '../../common/testing/fake-project.service.spec';
import { ProjectService } from '../../common/services';
import PageHeaderComponent from "../../common/components/page-header/page-header.component";
import { RouterLinkStubDirective, RouterOutletStubComponent, RouterStub } from "../../common/testing/router-stubs.spec";
import { Router } from "@angular/router";
import {FormsModule, ReactiveFormsModule} from "@angular/forms";

let comp: ProjectListComponent;
let fixture: ComponentFixture<ProjectListComponent>;
let router: RouterStub;
let links: RouterLinkStubDirective[];
let linkDes: DebugElement[];

const BASEURL= '/projects/';

function initTestBed() {
  router = new RouterStub();

  TestBed.configureTestingModule({
    imports: [FormsModule, ReactiveFormsModule],
    declarations: [ ProjectListComponent, ProjectSearchPipe, PageHeaderComponent, RouterLinkStubDirective, RouterOutletStubComponent  ],
    // schemas:      [ NO_ERRORS_SCHEMA ]
  }).overrideComponent(ProjectListComponent, {
    set: {
      providers: [
        { provide: Router, useValue: router },
        { provide: ProjectService, useClass: FakeProjectService }
      ]
    }
  }); // not necessary with webpack .compileComponents();

  fixture = TestBed.createComponent(ProjectListComponent);
  comp = fixture.componentInstance; // ProjectListComponent test instance

  fixture.detectChanges();
  return fixture.whenStable().then(() => fixture.detectChanges());
}

describe('ProjectListComponent', () => {
  beforeEach(async(() => initTestBed()));

  it('can instantiate it', () => expect(comp).not.toBeNull());

  describe('---YES---', () => {

    beforeEach(() => {
      fixture.detectChanges();
      linkDes = fixture.debugElement.queryAll(By.directive(RouterLinkStubDirective));
      links = linkDes.map(de => de.injector.get(RouterLinkStubDirective) as RouterLinkStubDirective);
    });

    it('can display the projectlist page', () => {
      const element: DebugElement = fixture.debugElement;

      const title: DebugElement[] = element.queryAll(By.css('h1'));
      expect(title.length).toBe(1);
      expect(title[0].nativeElement.textContent.trim()).toBe('Projects');

      const message: DebugElement[] = element.queryAll(By.css('small'));
      expect(message.length).toBe(1); //because pageHeader has a <small> tag in its template
      expect(message[0].nativeElement.textContent.trim()).toBe('');
    });

    it('should check if projects are displayed correctly', () => {
      const element: DebugElement = fixture.debugElement;

      const values: DebugElement[] = element.queryAll(By.css('a'));
      expect(values.length).toBe(9);

      // const projectLeftHrefs: DebugElement[] = element.queryAll(By.css('div.media-left a'));
      // expect(projectLeftHrefs.length).toBe(3);
      // expect(projectLeftHrefs[0].nativeElement.getAttribute('href')).toBe(BASEURL + PROJECTS[0]._id);
      // expect(projectLeftHrefs[1].nativeElement.getAttribute('href')).toBe(BASEURL + PROJECTS[1]._id);
      // expect(projectLeftHrefs[2].nativeElement.getAttribute('href')).toBe(BASEURL + PROJECTS[2]._id);

      const projectLeftImages: DebugElement[] = element.queryAll(By.css('img.media-object'));
      expect(projectLeftImages.length).toBe(3);
      expect(projectLeftImages[0].nativeElement.getAttribute('src')).toBe(PROJECTS[0].iconPath);
      expect(projectLeftImages[1].nativeElement.getAttribute('src')).toBe(PROJECTS[1].iconPath);
      expect(projectLeftImages[2].nativeElement.getAttribute('src')).toBe(PROJECTS[2].iconPath);

      const projectHeaders: DebugElement[] = element.queryAll(By.css('h4.media-heading a'));
      expect(projectHeaders.length).toBe(6);
      expect(projectHeaders[1].nativeElement.textContent).toBe(PROJECTS[0].name);
      expect(projectHeaders[3].nativeElement.textContent).toBe(PROJECTS[1].name);
      expect(projectHeaders[5].nativeElement.textContent).toBe(PROJECTS[2].name);
      // expect(projectHeaders[1].nativeElement.getAttribute('href')).toBe(BASEURL + PROJECTS[0]._id);
      // expect(projectHeaders[3].nativeElement.getAttribute('href')).toBe(BASEURL + PROJECTS[1]._id);
      // expect(projectHeaders[5].nativeElement.getAttribute('href')).toBe(BASEURL + PROJECTS[2]._id);
      // because there are also github icons
      // expect(projectHeaders[0].nativeElement.getAttribute('src')).toBe(PROJECTS[0].iconPath);
      // expect(projectHeaders[2].nativeElement.getAttribute('src')).toBe(PROJECTS[1].iconPath);
      // expect(projectHeaders[4].nativeElement.getAttribute('src')).toBe(PROJECTS[2].iconPath);
      expect(projectHeaders[0].nativeElement.getAttribute('href')).toBe(PROJECTS[0].url);
      expect(projectHeaders[2].nativeElement.getAttribute('href')).toBe(PROJECTS[1].url);
      expect(projectHeaders[4].nativeElement.getAttribute('href')).toBe(PROJECTS[2].url);

      const shortDescriptions: DebugElement[] = element.queryAll(By.css('p.name'));
      expect(shortDescriptions.length).toBe(3);
      expect(shortDescriptions[0].nativeElement.textContent).toBe(PROJECTS[0].shortDescription);
      expect(shortDescriptions[1].nativeElement.textContent).toBe(PROJECTS[1].shortDescription);
      expect(shortDescriptions[2].nativeElement.textContent).toBe(PROJECTS[2].shortDescription);

      const tags: DebugElement[] = element.queryAll(By.css('span.tag.tag-pill.tag-warning'));
      expect(tags.length).toBe(9);
      expect(tags[0].nativeElement.textContent.trim()).toBe(PROJECTS[0].tags[0]);
      expect(tags[1].nativeElement.textContent.trim()).toBe(PROJECTS[0].tags[1]);
      expect(tags[2].nativeElement.textContent.trim()).toBe(PROJECTS[0].tags[2]);
      expect(tags[3].nativeElement.textContent.trim()).toBe(PROJECTS[1].tags[0]);
      expect(tags[4].nativeElement.textContent.trim()).toBe(PROJECTS[1].tags[1]);
      expect(tags[5].nativeElement.textContent.trim()).toBe(PROJECTS[1].tags[2]);
      expect(tags[6].nativeElement.textContent.trim()).toBe(PROJECTS[2].tags[0]);
      expect(tags[7].nativeElement.textContent.trim()).toBe(PROJECTS[2].tags[1]);
      expect(tags[8].nativeElement.textContent.trim()).toBe(PROJECTS[2].tags[2]);

      const timelineHeader: DebugElement = element.query(By.css('h3#timeline'));
      expect(timelineHeader.nativeElement.textContent).toBe(comp.sidebarTitle);

      const timelineTitles: DebugElement[] = element.queryAll(By.css('h4.timeline-title'));
      expect(timelineTitles.length).toBe(5);
      expect(timelineTitles[0].nativeElement.textContent).toBe(comp.sidebar.timeline[0].title);
      expect(timelineTitles[1].nativeElement.textContent).toBe(comp.sidebar.timeline[1].title);
      expect(timelineTitles[2].nativeElement.textContent).toBe(comp.sidebar.timeline[2].title);
      expect(timelineTitles[3].nativeElement.textContent).toBe(comp.sidebar.timeline[3].title);
      expect(timelineTitles[4].nativeElement.textContent).toBe(comp.sidebar.timeline[4].title);

      const timelineTexts: DebugElement[] = element.queryAll(By.css('div.timeline-body p'));
      expect(timelineTexts.length).toBe(5);
      expect(timelineTexts[0].nativeElement.textContent).toBe(comp.sidebar.timeline[0].body);
      expect(timelineTexts[1].nativeElement.textContent).toBe(comp.sidebar.timeline[1].body);
      expect(timelineTexts[2].nativeElement.textContent).toBe(comp.sidebar.timeline[2].body);
      expect(timelineTexts[3].nativeElement.textContent).toBe(comp.sidebar.timeline[3].body);
      expect(timelineTexts[4].nativeElement.textContent).toBe(comp.sidebar.timeline[4].body);

      const timelineIconBadge = element.query(By.css('div.timeline-badge.badge i'));
      expect(timelineIconBadge.nativeElement.getAttribute('class')).toBe('fa fa-' + comp.sidebar.timeline[0].icon);
      const timelineIconDanger = element.query(By.css('div.timeline-badge.danger i'));
      expect(timelineIconDanger.nativeElement.getAttribute('class')).toBe('fa fa-' + comp.sidebar.timeline[1].icon);
      const timelineIconWarning = element.query(By.css('div.timeline-badge.warning i'));
      expect(timelineIconWarning.nativeElement.getAttribute('class')).toBe('fa fa-' + comp.sidebar.timeline[2].icon);
      const timelineIconInfo = element.query(By.css('div.timeline-badge.info i'));
      expect(timelineIconInfo.nativeElement.getAttribute('class')).toBe('fa fa-' + comp.sidebar.timeline[3].icon);
      const timelineIconSuccess = element.query(By.css('div.timeline-badge.success i'));
      expect(timelineIconSuccess.nativeElement.getAttribute('class')).toBe('fa fa-' + comp.sidebar.timeline[4].icon);
    });
  });
});
{'fraction_non_alphanumeric': 0.16367145097093233, 'fraction_numerical': 0.012121577614280545, 'mean_word_length': 4.962962962962963, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1880367', 'n_tokens_mistral': 4983, 'n_tokens_neox': 4980, 'n_words': 597}