mercredi 30 septembre 2020

Can anyone help me get 100% code coverage with this code? Simply checking whether the function has been called will also do

I have no idea how to access the remaining line of code. I simply need to get a full code coverage. Checking if the function has been called (toHaveBeenCalled() ) will also do.

Pasting in the TypeScript file and my Spec file. My current code-coverage is as follows:

TOTAL: 2 FAILED, 3 SUCCESS

================== Coverage summary ==================

Statements : 30.34% (27/89)

Branches : 0% (0/22)

Functions : 26.92% (7/26)

Lines : 29.07% (25/86)

=====================================================

update-notification.component.ts

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
import { NotificationService } from '../notification.service';
import { ActivatedRoute, Router } from '@angular/router';
import { ToastrService } from 'ngx-toastr';

@Component({
  selector: 'app-update-notification',
  templateUrl: './update-notification.component.html',
  styleUrls: ['./update-notification.component.scss']
})
export class UpdateNotificationComponent implements OnInit {
  notificationId: any;
  notificationDetails: any;
  parentNotifications: any[];
  editorInstance = ClassicEditor;
  ckEditorConfig = {
    placeholder: 'Add notification description'
  }
  editNotificationGroup = new FormGroup({
    title: new FormControl('', Validators.required),
    parentNotification: new FormControl(),
    description: new FormControl()
  })
  constructor(
    private activeRoute: ActivatedRoute,
    public notificationService: NotificationService,
    private router: Router,
    private toastr: ToastrService
  ) { }

  ngOnInit() {
    this.getParentNotificationsForDropdown();
    this.notificationId = this.activeRoute.snapshot.params.id;
    this.notificationService.getNotificationDetailsById(this.notificationId).
      subscribe((res: any) => {
        this.notificationDetails = res[0];
        this.editNotificationGroup.get('title').setValue(this.notificationDetails.notification_title);
        this.editNotificationGroup.get('description').setValue(this.notificationDetails.notification_message);
        this.editNotificationGroup.get('parentNotification').setValue(this.notificationDetails.linked_notification_id);
      }, error => {
        console.log(error);
        this.editNotificationGroup.get('title').setValue('title');
        this.editNotificationGroup.get('description').setValue('<strong>desc</strong> of test');
        this.editNotificationGroup.get('parentNotification').setValue('null');
      })
  }
  getParentNotificationsForDropdown() {
    this.notificationService.getTradeNotifications().
      subscribe((res: any) => {
        this.parentNotifications = res;
      }, error => {
        console.log('get parent notifications failed', error);
      })
  }

  submitEditNotification() {
    this.notificationService.updatedNotification(this.editNotificationGroup.value, this.notificationId).
      subscribe((res: any) => {
        console.log(res);
        this.toastr.success('Notification updated successfully', 'Notification Blast');
        this.router.navigate(['notifications/list']);
      }, error => {
        console.log(error);
      })
  }

  // if 400 then show error on popup, or else something went wrong or redirect on page

}

My test cases..

update-notificatoin.component.spec.ts

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { NotificationService } from '../notification.service';
import { ActivatedRoute } from '@angular/router';
import { Router } from '@angular/router';
import { ToastrService } from 'ngx-toastr';
import { ClassicEditor } from '@ckeditor/ckeditor5-build-classic';
import { UpdateNotificationComponent } from './update-notification.component';

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

  beforeEach(() => {
    const notificationServiceStub = () => ({
      getNotificationDetailsById: notificationId => ({ subscribe: f => f({}) }),
      getTradeNotifications: () => ({ subscribe: f => f({}) }),
      updatedNotification: (value, notificationId) => ({
        subscribe: f => f({})
      })
    });
    const activatedRouteStub = () => ({ snapshot: { params: { id: {} } } });
    const routerStub = () => ({ navigate: array => ({}) });
    const toastrServiceStub = () => ({ success: (string, string1) => ({}) });
    TestBed.configureTestingModule({
      schemas: [NO_ERRORS_SCHEMA],
      declarations: [UpdateNotificationComponent],
      providers: [
        { provide: NotificationService, useFactory: notificationServiceStub },
        { provide: ActivatedRoute, useFactory: activatedRouteStub },
        { provide: Router, useFactory: routerStub },
        { provide: ToastrService, useFactory: toastrServiceStub }
      ]
    });
    fixture = TestBed.createComponent(UpdateNotificationComponent);
    component = fixture.componentInstance;
  });

  it('can load instance', () => {
    expect(component).toBeTruthy();
  });

  it(`editorInstance has default value`, () => {
    expect(component.editorInstance).toEqual(ClassicEditor);
  });

  describe('ngOnInit', () => {
    it('makes expected calls', () => {
      const notificationServiceStub: NotificationService = fixture.debugElement.injector.get(
        NotificationService
      );
      spyOn(component, 'getParentNotificationsForDropdown').and.callThrough();
      spyOn(
        notificationServiceStub,
        'getNotificationDetailsById'
      ).and.callThrough();
      component.ngOnInit();
      expect(component.getParentNotificationsForDropdown).toHaveBeenCalled();
      expect(
        notificationServiceStub.getNotificationDetailsById
      ).toHaveBeenCalled();
    });
  });

  describe('getParentNotificationsForDropdown', () => {
    it('makes expected calls', () => {
      const notificationServiceStub: NotificationService = fixture.debugElement.injector.get(
        NotificationService
      );
      spyOn(notificationServiceStub, 'getTradeNotifications').and.callThrough();
      component.getParentNotificationsForDropdown();
      expect(notificationServiceStub.getTradeNotifications).toHaveBeenCalled();
    });
  });

  describe('submitEditNotification', () => {
    it('makes expected calls', () => {
      const notificationServiceStub: NotificationService = fixture.debugElement.injector.get(
        NotificationService
      );
      const routerStub: Router = fixture.debugElement.injector.get(Router);
      const toastrServiceStub: ToastrService = fixture.debugElement.injector.get(
        ToastrService
      );
      spyOn(notificationServiceStub, 'updatedNotification').and.callThrough();
      spyOn(routerStub, 'navigate').and.callThrough();
      spyOn(toastrServiceStub, 'success').and.callThrough();
      component.submitEditNotification();
      expect(notificationServiceStub.updatedNotification).toHaveBeenCalled();
      expect(routerStub.navigate).toHaveBeenCalled();
      expect(toastrServiceStub.success).toHaveBeenCalled();
    });
  });
});

enter image description here

Aucun commentaire:

Enregistrer un commentaire