Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

How to test useEffect in react testing library

import { List } from './';
import React from 'react';
import '@testing-library/jest-dom/extend-expect';
import { render, waitForElement } from '@testing-library/react';

describe('59892259', () => {
  let originFetch;
  beforeEach(() => {
    originFetch = (global as any).fetch;
  });
  afterEach(() => {
    (global as any).fetch = originFetch;
  });
  it('should pass', async () => {
    const fakeResponse = { title: 'example text' };
    const mRes = { json: jest.fn().mockResolvedValueOnce(fakeResponse) };
    const mockedFetch = jest.fn().mockResolvedValueOnce(mRes as any);
    (global as any).fetch = mockedFetch;
    const { getByTestId } = render(<List></List>);
    const div = await waitForElement(() => getByTestId('test'));
    expect(div).toHaveTextContent('example text');
    expect(mockedFetch).toBeCalledTimes(1);
    expect(mRes.json).toBeCalledTimes(1);
  });
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: leaflet change marker location 
Javascript :: vue 3 router redirect 
Javascript :: javascript sort array descending order 
Javascript :: jquery get cursor position 
Javascript :: react detect autofill 
Javascript :: javascript select n random from array 
Javascript :: get last letter of string javascript 
Javascript :: TypeError: Class constructor Model cannot be invoked without 
Javascript :: js array to string 
Javascript :: import js file 
Javascript :: node.js copy to clipboard 
Javascript :: react dynamic import 
Javascript :: delete in javascript 
Javascript :: dynamically added button onclick not working 
Javascript :: sequelize array of strings 
Javascript :: how to use infinite scroll in angular 
Javascript :: set lodash 
Javascript :: vuejs router params 
Javascript :: js for 
Javascript :: sort array of objects javascript by key value 
Javascript :: how to pass data in body of delete request angular 
Javascript :: window on resize 
Javascript :: preview image before upload reactjs 
Javascript :: node get value from map 
Javascript :: check object has key 
Javascript :: moment localization 
Javascript :: make country flags in js 
Javascript :: format to precision 2 javascript if double 
Javascript :: javascript timestamp to date 
Javascript :: js copy array 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =