diff --git a/jest.config.js b/jest.config.js index 73e3b69..5c94467 100644 --- a/jest.config.js +++ b/jest.config.js @@ -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/(.*)': '/$1', diff --git a/src/modules/inventory/models/inventory.model.test.js b/src/modules/inventory/models/inventory.model.test.js index e69de29..5149b22 100644 --- a/src/modules/inventory/models/inventory.model.test.js +++ b/src/modules/inventory/models/inventory.model.test.js @@ -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'); + }); + }); +}); diff --git a/src/modules/inventory/models/inventoryAuditItem.model.test.js b/src/modules/inventory/models/inventoryAuditItem.model.test.js new file mode 100644 index 0000000..0854eab --- /dev/null +++ b/src/modules/inventory/models/inventoryAuditItem.model.test.js @@ -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); + }); + }); +}); diff --git a/src/modules/inventory/services/GetCurrentStock.test.js b/src/modules/inventory/services/GetCurrentStock.test.js new file mode 100644 index 0000000..055ecc8 --- /dev/null +++ b/src/modules/inventory/services/GetCurrentStock.test.js @@ -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); + }); + }); +}); diff --git a/src/modules/inventory/stockCorrection/services/apis/CreateFormApprove.test.js b/src/modules/inventory/stockCorrection/services/apis/CreateFormApprove.test.js index 3fb1f4d..1c795e1 100644 --- a/src/modules/inventory/stockCorrection/services/apis/CreateFormApprove.test.js +++ b/src/modules/inventory/stockCorrection/services/apis/CreateFormApprove.test.js @@ -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); + }); }); }); diff --git a/src/modules/inventory/stockCorrection/services/apis/CreateFormRequest.test.js b/src/modules/inventory/stockCorrection/services/apis/CreateFormRequest.test.js index 80a1385..94a51ee 100644 --- a/src/modules/inventory/stockCorrection/services/apis/CreateFormRequest.test.js +++ b/src/modules/inventory/stockCorrection/services/apis/CreateFormRequest.test.js @@ -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', @@ -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, + }; +}; diff --git a/tests/utils/factory/item.js b/tests/utils/factory/item.js index 36511ed..3903e9f 100644 --- a/tests/utils/factory/item.js +++ b/tests/utils/factory/item.js @@ -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;