Skip to content

Commit

Permalink
feat: unit test for form update and stock returned
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoteo committed Dec 11, 2024
1 parent c4470e1 commit 2bc16a2
Show file tree
Hide file tree
Showing 5 changed files with 458 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/features/form-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,5 @@ async function handleSettingsSheet(workbook) {

module.exports = {
updateForm,
handleSettingsSheet
};
5 changes: 5 additions & 0 deletions src/features/stock-returned.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,9 @@ async function updateStockReturned(configs) {

module.exports = {
updateStockReturned,
getItemRows,
getAdditionalDoc,
addExportCalculation,
addReturnedSummaries,
getLabelColumns
};
91 changes: 75 additions & 16 deletions test/mocks/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
},
},
},
stockOutMockConfigs: {
stockMonitoringConfigs: {
features: {
stock_out: {
form_name: 'stock_out',
Expand All @@ -16,6 +16,20 @@ module.exports = {
fr: 'Titre du Stock'
},
},
stock_return: {
form_name: 'stock_return',
title: {
en: 'Stock Return',
fr: 'Retour de Stock'
},
confirmation: {
form_name: 'stock_returned',
title: {
en: 'Stock Returned',
fr: 'Stock Retourné'
},
}
}
},
levels: {
1: {
Expand Down Expand Up @@ -70,9 +84,21 @@ module.exports = {
},
useItemCategory: true,
defaultLanguage: 'fr',
forms: {
patient_assessment_under_5: {
items: {
paracetamol: {
deduction_type: 'by_user',
formular: '${s_constant_child_temperature} >= 38.5'
}
},
name: 'patient_assessment_under_5',
reportedDate: 'now()'
},
}
},

stockOutScenario: {
stockMonitoringScenario: {
initScenario: [
'init',
'2_levels',
Expand Down Expand Up @@ -104,15 +130,6 @@ module.exports = {
'by_user',
0,
],
addStockOutFeatureScenario: [
'add', 'feature', 'stock_out', 'stock_out', 'item_danger_qty', ['Stock Out', 'Stock Out']
],
productsScenario: [
'paracetamol_at_hand___set',
'paracetamol_at_hand___unit',
'paracetamol_required___set',
'paracetamol_required___unit'
],
invalidCommandInitScenario: [
'test',
'2_levels',
Expand Down Expand Up @@ -144,15 +161,50 @@ module.exports = {
'by_user',
0,
],
invalidAddStockOutFeatureScenario: [
invalidFeatureCommandScenario: [
'minus', 'data', 'stock_out', 'stock_out', 'item_danger_qty', ['Stock Out', 'Stock Out']
],
},

stockReturnScenario: {
addFeatureScenario: [
'add',
'feature',
'stock_return',
['Stock Return', 'Retour de Stock'],
'stock_returned',
['Stock Returned', 'Stock Retourné']
],
productCategoryScenario: [
'malaria'
],
productsScenario: [
'paracetamol___set',
'paracetamol___unit',
],
productReturnedScenario: [
'paracetamol__return___set',
'paracetamol__return___unit',
'paracetamol_received',
'paracetamol___set',
'paracetamol___unit',
'paracetamol_received_qty',
'paracetamol___count',
]
},

stockCountScenario: {
initScenario: [
'init',
stockOutScenario: {
addStockOutFeatureScenario: [
'add', 'feature', 'stock_out', 'stock_out', 'item_danger_qty', ['Stock Out', 'Stock Out']
],
productsScenario: [
'paracetamol_at_hand___set',
'paracetamol_at_hand___unit',
'paracetamol_required___set',
'paracetamol_required___unit'
],
invalidCommandInitScenario: [
'test',
'2_levels',
'c62_chw',
'chw',
Expand Down Expand Up @@ -180,8 +232,15 @@ module.exports = {
15,
15,
'by_user',
0
0,
],
invalidAddStockOutFeatureScenario: [
'minus', 'data', 'stock_out', 'stock_out', 'item_danger_qty', ['Stock Out', 'Stock Out']
],

},

stockCountScenario: {
productCategoryScenario: [
'malaria'
],
Expand Down
83 changes: 83 additions & 0 deletions test/stock-form-update-feature-functions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const {
stockMonitoringConfigs,
} = require('./mocks/mocks');
const {
updateForm,
} = require('../src/features/form-update');
const {
setDirToprojectConfig,
revertBackToProjectHome,
writeTranslationMessages,
resetTranslationMessages,
readDataFromXforms
} = require('./test-utils');

describe('Create and update stock_out.xlsx and properties files ', () => {
const workingDir = process.cwd();

const enMessage = 'cht-stock-monitoring-workflow.stock_count.balance_fill = Use this form to fill in balances on hand for all commodities as of today\n'+
'cht-stock-monitoring-workflow.stock_count.commodities_note = <h3 class="text-primary"> Commodities Balance on hand </h3>\n'+
'cht-stock-monitoring-workflow.stock_count.message.summary_header = Results/Summary page\n'+
'cht-stock-monitoring-workflow.stock_count.contact_summary.title = Stock count\n'+
'cht-stock-monitoring-workflow.stock_count.message.submit_note = <h4 style="text-align:center;">Be sure you Submit to complete this action.</h4>\n'+
'cht-stock-monitoring-workflow.stock_count.message.summary_note = Stock items you currently have.<i class="fa fa-list-ul"></i>\ncht-stock-monitoring-workflow.stock_count.tasks.stock-count = Stock count\n'+
'cht-stock-monitoring-workflow.stock_count.forms.additional_doc_title = Stock uses\ncht-stock-monitoring-workflow.stock_count.forms.item_used_question = Quantity of {{item}}\n'+
'cht-stock-monitoring-workflow.stock_count.message.set_unit_constraint_message = Should be in the form x/y for x {{set_label}} and y {{unit_label}}\n'+
'cht-stock-monitoring-workflow.stock_count.message.unit_quantity_hint = Add the quantity: {{quantity}} {{unit_label}}\ncht-stock-monitoring-workflow.items.paracetamol.label = Paracetamol\n';

const frMessage = 'cht-stock-monitoring-workflow.stock_count.balance_fill = Use this form to fill in balances on hand for all commodities as of today\n'+
'cht-stock-monitoring-workflow.stock_count.commodities_note = <h3 class="text-primary"> Commodities Balance on hand </h3>\n'+
'cht-stock-monitoring-workflow.stock_count.message.summary_header = Results/Summary page\n'+
'cht-stock-monitoring-workflow.stock_count.contact_summary.title = Stock count\n'+
'cht-stock-monitoring-workflow.stock_count.message.submit_note = <h4 style="text-align:center;">Be sure you Submit to complete this action.</h4>\n'+
'cht-stock-monitoring-workflow.stock_count.message.summary_note = Stock items you currently have.<i class="fa fa-list-ul"></i>\n'+
'cht-stock-monitoring-workflow.stock_count.tasks.stock-count = Stock count\n'+
'cht-stock-monitoring-workflow.stock_count.forms.additional_doc_title = Stock uses\n'+
'cht-stock-monitoring-workflow.stock_count.forms.item_used_question = Quantity of {{item}}\n'+
'cht-stock-monitoring-workflow.stock_count.message.set_unit_constraint_message = Should be in the form x/y for x {{set_label}} and y {{unit_label}}\n'+
'cht-stock-monitoring-workflow.stock_count.message.unit_quantity_hint = Add the quantity: {{quantity}} {{unit_label}}\ncht-stock-monitoring-workflow.items.paracetamol.label = Paracetamole\n';


beforeEach(async () => {
setDirToprojectConfig();
await writeTranslationMessages(frMessage, enMessage, process.cwd());
});

afterEach(async() => {
jest.clearAllMocks();
await resetTranslationMessages(process.cwd());
revertBackToProjectHome(workingDir);
});


it('should update forms from the configs', async () => {
const items = Object.values(stockMonitoringConfigs.items);
await updateForm(stockMonitoringConfigs);

// Check that items are inserted in forms from config
const formConfigs = Object.values(stockMonitoringConfigs.forms);
const categories = Object.values(stockMonitoringConfigs.categories);

const names = [];
for (const formConfig of formConfigs) {
const formItemIds = Object.keys(formConfig.items);
const formItems = items.filter(item => formItemIds.includes(item.name));
categories.map((category) => {
formItems.filter(item => item.category === category.name && item.isInSet).map((item) => {
names.push(`${item.name}___set`);
names.push(`${item.name}___unit`);
names.push(`${item.name}_used_in_${formConfig.name}`);
});
});

const stockReturnData = await readDataFromXforms([], names, `${formConfig.name}.xlsx` );

expect(stockReturnData.productsList.length).toBe(names.length);
for(let index =0; index < names.length; index ++){
expect(stockReturnData.productsList[index]).toEqual(names[index]);
}

}
});

});
Loading

0 comments on commit 2bc16a2

Please sign in to comment.