Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unit test for form update and stock returned #97

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions test/stock-count.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { spawnSync } = require('child_process');
const path = require('path');
const fs = require('fs-extra');

const { stockCountScenario, stockOutScenario } = require('./mocks/mocks');
const {stockCountScenario, stockOutScenario, stockMonitoringScenario } = require('./mocks/mocks');
const {
setDirToprojectConfig,
revertBackToProjectHome,
Expand Down Expand Up @@ -85,7 +85,7 @@ describe('Create and update stock_count.xlsx and properties files', () => {
expect(fs.existsSync(path.join(processDir, 'forms', 'app', createdAppFormFile))).toBe(false);
}

const childProcess = spawnSync('../../main.js', stockCountScenario.initScenario);
const childProcess = spawnSync('../../main.js', stockMonitoringScenario.initScenario);

if (childProcess.error) {
throw childProcess.error;
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]);
}

}
});

});
9 changes: 5 additions & 4 deletions test/stock-out-feature-functions.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const {
stockOutMockConfigs,
stockOutMockConfigs,
stockMonitoringConfigs,
mockConfigsWithNoFeauture
} = require('./mocks/mocks');

Expand Down Expand Up @@ -67,9 +68,9 @@ describe('Testing functions in Stock out feature file', () => {
];

const messages = getTranslations();
const items = Object.values(stockOutMockConfigs.items);
const items = Object.values(stockMonitoringConfigs.items);

const row = getItemRows(header, stockOutMockConfigs.languages, messages, items);
const row = getItemRows(header, stockMonitoringConfigs.languages, messages, items);
expect(row).not.toEqual([]);
expect(row.length).toBe(items.length);
expect(row[0].length).toBe(7);
Expand All @@ -89,7 +90,7 @@ describe('Testing functions in Stock out feature file', () => {
formular: 'item_danger_qty',
title: { en: 'Stock Out', fr: ' Rupture de Stock' }
};
const featureConfigs = await getStockOutConfigs(stockOutMockConfigs);
const featureConfigs = await getStockOutConfigs(stockMonitoringConfigs);
expect(featureConfigs).toEqual(configs);

});
Expand Down
4 changes: 2 additions & 2 deletions test/stock-out-feature.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const ExcelJS = require('exceljs');
const fs = require('fs');
const path = require('path');
const { stockOutMockConfigs, mockConfigsWithNoFeauture } = require('./mocks/mocks');
const { stockMonitoringConfigs, mockConfigsWithNoFeauture } = require('./mocks/mocks');
const { updateStockOut } = require('../src/features/stock-out');
const {
setDirToprojectConfig,
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('Create and update stock_out.xlsx and properties files ', () => {
expect(fs.existsSync(path.join(processDir, 'forms', 'app', createdAppFormFile))).toBe(false);
}
// Call the function updateStockOut and check that the stock_out files are generated
await updateStockOut(stockOutMockConfigs);
await updateStockOut(stockMonitoringConfigs);

for(const createdAppFormFile of createdAppFormFiles){
expect(fs.existsSync(path.join(processDir, 'forms', 'app', createdAppFormFile))).toBe(true);
Expand Down
4 changes: 2 additions & 2 deletions test/stock-out-integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { spawnSync } = require('child_process');
const path = require('path');
const fs = require('fs-extra');

const { stockOutScenario, stockCountScenario } = require('./mocks/mocks');
const { stockMonitoringScenario, stockOutScenario, stockCountScenario } = require('./mocks/mocks');
const {
setDirToprojectConfig,
revertBackToProjectHome,
Expand Down Expand Up @@ -43,7 +43,7 @@ describe('Create and update stock_out.xlsx and properties files', () => {
expect(fs.existsSync(path.join(processDir, 'forms', 'app', createdAppFormFile))).toBe(false);
}

const childProcess = spawnSync('../../main.js', stockOutScenario.initScenario);
const childProcess = spawnSync('../../main.js', stockMonitoringScenario.initScenario);

if (childProcess.error) {
throw childProcess.error;
Expand Down
Loading
Loading