--- /dev/null
+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');
+ });
+});
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';
configLinkBasePath="{{configLinkBasePath}}"
fieldOrder="{{fieldOrder}}"
readonlyFields="{{readonlyFields}}"
+ [defaultNewRecord]="defaultNewRecordIdl"
[disableOrgFilter]="disableOrgFilter"></eg-admin-page>
</ng-container>
`
persistKeyPfx: string;
fieldOrder = '';
readonlyFields = '';
+ defaultNewRecordIdl: IdlObject;
configLinkBasePath = '/staff/admin';
// Tell the admin page to disable and hide the automagic org unit filter
private schema: string;
private table: string;
+ private defaultNewRecord: Record<string, any>;
constructor(
private route: ActivatedRoute,
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'];
}
}));
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;
+ }
});
}