Skip to content

Commit

Permalink
Merge pull request #1063 from Abby-Wheelis/expand-enketo-helper
Browse files Browse the repository at this point in the history
Rewrite answer.js into enketoHelper.ts
  • Loading branch information
shankari authored Dec 4, 2023
2 parents 54b73f6 + b900d1e commit 9a321b7
Show file tree
Hide file tree
Showing 18 changed files with 757 additions and 277 deletions.
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ module.exports = {
"^.+\\.(ts|tsx|js|jsx)$": "babel-jest"
},
transformIgnorePatterns: [
"node_modules/(?!((jest-)?react-native(-.*)?|@react-native(-community)?)/)"
"node_modules/(?!((enketo-transformer/dist/enketo-transformer/web)|(jest-)?react-native(-.*)?|@react-native(-community)?)/)",
],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
moduleDirectories: ["node_modules", "src"],
globals: {"__DEV__": false},
collectCoverage: true,
};
3 changes: 2 additions & 1 deletion package.cordovabuild.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
},
"dependencies": {
"@havesource/cordova-plugin-push": "git+https://github.com/havesource/cordova-plugin-push.git#4.0.0-dev.0",
"@messageformat/core": "^3.2.0",
"@react-navigation/native": "^6.1.7",
"@react-navigation/stack": "^6.3.17",
"@shopify/flash-list": "^1.3.1",
Expand Down Expand Up @@ -141,6 +142,7 @@
"cordova-plugin-x-socialsharing": "6.0.4",
"core-js": "^2.5.7",
"enketo-core": "^6.1.7",
"enketo-transformer": "^4.0.0",
"fast-xml-parser": "^4.2.2",
"fs-extra": "^9.0.1",
"i18next": "^22.5.0",
Expand All @@ -151,7 +153,6 @@
"klaw-sync": "^6.0.0",
"leaflet": "^1.9.4",
"luxon": "^3.3.0",
"messageformat": "^2.3.0",
"moment": "^2.29.4",
"moment-timezone": "^0.5.43",
"ng-i18next": "^1.0.7",
Expand Down
3 changes: 2 additions & 1 deletion package.serve.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"prettier": "3.0.3"
},
"dependencies": {
"@messageformat/core": "^3.2.0",
"@react-navigation/native": "^6.1.7",
"@react-navigation/stack": "^6.3.17",
"@shopify/flash-list": "^1.3.1",
Expand All @@ -72,6 +73,7 @@
"chartjs-plugin-annotation": "^3.0.1",
"core-js": "^2.5.7",
"enketo-core": "^6.1.7",
"enketo-transformer": "^4.0.0",
"fast-xml-parser": "^4.2.2",
"fs-extra": "^9.0.1",
"i18next": "^22.5.0",
Expand All @@ -82,7 +84,6 @@
"klaw-sync": "^6.0.0",
"leaflet": "^1.9.4",
"luxon": "^3.3.0",
"messageformat": "^2.3.0",
"moment": "^2.29.4",
"moment-timezone": "^0.5.43",
"ng-i18next": "^1.0.7",
Expand Down
7 changes: 6 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ module.exports = {
/* Enketo expects its per-app configuration to be available as 'enketo-config',
so we have to alias it here.
https://github.com/enketo/enketo-core#global-configuration */
'enketo/config': path.resolve(__dirname, 'www/js/config/enketo-config')
'enketo/config': path.resolve(__dirname, 'www/js/config/enketo-config'),
/* enketo-transformer has 'libxslt' as an optional peer dependency.
We don't need it since we are only doing client-side transformations via
enketo-transformer/web (https://github.com/enketo/enketo-transformer#web).
So, we can tell webpack it's ok to ignore libxslt by aliasing it to false. */
'libxslt': false,
},
extensions: ['.web.js', '.jsx', '.tsx', '.ts', '.js'],
},
Expand Down
36 changes: 30 additions & 6 deletions www/__mocks__/cordovaMocks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import packageJsonBuild from '../../package.cordovabuild.json';
import fakeConfig from './fakeConfig.json';

