From d0a9a86f2b261025f84d520116bd236e6a6042d1 Mon Sep 17 00:00:00 2001 From: Anro Date: Wed, 6 Nov 2024 12:20:17 +0200 Subject: [PATCH] fix: tweaks --- .../contacts/contacts-edit.component.ts | 27 +-------------- webapp/src/ts/polyfills.ts | 2 +- .../contacts/contacts-edit.component.spec.ts | 34 +++++++++++++++++++ 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/webapp/src/ts/modules/contacts/contacts-edit.component.ts b/webapp/src/ts/modules/contacts/contacts-edit.component.ts index 2a1ebce2c6..d60c79ca7b 100644 --- a/webapp/src/ts/modules/contacts/contacts-edit.component.ts +++ b/webapp/src/ts/modules/contacts/contacts-edit.component.ts @@ -211,48 +211,23 @@ export class ContactsEditComponent implements OnInit, OnDestroy, AfterViewInit { } const docId = this.enketoContact.docId; - console.log('Doc ID: ', docId); - // eslint-disable-next-line eqeqeq this.isEdit = docId != null; - console.log('Is edit: ', this.isEdit); - - // this.parentId = ((contact || {}).parent || {})._id ?? this.routeSnapshot.params.parent_id; this.parentId = contact?.parent?._id ?? this.routeSnapshot.params.parent_id; - console.log('Parent ID: ', this.parentId); - - // this.contactType = (contactType || {}).id ?? (this.enketoContact || {}).type; this.contactType = contactType?.id ?? this.enketoContact?.type; - console.log('Contact type: ', this.contactType); - const temp = this.enketoContact.userContact; - console.log('User contact: ', temp); // This never seems to get populated - - // We could load "config" from our namespace: if (window?._phdcChanges?.hierarchyDuplicatePrevention){ - console.log('Hierarchy duplicate config: '); - console.log(window._phdcChanges.hierarchyDuplicatePrevention); if (this.contactType in window._phdcChanges.hierarchyDuplicatePrevention){ const ct: KeysType = (this.contactType as KeysType); - console.log('Keys type ct: ', ct); const conf: StrategyForContactType = window._phdcChanges.hierarchyDuplicatePrevention[ct]; - console.log('Conf: '); - console.log(conf); if (conf){ const strategyType = conf.type; - console.log('Strategy type', strategyType); this.strategy = this.strategies[strategyType]; - this.formPropPathsToCheck = conf.props ? conf.props: [{form_prop_path: `/data/${this.contactType}/name`, db_doc_ref: 'name'}]; - this.duplicateThreshold = conf.threshold; - console.log('Threshold: ', this.duplicateThreshold); - this.queryParams = conf.queryParams; - console.log('Query params: '); - console.log(this.queryParams); } else { console.error('No config has been loaded!'); } @@ -404,7 +379,7 @@ export class ContactsEditComponent implements OnInit, OnDestroy, AfterViewInit { private parseXmlForm(form): Document | undefined { // Below line is taken from enketo-core form-model line 114 - const xmlDoc = this.parser.parseFromString(form.getDataStr({ irrelevant: false }), 'text/xml'); + const xmlDoc = this.parser.parseFromString(form.getDataStr({ irrelevant: false }), 'text/xml'); console.log('Doc'); console.log(xmlDoc); if (xmlDoc.getElementsByTagName('parsererror').length > 0) { diff --git a/webapp/src/ts/polyfills.ts b/webapp/src/ts/polyfills.ts index e25ea7a399..f9dc17d187 100755 --- a/webapp/src/ts/polyfills.ts +++ b/webapp/src/ts/polyfills.ts @@ -108,7 +108,7 @@ declare global { type Strategy = { // Name is used by default (since it's hardcoded in the system) if no props are specified. // Should props be specified, only those items will be considered - props?: {form_prop_path: string; db_doc_ref: string}[]; + props?: {form_prop_path: string; db_doc_ref: string}[]; // TODO: perhaps add a weight to each property queryParams?: { // Usage example - see main.ts valuePaths: string[]; //https://stackoverflow.com/questions/76130608/in-typescript-can-you-define-a-function-type-where-each-argument-can-be-one-of diff --git a/webapp/tests/karma/ts/modules/contacts/contacts-edit.component.spec.ts b/webapp/tests/karma/ts/modules/contacts/contacts-edit.component.spec.ts index 1030cc6b21..cf6682c3ba 100644 --- a/webapp/tests/karma/ts/modules/contacts/contacts-edit.component.spec.ts +++ b/webapp/tests/karma/ts/modules/contacts/contacts-edit.component.spec.ts @@ -35,6 +35,7 @@ describe('ContactsEdit component', () => { let routeSnapshot; let stopPerformanceTrackStub; let performanceService; + let dummyFormXml; beforeEach(() => { contactTypesService = { @@ -101,9 +102,16 @@ describe('ContactsEdit component', () => { return TestBed.compileComponents().then(() => { fixture = TestBed.createComponent(ContactsEditComponent); component = fixture.componentInstance; + component.dbLookupRef = Promise.resolve({ + total_rows: 0, + offset: 0, + rows: [] + }); fixture.detectChanges(); }); }; + + dummyFormXml = `test`; }); afterEach(() => sinon.restore()); @@ -637,6 +645,7 @@ describe('ContactsEdit component', () => { component.enketoContact = { formInstance: { validate: sinon.stub().resolves(false), + getDataStr: sinon.stub().returns(dummyFormXml) }, }; @@ -656,6 +665,7 @@ describe('ContactsEdit component', () => { component.enketoContact = { formInstance: { validate: sinon.stub().resolves(true), + getDataStr: sinon.stub().returns(dummyFormXml) }, type: 'some_contact', }; @@ -682,6 +692,7 @@ describe('ContactsEdit component', () => { dbGet.resolves({ _id: 'clinic_create_form_id', the: 'form' }); const form = { validate: sinon.stub().resolves(true), + getDataStr: sinon.stub().returns(dummyFormXml) }; formService.render.resolves(form); @@ -689,6 +700,13 @@ describe('ContactsEdit component', () => { await fixture.whenStable(); formService.saveContact.resolves({ docId: 'new_clinic_id' }); + + // TODO: figure out why this test's dbLookupRef is null despite being set in the beforeEach + component.dbLookupRef = Promise.resolve({ + total_rows: 0, + offset: 0, + rows: [] + }); await component.save(); expect(performanceService.track.calledThrice).to.be.true; @@ -732,12 +750,20 @@ describe('ContactsEdit component', () => { dbGet.resolves({ _id: 'person_edit_form_id', the: 'form' }); const form = { validate: sinon.stub().resolves(true), + getDataStr: sinon.stub().returns(dummyFormXml) }; formService.render.resolves(form); await createComponent(); await fixture.whenStable(); + // TODO: figure out why this test's dbLookupRef is null despite being set in the beforeEach + component.dbLookupRef = Promise.resolve({ + total_rows: 0, + offset: 0, + rows: [] + }); + formService.saveContact.resolves({ docId: 'the_person' }); await component.save(); @@ -780,12 +806,20 @@ describe('ContactsEdit component', () => { dbGet.resolves({ _id: 'patient_create_form_id', the: 'form' }); const form = { validate: sinon.stub().resolves(true), + getDataStr: sinon.stub().returns(dummyFormXml) }; formService.render.resolves(form); await createComponent(); await fixture.whenStable(); + // TODO: figure out why this test's dbLookupRef is null despite being set in the beforeEach + component.dbLookupRef = Promise.resolve({ + total_rows: 0, + offset: 0, + rows: [] + }); + formService.saveContact.resolves({ docId: 'the_patient' }); await component.save();