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 .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
package-lock.json
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "software-dev-course-unit-testing",
"version": "1.0.0",
"description": "This is the README.md file! I, as the presenter, will fill it with content!",
"main": "index.js",
"scripts": {
"test": "jest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/LaunchCodeEducation/software-dev-course-unit-testing.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/LaunchCodeEducation/software-dev-course-unit-testing/issues"
},
"homepage": "https://github.com/LaunchCodeEducation/software-dev-course-unit-testing#readme",
"devDependencies": {
"jest": "^30.1.3"
}
}
36 changes: 36 additions & 0 deletions product-inventory/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function calculateDiscount(price, discountRate) {
if (typeof price !== 'number' || typeof discountRate !== 'number') return null;
if (discountRate < 0 || discountRate > 1) return null;
// TODO: Implement logic
let amount = price - (price * discountRate);
return amount;
}

function filterProducts(products, callback) {
if (!Array.isArray(products) || typeof callback !== 'function') return [];
// TODO: Implement filtering logic
let filteredProducts = products.filter(callback);
return filteredProducts;
}



function sortInventory(inventory, key) {
if (!Array.isArray(inventory) || typeof key !== 'string') return [];
// TODO: Implement sorting logic
inventory.sort(function (a, b) {
let keyA = a[key];
let keyB = b[key];

if (keyA < keyB) return -1;
if (keyA > keyB) return 1;
return 0;
})
return inventory;
}

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

describe("The calculateDiscount () function", () => {
test('should return the discounted amount', () => {
expect(store.calculateDiscount(100, 0.1)).toEqual(90);
})
test('should return null for invalid discount rate', () => {
expect(store.calculateDiscount(100, 10)).toEqual(null);
})
test('should return original price when discount rate is 0', () => {
expect(store.calculateDiscount(100, 0)).toEqual(100);
});
});

describe("The filterProducts () function", () => {
test('should return a filtered array', () => {

let initArray = ["Pizza", "Burger", "Fries"];
let solutionArray = ["Pizza", "Fries"];
expect(store.filterProducts(initArray, function (entry) {
return entry !== "Burger";
})).toEqual(solutionArray);
})
test('should return an empty string for invalid function', () => {

let initArray = ["Pizza", "Burger", "Fries"];

expect(store.filterProducts(initArray, null)).toEqual([]);
})
test('should return an empty string for removing everything', () => {
let initArray = ["Pizza", "Burger", "Fries"];

expect(store.filterProducts(initArray, function () { return false })).toEqual([]);
})
})

describe("The sortInventor () function", () => {
test('should return a sorted array', () => {

let initArray = [
{ "num": 30 },
{ "num": 20 },
{ "num": 32 },
{ "num": 10 },
];
let solutionArray = [
{ "num": 10 },
{ "num": 20 },
{ "num": 30 },
{ "num": 32 },
];
expect(store.sortInventory(initArray, "num")).toEqual(solutionArray);
})
test('should return an empty array for no key', () => {

let initArray = [
{ "num": 30 },
{ "num": 20 },
{ "num": 32 },
{ "num": 10 },
];

expect(store.sortInventory(initArray, null)).toEqual([]);
})
test('should return the same array for no change in sorting', () => {

let initArray = [
{ "num": 30 },
{ "num": 20 },
{ "num": 60 },
{ "num": 10 },
];

expect(store.sortInventory(initArray, "key")).toEqual(initArray);
})
})