Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intial props not working when rendering a hook (or I've done something wrong.) #1277

Open
gbatterbee opened this issue Jan 25, 2024 · 5 comments
Labels
BREAKING CHANGE This change will require a major version bump enhancement New feature or request

Comments

@gbatterbee
Copy link

    "@testing-library/dom": "^8.13.0",
    "@testing-library/jest-dom": "^6.1.3",
    "@testing-library/react": "13.3.0",
    "@testing-library/react-hooks": "^8.0.1",
    "@testing-library/user-event": "^14.4.3",
     "react": "^18.2.0",
    "react-dom": "^18.2.0",   
    "react-test-renderer: 18.1.0)"
    "`node` version: 18"
    "`pnpm` (or `yarn`) version: 7"

Relevant code or config:

In the Reproduction details.
Taking the example from the docs, logged the initial props;

const CounterStepContext = createContext(1);

export const CounterStepProvider = ({
  step,
  children,
}: {
  step: number;
  children: React.ReactNode;
}) => {
  console.log({ step });
  return (
    <CounterStepContext.Provider value={step}>
      {children}
    </CounterStepContext.Provider>
  );
};

export function useCounter(initialValue = 0) {
  const [count, setCount] = useState(initialValue);
  const step = useContext(CounterStepContext);
  const increment = useCallback(() => setCount((x) => x + step), [step]);
  const reset = useCallback(() => setCount(initialValue), [initialValue]);
  return { count, increment, reset };
}

test("should use custom step when incrementing", () => {
  const wrapper = ({
    children,
    step,
  }: {
    step: number;
    children: React.ReactNode;
  }) => <CounterStepProvider step={step}>{children}</CounterStepProvider>;
  const { result, rerender } = renderHook(() => useCounter(), {
    wrapper,
    initialProps: {
      step: 2,
    },
  });

  act(() => {
    result.current.increment();
  });

  expect(result.current.count).toBe(2);

  /**
   * Change the step value
   */
  rerender({ step: 8 });

  act(() => {
    result.current.increment();
  });

  expect(result.current.count).toBe(10);
});
  console.log
    { step: undefined }

What you did:

Trying to use initialProps with the renderHook

What happened:

Initial props are not being passed.
No obvious error message, component just isn't receiving the props.

Reproduction:

Problem description:

Suggested solution:

@gbatterbee gbatterbee added the bug Something isn't working label Jan 25, 2024
@mpeyper
Copy link
Member

mpeyper commented Jan 31, 2024

Hi @gbatterbee, is this still causing you issues?

I want to confirm whether you are using renderHook from @testing-library/react-hooks (this library, up to react 17) or @testing-library/react (that library, react 18 and newer). We have a test around this case, does that pass in your environment?

@gbatterbee
Copy link
Author

@mpeyper Sorry, didn't get notified of your message for some reason.
This is where we're using renderHook from.
image

I've not tried recently.
I'll take a look at your test case.
Thanks

@gbatterbee
Copy link
Author

This is what i get
image

@mpeyper
Copy link
Member

mpeyper commented Feb 6, 2024

@mpeyper Sorry, didn't get notified of your message for some reason. This is where we're using renderHook from. image

I've not tried recently. I'll take a look at your test case. Thanks

In that case, I’ll transfer your ticket to their repo ( @testing-library/react) instead as we’re actually a different library. I hope they can get to the bottom of it for you.

@mpeyper mpeyper transferred this issue from testing-library/react-hooks-testing-library Feb 6, 2024
@eps1lon
Copy link
Member

eps1lon commented Feb 7, 2024

initialProps is for the hook, not the component.

We should change the naming to avoid confusion. testing-library/react-hooks passed initialProps to both which is also confusing because it's harder to reason about (especially from a type perspective).

@eps1lon eps1lon added enhancement New feature or request BREAKING CHANGE This change will require a major version bump and removed bug Something isn't working labels Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BREAKING CHANGE This change will require a major version bump enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants