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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
10 changes: 10 additions & 0 deletions ShoppingCart.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@

// Triggering unit test run locally
class ShoppingCart {
constructor() {
this.items = [];
this.discount = 0;
}

// Add an item to the shopping cart
addItem(item) {
this.items.push(item);
}

// Remove an item from the shopping cart by index
removeItem(index) {
if (index >= 0 && index < this.items.length) {
this.items.splice(index, 1);
Expand All @@ -17,6 +20,7 @@ class ShoppingCart {
}
}

// Calculate the total price of all items in the cart, after applying the discount
getTotalPrice() {
let totalPrice = 0;
for (const item of this.items) {
Expand All @@ -25,31 +29,37 @@ class ShoppingCart {
return totalPrice - this.discount;
}

// Apply a discount amount to the total price
applyDiscount(amount) {
this.discount = amount;
}

// Display all items in the shopping cart with their index, name, and price
displayItems() {
console.log('Shopping Cart Items:');
this.items.forEach((item, index) => {
console.log(`${index + 1}. ${item.name} - $${item.price}`);
});
}

// Clear the shopping cart by removing all items and resetting the discount
clearCart() {
this.items = [];
this.discount = 0;
console.log('Cart cleared.');
}

// Get the total number of items in the shopping cart
getItemCount() {
return this.items.length;
}

// Get an item from the shopping cart by its name
getItemByName(name) {
return this.items.find(item => item.name === name);
}

// Check if an item with the given name exists in the shopping cart
containsItem(name) {
return this.items.some(item => item.name === name);
}
Expand Down
96 changes: 96 additions & 0 deletions TestCases/ShoppingCart_unittest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@

// ShoppingCart.test.js

const ShoppingCart = require('./ShoppingCart');
const Item = require('./Item');

describe('ShoppingCart', () => {
let cart;

beforeEach(() => {
cart = new ShoppingCart();
});

describe('addItem', () => {
it('should add an item to the cart', () => {
const item = new Item('Laptop', 1000);
cart.addItem(item);
expect(cart.getItemCount()).toBe(1);
expect(cart.containsItem('Laptop')).toBeTruthy();
});
});

describe('removeItem', () => {
it('should remove an item from the cart', () => {
const item1 = new Item('Laptop', 1000);
const item2 = new Item('Phone', 800);
cart.addItem(item1);
cart.addItem(item2);
cart.removeItem(1);
expect(cart.getItemCount()).toBe(1);
expect(cart.containsItem('Phone')).toBeFalsy();
});

it('should handle invalid index', () => {
const item = new Item('Laptop', 1000);
cart.addItem(item);
console.error = jest.fn();
cart.removeItem(1);
expect(console.error).toHaveBeenCalledWith('Invalid index');
});
});

describe('getTotalPrice', () => {
it('should calculate the total price', () => {
const item1 = new Item('Laptop', 1000);
const item2 = new Item('Phone', 800);
const item3 = new Item('Headphones', 100);
cart.addItem(item1);
cart.addItem(item2);
cart.addItem(item3);
expect(cart.getTotalPrice()).toBe(1900);
});

it('should apply the discount', () => {
const item1 = new Item('Laptop', 1000);
const item2 = new Item('Phone', 800);
cart.addItem(item1);
cart.addItem(item2);
cart.applyDiscount(200);
expect(cart.getTotalPrice()).toBe(1600);
});
});

describe('clearCart', () => {
it('should clear the cart', () => {
const item1 = new Item('Laptop', 1000);
const item2 = new Item('Phone', 800);
cart.addItem(item1);
cart.addItem(item2);
cart.clearCart();
expect(cart.getItemCount()).toBe(0);
expect(cart.getTotalPrice()).toBe(0);
});
});

describe('getItemByName', () => {
it('should return the item by name', () => {
const item1 = new Item('Laptop', 1000);
const item2 = new Item('Phone', 800);
cart.addItem(item1);
cart.addItem(item2);
const foundItem = cart.getItemByName('Laptop');
expect(foundItem).toEqual(item1);
});
});

describe('containsItem', () => {
it('should check if the cart contains an item', () => {
const item1 = new Item('Laptop', 1000);
const item2 = new Item('Phone', 800);
cart.addItem(item1);
expect(cart.containsItem('Laptop')).toBeTruthy();
expect(cart.containsItem('Phone')).toBeFalsy();
});
});
});
1 change: 1 addition & 0 deletions TestCases/TestEnv/node_modules/.bin/browserslist

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions TestCases/TestEnv/node_modules/.bin/create-jest

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions TestCases/TestEnv/node_modules/.bin/esparse

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions TestCases/TestEnv/node_modules/.bin/esvalidate

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions TestCases/TestEnv/node_modules/.bin/import-local-fixture

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions TestCases/TestEnv/node_modules/.bin/jest

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions TestCases/TestEnv/node_modules/.bin/js-yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions TestCases/TestEnv/node_modules/.bin/jsesc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions TestCases/TestEnv/node_modules/.bin/json5

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions TestCases/TestEnv/node_modules/.bin/node-which

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions TestCases/TestEnv/node_modules/.bin/parser

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions TestCases/TestEnv/node_modules/.bin/regjsparser

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions TestCases/TestEnv/node_modules/.bin/resolve

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions TestCases/TestEnv/node_modules/.bin/semver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions TestCases/TestEnv/node_modules/.bin/update-browserslist-db

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading