From c7959373be0c2a94b8bbb0aceab729adf589141c Mon Sep 17 00:00:00 2001 From: Thu Nguyen Date: Wed, 10 Sep 2025 21:36:09 -0400 Subject: [PATCH 1/3] created app.js & app.test.js --- .gitignore | 2 ++ package.json | 23 ++++++++++++++++ product-inventory/app.js | 28 ++++++++++++++++++++ product-inventory/test/app.test.js | 42 ++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 .gitignore create mode 100644 package.json create mode 100644 product-inventory/app.js create mode 100644 product-inventory/test/app.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/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..5cc3a2aa --- /dev/null +++ b/product-inventory/app.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 + 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 + return []; +} + +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..33fa35b9 --- /dev/null +++ b/product-inventory/test/app.test.js @@ -0,0 +1,42 @@ +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 null for invalid for non-numbers', () => { + expect(store.calculateDiscount(100, 0)).toEqual(100); + }) +}) + +// describe("The filterProducts () 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 null for invalid for non-numbers', () => { +// expect(store.calculateDiscount('100', 10)).toEqual(null); +// }) +// }) + + + + + + +// function filterProducts(products, callback) { +// if (!Array.isArray(products) || typeof callback !== 'function') return []; +// // TODO: Implement filtering logic +// return []; +// } + +// function sortInventory(inventory, key) { +// if (!Array.isArray(inventory) || typeof key !== 'string') return []; +// // TODO: Implement sorting logic +// return []; +// } From 22c7285039fdad419d68921be87bb88f37bff5d9 Mon Sep 17 00:00:00 2001 From: Thu Nguyen Date: Thu, 11 Sep 2025 21:00:02 -0400 Subject: [PATCH 2/3] Finishing Unit Testing --- product-inventory/test/app.test.js | 81 +++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 23 deletions(-) diff --git a/product-inventory/test/app.test.js b/product-inventory/test/app.test.js index 33fa35b9..d0375f8e 100644 --- a/product-inventory/test/app.test.js +++ b/product-inventory/test/app.test.js @@ -7,36 +7,71 @@ describe("The calculateDiscount () function", () => { test('should return null for invalid discount rate', () => { expect(store.calculateDiscount(100, 10)).toEqual(null); }) - test('should return null for invalid for non-numbers', () => { + 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"]; -// describe("The filterProducts () 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 null for invalid for non-numbers', () => { -// expect(store.calculateDiscount('100', 10)).toEqual(null); -// }) -// }) + 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 }, + ]; -// function filterProducts(products, callback) { -// if (!Array.isArray(products) || typeof callback !== 'function') return []; -// // TODO: Implement filtering logic -// return []; -// } + expect(store.sortInventory(initArray, "key")).toEqual(initArray); + }) +}) -// function sortInventory(inventory, key) { -// if (!Array.isArray(inventory) || typeof key !== 'string') return []; -// // TODO: Implement sorting logic -// return []; -// } From f0fd36013e18133f2492816ce3da3f9b02f4ccd3 Mon Sep 17 00:00:00 2001 From: Thu Nguyen Date: Thu, 11 Sep 2025 21:03:17 -0400 Subject: [PATCH 3/3] Finishing Unit Testing --- product-inventory/app.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/product-inventory/app.js b/product-inventory/app.js index 5cc3a2aa..073d1fcd 100644 --- a/product-inventory/app.js +++ b/product-inventory/app.js @@ -18,7 +18,15 @@ function filterProducts(products, callback) { function sortInventory(inventory, key) { if (!Array.isArray(inventory) || typeof key !== 'string') return []; // TODO: Implement sorting logic - return []; + 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 = {