简体   繁体   English

使用 Jest 在反应组件中测试渲染 JSON

[英]Testing render JSON in react component with Jest

I want to test the rendering of the images of my functional component, the products are in a json, I tried to import the json and pass it in the component but it didn't work:我想测试我的功能组件的图像渲染,产品在 json 中,我尝试导入 json 并将其传递到组件中,但它不起作用:

import "react-responsive-carousel/lib/styles/carousel.min.css";
import Modal from "../OverlayComponent/Modal";
import ImageGallery from "../ImageGallery/ImageGallery"
import "./ImageGalleryStatic.css";

function ImageGalleryStatic(props) {
    const { product } = props;
    return (
        <>
            {/* Image´s gallery */}
            <section className="photo-gallery">
                <div className="product-photos">
                    <div className="photos-container">
                        <figure className="photo">
                            <img src={product.images[0].url} alt="imagen del alojamiento" />
                        </figure>
                        <figure className="photo">
                            <img src={product.images[1].url} alt="imagen del alojamiento" />
                        </figure>
                        <figure className="photo">
                            <img src={product.images[2].url} alt="imagen del alojamiento" />
                        </figure>
                        <figure className="photo">
                            <img src={product.images[3].url} alt="imagen del alojamiento" />
                        </figure>
                        <figure className="photo">
                            <img src={product.images[4].url} alt="imagen del alojamiento" />
                        </figure>
                        {/* Image´s gallery pop-up */}
                        <Modal product={product} divider=" de "/>
                    </div>

                </div>
            </section>
            <section className="photo-gallery-tablet">
            <ImageGallery productImages={product.images} divider="/" showThumbs={false}/>
            </section>
        </>
    )

}

export default ImageGalleryStatic;

My initial test to verify rendering我验证渲染的初始测试

import React from 'react'
import {render} from '@testing-library/react'
import Enzyme from "enzyme";
import ImageGalleryStatic from './ImageGalleryStatic'

import Adapter from "enzyme-adapter-react-16"

const { shallow } = Enzyme;

Enzyme.configure({ adapter: new Adapter() })

describe('Test for RatingStars', () => {
    test("renders correctly", () => {
        shallow(<ImageGalleryStatic />);
    });
});

The error is in the next line, (TypeError: Cannot read property 'images' of undefined):错误在下一行,(TypeError:无法读取未定义的属性“图像”):

<img src={product.images[0].url} alt="imagen del alojamiento" />

You have a property product on your component, you need to pass that in like this:您的组件上有一个属性product ,您需要像这样传递它:

 test("renders correctly", () => {
        const testProduct = {
           images: [
              {
                 url: ''
              }
           ]
        };
        shallow(<ImageGalleryStatic product={testProduct} />);
  });

You'll need to construct your product object correctly with the other properties you are relying on.您需要使用您所依赖的其他属性正确构建您的产品 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM