Skip to content

Commit

Permalink
Fix type issues in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuester committed Nov 14, 2024
1 parent 77ab33e commit e7e4239
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion webapp/tests/karma/js/enketo/medic-xpath-extensions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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.');
Expand Down
3 changes: 2 additions & 1 deletion webapp/tests/karma/ts/providers/parse.provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
});
Expand Down
5 changes: 3 additions & 2 deletions webapp/tests/karma/ts/services/delete-docs.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {

Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down
3 changes: 2 additions & 1 deletion webapp/tests/karma/ts/services/enketo.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]');
}
}));

Expand Down
5 changes: 3 additions & 2 deletions webapp/tests/karma/ts/services/form.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}));
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 7 additions & 6 deletions webapp/tests/karma/ts/services/replication.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
});
Expand Down Expand Up @@ -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);
}
});
Expand Down Expand Up @@ -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');
}
});
});
Expand Down
3 changes: 2 additions & 1 deletion webapp/tests/karma/ts/services/rules-engine.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.');
}
});
Expand Down
3 changes: 2 additions & 1 deletion webapp/tests/karma/ts/services/transitions.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.'
);
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit e7e4239

Please sign in to comment.