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
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"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": "products.js",
"directories": {
"test": "tests"
},
"scripts": {
"test": "jest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/BryJime/software-dev-course-unit-testing.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/BryJime/software-dev-course-unit-testing/issues"
},
"homepage": "https://github.com/BryJime/software-dev-course-unit-testing#readme",
"dependencies": {
"jest": "^29.7.0"
}
}
60 changes: 60 additions & 0 deletions products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const sampleInventory = [
{ id: 1, name: "Classic Tee", category: "Apparel", price: 19.99, inStock: true, rating: 4.2 },
{ id: 2, name: "Running Sneakers", category: "Footwear", price: 89.95, inStock: false, rating: 4.8 },
{ id: 3, name: "Cozy Hoodie", category: "Apparel", price: 49.50, inStock: true, rating: 4.5 },
{ id: 4, name: "Ceramic Mug", category: "Home Goods", price: 12.00, inStock: true, rating: 4.0 },
{ id: 5, name: "Hardcover Book", category: "Books", price: 24.99, inStock: false, rating: 4.9 },
{ id: 6, name: "Wireless Mouse", category: "Electronics", price: 29.99, inStock: true, rating: 4.3 },
{ id: 7, name: "Bluetooth Headset", category: "Electronics", price: 59.99, inStock: true, rating: 4.6 },
{ id: 8, name: "Water Bottle", category: "Outdoor", price: 15.00, inStock: false, rating: 4.1 },
{ id: 9, name: "Yoga Mat", category: "Fitness", price: 35.00, inStock: true, rating: 4.7 },
{ id: 10, name: "Desk Lamp", category: "Home Goods", price: 22.50, inStock: true, rating: 4.4 }
];



function calculateDiscount(price, discountRate) {
if (typeof price !== 'number' || typeof discountRate !== 'number') return null;
if (discountRate < 0 || discountRate > 1) return null;
// TODO: Implement logic
let discount = price - (price * discountRate)
price = discount;
return price;
}

function filterProducts(products, callback) {

if (!Array.isArray(products) || typeof callback !== 'function') return [];
// TODO: Implement filtering logic

return products.filter(callback);

}


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

return inventory.sort(function (a, b) {
if (a[key] < b[key]) {
return -1;
} else if (a[key] > b[key]) {
return 1;
} else {
return 0;
}
});
}


module.exports = {
calculateDiscount,
filterProducts,
sortInventory,
sampleInventory
}




86 changes: 86 additions & 0 deletions tests/__snapshots__/products.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`The shopping cart should sort item names in alphbetical order 1`] = `
[
{
"category": "Electronics",
"id": 7,
"inStock": true,
"name": "Bluetooth Headset",
"price": 59.99,
"rating": 4.6,
},
{
"category": "Home Goods",
"id": 4,
"inStock": true,
"name": "Ceramic Mug",
"price": 12,
"rating": 4,
},
{
"category": "Apparel",
"id": 1,
"inStock": true,
"name": "Classic Tee",
"price": 19.99,
"rating": 4.2,
},
{
"category": "Apparel",
"id": 3,
"inStock": true,
"name": "Cozy Hoodie",
"price": 49.5,
"rating": 4.5,
},
{
"category": "Home Goods",
"id": 10,
"inStock": true,
"name": "Desk Lamp",
"price": 22.5,
"rating": 4.4,
},
{
"category": "Books",
"id": 5,
"inStock": false,
"name": "Hardcover Book",
"price": 24.99,
"rating": 4.9,
},
{
"category": "Footwear",
"id": 2,
"inStock": false,
"name": "Running Sneakers",
"price": 89.95,
"rating": 4.8,
},
{
"category": "Outdoor",
"id": 8,
"inStock": false,
"name": "Water Bottle",
"price": 15,
"rating": 4.1,
},
{
"category": "Electronics",
"id": 6,
"inStock": true,
"name": "Wireless Mouse",
"price": 29.99,
"rating": 4.3,
},
{
"category": "Fitness",
"id": 9,
"inStock": true,
"name": "Yoga Mat",
"price": 35,
"rating": 4.7,
},
]
`;
62 changes: 62 additions & 0 deletions tests/products.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const { calculateDiscount, filterProducts, sampleInventory, sortInventory } = require('../products.js');


describe("The shopping cart", () => {

test("should calculate discout accuratley", () => {
expect(calculateDiscount(100, .10)).toEqual(90)
})

test("should account for negative numbers", () => {
expect(calculateDiscount(100, .10)).not.toEqual(-90);
})


test("should account for discount over than 1", () => {
expect(calculateDiscount(100, 1.2)).toEqual(null);
})

test("should filter to find yoga mat", () => {
expect(filterProducts(sampleInventory, (products) => products.name === "Yoga Mat")).toEqual([
{
id: 9,
name: 'Yoga Mat',
category: 'Fitness',
price: 35,
inStock: true,
rating: 4.7
}
]);
})

test("should return empty array if callback is not a function", () => {
expect(filterProducts(sampleInventory, 'string')).toEqual([]);
})

test("should return empty array if function is broken", () => {
expect(filterProducts(sampleInventory, () => {})).toEqual([])
})

test("should sort item names in alphbetical order", () => {
expect(sortInventory(sampleInventory, "name" )).toMatchSnapshot();
})

test("should return empty array if inventory is not an array", () => {
expect(sortInventory("inventory", "name")).toEqual([]);
})

test("should return 0 if key is not found in array", () => {
expect(sortInventory(sampleInventory, undefined)).toEqual([])
})

});