A Node.js shopping cart application built using Test-Driven Development (TDD) and Object-Oriented Programming (OOP) principles.
-
Clone the repository:
git clone git@github.com:archit15singh/shopping-cart-nodejs.git cd shopping-cart-nodejs
-
Install dependencies:
npm install
-
Create a
.env
file with the following content:PORT=3000 DB_URI=mongodb://localhost:27017/shopping_cart JWT_SECRET=your_jwt_secret
-
Run MongoDB using Docker and start the application:
docker run --name mongodb -p 27017:27017 --rm mongo
npm start
-
Run tests:
npm test
-
Generate coverage report:
npx jest --coverage
- Description: Register a new user.
- Request Body:
{ "username": "string", "password": "string" }
- Response Body:
{ "message": "User registered successfully" }
- Description: Log in a user.
- Request Body:
{ "username": "string", "password": "string" }
- Response Body:
{ "token": "string" }
- Description: Create a new product.
- Request Body:
{ "name": "string", "price": "number", "stock": "number" }
- Response Body:
{ "_id": "string", "name": "string", "price": "number", "stock": "number", "createdAt": "string", "updatedAt": "string" }
- Description: Get all products.
- Response Body:
[ { "_id": "string", "name": "string", "price": "number", "stock": "number", "createdAt": "string", "updatedAt": "string" } ]
- Description: Get a product by ID.
- URL Parameter:
id
(Product ID) - Response Body:
{ "_id": "string", "name": "string", "price": "number", "stock": "number", "createdAt": "string", "updatedAt": "string" }
- Description: Update a product.
- URL Parameter:
id
(Product ID) - Request Body:
{ "name": "string", "price": "number", "stock": "number" }
- Response Body:
{ "_id": "string", "name": "string", "price": "number", "stock": "number", "createdAt": "string", "updatedAt": "string" }
- Description: Delete a product.
- URL Parameter:
id
(Product ID) - Response Body:
{ "message": "Product deleted successfully" }
- Description: Create a new cart.
- Response Body:
{ "_id": "string", "userId": "string", "products": [], "createdAt": "string", "updatedAt": "string" }
- Description: Get the user's cart.
- Response Body:
{ "_id": "string", "userId": "string", "products": [ { "productId": "string", "quantity": "number" } ], "createdAt": "string", "updatedAt": "string" }
- Description: Add a product to the cart.
- Request Body:
{ "productId": "string", "quantity": "number" }
- Response Body:
{ "_id": "string", "userId": "string", "products": [ { "productId": "string", "quantity": "number" } ], "createdAt": "string", "updatedAt": "string" }
- Description: Remove a product from the cart.
- Request Body:
{ "productId": "string" }
- Response Body:
{ "_id": "string", "userId": "string", "products": [], "createdAt": "string", "updatedAt": "string" }
- Description: Update product quantity in the cart.
- Request Body:
{ "productId": "string", "quantity": "number" }
- Response Body:
{ "_id": "string", "userId": "string", "products": [ { "productId": "string", "quantity": "number" } ], "createdAt": "string", "updatedAt": "string" }
- Description: Get the cart summary.
- Response Body:
{ "itemCount": "number", "totalPrice": "number" }
- Description: Empty the cart.
- Response Body:
{ "message": "Cart emptied successfully" }
- Description: Apply a discount code.
- Request Body:
{ "code": "string" }
- Response Body:
{ "discountApplied": "boolean", "totalPrice": "number" }
- Description: Save the cart for later.
- Response Body:
{ "message": "Cart saved successfully" }
- Description: Retrieve the saved cart.
- Response Body:
{ "cart": { "_id": "string", "userId": "string", "products": [ { "productId": "string", "quantity": "number" } ], "createdAt": "string", "updatedAt": "string" } }
- username: String, required, unique
- password: String, required
- createdAt: Date, default: Date.now
- updatedAt: Date, default: Date.now
- name: String, required
- price: Number, required
- description: String
- stock: Number, required
- createdAt: Date, default: Date.now
- updatedAt: Date, default: Date.now
- userId: ObjectId, ref: "User", required
- products: Array of
{ productId: ObjectId, quantity: Number }
- discountCode: String
- savedState: Mixed
- createdAt: Date, default: Date.now
- updatedAt: Date, default: Date.now
Located in src/tests/unit
.
- CartService: Tests creation, addition, removal, and quantity update of cart products, and error handling for non-existent carts or invalid quantities.
Located in src/tests/integration
.
- Cart Routes:
- Create a cart
- Add/remove/update products in the cart
- Apply discount codes
- Save/retrieve the cart
- Handle authentication
- Product Routes:
- Create/retrieve/update/delete products
- Handle invalid data
- Handle non-existent products
- User Routes:
- User registration
- User login
- Invalid login credentials
- Missing fields during registration
Located in client/clientTests.js
.
- Register User
- Login User
- Create Product
- Get All Products
- Get Product by ID
- Update Product
- Delete Product
- Create Cart
- Add Product to Cart
- Remove Product from Cart
- Update Product Quantity in Cart
- Get Cart Summary
- Empty Cart
- Apply Discount
- Save Cart
- Retrieve Saved Cart
- Chaos Monkey Tests: Tests various failure scenarios like invalid product creation, adding non-existent products to the cart, invalid discount codes, and invalid user login.
- Development Tools: Babel, Jest, Nodemon
- Dependencies: Express, Mongoose, JWT, Bcrypt, Axios, Dotenv
- Environment: MongoDB, Node.js