कैसे परीक्षण करने के लिए एक घटक है कि डेटा fetches में useEffect और यह स्टोर में राज्य के साथ प्रतिक्रिया का परीक्षण-लाइब्रेरी और मज़ाक में?

0

सवाल

मैं कर रहा हूँ काफी नए प्रतिक्रिया करने के लिए-परीक्षण-लाइब्रेरी और आम तौर पर परीक्षण । मैं परीक्षण करना चाहते हैं एक घटक है कि डेटा fetches से एक एपीआई में useEffect हुक. तो फिर यह दुकानों में यह स्थानीय, राज्य. यह renders इन सरणी डेटा के साथ सरणी.नक्शा है, लेकिन मैं कर रहा हूँ Error: Uncaught [TypeError: Cannot read properties of undefined (reading 'map')] त्रुटि. मैं शायद गलत कर अपने परीक्षण सुइट, मैं शोध किया है की एक बहुत कुछ है, लेकिन यह तय नहीं कर सकता.

    import React from 'react';
    import { render, screen } from '@testing-library/react';
    import '@testing-library/jest-dom'
    import { rest } from 'msw';
    import { setupServer } from 'msw/node';

    import { OnePiece } from '.';

    const server = setupServer(rest.get('server http address', (req, res, ctx) => {
        const totalData = [
            { name: "doffy", price: 100, image: "image url" },
            { name: "lamingo", price: 500, image: "image url" }
        ];
        return res(
            ctx.status(200),
            ctx.json({
                data: { crew: totalData }
            })
        )
    }))
    beforeAll(() => server.listen());
    afterAll(() => server.close());
    beforeEach(() => server.restoreHandlers());

    //console.log("mocking axios", axios)
    describe('OnePiece', () => {
        
        test('fetches the data from the API and correctly renders it', async () => {
            //Here's probably where i fail. Please someone tell me the right way :) 
            await render(<OnePiece />)
            const items = await screen.findAllByAltText('product-image');
            expect(items).toHaveLength(2);
            //     screen.debug()
        })
    })

और नीचे के भागों है कोड useEffect, और totalData.नक्शा घटक:

const [totalData, setTotalData] = useState([]);
const [crew, setCrew] = useState('straw-hat-pirates');

 useEffect(() => {
    let isApiSubscribed = true;
    const getOpData = async () => {
        const getCrews = await axios.get('http address');
        if (isApiSubscribed) {
            let data = getCrews.data;
            data = data[crew];
            // console.log("data", data);
            setTotalData(data);
        }
    }
    getOpData();
    return () => {
        isApiSubscribed=false;
    }
}, [crew])
.........

//in the return part
 <ProductsWrapper>
            {totalData.map((product, index) =>
                <ProductCard key={index} name={product.name} price={product.price} imageUrl={product.image} />
            )}
        </ProductsWrapper>
1

सबसे अच्छा जवाब

0

मैं के रूप में भविष्यवाणी की, समस्या थी async डेटा लाया जा रहा है. वर्तमान में setTimeOut है मेरे लिए पर्याप्त से अधिक है, लेकिन अगर किसी को देखता है, यह भविष्य में, आप कर सकते हैं के लिए देखो waitFor विधि की प्रतिक्रिया का परीक्षण-पुस्तकालय. यहाँ निश्चित हिस्सा:

  describe('OnePiece', () => {
      test('fetches the data from the API and correctly renders it', async () => {
          render(<OnePiece />)
          setTimeout(async () => {
              const items = await screen.findAllByAltText('product-image');
              expect(items).toHaveLength(2);
            }, 4000)
            //screen.debug()
        })
    })
2021-11-23 19:55:14

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में