export const mockCordova = () => {
window['cordova'] ||= {};
Expand Down Expand Up @@ -34,7 +35,7 @@ export const mockFile = () => {
//for consent document
const _storage = {};

export const mockBEMUserCache = () => {
export const mockBEMUserCache = (config?) => {
const _cache = {};
const messages = [];
const mockBEMUserCache = {
Expand Down Expand Up @@ -100,11 +101,20 @@ export const mockBEMUserCache = () => {
);
},
getDocument: (key: string, withMetadata?: boolean) => {
return new Promise<any[]>((rs, rj) =>
setTimeout(() => {
rs(_storage[key]);
}, 100),
);
//returns the config provided as a paramenter to this mock!
if (key == 'config/app_ui_config') {
return new Promise<any>((rs, rj) =>
setTimeout(() => {
rs(config || fakeConfig);
}, 100),
);
} else {
return new Promise<any[]>((rs, rj) =>
setTimeout(() => {
rs(_storage[key]);
}, 100),
);
}
},
isEmptyDoc: (doc) => {
if (doc == undefined) {
Expand All @@ -117,6 +127,20 @@ export const mockBEMUserCache = () => {
return false;
}
},
getAllTimeQuery: () => {
return { key: 'write_ts', startTs: 0, endTs: Date.now() / 1000 };
},
getSensorDataForInterval: (key, tq, withMetadata) => {
if (key == `manual/demographic_survey`) {
return new Promise<any>((rs, rj) =>
setTimeout(() => {
rs({ metadata: { write_ts: '1699897723' }, data: 'completed', time: '01/01/2001' });
}, 100),
);
} else {
return undefined;
}
},
};
window['cordova'] ||= {};
window['cordova'].plugins ||= {};
Expand Down
88 changes: 88 additions & 0 deletions www/__mocks__/fakeConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"version": 1,
"ts": 1655143472,
"server": {
"connectUrl": "https://openpath-test.nrel.gov/api/",
"aggregate_call_auth": "user_only"
},
"intro": {
"program_or_study": "study",
"start_month": "10",
"start_year": "2023",
"program_admin_contact": "K. Shankari",
"deployment_partner_name": "NREL",
"translated_text": {
"en": {
"deployment_partner_name": "NREL",
"deployment_name": "Testing environment for Jest testing",
"summary_line_1": "",
"summary_line_2": "",
"summary_line_3": "",
"short_textual_description": "",
"why_we_collect": "",
"research_questions": ["", ""]
},
"es": {
"deployment_partner_name": "NREL",
"deployment_name": "Ambiente prueba para las pruebas de Jest",
"summary_line_1": "",
"summary_line_2": "",
"summary_line_3": "",
"short_textual_description": "",
"why_we_collect": "",
"research_questions": ["", ""]
}
}
},
"survey_info": {
"surveys": {
"TimeUseSurvey": {
"compatibleWith": 1,
"formPath": "https://raw.githubusercontent.com/sebastianbarry/nrel-openpath-deploy-configs/surveys-info-and-surveys-data/survey-resources/data-json/time-use-survey-form-v9.json",
"labelTemplate": {
"en": "{ erea, plural, =0 {} other {# Employment/Education, } }{ da, plural, =0 {} other {# Domestic, } }",
"es": "{ erea, plural, =0 {} other {# Empleo/Educación, } }{ da, plural, =0 {} other {# Actividades domesticas, }}"
},
"labelVars": {
"da": {
"key": "Domestic_activities",
"type": "length"
},
"erea": {
"key": "Employment_related_a_Education_activities",
"type": "length"
}
},
"version": 9
}
},
"trip-labels": "ENKETO"
},
"display_config": {
"use_imperial": false
},
"profile_controls": {
"support_upload": true,
"trip_end_notification": false
},
"admin_dashboard": {
"overview_users": true,
"overview_active_users": true,
"overview_trips": true,
"overview_signup_trends": true,
"overview_trips_trend": true,
"data_uuids": true,
"data_trips": true,
"data_trips_columns_exclude": [],
"additional_trip_columns": [],
"data_uuids_columns_exclude": [],
"token_generate": true,
"token_prefix": "nrelop",
"map_heatmap": true,
"map_bubble": true,
"map_trip_lines": true,
"push_send": true,
"options_uuids": true,
"options_emails": true
}
}
29 changes: 29 additions & 0 deletions www/__mocks__/messageFormatMocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//call signature MessageFormat.compile(templage)(vars);
//in - template an vars -- {... pca: 0, ...}
//out - 1 Personal Care,

export default class MessageFormat {
constructor(locale: string) {}

compile(message: string) {
return (vars: {}) => {
let label = '';
const brokenList = message.split('}{');
console.log(brokenList);

for (let key in vars) {
brokenList.forEach((item) => {
let brokenItem = item.split(',');
if (brokenItem[0] == key) {
let getLabel = brokenItem[2].split('#');
console.log(getLabel);
label = vars[key] + ' ' + getLabel[1];
return label;
}
});
}
};
}
}

exports.MessageFormat = MessageFormat;
Loading

0 comments on commit 9a321b7

Please sign in to comment.