From c9a7dc657c76a6c05bbb2927f182c229686382b1 Mon Sep 17 00:00:00 2001 From: Foromo Ernest Teoro Date: Tue, 1 Oct 2024 17:51:23 +0100 Subject: [PATCH] Add unit test for stock out --- src/add-feature.js | 10 ++- src/features/stock-out.js | 34 ++++++++- test/mocks/mocks.js | 44 ++++++++++++ test/stock-out.spec.js | 147 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 232 insertions(+), 3 deletions(-) create mode 100644 test/stock-out.spec.js diff --git a/src/add-feature.js b/src/add-feature.js index 8819ffc..cbbfeae 100644 --- a/src/add-feature.js +++ b/src/add-feature.js @@ -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; + } } ]); diff --git a/src/features/stock-out.js b/src/features/stock-out.js index 88ec7a4..477d9d1 100644 --- a/src/features/stock-out.js +++ b/src/features/stock-out.js @@ -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', @@ -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; diff --git a/test/mocks/mocks.js b/test/mocks/mocks.js index e1c41e6..df4ad7b 100644 --- a/test/mocks/mocks.js +++ b/test/mocks/mocks.js @@ -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' + ] + } }; diff --git a/test/stock-out.spec.js b/test/stock-out.spec.js new file mode 100644 index 0000000..cac2d2d --- /dev/null +++ b/test/stock-out.spec.js @@ -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(',', '')); + } + + } + + }); + +}); + +