Skip to content

Commit

Permalink
Add unit test for stock out
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoteo committed Oct 1, 2024
1 parent d956af7 commit c9a7dc6
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/add-feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ async function selectFeature(configs) {
choices: remainingFeatures.map((ft) => ({
name: FEATURES[ft],
value: ft,
}))
})),
when: function (answers){
const argv = process.argv;
if (!argv[4]){
return true;
}
answers.name = argv[4];
return false;
}
}
]);

Expand Down
34 changes: 32 additions & 2 deletions src/features/stock-out.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,15 @@ async function getStockOutConfigs({
type: 'input',
name: 'form_name',
message: 'Enter stock out form ID',
default: 'stock_out'
default: 'stock_out',
when: function (answers){
const argv = process.argv;
if (!argv[5]){
return true;
}
answers.form_name = argv[5];
return false;
}
},
{
type: 'list',
Expand All @@ -279,12 +287,34 @@ async function getStockOutConfigs({
value: 'weekly_qty'
}
],
when: function (answers){
const argv = process.argv;
if (!argv[6]){
return true;
}
answers.formular = argv[6];
return false;
}
},
...languages.map((language) => ({
type: 'input',
name: `title.${language}`,
message: `Enter stock out form title in ${language}`,
default: 'Stock Out'
default: 'Stock Out',
when: function (answers){
const argv = process.argv;
if (!argv[7]){
return true;
}
const answer = {
title:{
'en': argv[7].split(',')[0],
'fr': argv[7].split(',')[1]
}
};
Object.assign(answers, answer);
return false;
}
}))
]);
return configs;
Expand Down
44 changes: 44 additions & 0 deletions test/mocks/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,50 @@ module.exports = {
'paracetamol___count'
]

},

stockOutScenario: {
initScenario: [
'init',
'2_levels',
'c62_chw',
'chw',
'c52_supervisor',
'supervisor',
'Y',
'stock_count',
'[{contact_type: \'c62_chw\', role: \'chw\', place_type: \'c60_chw_site\' },{contact_type: \'c52_supervisor\',role: \'supervisor\',place_type: \'c50_supervision_area\'}]',
'action',
'end_of_week',
['Stock count', 'Stock count'],
'patient_assessment_under_5',
'Y',
'now()',
'malaria',
['Category', 'Categorie'],
['Category', 'Categorie'],
'paracetamol',
['Paracetamol', 'Paracetamole'],
'Y',
['Box of 8', 'Boite de 8'],
8,
['Tablet', 'Comprimes'],
20,
15,
15,
'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'
]

}
};

Expand Down
147 changes: 147 additions & 0 deletions test/stock-out.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
const { spawnSync } = require('child_process');
const path = require('path');
const fs = require('fs-extra');
//const { once } = require('events');
const ExcelJS = require('exceljs');

const { stockOutScenario, stockCountScenario } = require('./mocks/mocks');
const {
setDirToprojectConfig,
revertBackToProjectHome
} = require('./test-utils');


