Comment écrire des scénarios de test unitaire angulaire 6

Je suis nouveau à l'unité d'écriture de scénario de test angulaire. J'ai écrit quelques scénarios de test unitaire pour mon composant, je ne sais pas qu'elle est correcte ou pas. pouvez-vous s'il vous plaît tout on m'aider sur ce point. Je suis nouveau à l'unité d'écriture de scénario de test angulaire. J'ai écrit quelques scénarios de test unitaire pour mon composant, je ne sais pas qu'elle est correcte ou pas. pouvez-vous s'il vous plaît tout on m'aider sur ce point.

Mon AdminComponent.ts

import { Title } from '@angular/platform-browser';
import { Router } from '@angular/router';
import { LoginService } from '../login/services/login.service';
import { Component, OnInit } from '@angular/core';
import {
  BreakpointState,
  Breakpoints,
  BreakpointObserver
} from '@angular/cdk/layout';
import { environment } from './../../environments/environment';

@Component({
  selector: 'app-admin',
  templateUrl: './admin.component.html',
  styleUrls: ['./admin.component.css']
})
export class AdminComponent implements OnInit {
  isMobile = false;
  sideNavOpen = false;

  reassign = false;
  reassignEnabled = false;

  aggregate = false;
  aggregateEnabled = false;

  events = false;
  eventsEnabled = false;

  properties = false;
  propertiesEnabled = false;

  isUserLoggedIn = false;
  admin = environment.admin;
  constructor(
    private breakpointObserver: BreakpointObserver,
    private router: Router,
    private loginService: LoginService,
    private title: Title
  ) {}

  ngOnInit(): void {
    this.breakpointObserver
      .observe(Breakpoints.Handset)
      .subscribe((state: BreakpointState) => {
        this.isMobile = state.matches;
      });
    this.reassignEnabled = environment.accessRoles.reassign;
    this.aggregateEnabled = environment.accessRoles.aggregate;
    this.eventsEnabled = environment.accessRoles.events;
    this.propertiesEnabled = environment.accessRoles.properties;
    this.isUserLoggedIn = this.loginService.isLoggedIn;
    console.log('user Login Status', this.isUserLoggedIn);
    this.sideNavOpen = !this.isMobile;
    this.reassign = this.isUserLoggedIn;
    this.title.setTitle('Reassign Labels');
  }

  onClick(value: string) {
    value = value.toLowerCase();
    this.reassign = value === 'reassign';
    this.aggregate = value === 'aggregate';
    this.events = value === 'events';
    this.properties = value === 'properties';
    if (this.isMobile) {
      this.sideNavOpen = !this.sideNavOpen;
    }
    if (value === 'logout') {
      this.loginService.logout();
      this.router.navigate(['/login']);
    }
    switch (value) {
      case 'reassign':
        this.title.setTitle('Reassign Labels');
        break;
      case 'aggregate':
        this.title.setTitle('Aggregate Labels');
        break;
      case 'events':
        this.title.setTitle('Events');
        break;
        case 'properties':
          this.title.setTitle('Propeties');
          break;
    }
  }
}

Mon AdminComponent.Spec.ts

import { PropertiesComponent } from './../properties/properties.component';
import { EventsComponent } from './../events/events.component';
import { AggregateComponent } from './../aggregate/aggregate.component';
import { ReassignComponent } from './../reassign/reassign.component';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { AdminComponent } from './admin.component';
import { CommonTestModules } from './../shared/common.test.modules';
import { AlertComponent } from '../alert/alert.component';

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

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [CommonTestModules],
      declarations: [
        AdminComponent,
        ReassignComponent,
        AggregateComponent,
        EventsComponent,
        AlertComponent,
        PropertiesComponent
      ]
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(AdminComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should call reassign component', () => {
    component.onClick('reassign');
  });

  it('should call aggregate component', () => {
    component.onClick('aggregate');
  });

  it('should call events component', () => {
    component.onClick('events');
  });

  it('should call properties component', () => {
    component.onClick('properties');
  });
});
-1
2019-09-17 10:48:35
source
0 réponses

Voir d'autres questions sur les étiquettes