-
Notifications
You must be signed in to change notification settings - Fork 231
Closed as not planned
Labels
questionFurther information is requestedFurther information is requested
Description
I have written a simple component and test file which aims to test the hook and the UI. It uses the useCounter hook example from the docs.
TextComp.tsx
import React, { FC, useCallback, useState } from 'react';
export const TestComp: FC = () => {
const { count } = useCounter();
return <p>{`COUNT: ${count}`}</p>;
};
export const useCounter = () => {
const [count, setCount] = useState(0);
const increment = useCallback(() => setCount((x) => x + 1), []);
return { count, increment };
};
TestComp.spec.tsx
import { TestComp, useCounter } from './TestComp';
import { act, render, renderHook, screen } from '@testing-library/react';
it('should render successfully', () => {
const { asFragment } = render(<TestComp />);
expect(asFragment()).toMatchSnapshot();
});
it('should have init value of 0', () => {
render(<TestComp />);
expect(screen.getByText('COUNT: 0')).toBeInTheDocument();
});
it('should increment via hook', () => {
const { result } = renderHook(() => useCounter());
expect(result.current.count).toBe(0);
act(() => {
result.current.increment();
});
expect(result.current.count).toBe(1);
});
it('should update ui from hook', async () => {
render(<TestComp />);
const { result } = renderHook(() => useCounter());
await act(() => {
result.current.increment();
});
expect(result.current.count).toBe(1);
await expect(screen.getByText('COUNT: 1')).toBeInTheDocument();
});
The test file has 4 tests.
- The component renders properly and matches a snapshot ✅
- The UI of the component shows the string "COUNT: 0" (0 is the default value for the useCounter hook) ✅
- The hook can be tested and the increment function is working correctly ✅
- The UI reflects the hooks value changing - I would expect it to now say "COUNT: 1" after running the increment function ❌
All of these tests pass except 4. Where I expect to see "COUNT: 1" I actually see "COUNT: 0". Why is this?
Is this due to a race condition / async operation? Am I using the library completely wrong? I want to be able to test that the hook functionality properly updates the UI, but I am seeing what looks like an out of date Ui version. Thanks for the help.
Metadata
Metadata
Assignees
Labels
questionFurther information is requestedFurther information is requested