describe('Stock out', () => {
const workingDir = process.cwd();

beforeEach(() => {
revertBackToProjectHome(workingDir);
setDirToprojectConfig();
});

afterEach(async() => {
revertBackToProjectHome(workingDir);
});

it('Add stock out summaries test', async() => {
const processDir = process.cwd();
const workbook = new ExcelJS.Workbook();
const childProcess = spawnSync('../../main.js', stockOutScenario.initScenario);

if(childProcess.status === 0) {
// Check that stock monitoring is initialized and stock count and stock out xform and properties files are generated
//const stockMonitoringConfig = path.join(processDir, 'stock-monitoring.config.json');

const formFiles = fs.readdirSync(path.join(processDir, 'forms', 'app'));
for(const formFile of formFiles){
if(formFile.toString().includes('stock_count') || formFile.toString().includes('stock_out')){
expect(fs.existsSync(path.join(processDir, 'forms', 'app', formFile))).toBe(true);
}
}

expect(fs.existsSync(path.join(processDir, 'stock-monitoring.config.json'))).toBe(true);

// Check that the products are available in stock count xform
const productCategoryList = stockCountScenario.productCategoryScenario;
const productsList = stockCountScenario.productsScenario;
await workbook.xlsx.readFile(path.join(processDir, 'forms', 'app', `stock_count.xlsx`));
const surveyWorkSheet = workbook.getWorksheet('survey');
const nameCol = surveyWorkSheet.getColumn('B');
const cellProductCategoriesList = [];
const cellProductsList = [];
let productIndex = 0;
let productCatIndex =0;
nameCol.eachCell(function(cell){

if(cell.value === productCategoryList[productCatIndex] && productCatIndex < productCategoryList.length){
cellProductCategoriesList.push(cell.value);
productCatIndex ++;
productIndex = 0;
}

if(cell.value === productsList[productIndex] && productIndex < productsList.length){
cellProductsList.push(cell.value);
productIndex ++;
}

});

expect(productsList.length).toBe(cellProductsList.length);
expect(productsList.entries).toStrictEqual(cellProductsList.entries);

expect(productCategoryList.length).toBe(cellProductCategoriesList.length);
expect(productCategoryList.entries).toStrictEqual(cellProductCategoriesList.entries);

// Add stock out feature test
const stockOutChildProcess = spawnSync('../../main.js', stockOutScenario.addStockOutFeatureScenario);
if(stockOutChildProcess.status === 0){
const stockOutFormFiles = fs.readdirSync(path.join(processDir, 'forms', 'app'));
for(const formFile of stockOutFormFiles){
if(formFile.toString().includes('stock_out')){
expect(fs.existsSync(path.join(processDir, 'forms', 'app', formFile))).toBe(true);
}
}

// Check that the products are available in stock out xform
const stockOutProductsList = stockOutScenario.productsScenario;
await workbook.xlsx.readFile(path.join(processDir, 'forms', 'app', `stock_out.xlsx`));
const surveyWorkSheet = workbook.getWorksheet('survey');
const nameCol = surveyWorkSheet.getColumn('B');
const stockOutCellProductsList = [];
productIndex = 0;
nameCol.eachCell(function(cell){
if(cell.value === stockOutProductsList[productIndex] && productIndex < stockOutProductsList.length){
stockOutCellProductsList.push(cell.value);
productIndex ++;
}
});

expect(stockOutProductsList.length).toBe(stockOutCellProductsList.length);
expect(stockOutProductsList.entries).toStrictEqual(stockOutCellProductsList.entries);

}

const stockOutFormFiles = fs.readdirSync(path.join(processDir, 'forms', 'app'));

for(const formFile of stockOutFormFiles){
if(formFile.toString().includes('stock_out') || formFile.toString().includes('stock_count')){
expect(fs.unlinkSync(path.join(processDir, 'forms', 'app', formFile))).toBe(undefined);
}
}

// Removing the stock monitoring init file and stock count file
const stockMonitoringInitPath = path.join(processDir, 'stock-monitoring.config.json');
//const stockMonitoringInitFile = fs.stat
fs.stat(stockMonitoringInitPath, (error) => {
if (error) {
//console.log(error);
}
else {
expect(fs.unlinkSync(stockMonitoringInitPath)).toBe(undefined);
//console.log(stats);
}
});

const translationFiles = fs.readdirSync(path.join(processDir, 'translations'));
for(const translationFile of translationFiles){

const messageFileContent = fs.readFileSync(path.join(processDir, 'translations', translationFile), {encoding: 'utf-8'});
expect(messageFileContent).not.toBe('');
const newMessageContent = messageFileContent.split('\n').map(message => {
if(!message.toString().includes('cht-stock-monitoring-workflow') && message.toString()!==''){
return `${message.toString()}\n`;
}
});

expect(newMessageContent.includes('cht-stock-monitoring-workflow')).toBe(false);
fs.truncate(path.join(processDir, 'translations', translationFile), 0, function () {});
fs.writeFile(path.join(processDir, 'translations', translationFile),newMessageContent.toString().replaceAll(',', ''));
}

}

});

});


0 comments on commit c9a7dc6

Please sign in to comment.