AAdd test coverage of NotificationService 86/5886/1
authorelinuxhenrik <henrik.b.andersson@est.tech>
Wed, 14 Apr 2021 12:06:18 +0000 (14:06 +0200)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Wed, 14 Apr 2021 12:10:35 +0000 (14:10 +0200)
Change-Id: I4b9c2e66e1f75931eff60e68cc33214f9933e352
Issue-ID: NONRTRIC-474
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
webapp-frontend/src/app/services/ui/notification.service.spec.ts
webapp-frontend/src/app/services/ui/notification.service.ts

index f522b11..87757ef 100644 (file)
  * ========================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<ToastrService>;
 
+  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,
+    });
+  });
 });
index 09baa38..465a3dd 100644 (file)
  * ========================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);
   }
-
 }