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:
- Define a new test function, passing the initial value 123 to the Hook:
test('should use initial value', () => {
const { result } = renderHook(() => useCounter(123))
- 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.