Skip to content

Commit

Permalink
feat: new test cases and cleanup some code
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoteo committed Nov 21, 2024
1 parent 5f2db46 commit f2efbb7
Show file tree
Hide file tree
Showing 11 changed files with 553 additions and 125 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
node_modules/
.DS_store
19 changes: 12 additions & 7 deletions src/features/stock-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { copyFileSync, writeFileSync } = require('fs');
const path = require('path');
const chalk = require('chalk');
const { Workbook } = require('exceljs');
const validator = require('validator');
const { getRowWithValueAtPosition, getTranslations, buildRowValues, getSheetGroupBeginEnd, getDefaultSurveyLabels,
addCategoryItemsToChoice
} = require('../common');
Expand Down Expand Up @@ -383,7 +384,7 @@ async function getStockReturnConfigs({
if (!argv[4]){
return true;
}
answers.form_name = argv[4];
answers.form_name = validator.escape(argv[4]);
return false;
}
},
Expand All @@ -399,8 +400,8 @@ async function getStockReturnConfigs({
}
const answer ={
title: {
'en': argv[5].split(',')[0],
'fr': argv[5].split(',')[1],
'en': validator.escape(argv[5].split(',')[0]),
'fr': validator.escape(argv[5].split(',')[1]),
}
};
Object.assign(answers, answer);
Expand All @@ -419,7 +420,7 @@ async function getStockReturnConfigs({
}
const answer = {
confirmation: {
form_name: argv[6]
form_name: validator.escape(argv[6])
}
};
Object.assign(answers, answer);
Expand All @@ -439,10 +440,10 @@ async function getStockReturnConfigs({
const answer = {
confirmation: {
title:{
'en': argv[7].split(',')[0],
'fr': argv[7].split(',')[1]
'en': validator.escape(argv[7].split(',')[0]),
'fr': validator.escape(argv[7].split(',')[1])
},
form_name: argv[6]
form_name: validator.escape(argv[6])
}
};

Expand All @@ -456,4 +457,8 @@ async function getStockReturnConfigs({
module.exports = {
updateStockReturn,
getStockReturnConfigs,
addExportCalculation,
addReturnedSummaries,
getItemRows,
getChoicesFromMessage
};
99 changes: 55 additions & 44 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 @@ -71,8 +85,7 @@ module.exports = {
useItemCategory: true,
defaultLanguage: 'fr',
},

stockOutScenario: {
stockMonitoringScenario: {
initScenario: [
'init',
'2_levels',
Expand Down Expand Up @@ -104,15 +117,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,44 +148,24 @@ module.exports = {
'by_user',
0,
],
invalidAddStockOutFeatureScenario: [
invalidFeatureCommandScenario: [
'minus', 'data', 'stock_out', 'stock_out', 'item_danger_qty', ['Stock Out', 'Stock Out']
],
},

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'
],
},

stockCountScenario: {
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
],
productCategoryScenario: [
'malaria'
],
Expand All @@ -192,6 +176,33 @@ module.exports = {
'paracetamol___count'
]

},

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',
]
}
};

Expand Down
8 changes: 4 additions & 4 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, stockMonitoringScenario } = require('./mocks/mocks');
const {
setDirToprojectConfig,
revertBackToProjectHome,
Expand Down Expand Up @@ -55,15 +55,15 @@ describe('Create and update stock_count.xlsx and properties files', () => {
for(const createdAppFormFile of createdAppFormFiles){
expect(fs.existsSync(path.join(processDir, 'forms', 'app', createdAppFormFile))).toBe(false);
}
const invalidInputScenario = stockOutScenario.invalidCommandInitScenario;
const invalidInputScenario = stockMonitoringScenario.invalidCommandInitScenario;
const stockCountChildProcess = spawnSync('../../main.js', invalidInputScenario);
if(stockCountChildProcess.error) {
expect(stockCountChildProcess.stdout.toString()).toThrow(Error);
}
let message = stockCountChildProcess.stdout.toString().replace('\n','');
expect(message).toEqual(`ERROR Unknown command ${invalidInputScenario[0]}`);

const invalidStockOutScenario = stockOutScenario.invalidAddStockOutFeatureScenario;
const invalidStockOutScenario = stockMonitoringScenario.invalidFeatureCommandScenario;
const stockOutChildProcess = spawnSync('../../main.js', invalidStockOutScenario );
if(stockOutChildProcess.error) {
expect(stockOutChildProcess.stdout.toString()).toThrow(Error);
Expand All @@ -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
12 changes: 6 additions & 6 deletions test/stock-out-feature-functions.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {
stockOutMockConfigs,
stockMonitoringConfigs,
mockConfigsWithNoFeauture
} = require('./mocks/mocks');

Expand All @@ -17,7 +17,7 @@ const {

const { getTranslations } = require('../src/common');

describe('Testing functions in Stock out feature file', () => {
describe('Testing functions in Stock return feature file', () => {
const processInitialArgv = process.argv; // Save the original argv
const workingDir = process.cwd();
const enMessage = 'cht-stock-monitoring-workflow.stock_out.tasks.stock_out = Stock out\ncht-stock-monitoring-workflow.stock_out.message.stock_at_hand = Stock at hand: {{qty}}\ncht-stock-monitoring-workflow.stock_out.message.stock_required = Stock required: {{qty}}\ncht-stock-monitoring-workflow.stock_out.message.summary_header = Summary\ncht-stock-monitoring-workflow.stock_out.message.submit_note = {{name}} has low stock of the following items\ncht-stock-monitoring-workflow.stock_out.message.summary_note = Stock out\ncht-stock-monitoring-workflow.items.paracetamol.label = Paracetamol\n';
Expand All @@ -43,7 +43,7 @@ describe('Testing functions in Stock out feature file', () => {
const messages = {};
const items = [{}];

expect(() => getItemRows(header, stockOutMockConfigs.languages, messages, items)).toThrow(TypeError);
expect(() => getItemRows(header, stockMonitoringConfigs.languages, messages, items)).toThrow(TypeError);

});

Expand All @@ -67,9 +67,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 +89,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
18 changes: 5 additions & 13 deletions test/stock-out-feature.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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,
revertBackToProjectHome,
writeTranslationMessages,
resetTranslationMessages
resetTranslationMessages,
cleanUp
} = require('./test-utils');

describe('Create and update stock_out.xlsx and properties files ', () => {
Expand All @@ -24,6 +25,7 @@ describe('Create and update stock_out.xlsx and properties files ', () => {
afterEach(async() => {
jest.clearAllMocks();
await resetTranslationMessages(process.cwd());
cleanUp(workingDir, createdAppFormFiles);
revertBackToProjectHome(workingDir);
});

Expand All @@ -47,7 +49,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 Expand Up @@ -89,16 +91,6 @@ describe('Create and update stock_out.xlsx and properties files ', () => {
}
]
});

// Delete generated stock out files
for(const createdAppFormFile of createdAppFormFiles){
fs.stat(path.join(processDir, 'forms', 'app', createdAppFormFile), (error) => {
if (!error) {
expect(fs.unlinkSync(path.join(processDir, 'forms', 'app', createdAppFormFile))).toBe(undefined);
}
});
}

});

});
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 { stockOutScenario, stockCountScenario, stockMonitoringScenario } = 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

0 comments on commit f2efbb7

Please sign in to comment.