From e7e4239187656a819f2ac78bc113328747383505 Mon Sep 17 00:00:00 2001 From: Joshua Kuestersteffen Date: Thu, 14 Nov 2024 11:22:28 -0600 Subject: [PATCH] Fix type issues in unit tests --- .../edit-user/update-password.component.ts | 2 +- .../js/enketo/medic-xpath-extensions.spec.ts | 3 ++- .../karma/ts/providers/parse.provider.spec.ts | 3 ++- .../ts/services/delete-docs.service.spec.ts | 5 +++-- .../karma/ts/services/enketo.service.spec.ts | 3 ++- .../karma/ts/services/form.service.spec.ts | 5 +++-- .../ts/services/replication.service.spec.ts | 13 +++++++------ .../ts/services/rules-engine.service.spec.ts | 3 ++- .../ts/services/training-cards.service.spec.ts | 3 ++- .../ts/services/transitions.service.spec.ts | 3 ++- .../create-user-for-contacts.transition.spec.ts | 17 +++++++++++------ 11 files changed, 37 insertions(+), 23 deletions(-) diff --git a/webapp/src/ts/modals/edit-user/update-password.component.ts b/webapp/src/ts/modals/edit-user/update-password.component.ts index 24fef19bfd6..913137c774c 100644 --- a/webapp/src/ts/modals/edit-user/update-password.component.ts +++ b/webapp/src/ts/modals/edit-user/update-password.component.ts @@ -65,7 +65,7 @@ export class UpdatePasswordComponent { try { await this.userLoginService.login(username, newPassword); } catch (err) { - if (typeof err === 'object' && err !== null && 'status' in err && err.status === 302) { + if (getProperty(err, 'status') === 302) { this.close(); const snackText = await this.translateService.get('password.updated'); this.globalActions.setSnackbarContent(snackText); diff --git a/webapp/tests/karma/js/enketo/medic-xpath-extensions.spec.ts b/webapp/tests/karma/js/enketo/medic-xpath-extensions.spec.ts index d8735d9c357..4e40bda4ecd 100644 --- a/webapp/tests/karma/js/enketo/medic-xpath-extensions.spec.ts +++ b/webapp/tests/karma/js/enketo/medic-xpath-extensions.spec.ts @@ -2,6 +2,7 @@ import { expect } from 'chai'; import sinon from 'sinon'; import * as medicXpathExtensions from '../../../../src/js/enketo/medic-xpath-extensions.js'; +import { getProperty } from '../../../../src/ts/libs/schema'; describe('Medic XPath Extensions', () => { it('should have expected attributes', () => { @@ -78,7 +79,7 @@ describe('Medic XPath Extensions', () => { try { extensionLib({ v: 'myfunc' }, { t: 'string', v: 'hello' }); } catch (e) { - expect(e.message).to.equal('Form configuration error: no extension-lib with ID "myfunc" found'); + expect(getProperty(e, 'message')).to.equal('Form configuration error: no extension-lib with ID "myfunc" found'); return; } throw new Error('Expected exception to be thrown.'); diff --git a/webapp/tests/karma/ts/providers/parse.provider.spec.ts b/webapp/tests/karma/ts/providers/parse.provider.spec.ts index 094fff3448c..46860809b77 100644 --- a/webapp/tests/karma/ts/providers/parse.provider.spec.ts +++ b/webapp/tests/karma/ts/providers/parse.provider.spec.ts @@ -11,6 +11,7 @@ import { PhonePipe } from '@mm-pipes/phone.pipe'; import { FormatDateService } from '@mm-services/format-date.service'; import { RelativeDateService } from '@mm-services/relative-date.service'; import { XmlFormsContextUtilsService } from '@mm-services/xml-forms-context-utils.service'; +import { getProperty } from '../../../../src/ts/libs/schema'; describe('Parse provider', () => { let provider:ParseProvider; @@ -47,7 +48,7 @@ describe('Parse provider', () => { result = parse('2 ===== 3'); assert.fail('should have thrown'); } catch (e) { - expect(e.message.startsWith('Parser Error: Unexpected token')).to.equal(true); + expect((getProperty(e, 'message') as string).startsWith('Parser Error: Unexpected token')).to.equal(true); expect(result).to.equal(undefined); } }); diff --git a/webapp/tests/karma/ts/services/delete-docs.service.spec.ts b/webapp/tests/karma/ts/services/delete-docs.service.spec.ts index 4225f074194..1c72e96bdd0 100644 --- a/webapp/tests/karma/ts/services/delete-docs.service.spec.ts +++ b/webapp/tests/karma/ts/services/delete-docs.service.spec.ts @@ -7,6 +7,7 @@ import { SessionService } from '@mm-services/session.service'; import { ChangesService } from '@mm-services/changes.service'; import { DeleteDocsService } from '@mm-services/delete-docs.service'; import { ExtractLineageService } from '@mm-services/extract-lineage.service'; +import { getProperty } from '../../../../src/ts/libs/schema'; describe('DeleteDocs service', () => { @@ -266,7 +267,7 @@ describe('DeleteDocs service', () => { try { JSON.stringify(report); } catch (e) { - if (e.message.startsWith('Converting circular structure to JSON')) { + if ((getProperty(e, 'message') as string).startsWith('Converting circular structure to JSON')) { isCircularBefore = true; } } @@ -277,7 +278,7 @@ describe('DeleteDocs service', () => { try { JSON.stringify(bulkDocs.args[0][0][0]); } catch (e) { - if (e.message.startsWith('Converting circular structure to JSON')) { + if ((getProperty(e, 'message') as string).startsWith('Converting circular structure to JSON')) { isCircularAfter = true; } } diff --git a/webapp/tests/karma/ts/services/enketo.service.spec.ts b/webapp/tests/karma/ts/services/enketo.service.spec.ts index 5386a298f11..ec7db515e70 100644 --- a/webapp/tests/karma/ts/services/enketo.service.spec.ts +++ b/webapp/tests/karma/ts/services/enketo.service.spec.ts @@ -12,6 +12,7 @@ import { TranslateService } from '@mm-services/translate.service'; import { EnketoFormContext, EnketoService } from '@mm-services/enketo.service'; import { ExtractLineageService } from '@mm-services/extract-lineage.service'; import * as FileManager from '../../../../src/js/enketo/file-manager.js'; +import { getProperty } from '../../../../src/ts/libs/schema'; describe('Enketo service', () => { // return a mock form ready for putting in #dbContent @@ -130,7 +131,7 @@ describe('Enketo service', () => { expect.fail('Should throw error'); } catch (error) { expect(enketoInit.callCount).to.equal(1); - expect(error.message).to.equal('["nope","still nope"]'); + expect(getProperty(error, 'message')).to.equal('["nope","still nope"]'); } })); diff --git a/webapp/tests/karma/ts/services/form.service.spec.ts b/webapp/tests/karma/ts/services/form.service.spec.ts index 5b8599081a5..d88889fd16b 100644 --- a/webapp/tests/karma/ts/services/form.service.spec.ts +++ b/webapp/tests/karma/ts/services/form.service.spec.ts @@ -36,6 +36,7 @@ import { EnketoTranslationService } from '@mm-services/enketo-translation.servic import * as FileManager from '../../../../src/js/enketo/file-manager.js'; import { TargetAggregatesService } from '@mm-services/target-aggregates.service'; import { ContactViewModelGeneratorService } from '@mm-services/contact-view-model-generator.service'; +import { getProperty } from '../../../../src/ts/libs/schema'; describe('Form service', () => { // return a mock form ready for putting in #dbContent @@ -297,7 +298,7 @@ describe('Form service', () => { { doc: { _id: '123-patient-contact' }, contactSummary: { pregnant: false }, shouldEvaluateExpression: true }, ]); expect(enketoInit.callCount).to.equal(1); - expect(error.message).to.equal(expectedErrorMessage); + expect(getProperty(error, 'message')).to.equal(expectedErrorMessage); expect(consoleErrorMock.callCount).to.equal(0); } })); @@ -585,7 +586,7 @@ describe('Form service', () => { flush(); expect.fail('Should throw error'); } catch (error) { - expect(error.message).to.equal('Failed during the form "myform" rendering : invalid user'); + expect(getProperty(error, 'message')).to.equal('Failed during the form "myform" rendering : invalid user'); expect(UserContact.calledOnce).to.be.true; expect(renderForm.notCalled).to.be.true; expect(enketoInit.notCalled).to.be.true; diff --git a/webapp/tests/karma/ts/services/replication.service.spec.ts b/webapp/tests/karma/ts/services/replication.service.spec.ts index 9fcb44ea1c2..a9965d792e4 100644 --- a/webapp/tests/karma/ts/services/replication.service.spec.ts +++ b/webapp/tests/karma/ts/services/replication.service.spec.ts @@ -7,6 +7,7 @@ import { ReplicationService } from '@mm-services/replication.service'; import { DbService } from '@mm-services/db.service'; import { of, throwError } from 'rxjs'; import { RulesEngineService } from '@mm-services/rules-engine.service'; +import { getProperty } from '../../../../src/ts/libs/schema'; describe('ContactTypes service', () => { @@ -330,7 +331,7 @@ describe('ContactTypes service', () => { await service.replicateFrom(); expect.fail('Should have thrown'); } catch (err) { - expect(err.message).to.equal('omg'); + expect(getProperty(err, 'message')).to.equal('omg'); expect(localDb.allDocs.callCount).to.equal(0); expect(localDb.bulkDocs.callCount).to.equal(0); expect(remoteDb.bulkGet.callCount).to.equal(0); @@ -351,7 +352,7 @@ describe('ContactTypes service', () => { await service.replicateFrom(); expect.fail('Should have thrown'); } catch (err) { - expect(err.message).to.equal('alldocsfail'); + expect(getProperty(err, 'message')).to.equal('alldocsfail'); expect(localDb.bulkDocs.callCount).to.equal(0); expect(remoteDb.bulkGet.callCount).to.equal(0); expect(http.post.callCount).to.equal(0); @@ -381,7 +382,7 @@ describe('ContactTypes service', () => { await service.replicateFrom(); expect.fail('Should have thrown'); } catch (err) { - expect(err.message).to.equal('bulkgeterror'); + expect(getProperty(err, 'message')).to.equal('bulkgeterror'); expect(localDb.bulkDocs.callCount).to.equal(0); expect(http.post.callCount).to.equal(0); } @@ -416,7 +417,7 @@ describe('ContactTypes service', () => { await service.replicateFrom(); expect.fail('Should have thrown'); } catch (err) { - expect(err.message).to.equal('bulkdocserr'); + expect(getProperty(err, 'message')).to.equal('bulkdocserr'); expect(http.post.callCount).to.equal(0); } }); @@ -444,7 +445,7 @@ describe('ContactTypes service', () => { await service.replicateFrom(); expect.fail('Should have thrown'); } catch (err) { - expect(err.message).to.equal('getdeleteserror'); + expect(getProperty(err, 'message')).to.equal('getdeleteserror'); expect(localDb.bulkDocs.callCount).to.equal(0); } }); @@ -474,7 +475,7 @@ describe('ContactTypes service', () => { await service.replicateFrom(); expect.fail('Should have thrown'); } catch (err) { - expect(err.message).to.equal('bulkdocserror2'); + expect(getProperty(err, 'message')).to.equal('bulkdocserror2'); } }); }); diff --git a/webapp/tests/karma/ts/services/rules-engine.service.spec.ts b/webapp/tests/karma/ts/services/rules-engine.service.spec.ts index a3dbc7e793d..22254e0d65b 100644 --- a/webapp/tests/karma/ts/services/rules-engine.service.spec.ts +++ b/webapp/tests/karma/ts/services/rules-engine.service.spec.ts @@ -18,6 +18,7 @@ import { TranslateFromService } from '@mm-services/translate-from.service'; import { RulesEngineCoreFactoryService, RulesEngineService } from '@mm-services/rules-engine.service'; import { PipesService } from '@mm-services/pipes.service'; import { CHTDatasourceService } from '@mm-services/cht-datasource.service'; +import { getProperty } from '../../../../src/ts/libs/schema'; describe('RulesEngineService', () => { let service: RulesEngineService; @@ -215,7 +216,7 @@ describe('RulesEngineService', () => { await func(); assert.fail('Should throw'); } catch (err) { - expect(err.name).to.include(include); + expect(getProperty(err, 'name')).to.include(include); } }; diff --git a/webapp/tests/karma/ts/services/training-cards.service.spec.ts b/webapp/tests/karma/ts/services/training-cards.service.spec.ts index e7a67de3c7d..18ae37fdc5c 100644 --- a/webapp/tests/karma/ts/services/training-cards.service.spec.ts +++ b/webapp/tests/karma/ts/services/training-cards.service.spec.ts @@ -11,6 +11,7 @@ import { TrainingCardsService } from '@mm-services/training-cards.service'; import { SessionService } from '@mm-services/session.service'; import { RouteSnapshotService } from '@mm-services/route-snapshot.service'; import { FeedbackService } from '@mm-services/feedback.service'; +import { getProperty } from '../../../../src/ts/libs/schema'; describe('TrainingCardsService', () => { let service: TrainingCardsService; @@ -767,7 +768,7 @@ describe('TrainingCardsService', () => { service.getTrainingCardDocId().startsWith('training:ronald:'); assert.fail('should have thrown'); } catch (error) { - expect(error.message) + expect(getProperty(error, 'message')) .to.equal('Training Cards :: Cannot create document ID, user context does not have the "name" property.'); } }); diff --git a/webapp/tests/karma/ts/services/transitions.service.spec.ts b/webapp/tests/karma/ts/services/transitions.service.spec.ts index 56bac18ef76..f48ce2a118a 100644 --- a/webapp/tests/karma/ts/services/transitions.service.spec.ts +++ b/webapp/tests/karma/ts/services/transitions.service.spec.ts @@ -8,6 +8,7 @@ import { SettingsService } from '@mm-services/settings.service'; import { MutingTransition } from '@mm-services/transitions/muting.transition'; import { ValidationService } from '@mm-services/validation.service'; import { CreateUserForContactsTransition } from '@mm-services/transitions/create-user-for-contacts.transition'; +import { getProperty } from '../../../../src/ts/libs/schema'; describe('Transitions Service', () => { let settingsService; @@ -264,7 +265,7 @@ describe('Transitions Service', () => { await service.applyTransitions(docs); expect.fail('should have thrown an error'); } catch (err) { - expect(err.message).to.equal(`An array of valid doc objects must be provided.`); + expect(getProperty(err, 'message')).to.equal(`An array of valid doc objects must be provided.`); } expect(mutingTransition.filter.callCount).to.equal(0); diff --git a/webapp/tests/karma/ts/services/transitions/create-user-for-contacts.transition.spec.ts b/webapp/tests/karma/ts/services/transitions/create-user-for-contacts.transition.spec.ts index 2ff76b6eab9..1266cd42290 100644 --- a/webapp/tests/karma/ts/services/transitions/create-user-for-contacts.transition.spec.ts +++ b/webapp/tests/karma/ts/services/transitions/create-user-for-contacts.transition.spec.ts @@ -8,6 +8,7 @@ import { expect } from 'chai'; import { UserContactService } from '@mm-services/user-contact.service'; import { ExtractLineageService } from '@mm-services/extract-lineage.service'; import { CHTDatasourceService } from '@mm-services/cht-datasource.service'; +import { getProperty } from '../../../../../src/ts/libs/schema'; const deepFreeze = obj => { Object @@ -395,7 +396,7 @@ describe('Create User for Contacts Transition', () => { await transition.run([replaceUserDoc]); expect(true).to.equal('should have thrown an error'); } catch (err) { - expect(err.message).to.equal( + expect(getProperty(err, 'message')).to.equal( 'Only the contact associated with the currently logged in user can be replaced.' ); } @@ -417,8 +418,10 @@ describe('Create User for Contacts Transition', () => { await transition.run([replaceUserDoc]); expect(true).to.equal('should have thrown an error'); } catch (err) { - expect(err.message).to.equal('The form for replacing a user must include a replacement_contact_id field ' + - 'containing the id of the new contact.'); + expect(getProperty(err, 'message')).to.equal( + 'The form for replacing a user must include a replacement_contact_id field ' + + 'containing the id of the new contact.' + ); } expect(userContactService.get.callCount).to.equal(1); @@ -437,7 +440,7 @@ describe('Create User for Contacts Transition', () => { await transition.run([REPLACE_USER_DOC]); expect(true).to.equal('should have thrown an error'); } catch (err) { - expect(err.message).to.equal(`The new contact could not be found [${NEW_CONTACT._id}].`); + expect(getProperty(err, 'message')).to.equal(`The new contact could not be found [${NEW_CONTACT._id}].`); } expect(userContactService.get.callCount).to.equal(1); @@ -455,7 +458,7 @@ describe('Create User for Contacts Transition', () => { await transition.run([REPLACE_USER_DOC]); expect(true).to.equal('should have thrown an error'); } catch (err) { - expect(err.message).to.equal(`Server Error`); + expect(getProperty(err, 'message')).to.equal(`Server Error`); } expect(userContactService.get.callCount).to.equal(1); @@ -475,7 +478,9 @@ describe('Create User for Contacts Transition', () => { await transition.run([REPLACE_USER_DOC, NEW_CONTACT, REPLACE_USER_DOC]); expect(true).to.equal('should have thrown an error'); } catch (err) { - expect(err.message).to.equal(`Only one user replace form is allowed to be submitted per transaction.`); + expect(getProperty(err, 'message')).to.equal( + `Only one user replace form is allowed to be submitted per transaction.` + ); } expect(userContactService.get.callCount).to.equal(1);