Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions product-inventory/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
package-lock.json
15 changes: 15 additions & 0 deletions product-inventory/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
43 changes: 43 additions & 0 deletions product-inventory/products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const products = [
{Item: 'Laptop', Quantity: 50, Price: 1000},
{Item: 'Smartphone', Quantity: 200, Price: 1200},
{Item: 'Headphones', Quantity: 150, Price: 500},
{Item: 'Keyboard', Quantity: 75, Price: 200},
{Item: 'Mouse', Quantity: 120, Price: 50},
];

function calculateDiscount(price, discountRate) {
if (typeof price !== 'number' || typeof discountRate !== 'number') {
return null;
}
if (discountRate < 0 || discountRate > 1) {
return null;
}

return price-price*discountRate;
}

console.log(calculateDiscount(100, 0.1));

function filterProducts(products, callback) {
if (!Array.isArray(products) || typeof callback !== 'function'){

return [];
}
return products.filter(callback);
}
console.log(filterProducts(products, product => product.price<500));

function sortInventory(inventory, key) {
if (!Array.isArray(inventory) || typeof key !== 'string'){
return [];
}
return inventory.sort((a,b) => (b[key]-a[key]));
};
console.log(sortInventory(products, "quantity"));

module.exports = {
calculateDiscount,
filterProducts,
sortInventory
}
66 changes: 66 additions & 0 deletions product-inventory/tests/products.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const { calculateDiscount, filterProducts, sortInventory } = require('../products.js');

const products = [
{ Item: 'Laptop', Quantity: 50, Price: 1000 },
{ Item: 'Smartphone', Quantity: 200, Price: 1200 },
{ Item: 'Headphones', Quantity: 150, Price: 500 },
{ Item: 'Keyboard', Quantity: 75, Price: 200 },
{ Item: 'Mouse', Quantity: 120, Price: 50 },
];

describe("apply dicount function", () => {
test("applies a valid discount rate", () => {
expect(calculateDiscount(100, 0.1)).toBe(90);
});

test("handles an invalid discount rate gracefully", () => {
expect(calculateDiscount(100, -0.1)).toBe(null);
});

test("handles edge case with price of 0", () => {
expect(calculateDiscount(0, 0.2)).toBe(0);
});

//This test case was added later
test("accepts numbers for price not strings",() =>{
expect(calculateDiscount(100,"string")).toBe(null);
})
})

const cat = "Nick";
describe("filter products by price", () => {
test("filters products less than 500", () => {
expect(filterProducts(products, product => product.Price < 500)).toEqual([
{ Item: 'Keyboard', Quantity: 75, Price: 200 },
{ Item: 'Mouse', Quantity: 120, Price: 50 },
])
})
test("Negative Test Case: Should give empty array for non-array first argument", () => {
expect(filterProducts(cat, product => product.Price < 500)).toEqual([]);
});

test("Edge Test Case: Should handle filtering everything out of array", () => {
expect(filterProducts(products, product => product.Price < 20)).toEqual([]);
});
});

//sortInventory
describe("inventoryFunctions.sortInventory", function () {
test("Sort by Quantity", () => {
expect(sortInventory(products, "quantity")).toEqual([
{ Item: 'Laptop', Quantity: 50, Price: 1000 },
{ Item: 'Smartphone', Quantity: 200, Price: 1200 },
{ Item: 'Headphones', Quantity: 150, Price: 500 },
{ Item: 'Keyboard', Quantity: 75, Price: 200 },
{ Item: 'Mouse', Quantity: 120, Price: 50 }
]);
});

test("Number input instead of a string", () => {
expect(sortInventory(products, 587609)).toEqual([]);
});

test("Empty array input", () => {
expect(sortInventory([], "quantity")).toEqual([]);
});
});