From 82e05a5f48ff4e3f19e878ac22ee6b79f84c76fe Mon Sep 17 00:00:00 2001 From: elinuxhenrik Date: Wed, 14 Apr 2021 14:06:18 +0200 Subject: [PATCH] AAdd test coverage of NotificationService Change-Id: I4b9c2e66e1f75931eff60e68cc33214f9933e352 Issue-ID: NONRTRIC-474 Signed-off-by: elinuxhenrik --- .../app/services/ui/notification.service.spec.ts | 47 +++++++++++++++++----- .../src/app/services/ui/notification.service.ts | 27 ++++--------- 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/webapp-frontend/src/app/services/ui/notification.service.spec.ts b/webapp-frontend/src/app/services/ui/notification.service.spec.ts index f522b11..87757ef 100644 --- a/webapp-frontend/src/app/services/ui/notification.service.spec.ts +++ b/webapp-frontend/src/app/services/ui/notification.service.spec.ts @@ -18,22 +18,47 @@ * ========================LICENSE_END=================================== */ -import { TestBed } from '@angular/core/testing'; +import { async, TestBed } from "@angular/core/testing"; -import { NotificationService } from './notification.service'; -import { ToastrModule } from 'ngx-toastr'; +import { NotificationService } from "./notification.service"; +import { ToastrService } from "ngx-toastr"; -describe('NotificationService', () => { - beforeEach(() => TestBed.configureTestingModule({ - imports: [ToastrModule.forRoot()], - providers: [ - {provide: ToastrModule} - ] +describe("NotificationService", () => { + let service: NotificationService; + let toastrSpy: jasmine.SpyObj; + beforeEach(async(() => { + toastrSpy = jasmine.createSpyObj("ToastrService", [ + "success", + "warning", + "error", + ]); + + TestBed.configureTestingModule({ + providers: [{ provide: ToastrService, useValue: toastrSpy }], + }); + service = TestBed.inject(NotificationService); })); - it('should be created', () => { - const service: NotificationService = TestBed.inject(NotificationService); + it("should be created", () => { expect(service).toBeTruthy(); }); + + it("should open success with provided message and correct configuration", () => { + service.success("Success!"); + + expect(toastrSpy.success).toHaveBeenCalledWith("Success!", "", { + timeOut: 10000, + closeButton: true, + }); + }); + + it("should open error with provided message and correct configuration", () => { + service.error("Error!"); + + expect(toastrSpy.error).toHaveBeenCalledWith("Error!", "", { + disableTimeOut: true, + closeButton: true, + }); + }); }); diff --git a/webapp-frontend/src/app/services/ui/notification.service.ts b/webapp-frontend/src/app/services/ui/notification.service.ts index 09baa38..465a3dd 100644 --- a/webapp-frontend/src/app/services/ui/notification.service.ts +++ b/webapp-frontend/src/app/services/ui/notification.service.ts @@ -18,41 +18,30 @@ * ========================LICENSE_END=================================== */ -import { Injectable } from '@angular/core'; -import { ToastrService } from 'ngx-toastr'; +import { Injectable } from "@angular/core"; +import { ToastrService } from "ngx-toastr"; @Injectable({ - providedIn: 'root' + providedIn: "root", }) export class NotificationService { - - constructor(public toastr: ToastrService) { } + constructor(public toastr: ToastrService) {} successConfig = { timeOut: 10000, - closeButton: true - }; - - warningConfig = { - disableTimeOut: true, - closeButton: true + closeButton: true, }; errorConfig = { disableTimeOut: true, - closeButton: true + closeButton: true, }; success(msg: string) { - this.toastr.success(msg, '', this.successConfig); - } - - warn(msg: string) { - this.toastr.warning(msg, '', this.warningConfig); + this.toastr.success(msg, "", this.successConfig); } error(msg: string) { - this.toastr.error(msg, '', this.errorConfig); + this.toastr.error(msg, "", this.errorConfig); } - } -- 2.16.6