LP#1989348: set default new record via route data
authorJulian Clementson <51331324+oajulianclementson@users.noreply.github.com>
Mon, 12 Sep 2022 14:39:06 +0000 (15:39 +0100)
committerJane Sandberg <js7389@princeton.edu>
Tue, 13 Sep 2022 21:12:06 +0000 (14:12 -0700)
Allows us to set defaultNewRecord on BasicAdminPageComponent via route data

Signed-off-by: Julian Clementson <51331324+oajulianclementson@users.noreply.github.com>
Signed-off-by: Jane Sandberg <js7389@princeton.edu>
Open-ILS/src/eg2/src/app/staff/admin/basic-admin-page.component.spec.ts [new file with mode: 0644]
Open-ILS/src/eg2/src/app/staff/admin/basic-admin-page.component.ts

diff --git a/Open-ILS/src/eg2/src/app/staff/admin/basic-admin-page.component.spec.ts b/Open-ILS/src/eg2/src/app/staff/admin/basic-admin-page.component.spec.ts
new file mode 100644 (file)
index 0000000..7a5cb9d
--- /dev/null
@@ -0,0 +1,106 @@
+import { Component, Input } from '@angular/core';
+import { ComponentFixture, TestBed } from '@angular/core/testing';
+import { By } from '@angular/platform-browser';
+import { ActivatedRoute, ParamMap } from '@angular/router';
+import { IdlObject, IdlService } from '@eg/core/idl.service';
+import { of } from 'rxjs';
+import { BasicAdminPageComponent } from './basic-admin-page.component';
+
+@Component({
+    selector: 'eg-title',
+    template: ''
+})
+class MockTitleComponent {
+    @Input() prefix: string;
+}
+
+@Component({
+    selector: 'eg-staff-banner',
+    template: ''
+})
+class MockStaffBannerComponent {
+    @Input() bannerText: string;
+}
+
+@Component({
+    selector: 'eg-admin-page',
+    template: ''
+})
+class MockAdminPageComponent {
+    @Input() configLinkBasePath: string;
+    @Input() defaultNewRecord: IdlObject;
+    @Input() disableOrgFilter: boolean;
+    @Input() fieldOrder: string;
+    @Input() idlClass: string;
+    @Input() persistKeyPfx: string;
+    @Input() readonlyFields: string;
+}
+
+describe('Component: BasicAdminPage', () => {
+    let component: BasicAdminPageComponent;
+    let fixture: ComponentFixture<BasicAdminPageComponent>;
+    let idlServiceStub: Partial<IdlService>;
+    let routeStub: any;
+
+    beforeEach(() => {
+        idlServiceStub = {
+            create: (cls: string, seed?: []) => {
+                return {
+                    a: seed || [],
+                    classname: cls,
+                    _isfieldmapper: true,
+
+                    field1(value: any): any {
+                        this.a[0] = value;
+                        return this.a[0];
+                    }
+                };
+            },
+            classes: [{ tbl1: { table: 'schema1.table1' } }]
+        };
+
+        const emptyParamMap: ParamMap = {
+            has: (name: string) => false,
+            get: (name: string) => null,
+            getAll: (name: string) => [],
+            keys: []
+        };
+        const data = [{
+            schema: 'schema1',
+            table: 'table1',
+            defaultNewRecord: { field1: 'value1' }
+        }];
+        const parentRoute = { url: of('') };
+        const snapshot = { parent: { url: [{ path: '' }] } };
+        routeStub = {
+            paramMap: of(emptyParamMap),
+            data: of(data),
+            parent: parentRoute,
+            snapshot
+        };
+
+        TestBed.configureTestingModule({
+            imports: [],
+            providers: [
+                { provide: IdlService, useValue: idlServiceStub },
+                { provide: ActivatedRoute, useValue: routeStub }
+            ],
+            declarations: [
+                BasicAdminPageComponent,
+                MockTitleComponent,
+                MockStaffBannerComponent,
+                MockAdminPageComponent
+            ]
+        });
+        fixture = TestBed.createComponent(BasicAdminPageComponent);
+        component = fixture.componentInstance;
+        component.idlClass = 'tbl1';
+        fixture.detectChanges();
+    });
+
+    it('sets default new record from routing data', () => {
+        const adminPage: MockAdminPageComponent = fixture.debugElement.query(
+            By.directive(MockAdminPageComponent)).componentInstance;
+        expect(adminPage.defaultNewRecord.a[0]).toEqual('value1');
+    });
+});
index 91b2040..1f63253 100644 (file)
@@ -1,6 +1,6 @@
 import {Component, OnInit} from '@angular/core';
 import {ActivatedRoute, ParamMap} from '@angular/router';
-import {IdlService} from '@eg/core/idl.service';
+import {IdlObject, IdlService} from '@eg/core/idl.service';
 import {Observable} from 'rxjs';
 import {tap, switchMap} from 'rxjs/operators';
 
@@ -19,6 +19,7 @@ import {tap, switchMap} from 'rxjs/operators';
         configLinkBasePath="{{configLinkBasePath}}"
         fieldOrder="{{fieldOrder}}"
         readonlyFields="{{readonlyFields}}"
+        [defaultNewRecord]="defaultNewRecordIdl"
         [disableOrgFilter]="disableOrgFilter"></eg-admin-page>
       </ng-container>
     `
@@ -31,6 +32,7 @@ export class BasicAdminPageComponent implements OnInit {
     persistKeyPfx: string;
     fieldOrder = '';
     readonlyFields = '';
+    defaultNewRecordIdl: IdlObject;
     configLinkBasePath = '/staff/admin';
 
     // Tell the admin page to disable and hide the automagic org unit filter
@@ -42,6 +44,7 @@ export class BasicAdminPageComponent implements OnInit {
 
     private schema: string;
     private table: string;
+    private defaultNewRecord: Record<string, any>;
 
     constructor(
         private route: ActivatedRoute,
@@ -69,9 +72,10 @@ export class BasicAdminPageComponent implements OnInit {
                     if (!this.table) {
                         this.table = data['table'];
                     }
-                this.disableOrgFilter = data['disableOrgFilter'];
-                this.fieldOrder = data['fieldOrder'];
-                this.readonlyFields = data['readonlyFields'];
+                    this.disableOrgFilter = data['disableOrgFilter'];
+                    this.fieldOrder = data['fieldOrder'];
+                    this.readonlyFields = data['readonlyFields'];
+                    this.defaultNewRecord = data['defaultNewRecord'];
                 }
 
             }));
@@ -111,6 +115,19 @@ export class BasicAdminPageComponent implements OnInit {
             if (!this.idlClass) {
                 throw new Error('Unable to find IDL class for table ' + fullTable);
             }
+
+            if (this.defaultNewRecord) {
+                const record = this.idl.create(this.idlClass);
+                // Call IDL setter for each field that has a default value
+                Object.keys(this.defaultNewRecord).forEach(key => {
+                    if (typeof (record[key]) === 'function') {
+                        record[key].apply(record, [this.defaultNewRecord[key]]);
+                    } else {
+                        console.warn('Unknown field "' + key + '" in defaultNewRecord for table ' + fullTable);
+                    }
+                });
+                this.defaultNewRecordIdl = record;
+            }
         });
     }