showing results for - "react enzyme hooks test"
Amber
26 Mar 2017
1//app.js
2import React from 'react'
3import FetchData from './FetchData'
4
5const fetchUsers = () =>
6fetch('https://jsonplaceholder.typicode.com/users').then((res) => res.json())
7
8function App() {
9  return (
10    <>
11      <FetchData fetchUsers={fetchUsers} />
12    </>
13  )
14}
15
16export default App
17
18//fetchdata.js
19import React, { useState } from 'react'
20
21function fetchData({ fetchUsers }) {
22  const [state, setState] = useState([])
23
24  React.useEffect(() => {
25    fetchUsers().then(setState)
26  }, [])
27
28  return (
29    <>
30      {state &&
31        state.map((val, id) => (
32          <ul key={id}>
33            <li>{val.name}</li>
34          </ul>
35        ))}
36    </>
37  )
38}
39
40export default fetchData
41
42// fetchdata.test.js
43import React from 'react'
44import { shallow } from 'enzyme'
45import FetchData from '../FetchData'
46
47describe('Fetch All Data Hooks', () => {
48  let props
49  let wrapper
50  let useEffect
51
52  const users = [
53    { id: 1, name: 'jamal' },
54    { id: 2, name: 'karyo' },
55    { id: 3, name: 'mirdad' }
56  ]
57
58  beforeEach(() => {
59    useEffect = jest.spyOn(React, 'useEffect').mockImplementationOnce((f) => f())
60    props = { fetchUsers: jest.fn().mockResolvedValue(users) }
61    wrapper = shallow(<FetchData {...props} />)
62  })
63
64  afterAll(() => {
65    jest.clearAllMocks()
66  })
67
68  it('result all users', () => {
69    expect(useEffect).toHaveBeenCalled()
70    expect(props.fetchUsers).toHaveBeenCalled()
71    expect(props.fetchUsers).toHaveBeenCalledTimes(1)
72    expect(jest.isMockFunction(props.fetchUsers)).toBeTruthy()
73  })
74
75  it('find render all users exits count', () => {
76    expect(wrapper.find('ul > li')).toHaveLength(3)
77    expect(
78      wrapper.containsAllMatchingElements([
79        <ul>
80          <li>jamal</li>
81        </ul>,
82        <ul>
83          <li>karyo</li>
84        </ul>,
85        <ul>
86          <li>mirdad</li>
87        </ul>
88      ])
89    ).toBeTruthy()
90  })
91})
queries leading to this page
react hooks fetch datahow to make unit test for react hooks with enzymeenzyme react testing useshow to fetch api data using react hooks in react jshow to fetch data from api in react hookstesting react hooksfetch functions react hooksenzyme testing hooksreact hooks testing library mock api callshow to fetch data from an api with react hookstest jest enzyme hooksreact test fatch api mediumreactjs hooks jest enzyme testing react hooks with enzymetesting custom hooks that contain useeffectfetch api hookstest a custom hook enzymetest custom hook react enzymehow to jest enzyme test cases for hooksreact enzyme async fetch api using hooksuseeffect with fetchhow to test react hooks with jest enzyme do enzyme support testing react hookstesting react hooks with jest and enzymehow to use react hooks in test file with jest and enzymefetch api data in react hook with awaithow to unit test react hooks 2021 jest enzymehow to test useeffect enzyme jestjest enzyme test hookshooks in react js jest enzyme testingreact hooks test apijest with react hooksfetch api data using react hooks in react jsfetch api react hookshow to i do fetch get request using react hooksreact fetch api post calltest state react hooksreact fetch hookfetch data from api using react hookenzyme testing hooks with fetch callsunit testing react hooks enzyme jesrtest custom hook enzymehow to test mounted component after hook has runreact eznyme fetch api using hooksenzyme fetch url hooksfetch api using react hooksreact hook testinghooks testinguse fetch hook reacthow to test useeffect hookapi fetch calls reactreact enzyme hooks test