Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

230
Vistas
TypeError: getItem is not a function when testing a component with node module hooks

I am learning React, and got stuck in testing one of my components. I use react-use-cart to manage the cart, this is how code looks

import React from "react";
import { Link } from "react-router-dom";
import { useCart } from "react-use-cart";
import PropTypes from "prop-types";

import handleQuantityChange from "../../logic/product/handleQuantityChange";

function AddToCart(props) {
    const {product, styles, loading} = props;
    const { addItem, inCart, getItem, updateItemQuantity } = useCart();
    const productInCart = getItem(product.id);

The test breaks at "productInCart = getItem(product.id)" saying " TypeError: getItem is not a function". I've been stuck at this for several days, and would really appreciate some help, it's my first time asking a question, so if you need any more info let me know, thank you.

Also about the test, test breaks on the render stage

import React from "react";
import { render, screen } from "@testing-library/react";
import AddToCart from "../AddToCart";
import { MemoryRouter as Router } from "react-router-dom";
describe("get item is not a function", () => { 
    const product = {
        name: "test name",
        imgUrl: "test image",
        price: 555,
        shipping: 1000,
        id: 55
    }; 
    const styles = []


    beforeEach(() => {
        render(
            <Router>
                <AddToCart product={product} styles={styles}/>
            </Router>
        );
    });

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You are missing CartProvider:

{...}

    beforeEach(() => {
        render(
          <CartProvider>
            <Router>
                <AddToCart product={product} styles={styles}/>
            </Router>
          </CartProvider>
        );
    });
about 4 years ago · Juan Pablo Isaza Denunciar

0

Did you try logging the value of getItem to see if it's indeed a function ? Though I'm not familiar with use-cart it does seem like you're doing it correctly. Other than that, the addToCart function is missing an ending curly brace, I assume you just didn't paste the whole code in ?

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda