Skip to content

Commit

Permalink
fix: feedback from review and update old test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoteo committed Nov 5, 2024
1 parent dc96d2d commit ddeeabe
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 127 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@
"SwitchCase": 1
}
]
},
"globals": {
"jest": true
}
}
79 changes: 30 additions & 49 deletions test/stock-count.spec.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,60 @@
const { spawnSync } = require('child_process');
const path = require('path');
const fs = require('fs-extra');
const ExcelJS = require('exceljs');

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


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


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

afterEach(() => {
cleanUp(workingDir, createdAppFormFiles);
revertBackToProjectHome(workingDir);
});

it('Add stock count summaries test', async() => {
it('Add stock count integration test', async() => {
const processDir = process.cwd();
// Check that stock count xform and properties files does not exist
for(const createdAppFormFile of createdAppFormFiles){
expect(fs.existsSync(path.join(processDir, 'forms', 'app', createdAppFormFile))).toBe(false);
}

const childProcess = spawnSync('../../main.js', stockCountScenario.initScenario);

if (childProcess.error) {
throw childProcess.error;
} else {
const formPath = path.join(processDir, 'forms', 'app', `stock_count.xlsx`);
const formPropertiesPath = path.join(processDir, 'forms', 'app', `stock_count.properties.json`);
const stockMonitoringConfig = path.join(processDir, 'stock-monitoring.config.json');

// Check that stock monitoring is initialized and stock count xform is generated
expect(fs.existsSync(stockMonitoringConfig)).toBe(true);
expect(fs.existsSync(formPropertiesPath)).toBe(true);
expect(fs.existsSync(formPath)).toBe(true);

// Check that the products are available in stock count xform
const productCategoryList = stockCountScenario.productCategoryScenario;
const productsList = stockCountScenario.productsScenario;
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.readFile(formPath);
const surveyWorkSheet = workbook.getWorksheet('survey');
const nameCol = surveyWorkSheet.getColumn('B');
const cellProductCategoriesList = [];
const cellProductsList = [];
let productCatIndex =0;
let productIndex = 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);

//Removing the stock monitoring init file and stock count file
expect(fs.unlinkSync(stockMonitoringConfig)).toBe(undefined);
expect(fs.unlinkSync(formPath)).toBe(undefined);
expect(fs.unlinkSync(formPropertiesPath)).toBe(undefined);
}

const stockMonitoringConfig = path.join(processDir, 'stock-monitoring.config.json');

// Check that stock monitoring is initialized and stock count xform is generated
expect(fs.existsSync(stockMonitoringConfig)).toBe(true);

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

// Check that the products and categories are available in stock count xform
const { productsList, productCategoryList } = await readDataFromXforms(stockCountScenario.productCategoryScenario, stockCountScenario.productsScenario, 'stock_count.xlsx');

expect(productsList.length).toBe(stockCountScenario.productsScenario.length);
expect(productsList.entries).toStrictEqual(stockCountScenario.productsScenario.entries);
expect(productCategoryList.length).toBe(stockCountScenario.productCategoryScenario.length);
expect(productCategoryList.entries).toStrictEqual(stockCountScenario.productCategoryScenario.entries);

});

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

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


describe('Stock out integration test', () => {
const workingDir = process.cwd();
const createdAppFormFiles = ['stock_count.properties.json', 'stock_count.xlsx', 'stock_out.properties.json', 'stock_out.xlsx'];


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

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

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

// Check that stock count and stock out xform and properties files does not exist
for(const createdAppFormFile of createdAppFormFiles){
expect(fs.existsSync(path.join(processDir, 'forms', 'app', createdAppFormFile))).toBe(false);
}

const childProcess = spawnSync('../../main.js', stockOutScenario.initScenario);

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 createdAppFormFiles = ['stock_count.properties.json', 'stock_count.xlsx'];

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

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

cleanUp(workingDir);

// Add stock out feature test
const stockOutChildProcess = spawnSync('../../main.js', stockOutScenario.addStockOutFeatureScenario);
if (stockOutChildProcess.error) {
throw stockOutChildProcess.error;
}

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

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

// Check that the products and categories are available in stock count xform
const { productsList, productCategoryList } = await readDataFromXforms(stockCountScenario.productCategoryScenario, stockCountScenario.productsScenario, 'stock_count.xlsx');

expect(productsList.length).toBe(stockCountScenario.productsScenario.length);
expect(productsList.entries).toStrictEqual(stockCountScenario.productsScenario.entries);
expect(productCategoryList.length).toBe(stockCountScenario.productCategoryScenario.length);
expect(productCategoryList.entries).toStrictEqual(stockCountScenario.productCategoryScenario.entries);

// Check that the products are available in stock out xform
const stockOutProducts = await readDataFromXforms([], stockOutScenario.productsScenario, 'stock_out.xlsx');
expect(stockOutScenario.productsScenario.length).toBe(stockOutProducts.productsList.length);
expect(stockOutScenario.productsScenario.entries).toStrictEqual(stockOutProducts.productsList.entries);

});

});
Expand Down
44 changes: 41 additions & 3 deletions test/test-utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const process = require('process');
const path = require('path');
const fs = require('fs-extra');
const ExcelJS = require('exceljs');

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

const cleanUp = (workingDir) => {
const cleanUp = (workingDir, fileNames) => {
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){
for(const formFile of fileNames){
fs.unlinkSync(path.join(processDir, 'forms', 'app', formFile));
}

Expand All @@ -32,11 +32,49 @@ const cleanUp = (workingDir) => {

};

const readDataFromXforms = async (productCategoryScenario, productsScenario, fileName) => {
const projectPath = process.cwd();
const productCategoryList = [];
const productsList = [];

const workbook = new ExcelJS.Workbook();
const xlsx = workbook.xlsx;
await xlsx.readFile(path.join(projectPath, 'forms', 'app', fileName));
const surveyWorkSheet = workbook.getWorksheet('survey');
const nameCol = surveyWorkSheet.getColumn('B');
let productIndex = 0;
let productCatIndex =0;
nameCol.eachCell(function(cell){

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

if(productsScenario !== undefined && productsScenario.length > 0){
if(cell.value === productsScenario[productIndex] && productIndex < productsScenario.length){
productsList.push(cell.value);
productIndex ++;
}
}
});

return {
productsList,
productCategoryList
};

};

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


Expand Down

0 comments on commit ddeeabe

Please sign in to comment.