From 3de398adb80535cb7499746c2ccad6d8c28dc6d4 Mon Sep 17 00:00:00 2001 From: lfrederick034 Date: Mon, 15 Sep 2025 19:31:52 -0500 Subject: [PATCH] Submission --- sharing-task.js | 109 +++++++++++++++++++++++++++++++++++ test.js/sharing-task.test.js | 60 +++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 sharing-task.js create mode 100644 test.js/sharing-task.test.js diff --git a/sharing-task.js b/sharing-task.js new file mode 100644 index 00000000..c4e86662 --- /dev/null +++ b/sharing-task.js @@ -0,0 +1,109 @@ +// # πŸ§ͺ Shared Codebase Testing Activity + +// ## 🎯 Objective +// Students will work together to write unit tests for a shared codebase, verify method behavior, and provide feedback to their peers. This activity emphasizes collaboration, critical thinking, and knowledge sharing within the learning community. + +// --- + +// ## 🧱 Activity Overview + +// ### πŸ”§ Setup a Shared Codebase + +// Provide a starter project containing partially implemented methods (e.g., `calculateDiscount`, `filterProducts`, `sortInventory`). +// Include minimal or no existing tests, encouraging students to create them from scratch. + +// ### πŸ“‚ Partially Implemented Methods + + + + + +// In a group with Aaron B, Chris L, Eric S, Oznur K + +function calculateDiscount(price, discountRate) { + if (typeof price !== 'number' || typeof discountRate !== 'number') return null; + if (discountRate < 0 || discountRate > 1) return null; + // TODO: Implement logic + + return price - (discountRate * price); +} + +function filterProducts(products, callback) { + if (!Array.isArray(products) || typeof callback !== 'function') return []; + // TODO: Implement filtering logic + // Return products.filter(callback) - no callback to function so just return an empty array... + return filteredProducts; +} + +function sortInventory(inventory, key) { + + // Checks if arguments are of the correct type + if (!Array.isArray(inventory) || typeof key !== 'string' || inventory.some(object => key in object) === false) return []; + // TODO: Implement sorting logic + + let sortedInventory = inventory.sort((a, b) => a[key].localeCompare(b[key])); + return sortedInventory; + +} + +console.log(calculateDiscount(100, 0.1)); + +let dogObject = {dogname: 'Roofus', dogType: 'Lab'}; +let produceObjectList = [ + {produceName: 'carrot', produceType: 'veggie'}, + {produceName: 'pineapple', produceType: 'fruit'}, + {produceName: 'radish', produceType: 'veggie'}, + {produceName: 'apple', produceType: 'fruit'}, + {produceName: 'corn', produceType: 'veggie'} +]; + +console.log(sortInventory(produceObjectList, 'produceName')); + +module.exports = { calculateDiscount, filterProducts, sortInventory }; + + +// ## πŸ‘₯ Form Groups + +// Divide students into small groups of 3–5. Each group will be responsible for writing unit tests for one or more methods in the shared codebase. + +// ### πŸ§‘β€πŸ€β€πŸ§‘ Assign Roles Within the Group: + +// - **Test Writer**: Writes initial test cases. +// - **Code Reviewer**: Reviews and improves the test cases. +// - **Debugger**: Ensures the implementation passes all tests. +// - **Presenter**: Summarizes the group’s contributions for sharing with the class. + +// --- + +// ## πŸ“ Instructions + +// ### πŸ§ͺ Write Tests +// - Use **Jest** to write unit tests for their assigned method(s). +// - Ensure they include **positive**, **negative**, and **edge cases**. + +// ### 🀝 Collaborate +// - Share and discuss test ideas within their group. +// - Ensure all possible scenarios are covered. + +// ### πŸ§ͺ Run Tests +// - Use the command below to execute the test suite and refine the implementation: + +// ```bash +// npm test +// ``` + +// ## πŸ“„ Sample Test Code (Students Will Write) + +// ```js +// 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); +// }); +// ``` \ No newline at end of file diff --git a/test.js/sharing-task.test.js b/test.js/sharing-task.test.js new file mode 100644 index 00000000..0a2b9930 --- /dev/null +++ b/test.js/sharing-task.test.js @@ -0,0 +1,60 @@ +const sharingTask = require('../sharing-task.js'); + +describe('calculateDiscount', function() { + test('should return applied discount', function() { + expect(sharingTask.calculateDiscount(100, .1)).toBe(90); + }); + + test('should not return applied discount', function() { + expect(sharingTask.calculateDiscount(100, -1)).toBe(null); + }); + + test('should not return applied discount', function() { + expect(sharingTask.calculateDiscount("four", "five")).toBe(null); + }); +}); + +describe('filterProducts', function () { + test('should return filtered array', function() { + expect(sharingTask.filterProducts(['Squash', 'Ice', 'Sandwich', 'Corn', 'Apple'], str => str.length >= 5)).toStrictEqual(['Squash', 'Sandwich', 'Apple']); + }); + + test('should not return filtered array', function() { + expect(sharingTask.filterProducts(['Squash', 'Ice', 'Sandwich', 'Corn', 'Apple'], 24)).toStrictEqual([]); + }); + + test('should not return filtered array', function() { + expect(sharingTask.filterProducts(sharingTask.dogObject, str => str.length >= 5)).toStrictEqual([]); + }); + +}); + +let produceObjectList = [ //for testing purposes + {produceName: 'carrot', produceType: 'veggie'}, + {produceName: 'pineapple', produceType: 'fruit'}, + {produceName: 'radish', produceType: 'veggie'}, + {produceName: 'apple', produceType: 'fruit'}, + {produceName: 'corn', produceType: 'veggie'} +]; + +let sortedProduceObjectList = [ //for testing purposes + {produceName: 'apple', produceType: 'fruit'}, + {produceName: 'carrot', produceType: 'veggie'}, + {produceName: 'corn', produceType: 'veggie'}, + {produceName: 'pineapple', produceType: 'fruit'}, + {produceName: 'radish', produceType: 'veggie'} +]; + +describe('sortInventory', function() { + test('should return a sorted inventory', function() { + expect(sharingTask.sortInventory(produceObjectList, 'produceName')).toStrictEqual(sortedProduceObjectList); + }); + + test('should not return a sorted inventory', function() { + expect(sharingTask.sortInventory(produceObjectList, 45)).toStrictEqual([]); + }); + + test('should not return a sorted inventory', function() { + expect(sharingTask.sortInventory(produceObjectList, '')).toStrictEqual([]); + }); +}); \ No newline at end of file