Testing the useCounter initial value

We can also check the result before and after calling act and pass an initial value to our Hook.

Let's now test the initial value of our Hook:

  1. Define a new test function, passing the initial value 123 to the Hook:
test('should use initial value', () => {
const { result } = renderHook(() => useCounter(123))
  1. Now we can check if the current value equals the initial value, call increment, and ensure the count was increased from the initial value:
    expect(result.current.count).toBe(123)
act(() => result.current.increment())
expect(result.current.count).toBe(124)
})

As we can see, we can simply pass the initial value to the Hook and check whether the value is the same.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.120.109