-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: stock out unit test and integration test
- Loading branch information
1 parent
d956af7
commit 1c7e986
Showing
11 changed files
with
420 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,5 +32,8 @@ | |
"SwitchCase": 1 | ||
} | ||
] | ||
}, | ||
"globals": { | ||
"jest": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,60 @@ | ||
const { fork } = require('child_process'); | ||
const { spawnSync } = require('child_process'); | ||
const path = require('path'); | ||
const fs = require('fs-extra'); | ||
const { once } = require('events'); | ||
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(() => { | ||
revertBackToProjectHome(process.cwd()); | ||
cleanUp(workingDir, createdAppFormFiles); | ||
revertBackToProjectHome(workingDir); | ||
}); | ||
|
||
it('Add stock count summaries test', async() => { | ||
it('Add stock count integration test', async() => { | ||
const processDir = process.cwd(); | ||
const childProcess = fork('../../main.js', stockCountScenario.initScenario); | ||
await once(childProcess, 'close'); | ||
|
||
const formPath = path.join(processDir, 'forms', 'app', `stock_count.xlsx`); | ||
const formPropertiesPath = path.join(processDir, 'forms', 'app', `stock_count.properties.json`); | ||
// 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; | ||
} | ||
|
||
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 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(',', '')); | ||
|
||
|
||
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); | ||
|
||
}); | ||
|
||
}); |
Oops, something went wrong.