Skip to content

Commit

Permalink
add test for get current stock
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrixsilaen committed Mar 8, 2022
1 parent fad4484 commit 81eb835
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 19 deletions.
9 changes: 8 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ module.exports = {
NODE_ENV: 'test',
},
restoreMocks: true,
coveragePathIgnorePatterns: ['node_modules', 'src/config', 'src/app.js', 'tests', 'models/tenantModels.js'],
coveragePathIgnorePatterns: [
'node_modules',
'src/config',
'src/app.js',
'src/models/index.js',
'tests',
'models/tenantModels.js',
],
coverageReporters: ['text', 'lcov', 'clover', 'html'],
moduleNameMapper: {
'@root/(.*)': '<rootDir>/$1',
Expand Down
29 changes: 29 additions & 0 deletions src/modules/inventory/models/inventory.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { Inventory } = require('../../../models').tenant;

describe('Inventory Model', () => {
describe('custom getters', () => {
it('returns correct quantity value', () => {
const inventory = Inventory.build({ quantity: 10 });
expect(inventory.quantity).toEqual(10);
});

it('returns correct quantity reference value', () => {
const inventory = Inventory.build({ quantityReference: 10 });
expect(inventory.quantityReference).toEqual(10);
});

it('returns correct converter reference value', () => {
const inventory = Inventory.build({ converterReference: 10 });
expect(inventory.converterReference).toEqual(10);
});

it('returns correct expiry date value', () => {
const inventory = Inventory.build({ expiryDate: null });
expect(inventory.expiryDate).toEqual(null);

const expiryDate = new Date('2022-03-03');
inventory.expiryDate = expiryDate;
expect(inventory.expiryDate).toEqual('2022-03-03 07:00:00');
});
});
});
29 changes: 29 additions & 0 deletions src/modules/inventory/models/inventoryAuditItem.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { InventoryAuditItem } = require('../../../models').tenant;

describe('Inventory Audit Item Model', () => {
describe('custom getters', () => {
it('returns correct quantity value', () => {
const inventoryAuditItem = InventoryAuditItem.build({ quantity: 10 });
expect(inventoryAuditItem.quantity).toEqual(10);
});

it('returns correct expiry date value', () => {
const inventoryAuditItem = InventoryAuditItem.build({ expiryDate: null });
expect(inventoryAuditItem.expiryDate).toEqual(null);

const expiryDate = new Date('2022-03-03');
inventoryAuditItem.expiryDate = expiryDate;
expect(inventoryAuditItem.expiryDate).toEqual('2022-03-03 07:00:00');
});

it('returns correct price value', () => {
const inventoryAuditItem = InventoryAuditItem.build({ price: 10000 });
expect(inventoryAuditItem.price).toEqual(10000);
});

it('returns correct converter value', () => {
const inventoryAuditItem = InventoryAuditItem.build({ converter: 10 });
expect(inventoryAuditItem.converter).toEqual(10);
});
});
});
64 changes: 64 additions & 0 deletions src/modules/inventory/services/GetCurrentStock.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const factory = require('@root/tests/utils/factory');
const tenantDatabase = require('@src/models').tenant;
const GetCurrentStock = require('./GetCurrentStock');

describe('Get Current Stock', () => {
describe('when item require production number and expiry date', () => {
let warehouse, item, inventory;
beforeEach(async (done) => {
const maker = await factory.user.create();
const branch = await factory.branch.create();
warehouse = await factory.warehouse.create({ branch });
item = await factory.item.create({ requireProductionNumber: true, requireExpiryDate: true });
const inventoryForm = await factory.form.create({
date: new Date('2022-01-01'),
branch,
number: 'PI2101001',
formable: { id: 1 },
formableType: 'PurchaseInvoice',
createdBy: maker.id,
updatedBy: maker.id,
});
inventory = await factory.inventory.create({
form: inventoryForm,
warehouse,
item,
productionNumber: '001',
expiryDate: new Date('2022-03-01'),
});

done();
});

it('returns correct stock', async () => {
const currentStock = await new GetCurrentStock(tenantDatabase, {
item,
date: new Date('2022-03-01'),
warehouseId: warehouse.id,
useDna: true,
options: {
productionNumber: '001',
expiryDate: new Date('2022-03-01'),
},
}).call();

expect(currentStock).toEqual(100);
});

it('returns 0 when no inventories recorded', async () => {
await inventory.destroy();
const currentStock = await new GetCurrentStock(tenantDatabase, {
item,
date: new Date('2022-03-01'),
warehouseId: warehouse.id,
useDna: true,
options: {
productionNumber: '001',
expiryDate: new Date('2022-03-01'),
},
}).call();

expect(currentStock).toEqual(0);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ describe('Stock Correction - Create Form Approve', () => {
const journals = await tenantDatabase.Journal.findAll({ where: { formId: stockCorrectionForm.id } });
expect(journals.length).toEqual(2);
});

it('create the inventory', async () => {
const inventories = await tenantDatabase.Inventory.findAll({ where: { formId: stockCorrectionForm.id } });
expect(inventories.length).toEqual(1);
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const generateRecordFactories = async ({
warehouse = await factory.warehouse.create({ branch, ...warehouse });
userWarehouse = await factory.userWarehouse.create({ user: maker, warehouse, isDefault: true });
allocation = await factory.allocation.create({ branch, ...allocation });
item = await factory.item.create(item);
item = await factory.item.create({ ...item });
inventoryForm = await factory.form.create({
branch,
number: 'PI2101001',
Expand All @@ -108,19 +108,21 @@ const generateRecordFactories = async ({
};
};

const generateCreateFormRequestDto = ({ warehouse, item, allocation, approver }) => ({
warehouseId: warehouse.id,
dueDate: new Date('2021-01-01'),
items: [
{
itemId: item.id,
unit: 'PCS',
converter: 1,
stockCorrection: -10,
notes: 'example stock correction item note',
allocationId: allocation.id,
},
],
notes: 'example stock correction note',
requestApprovalTo: approver.id,
});
const generateCreateFormRequestDto = ({ warehouse, item, allocation, approver }) => {
return {
warehouseId: warehouse.id,
dueDate: new Date('2021-01-01'),
items: [
{
itemId: item.id,
unit: 'PCS',
converter: 1,
stockCorrection: -10,
notes: 'example stock correction item note',
allocationId: allocation.id,
},
],
notes: 'example stock correction note',
requestApprovalTo: approver.id,
};
};
4 changes: 3 additions & 1 deletion tests/utils/factory/item.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const faker = require('faker');
const { Item } = require('@src/models').tenant;

async function create({ chartOfAccount } = {}) {
async function create({ chartOfAccount, requireExpiryDate = false, requireProductionNumber = false } = {}) {
const item = await Item.create({
code: faker.datatype.string(),
name: faker.commerce.productName(),
stock: 100,
chartOfAccountId: chartOfAccount?.id,
requireExpiryDate,
requireProductionNumber,
});

return item;
Expand Down

0 comments on commit 81eb835

Please sign in to comment.