Skip to content

cartTest.js and cart js with jasmine null value reading error #186

Description

@Atticadevs

// cartTest.js code


import { addToCart,cart,loadFromStorage } from "../../data/cart.js";

describe('test suit: addToCart',()=>{
   it('adds an existing product to the cart',()=>{
     spyOn(localStorage,'setItem');
    
    spyOn(localStorage,'getItem').and.callFake(()=>{
       return JSON.stringify([{
        productID:'e43638ce-6aa0-4b85-b27f-e1d07eb678c6',
        quantity:1,
        deliveryOptionId:'1'
       }]);
    });
     loadFromStorage();
     addToCart('e43638ce-6aa0-4b85-b27f-e1d07eb678c6');
    expect(cart.length).toEqual(1);
    expect(localStorage.setItem).toHaveBeenCalledTimes(1);
    expect(cart[0].productID).toEqual('e43638ce-6aa0-4b85-b27f-e1d07eb678c6')
    expect(cart[0].quantity).toEqual(2);

   });
   it('add new product to cart ',()=>{
    spyOn(localStorage,'setItem');

    spyOn(localStorage,'getItem').and.callFake(()=>{
       return JSON.stringify([]);
    });
     loadFromStorage();
    addToCart('e43638ce-6aa0-4b85-b27f-e1d07eb678c6');
    expect(cart.length).toEqual(1);
    expect(localStorage.setItem).toHaveBeenCalledTimes(1);
    expect(cart[0].productID).toEqual('e43638ce-6aa0-4b85-b27f-e1d07eb678c6')
    expect(cart[0].quantity).toEqual(1);
   });
}) 

// cart.js code

export let cart;
loadFromStorage();
export function loadFromStorage()
{
cart=JSON.parse(localStorage.getItem('cart'))||
[{
   productID:"e43638ce-6aa0-4b85-b27f-e1d07eb678c6",
   quantity:2,
   deliveryOptionId:'1'
},{
  productID:"15b6fc6f-327a-4ec4-896f-486349e85a3d",
  quantity:1,
     deliveryOptionId:'2'
}];
}

export function addToCart(productID)
{
let selector_quantity=Number(document.querySelector(`.js-quantity-selector-${productID}`).value);

     let matchingItem;

      cart.forEach((cartItem)=>{
             if(productID === cartItem.productID)
            {
            matchingItem=cartItem;
            }
            });
      if(matchingItem)
      {
        matchingItem.quantity+=selector_quantity;
      } else 
      {
        cart.push({
                productID:productID,
                quantity:selector_quantity,
                deliveryOptionId:'1'
                });
     }    
     saveToStorage();
}
function saveToStorage()
{
  localStorage.setItem('cart',JSON.stringify(cart))
}
export function removefromCart(productID)
{
 const newCart=[];
 cart.forEach((cartItem)=>{
   if(productID !==cartItem.productID)
     newCart.push(cartItem)
 })
cart=newCart;
saveToStorage();
};

export function updateDeliveryOption(productId,deliveryOptionID)
{
     let matchingItem;

      cart.forEach((cartItem)=>{
             if(productId === cartItem.productID)
            {
            matchingItem=cartItem;
            }
            });
     matchingItem.deliveryOptionId= deliveryOptionID;
     saveToStorage();
}
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions