From 3ba3b23f571073d5f0582e24cdb3ecc32354f149 Mon Sep 17 00:00:00 2001 From: DeepthiGirijaThampi Date: Thu, 11 Sep 2025 16:34:41 -0500 Subject: [PATCH] completed the product-invory.js and the unit tests realted to it --- .gitignore | 2 + product-inventory/package.json | 15 +++++ product-inventory/products.js | 28 +++++++++ product-inventory/tests/products.test.js | 75 ++++++++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 .gitignore create mode 100644 product-inventory/package.json create mode 100644 product-inventory/products.js create mode 100644 product-inventory/tests/products.test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..25c8fdba --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +package-lock.json \ No newline at end of file diff --git a/product-inventory/package.json b/product-inventory/package.json new file mode 100644 index 00000000..b87a0fbb --- /dev/null +++ b/product-inventory/package.json @@ -0,0 +1,15 @@ +{ + "name": "product-inventory", + "version": "1.0.0", + "main": "index.js", + "scripts": { + "test": "jest" + }, + "keywords": [], + "author": "", + "license": "ISC", + "description": "", + "devDependencies": { + "jest": "^29.7.0" + } +} diff --git a/product-inventory/products.js b/product-inventory/products.js new file mode 100644 index 00000000..e720bc9e --- /dev/null +++ b/product-inventory/products.js @@ -0,0 +1,28 @@ +function calculateDiscount(price, discountRate) { + if (typeof price !== 'number' || typeof discountRate !== 'number') return null; + if (discountRate < 0 || discountRate > 1) return null; + // TODO: Implement logic + const discount = price *(1-discountRate); + return discount; +} + +function filterProducts(products, callback) { + if (!Array.isArray(products) || typeof callback !== 'function') return []; + // TODO: Implement filtering logic + + return products.filter(callback); +} + +function sortInventory(inventory, key) { + if (!Array.isArray(inventory) || typeof key !== 'string') return []; + // TODO: Implement sorting logic + const sortedArray = [...inventory].sort((a,b) =>a[key] - b[key]); + return sortedArray; +} + + +module.exports = { + calculateDiscount : calculateDiscount, + filterProducts : filterProducts, + sortInventory : sortInventory +} \ No newline at end of file diff --git a/product-inventory/tests/products.test.js b/product-inventory/tests/products.test.js new file mode 100644 index 00000000..eb3f759f --- /dev/null +++ b/product-inventory/tests/products.test.js @@ -0,0 +1,75 @@ +const products = require('../products.js'); + +let inventory = [ + {item : "peach", price : 10}, + {item : "apple", price : 2}, + {item : "orange", price : 3}, + {item : "banana" , price : 4} +]; + + + +describe("calculate Discount", ()=>{ + test("applies a valid discount rate", () => { + expect(products.calculateDiscount(100, 0.1)).toBe(90); + }); + + test("handles an invalid discount rate gracefully", () => { + expect(products.calculateDiscount(100, -0.1)).toBe(null); + }); + + test("handles edge case with price of 0", () => { + expect(products.calculateDiscount(0, 0.2)).toBe(0); + }); +}); + +describe("filter products", ()=>{ + test("filter products based on callback", ()=>{ + const filtered = products.filterProducts(inventory, product => product.price >1 ) + expect(filtered).toEqual([ + {item : "peach", price : 10}, + {item : "apple", price : 2}, + {item : "orange", price : 3}, + {item : "banana" , price : 4}, + + ]) + }); + + test("returns empty array if products is not an array ", ()=>{ + expect(products.filterProducts("invalid",product => product.price >1)).toEqual([]); + }); + + test("return empty array if no products match",()=>{ + expect(products.filterProducts(inventory, product => product.price ==1)).toEqual([]); + }); + + test("returns empty array if callback is not a function", ()=>{ + expect(products.filterProducts(inventory,"not a function")).toEqual([]); + }); + + +}); + +describe("sortInventory", ()=>{ + + test("sorts the inventory by price in ascending order", ()=>{ + + expect(products.sortInventory(inventory,"price")).toEqual([ + {item : "apple", price : 2}, + {item : "orange", price : 3}, + {item : "banana", price : 4}, + {item : "peach", price : 10} + ]); + + }); + + test("returns an empty array for invalid inventory",()=>{ + expect(products.sortInventory("nothing","price")).toEqual([]) + }); + + test("returns an empty array for invalid key",()=>{ + + expect(products.sortInventory(inventory,123)).toEqual([]) + }); + +}); \ No newline at end of file