Skip to content

Commit

Permalink
rework resolveTimestamps validation to fix test
Browse files Browse the repository at this point in the history
We need a safe way to mark that validation fails in this function, without actually throwing an error. Let's use a third parameter for this purpose which is a callback function 'onFail' to handle the invalid case.
- Updated tests accordingly - now passing
  • Loading branch information
JGreenlee committed Dec 5, 2023
1 parent 47acc03 commit ee2fe2a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
17 changes: 8 additions & 9 deletions www/__tests__/enketoHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,25 @@ it('resolves the timestamps', () => {
const missingData =
'<tag> <Start_date>2016-08-28</Start_date> <End_date>2016-07-25</End_date> <End_time>17:30:31.000-06:00</End_time> </tag>';
const missDataDoc = xmlParser.parseFromString(missingData, 'text/html');
expect(resolveTimestamps(missDataDoc, timelineEntry)).toBeNull();
expect(resolveTimestamps(missDataDoc, timelineEntry, () => {})).toBeNull();
//bad time returns undefined
const badTimes =
'<tag> <Start_date>2016-08-28</Start_date> <End_date>2016-07-25</End_date> <Start_time>17:32:32.928-06:00</Start_time> <End_time>17:30:31.000-06:00</End_time> </tag>';
const badTimeDoc = xmlParser.parseFromString(badTimes, 'text/xml');
expect(resolveTimestamps(badTimeDoc, timelineEntry)).toBeUndefined();
expect(resolveTimestamps(badTimeDoc, timelineEntry, () => {})).toBeUndefined();
//if within a minute, timelineEntry timestamps
const timeEntry =
'<tag> <Start_date>2016-07-25</Start_date> <End_date>2016-07-25</End_date> <Start_time>17:24:32.928-06:00</Start_time> <End_time>17:30:31.000-06:00</End_time> </tag>';
const xmlDoc1 = xmlParser.parseFromString(timeEntry, 'text/xml');
expect(resolveTimestamps(xmlDoc1, timelineEntry)).toMatchObject({
expect(resolveTimestamps(xmlDoc1, timelineEntry, () => {})).toMatchObject({
start_ts: 1469492672.928242,
end_ts: 1469493031,
});
// else survey timestamps
const timeSurvey =
'<tag> <Start_date>2016-07-25</Start_date> <End_date>2016-07-25</End_date> <Start_time>17:22:33.928-06:00</Start_time> <End_time>17:33:33.000-06:00</End_time> </tag>';
const xmlDoc2 = xmlParser.parseFromString(timeSurvey, 'text/xml');
expect(resolveTimestamps(xmlDoc2, timelineEntry)).toMatchObject({
expect(resolveTimestamps(xmlDoc2, timelineEntry, () => {})).toMatchObject({
start_ts: 1469492553.928,
end_ts: 1469493213,
});
Expand Down Expand Up @@ -205,7 +205,7 @@ it('resolves the label, if no labelVars, returns template', async () => {
* @returns Promise of the saved result, or an Error if there was a problem
*/
// export function saveResponse(surveyName: string, enketoForm: Form, appConfig, opts: SurveyOptions) {
it('gets the saved result or throws an error', () => {
it('gets the saved result or throws an error', async () => {
const surveyName = 'TimeUseSurvey';
const form = {
getDataStr: () => {
Expand Down Expand Up @@ -251,10 +251,9 @@ it('gets the saved result or throws an error', () => {
label: '1 Personal Care',
name: 'TimeUseSurvey',
});
expect(saveResponse(surveyName, badForm, config, opts)).resolves.toMatchObject({
message:
'The times you entered are invalid. Please ensure that the start time is before the end time.',
});
expect(async () => await saveResponse(surveyName, badForm, config, opts)).rejects.toEqual(
'The times you entered are invalid. Please ensure that the start time is before the end time.',
);
});

/*
Expand Down
15 changes: 11 additions & 4 deletions www/js/survey/enketo/enketoHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,15 @@ export function getInstanceStr(xmlModel: string, opts?: SurveyOptions): string |
* resolve timestamps label from the survey response
* @param {XMLDocument} xmlDoc survey response as XML object
* @param {object} timelineEntry trip or place object
* @param {function} onFail callback function to be called if timestamp validation fails
* @returns {object} object with `start_ts` and `end_ts`
* - null if no timestamps are resolved
*/
export function resolveTimestamps(xmlDoc: XMLDocument, timelineEntry: TimelineEntry) {
export function resolveTimestamps(
xmlDoc: XMLDocument,
timelineEntry: TimelineEntry,
onFail: (e: Error) => void,
) {
// check for Date and Time fields
const startDate = xmlDoc.getElementsByTagName('Start_date')?.[0]?.innerHTML;
let startTime = xmlDoc.getElementsByTagName('Start_time')?.[0]?.innerHTML;
Expand All @@ -197,7 +202,8 @@ export function resolveTimestamps(xmlDoc: XMLDocument, timelineEntry: TimelineEn
let additionEndTs = DateTime.fromISO(endDate + 'T' + endTime, { zone: timezone }).toSeconds();

if (additionStartTs > additionEndTs) {
throw new Error(i18next.t('survey.enketo-timestamps-invalid')); //"Timestamps are invalid. Please ensure that the start time is before the end time.");
onFail(new Error(i18next.t('survey.enketo-timestamps-invalid'))); //"Timestamps are invalid. Please ensure that the start time is before the end time.");
return;
}

/* Enketo survey time inputs are only precise to the minute, while trips/places are precise to
Expand Down Expand Up @@ -243,7 +249,9 @@ export function saveResponse(
let timestamps: TimestampRange | { ts: number; fmt_time: string } | undefined;
let match_id: string | undefined;
if (opts?.timelineEntry) {
const resolvedTimestamps = resolveTimestamps(xmlDoc, opts.timelineEntry);
const resolvedTimestamps = resolveTimestamps(xmlDoc, opts.timelineEntry, (errOnFail) => {
return Promise.reject(errOnFail);

Check warning on line 253 in www/js/survey/enketo/enketoHelper.ts

View check run for this annotation

Codecov / codecov/patch

www/js/survey/enketo/enketoHelper.ts#L251-L253

Added lines #L251 - L253 were not covered by tests
});
if (resolvedTimestamps?.start_ts && resolvedTimestamps?.end_ts) {
timestamps = resolvedTimestamps;
} else {

Check warning on line 257 in www/js/survey/enketo/enketoHelper.ts

View check run for this annotation

Codecov / codecov/patch

www/js/survey/enketo/enketoHelper.ts#L255-L257

Added lines #L255 - L257 were not covered by tests
Expand All @@ -257,7 +265,6 @@ export function saveResponse(
: opts.timelineEntry.exit_ts,

Check warning on line 265 in www/js/survey/enketo/enketoHelper.ts

View check run for this annotation

Codecov / codecov/patch

www/js/survey/enketo/enketoHelper.ts#L264-L265

Added lines #L264 - L265 were not covered by tests
};
}

// UUID generated using this method https://stackoverflow.com/a/66332305
match_id = URL.createObjectURL(new Blob([])).slice(-36);

Check warning on line 269 in www/js/survey/enketo/enketoHelper.ts

View check run for this annotation

Codecov / codecov/patch

www/js/survey/enketo/enketoHelper.ts#L269

Added line #L269 was not covered by tests
} else {
Expand Down

0 comments on commit ee2fe2a

Please sign in to comment.