Skip to content

Commit

Permalink
fix: review feedback and added unit test stock out
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoteo committed Oct 22, 2024
1 parent c9a7dc6 commit 69f6cb9
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 855 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"scripts": {
"cz": "cz",
"test": "jest",
"test": "jest --testTimeout=25000",
"semantic-release": "semantic-release",
"prepare": "husky"
},
Expand Down
88 changes: 88 additions & 0 deletions test/mocks/mocks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,92 @@
module.exports = {
mockConfigs: {
features: {
stock_count: {
form_name: 'stock_count',
contact_types: [
{
contact_type: 'c62_chw',
role: 'chw',
place_type: 'c60_chw_site'
},
{
contact_type: 'c52_supervisor',
role: 'supervisor',
place_type: 'c50_supervision_area'
}
],
type: 'action',
title: {
en: 'Stock count',
fr: 'Stock count'
}
},
stock_out: {
form_name: 'stock_out',
formular: 'item_danger_qty',
title: {
en: 'Stock Out Title',
fr: 'Titre du Stock'
},
},
},
levels: {
1: {
contact_type: 'c62_chw',
role: 'chw',
place_type: 'c62_chw_site'
},
2: {
contact_type: 'c52_supervisor',
role: 'supervisor',
place_type: 'c50_supervision_area'
},
},
languages: ['en', 'fr'],
items: {
paracetamol: {
name: 'paracetamol',
label: {
en: 'Paracetamol',
fr: 'Paracetamole'
},
isInSet: 'Y',
set: {
label: {
en: 'Box of 8',
fr: 'Boite de 8'
},
count: '8'
},
unit: {
label: {
en: 'Tablet',
fr: 'Comprimes'
}
},
warning_total: '20',
danger_total: '15',
max_total: '15',
category: 'malaria'
}
},
categories: {
malaria: {
name: 'malaria',
label: {
fr: 'Categorie'
},
description: {
fr: 'Categorie'
}
}
},
useItemCategory: true,
defaultLanguage: 'fr',
version: '1.1.3',
last_update_date: '2024-10-21T11:09:33.013Z'
},

stockCountScenario: {
initScenario: [
'init',
Expand Down
339 changes: 7 additions & 332 deletions test/project-config/translations/messages-en.properties

Large diffs are not rendered by default.

474 changes: 7 additions & 467 deletions test/project-config/translations/messages-fr.properties

Large diffs are not rendered by default.

84 changes: 84 additions & 0 deletions test/stock-out-feature.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const ExcelJS = require('exceljs');
const fs = require('fs');
const path = require('path');
const { mockConfigs } = require('./mocks/mocks');
const { updateStockOut } = require('../src/features/stock-out');
const {
setDirToprojectConfig,
} = require('./test-utils');

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

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

afterEach(() => {
jest.clearAllMocks();
process.chdir(workingDir);
});

it('should update the stock out form with correct values', async () => {
const createdAppFormFiles = ['stock_out.properties.json', 'stock_out.xlsx'];
const processDir = process.cwd();

// Check that stock out xlsx and properties files exist.
for(const createdAppFormFile of createdAppFormFiles){
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(mockConfigs);

for(const createdAppFormFile of createdAppFormFiles){
expect(fs.existsSync(path.join(processDir, 'forms', 'app', createdAppFormFile))).toBe(true);
}

// Check that stock out files content are correctly written.
const formPath = path.join(processDir, 'forms', 'app', 'stock_out.xlsx');
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.readFile(formPath);
const spy = jest.spyOn(workbook, 'getWorksheet');
const surveyWorkSheet = workbook.getWorksheet('survey');
expect(spy).toHaveBeenCalledTimes(1);
const settingWorkSheet = workbook.getWorksheet('settings');
expect(spy).toHaveBeenCalledTimes(2);
expect(surveyWorkSheet).not.toEqual([]);
expect(settingWorkSheet).not.toEqual([]);


const propertiesFileContent = fs.readFileSync(
path.join(processDir, 'forms', 'app', 'stock_out.properties.json'),
{encoding: 'utf-8'}
);

expect(JSON.parse(propertiesFileContent)).toEqual({
'context': {
'expression': 'user.parent.contact_type === \'c50_supervision_area\'',
'person': false,
'place': false
},
'icon': 'icon-healthcare-medicine',
'title': [
{
'content': 'Stock Out Title',
'locale': 'en'
},
{
'content': 'Titre du Stock',
'locale': 'fr'
}
]
});

// 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);
}
});
}

});
});
71 changes: 17 additions & 54 deletions test/stock-out.spec.js → test/stock-out-integration.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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
revertBackToProjectHome,
cleanUp
} = require('./test-utils');


Expand All @@ -20,6 +20,7 @@ describe('Stock out', () => {
});

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

Expand All @@ -28,15 +29,15 @@ describe('Stock out', () => {
const workbook = new ExcelJS.Workbook();
const childProcess = spawnSync('../../main.js', stockOutScenario.initScenario);

if(childProcess.status === 0) {
if (childProcess.error) {
throw childProcess.error;
}
else {
// 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 createdAppFormFiles = ['stock_count.properties.json', 'stock_count.xlsx'];

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);
}
for(const createdAppFormFile of createdAppFormFiles){
expect(fs.existsSync(path.join(processDir, 'forms', 'app', createdAppFormFile))).toBe(true);
}

expect(fs.existsSync(path.join(processDir, 'stock-monitoring.config.json'))).toBe(true);
Expand Down Expand Up @@ -74,12 +75,13 @@ describe('Stock out', () => {

// 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);
}
if (stockOutChildProcess.error) {
throw stockOutChildProcess.error;
}
else {
const createdAppFormFiles = ['stock_out.properties.json', 'stock_out.xlsx'];
for(const createdAppFormFile of createdAppFormFiles){
expect(fs.existsSync(path.join(processDir, 'forms', 'app', createdAppFormFile))).toBe(true);
}

// Check that the products are available in stock out xform
Expand All @@ -98,46 +100,7 @@ describe('Stock out', () => {

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

}

});
Expand Down
36 changes: 35 additions & 1 deletion test/test-utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const process = require('process');
const path = require('path');
const fs = require('fs-extra');

const currentWorkingDirectory = () =>{
return process.cwd();
Expand All @@ -14,10 +15,43 @@ const revertBackToProjectHome = (projectHome) =>{
process.chdir(projectHome);
};

const cleanUp = (workingDir) => {
const processDir = path.join(workingDir,'test/project-config/');
const stockOutFormFiles = ['stock_out.xlsx', 'stock_count.xlsx', 'stock_count.properties.json', 'stock_out.properties.json'];
for(const formFile of stockOutFormFiles){
fs.unlinkSync(path.join(processDir, 'forms', 'app', formFile));
}

// Removing the stock monitoring init file and stock count file
const stockMonitoringInitPath = path.join(processDir, 'stock-monitoring.config.json');
fs.stat(stockMonitoringInitPath, (error) => {
if (!error) {
fs.unlinkSync(stockMonitoringInitPath);
}
});

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'});
if(messageFileContent !== ''){
const newMessageContent = messageFileContent.split('\n').map(message => {
if(!message.toString().includes('cht-stock-monitoring-workflow.stock_count') && message.toString()!==''){
return `${message.toString()}\n`;
}
});

fs.truncate(path.join(processDir, 'translations', translationFile), 0, function () {});
fs.writeFile(path.join(processDir, 'translations', translationFile),newMessageContent.toString().replaceAll(',', ''));
}
}
};

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


Expand Down

0 comments on commit 69f6cb9

Please sign in to comment.