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);
});
});
Also in JavaScript:
- nodejs sharp change image to multiple sizes
- event listener javascript
- vuejs router params
- get all image tags javascript
- how to animate a flash in jquery
- javascript pass by value
- javascript remove parameters from current url
- javascript dynamic variable name
- javascript scroll to bottom
- game winner pop up in javascript
- get value of ajax success in variable
- javascript modify url without reloading page
- react lifecycle hooks
- jquery wrap inner text
- axios react
- what is .eslint.js in vue
- convert an array of strings to numbers
- js reload page 1024 breakpoint
- draw line between two div css
- stripe stripe js
- node.js version
- fullscreen electron
- angular generate component without spec
- jquery: get selected option of the drop down list