All files / app/utils jasmine.util.ts

100% Statements 36/36
83.33% Branches 10/12
100% Functions 23/23
100% Lines 30/30

Kamil Chmielowski

Web / Angular

Jasmine code coverage report for cv app.

You can contact with me by email: kamilchmielowski94@gmail.com or my page.

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100                          106x                 84x       2x 2x       8x 176x 41x 8x       6x       12x       8x       1x 4x         1x 2x       1x   2x 3x         4x 4x 16x         5x 5x       3x   3x 3x   3x         3x   66x 3x          
import { By } from '@angular/platform-browser';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ChangeDetectorRef, DebugElement } from '@angular/core';
 
import { SvgIconRegistryService } from 'angular-svg-icon';
import { TranslateTestingModule } from 'ngx-translate-testing';
 
import { appIcons } from '../app-icons-map';
import { routes } from '../app-routing.module';
import { ValidatorUtil } from './validator.util';
 
export class JasmineUtil {
  static moduleWithTranslations(modules: any[]): any[] {
    return [
      ...modules,
      TranslateTestingModule
        .withTranslations('en', require('../../assets/i18n/en.json'))
        .withTranslations('pl', require('../../assets/i18n/pl.json')),
    ];
  }
 
  static svgIconSpyProvider(): any {
    return { provide: SvgIconRegistryService, useValue: jasmine.createSpyObj(['getSvgByName']) }
  }
 
  static markForCheckSpy(fixture: ComponentFixture<any>): jasmine.Spy<any> {
    const changeDetectorRef = fixture.debugElement.injector.get(ChangeDetectorRef);
    return spyOn(changeDetectorRef.constructor.prototype, 'markForCheck');
  }
 
  static shouldDisplayExistingIcons(fixture: ComponentFixture<any>): void {
    const svgIconsEl = fixture.debugElement.queryAll(By.css('svg-icon'));
    const iconNames = appIcons.map(icons => icons[0]);
    const everyIconsExist = svgIconsEl.every(icon => iconNames.includes(icon.componentInstance.name));
    expect(everyIconsExist).toBeTrue();
  }
 
  static shouldSetTextOrAriaLabelToClickableElement(fixture: ComponentFixture<any>, selector = '.clickable'): void {
    const clickableEl = [
      ...fixture.debugElement.queryAll(By.css('button')),
      ...fixture.debugElement.queryAll(By.css(selector)),
    ];
    expect(clickableEl.every(el => !!el.nativeElement.textContent || !!el.nativeElement.ariaLabel)).toBeTrue();
  }
 
  static shouldRenderMinElements(fixture: ComponentFixture<any>, selector: string, min: number): void {
    expect(fixture.debugElement.queryAll(By.css(selector)).length).withContext(`Missing ${selector} selector`).toBeGreaterThanOrEqual(min);
  }
 
  static shouldRenderComponents(components: { [key: string]: DebugElement[] }): void {
    Object.entries(components).forEach(([key, component]) => {
      expect(component.length === 1).withContext(`missing ${key} component`).toBeTrue();
    });
  }
 
  static shouldSetValidHrefToElement(fixture: ComponentFixture<any>, selector = '.clickable'): void {
    const elements = fixture.debugElement.queryAll(By.css(selector));
    expect(elements.every(el => ValidatorUtil.isValidHttpUrl(el.nativeElement.href))).toBeTrue();
  }
 
  static shouldSetValidRouterLinkToElement(fixture: ComponentFixture<any>, selector = '.clickable'): void {
    const elements = fixture.debugElement.queryAll(By.css(selector))
 
    expect(elements.every(el => routes
      .some(route => route.path === el.attributes['routerLink'] || el.attributes['routerLink'] === '/')
    )).toBeTrue();
  }
 
  static shouldSetValidHrefToComponent(fixture: ComponentFixture<any>, selector = '.clickable', propertyName = 'href'): void {
    const elements = fixture.debugElement.queryAll(By.css(selector))
    expect(elements.every(el => {
      return el.componentInstance[propertyName] ? ValidatorUtil.isValidHttpUrl(el.componentInstance[propertyName]) : true;
    })).toBeTrue();
  }
 
  static shouldRenderNgContent(component: any, text = 'ng-content test'): void {
    const testFixture = TestBed.createComponent(component);
    expect(testFixture.nativeElement.textContent).toContain(text);
  }
 
  static shouldRenderOneWrapperElement(fixture: ComponentFixture<any>, selector: string): void {
    const elements = fixture.debugElement.queryAll(By.css(selector));
 
    expect(elements.length).withContext('too many elements').toBe(1);
    expect(fixture.debugElement.nativeElement.firstElementChild.localName)
      .withContext('component doesn\'t start with html mark').toEqual(selector);
    expect(fixture.debugElement.children.length)
      .withContext('doesn\'t wrap a html content').toBe(1);
  }
 
  static sectionTitleComponentUnitTests() {
    return {
      existingIcon: (element: DebugElement) => {
        const iconNames = appIcons.map(icons => icons[0]);
        expect(iconNames.includes(element.componentInstance.icon)).toBeTrue();
      }
    }
  }
}