Skip to content

Commit

Permalink
fix: feedback from PR review and add new test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoteo committed Nov 19, 2024
1 parent 077bd3d commit 9349743
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 831 deletions.
1 change: 1 addition & 0 deletions src/features/stock-out.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,5 @@ async function getStockOutConfigs({
module.exports = {
getStockOutConfigs,
updateStockOut,
getItemRows
};
340 changes: 0 additions & 340 deletions test/project-config/translations/messages-en.properties

Large diffs are not rendered by default.

475 changes: 0 additions & 475 deletions test/project-config/translations/messages-fr.properties

Large diffs are not rendered by default.

35 changes: 29 additions & 6 deletions test/stock-count.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,43 @@ const {
setDirToprojectConfig,
revertBackToProjectHome,
cleanUp,
readDataFromXforms
readDataFromXforms,
writeTranslationMessages,
resetTranslationMessages
} = require('./test-utils');


describe('Stock count', () => {
describe('Create and update stock_count.xlsx and properties files', () => {
const workingDir = process.cwd();
const createdAppFormFiles = ['stock_count.properties.json', 'stock_count.xlsx'];


beforeEach(() => {
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(() => {
afterEach(async() => {
await resetTranslationMessages(process.cwd());
cleanUp(workingDir, createdAppFormFiles);
revertBackToProjectHome(workingDir);
});
Expand Down
102 changes: 102 additions & 0 deletions test/stock-out-feature-functions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const {
stockOutMockConfigs,
mockConfigsWithNoFeauture
} = require('./mocks/mocks');

const {
getItemRows,
getStockOutConfigs
} = require('../src/features/stock-out');

const {
setDirToprojectConfig,
revertBackToProjectHome,
writeTranslationMessages,
resetTranslationMessages
} = require('./test-utils');

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

describe('Testing functions in Stock out 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';
const frMessage = 'cht-stock-monitoring-workflow.stock_out.tasks.stock_out = Stock épuisé\ncht-stock-monitoring-workflow.stock_out.message.stock_at_hand = Stock actuel: {{qty}}\ncht-stock-monitoring-workflow.stock_out.message.stock_required = Stock nécessaire: {{qty}}\ncht-stock-monitoring-workflow.stock_out.message.summary_header = Résumé\ncht-stock-monitoring-workflow.stock_out.message.submit_note = {{name}} a épuisé son stock des éléments suivants:\ncht-stock-monitoring-workflow.stock_out.message.summary_note = Stock épuisé\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);
process.argv = processInitialArgv; // Restore the original argv after each test
});

/** Testing getItemRows function */
// Testing get item row function out config generation with no or wrong item config and params
it('This should throw error and should not generate rows with empty header, messages, languages(fr, en) and items to be added ', async () => {

const header = [];
const messages = {};
const items = [{}];

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

});

// Testing get item row function out config generation with correct item config and params
it('This should return rows with header, messages, languages(fr, en) and items to be added ', async () => {

const header = [
'type',
'name',
'required',
'relevant',
'appearance',
'constraint',
'constraint_message',
'calculation',
'default',
'label::en',
'label::fr',
'hint:en',
'hint:fr'
];

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

const row = getItemRows(header, stockOutMockConfigs.languages, messages, items);
expect(row).not.toEqual([]);
expect(row.length).toBe(items.length);
expect(row[0].length).toBe(7);
const rowData = row[0][0];
expect(rowData.length).toBe(13);
expect(rowData[0]).toEqual('note');
expect(rowData[1]).toContain(items[0].name);

});

/** Testing getStockOutConfigs function */
// Testing stock out config generation with correct item config
it('This should return stock out configurations based the item config provided ', async () => {
process.argv = ['node', '', '','', '' ,'stock_out', 'item_danger_qty', 'Stock Out, Rupture de Stock'];
const configs = {
form_name: 'stock_out',
formular: 'item_danger_qty',
title: { en: 'Stock Out', fr: ' Rupture de Stock' }
};
const featureConfigs = await getStockOutConfigs(stockOutMockConfigs);
expect(featureConfigs).toEqual(configs);

});

// Testing stock out config not generating with no or wrong item config
it('This should throw error for xform configurations with no or wrong item config', async () => {
await expect(getStockOutConfigs(mockConfigsWithNoFeauture)).rejects.toThrow(Error);
});

});
19 changes: 13 additions & 6 deletions test/stock-out-feature.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,29 @@ const { stockOutMockConfigs, mockConfigsWithNoFeauture } = require('./mocks/mock
const { updateStockOut } = require('../src/features/stock-out');
const {
setDirToprojectConfig,
revertBackToProjectHome
revertBackToProjectHome,
writeTranslationMessages,
resetTranslationMessages
} = require('./test-utils');

describe('updateStockOut', () => {
describe('Create and update stock_out.xlsx and properties files ', () => {
const workingDir = process.cwd();
const createdAppFormFiles = ['stock_out.properties.json', 'stock_out.xlsx'];
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';
const frMessage = 'cht-stock-monitoring-workflow.stock_out.tasks.stock_out = Stock épuisé\ncht-stock-monitoring-workflow.stock_out.message.stock_at_hand = Stock actuel: {{qty}}\ncht-stock-monitoring-workflow.stock_out.message.stock_required = Stock nécessaire: {{qty}}\ncht-stock-monitoring-workflow.stock_out.message.summary_header = Résumé\ncht-stock-monitoring-workflow.stock_out.message.submit_note = {{name}} a épuisé son stock des éléments suivants:\ncht-stock-monitoring-workflow.stock_out.message.summary_note = Stock épuisé\ncht-stock-monitoring-workflow.items.paracetamol.label = Paracetamole\n';

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

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

it('should not generate and update stock out form', async () => {
it('should not generate and update stock_out.xlsx form when no feature provider in the config', async () => {
const projectDataDir = process.cwd();
// Check that stock out xlsx and properties files does not exist.
for(const createdAppFormFile of createdAppFormFiles){
Expand All @@ -33,7 +39,7 @@ describe('updateStockOut', () => {

});

it('should update the stock out form with correct values', async () => {
it('should generate and update the stock_out.xlsx form with correct values when a config provided with the stock_out feature', async () => {
const processDir = process.cwd();

// Check that stock out xlsx and properties files exist.
Expand Down Expand Up @@ -94,4 +100,5 @@ describe('updateStockOut', () => {
}

});

});
16 changes: 13 additions & 3 deletions test/stock-out-integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,30 @@ const {
setDirToprojectConfig,
revertBackToProjectHome,
cleanUp,
readDataFromXforms
readDataFromXforms,
writeTranslationMessages,
resetTranslationMessages
} = require('./test-utils');


describe('Stock out integration test', () => {
describe('Create and update stock_out.xlsx and properties files', () => {
const workingDir = process.cwd();
const createdAppFormFiles = ['stock_count.properties.json', 'stock_count.xlsx', 'stock_out.properties.json', 'stock_out.xlsx'];
const enStockCountMessage = 'cht-stock-monitoring-workflow.stock_count.balance_fill = Use this form to fill in balances on hand for all commodities as of today\ncht-stock-monitoring-workflow.stock_count.commodities_note = <h3 class="text-primary"> Commodities Balance on hand </h3>\ncht-stock-monitoring-workflow.stock_count.message.summary_header = Results/Summary page\ncht-stock-monitoring-workflow.stock_count.contact_summary.title = Stock count\ncht-stock-monitoring-workflow.stock_count.message.submit_note = <h4 style="text-align:center;">Be sure you Submit to complete this action.</h4>\ncht-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\ncht-stock-monitoring-workflow.stock_count.forms.additional_doc_title = Stock uses\ncht-stock-monitoring-workflow.stock_count.forms.item_used_question = Quantity of {{item}}\ncht-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}}\ncht-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 frStockCountMessage = 'cht-stock-monitoring-workflow.stock_count.balance_fill = Use this form to fill in balances on hand for all commodities as of today\ncht-stock-monitoring-workflow.stock_count.commodities_note = <h3 class="text-primary"> Commodities Balance on hand </h3>\ncht-stock-monitoring-workflow.stock_count.message.summary_header = Results/Summary page\ncht-stock-monitoring-workflow.stock_count.contact_summary.title = Stock count\ncht-stock-monitoring-workflow.stock_count.message.submit_note = <h4 style="text-align:center;">Be sure you Submit to complete this action.</h4>\ncht-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\ncht-stock-monitoring-workflow.stock_count.forms.additional_doc_title = Stock uses\ncht-stock-monitoring-workflow.stock_count.forms.item_used_question = Quantity of {{item}}\ncht-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}}\ncht-stock-monitoring-workflow.stock_count.message.unit_quantity_hint = Add the quantity: {{quantity}} {{unit_label}}\ncht-stock-monitoring-workflow.items.paracetamol.label = Paracetamole\n';

const enStockOutMessage = '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';
const frStockOutMessage = 'cht-stock-monitoring-workflow.stock_out.tasks.stock_out = Stock épuisé\ncht-stock-monitoring-workflow.stock_out.message.stock_at_hand = Stock actuel: {{qty}}\ncht-stock-monitoring-workflow.stock_out.message.stock_required = Stock nécessaire: {{qty}}\ncht-stock-monitoring-workflow.stock_out.message.summary_header = Résumé\ncht-stock-monitoring-workflow.stock_out.message.submit_note = {{name}} a épuisé son stock des éléments suivants:\ncht-stock-monitoring-workflow.stock_out.message.summary_note = Stock épuisé\ncht-stock-monitoring-workflow.items.paracetamol.label = Paracetamole\n';


beforeEach(() => {
beforeEach( async() => {
setDirToprojectConfig();
await writeTranslationMessages(frStockCountMessage, enStockCountMessage, process.cwd());
await writeTranslationMessages(frStockOutMessage, enStockOutMessage, process.cwd());
});

afterEach(async() => {
await resetTranslationMessages(process.cwd());
cleanUp(workingDir, createdAppFormFiles);
revertBackToProjectHome(workingDir);
});
Expand Down
22 changes: 21 additions & 1 deletion test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,33 @@ const readDataFromXforms = async (productCategoryScenario, productsScenario, fil

};

const writeTranslationMessages = async (frMessages, enMessages, workingDir) => {
const translationFiles = fs.readdirSync(path.join(workingDir, 'translations'));
for(const translationFile of translationFiles){
if(translationFile.includes('messages-en')) {
await fs.writeFile(path.join(workingDir, 'translations', translationFile), enMessages.toString().replaceAll(',', ''));
}else{
await fs.writeFile(path.join(workingDir, 'translations', translationFile), frMessages.toString().replaceAll(',', ''));
}
}
};

const resetTranslationMessages = async (workingDir) => {
const translationFiles = fs.readdirSync(path.join(workingDir, 'translations'));
for(const translationFile of translationFiles){
await fs.truncate(path.join(workingDir, 'translations', translationFile), 0, function () {});
}
};


module.exports = {
setDirToprojectConfig,
currentWorkingDirectory,
revertBackToProjectHome,
cleanUp,
readDataFromXforms
readDataFromXforms,
writeTranslationMessages,
resetTranslationMessages
};


Expand Down

0 comments on commit 9349743

Please sign in to comment.