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/package.json b/package.json new file mode 100644 index 00000000..9cc0c1b3 --- /dev/null +++ b/package.json @@ -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" + } +} \ No newline at end of file diff --git a/product-inventory/app.js b/product-inventory/app.js new file mode 100644 index 00000000..073d1fcd --- /dev/null +++ b/product-inventory/app.js @@ -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 +}; \ No newline at end of file diff --git a/product-inventory/test/app.test.js b/product-inventory/test/app.test.js new file mode 100644 index 00000000..d0375f8e --- /dev/null +++ b/product-inventory/test/app.test.js @@ -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); + }) +}) +