From 247b7164c80faca7f5aae50db907c6984c396b36 Mon Sep 17 00:00:00 2001 From: Julian Clementson <51331324+oajulianclementson@users.noreply.github.com> Date: Mon, 12 Sep 2022 15:39:06 +0100 Subject: [PATCH] LP#1989348: set default new record via route data 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 --- .../staff/admin/basic-admin-page.component.spec.ts | 106 +++++++++++++++++++++ .../app/staff/admin/basic-admin-page.component.ts | 25 ++++- 2 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 Open-ILS/src/eg2/src/app/staff/admin/basic-admin-page.component.spec.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 index 0000000000..7a5cb9ddfe --- /dev/null +++ b/Open-ILS/src/eg2/src/app/staff/admin/basic-admin-page.component.spec.ts @@ -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; + let idlServiceStub: Partial; + 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'); + }); +}); diff --git a/Open-ILS/src/eg2/src/app/staff/admin/basic-admin-page.component.ts b/Open-ILS/src/eg2/src/app/staff/admin/basic-admin-page.component.ts index 91b2040a34..1f63253b7f 100644 --- a/Open-ILS/src/eg2/src/app/staff/admin/basic-admin-page.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/admin/basic-admin-page.component.ts @@ -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"> ` @@ -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; 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; + } }); } -- 2.11.0