TypeError:GetItem在测试具有节点模块钩的组件时不是功能

发布于 2025-02-10 13:50:14 字数 1415 浏览 0 评论 0原文

我正在学习反应,并陷入测试我的一个组件之一。 我使用React-use-cart管理购物车,这就是代码外观

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);

测试在“ productIncart = getItem(product.id)”上打破,说“ typeError:getItem不是函数”。 我一直陷入困境几天,真的很感谢您的帮助,这是我第一次问一个问题,因此,如果您需要更多信息,请告诉我,谢谢。

也关于测试,在渲染阶段进行测试中断

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>
        );
    });

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>
        );
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲念泪 2025-02-17 13:50:14

您缺少cartprovider

{...}

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

You are missing CartProvider:

{...}

    beforeEach(() => {
        render(
          <CartProvider>
            <Router>
                <AddToCart product={product} styles={styles}/>
            </Router>
          </CartProvider>
        );
    });
卖梦商人 2025-02-17 13:50:14

您是否尝试记录getItem的值以查看它是否确实是一个函数?尽管我不熟悉用途,但似乎您确实正确地进行了操作。
除此之外,addTocart函数缺少结尾的卷发括号,我认为您只是没有粘贴整个代码?

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 ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文