Add creation test to PolicyControlComponent 60/5460/2
authorelinuxhenrik <henrik.b.andersson@est.tech>
Mon, 18 Jan 2021 16:06:18 +0000 (17:06 +0100)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Tue, 19 Jan 2021 07:05:45 +0000 (08:05 +0100)
Add test that PolicyControlComponent is created.

Cleaned away non functional sorting in PolicyTypeDataSource and made it
injectable.

Changed so PolicyTypeDataSource is injected in PolicyControlComponent
instead of created with new.

Change-Id: I8f6c80c37b9491b3508e34accecb669905376b38
Issue-ID: NONRTRIC-383
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
.gitignore
webapp-frontend/src/app/mock/policytypes.json
webapp-frontend/src/app/policy-control/policy-control.component.spec.ts
webapp-frontend/src/app/policy-control/policy-control.component.ts
webapp-frontend/src/app/policy-control/policy-type.datasource.ts

index 7db901f..6926ab1 100644 (file)
@@ -50,6 +50,7 @@ target
 
 # OSx cruft
 **/.DS_Store
+.history
 
 # documentation
 .tox
index 14b835a..89a7d0b 100644 (file)
@@ -1,6 +1,10 @@
 [
-    {
-      "name": "1",
-      "schema": "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"description\":\"Type 1 policy type\",\"additionalProperties\":false,\"title\":\"1\",\"type\":\"object\",\"properties\":{\"qosObjectives\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"priorityLevel\":{\"type\":\"number\"}},\"required\":[\"priorityLevel\"]},\"scope\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"qosId\":{\"type\":\"string\"},\"ueId\":{\"type\":\"string\"}},\"required\":[\"ueId\",\"qosId\"]}},\"required\":[\"scope\",\"qosObjectives\"]}"
-    }
+  {
+    "name": "2",
+    "schema": "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"description\":\"Type 2 policy type\",\"additionalProperties\":false,\"title\":\"2\",\"type\":\"object\",\"properties\":{\"qosObjectives\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"priorityLevel\":{\"type\":\"number\"}},\"required\":[\"priorityLevel\"]},\"scope\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"qosId\":{\"type\":\"string\"},\"ueId\":{\"type\":\"string\"}},\"required\":[\"ueId\",\"qosId\"]}},\"required\":[\"scope\",\"qosObjectives\"]}"
+  },
+  {
+    "name": "1",
+    "schema": "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"description\":\"Type 1 policy type\",\"additionalProperties\":false,\"title\":\"1\",\"type\":\"object\",\"properties\":{\"qosObjectives\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"priorityLevel\":{\"type\":\"number\"}},\"required\":[\"priorityLevel\"]},\"scope\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"qosId\":{\"type\":\"string\"},\"ueId\":{\"type\":\"string\"}},\"required\":[\"ueId\",\"qosId\"]}},\"required\":[\"scope\",\"qosObjectives\"]}"
+  }
   ]
\ No newline at end of file
index 094ff45..a817cf1 100644 (file)
  * ========================LICENSE_END===================================
  */
 import { async, ComponentFixture, TestBed } from '@angular/core/testing';
+import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
+import { MatDialog } from '@angular/material/dialog';
+import { MatIconModule, MatSort, MatTableModule } from '@angular/material';
+import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
+import { of } from 'rxjs';
 
+import { NotificationService } from '../services/ui/notification.service';
 import { PolicyControlComponent } from './policy-control.component';
-import { MatIconModule, MatTableModule } from '@angular/material';
-import { PolicyType } from '../interfaces/policy.types';
+import { PolicyTypeDataSource } from './policy-type.datasource';
+import { UiService } from '../services/ui/ui.service';
 
 describe('PolicyControlComponent', () => {
   let component: PolicyControlComponent;
   let fixture: ComponentFixture<PolicyControlComponent>;
 
   beforeEach(async(() => {
+    const policyTypeDataSourceSpy = jasmine.createSpyObj('PolicyTypeDataSource', [ 'connect', 'loadTable',  'disconnect' ]);
+    const policyType = {
+      name: "type1",
+      schema: "{}",
+      schemaObject: "{}"
+    }
+    policyTypeDataSourceSpy.connect.and.returnValue(of([ policyType]));
+    policyTypeDataSourceSpy.disconnect();
+
+    let matDialogStub: Partial<MatDialog>;
+    let notificationServiceStub: Partial<NotificationService>;
+
     TestBed.configureTestingModule({
-      imports: [ MatIconModule,MatTableModule ],
-      declarations: [ PolicyControlComponent ]
+      imports: [
+        MatIconModule,
+        MatTableModule,
+        BrowserAnimationsModule
+      ],
+      schemas: [
+        CUSTOM_ELEMENTS_SCHEMA
+      ],
+      declarations: [
+        PolicyControlComponent
+       ],
+      providers: [
+        { provide: PolicyTypeDataSource, useValue: policyTypeDataSourceSpy },
+        { provide: MatDialog, useValue: matDialogStub },
+        { provide: NotificationService, useValue: notificationServiceStub },
+        UiService
+       ]
     })
     .compileComponents();
   }));
@@ -41,4 +74,7 @@ describe('PolicyControlComponent', () => {
     fixture.detectChanges();
   });
 
+  it('should create', () => {
+    expect(component).toBeTruthy();
+  });
 });
index 56c0ec3..f1629e0 100644 (file)
  */
 import { Component, OnInit, ViewChild } from '@angular/core';
 import { MatDialog } from '@angular/material/dialog';
-import { MatSort } from '@angular/material/sort';
 import { animate, state, style, transition, trigger } from '@angular/animations';
 
-import { PolicyService } from '../services/policy/policy.service';
 import { PolicyType } from '../interfaces/policy.types';
 import { PolicyTypeDataSource } from './policy-type.datasource';
 import { getPolicyDialogProperties } from './policy-instance-dialog.component';
@@ -53,21 +51,16 @@ class PolicyTypeInfo {
 })
 export class PolicyControlComponent implements OnInit {
 
-
-    policyTypesDataSource: PolicyTypeDataSource;
-    @ViewChild(MatSort, { static: true }) sort: MatSort;
-
     policyTypeInfo = new Map<string, PolicyTypeInfo>();
     darkMode: boolean;
 
     constructor(
-        private policySvc: PolicyService,
+        private policyTypesDataSource: PolicyTypeDataSource,
         private dialog: MatDialog,
         private notificationService: NotificationService,
         private ui: UiService) { }
 
     ngOnInit() {
-        this.policyTypesDataSource = new PolicyTypeDataSource(this.policySvc, this.sort, this.notificationService);
         this.policyTypesDataSource.loadTable();
         this.ui.darkModeState.subscribe((isDark) => {
             this.darkMode = isDark;
index 983fc96..61ecc4a 100644 (file)
 
 import { CollectionViewer, DataSource } from '@angular/cdk/collections';
 import { HttpErrorResponse } from '@angular/common/http';
-import { MatSort } from '@angular/material';
-import { Observable } from 'rxjs/Observable';
+import { Injectable } from '@angular/core';
 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
-import { merge } from 'rxjs';
 import { of } from 'rxjs/observable/of';
 import { catchError, finalize, map } from 'rxjs/operators';
+import { Observable } from 'rxjs/Observable';
+
 import { PolicyType } from '../interfaces/policy.types';
 import { PolicyService } from '../services/policy/policy.service';
 import { NotificationService } from '../services/ui/notification.service';
 
+@Injectable({
+    providedIn: 'root'
+})
+
 export class PolicyTypeDataSource extends DataSource<PolicyType> {
 
     private policyTypeSubject = new BehaviorSubject<PolicyType[]>([]);
@@ -41,7 +45,6 @@ export class PolicyTypeDataSource extends DataSource<PolicyType> {
     public rowCount = 1; // hide footer during intial load
 
     constructor(private policySvc: PolicyService,
-        private sort: MatSort,
         private notificationService: NotificationService) {
         super();
     }
@@ -72,35 +75,11 @@ export class PolicyTypeDataSource extends DataSource<PolicyType> {
     }
 
     connect(collectionViewer: CollectionViewer): Observable<PolicyType[]> {
-        const dataMutations = [
-            this.policyTypeSubject.asObservable(),
-            this.sort.sortChange
-        ];
-        return merge(...dataMutations).pipe(map(() => {
-            return this.getSortedData([...this.policyTypeSubject.getValue()]);
-        }));
+        return of(this.policyTypeSubject.getValue());
     }
 
     disconnect(collectionViewer: CollectionViewer): void {
         this.policyTypeSubject.complete();
         this.loadingSubject.complete();
     }
-
-    private getSortedData(data: PolicyType[]) {
-        if (!this.sort.active || this.sort.direction === '') {
-            return data;
-        }
-
-        return data.sort((a, b) => {
-            const isAsc = this.sort.direction === 'asc';
-            switch (this.sort.active) {
-                case 'name': return compare(a.name, b.name, isAsc);
-                default: return 0;
-            }
-        });
-    }
-}
-
-function compare(a: any, b: any, isAsc: boolean) {
-    return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
 }