Intial props not working when rendering a hook (or I've done something wrong.)
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 45/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- react, typescript
- 领域
- testing
调研方向
先从所提供复现中的 renderHook 调用和 wrapper 开始,然后检查 initialProps 是如何传递给 wrapper 的。重新运行 custom-step 测试,并确认 wrapper 初始接收到 step 2,在 rerender 后接收到 step 8,且 count 断言通过。
由索引模型根据 Issue 内容生成。
描述
"@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:
- 主要语言
- JavaScript
- 星标
- 19.7k
- 派生
- 1.2k
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
testing-library/react-testing-library 的其他 Issue
-
fireEvent.select does not wrap its automatic native focus in act可能已有人在做 @sergioperezcheco 于 5 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
-
bug: calling configure() without reactStrictMode resets it to undefined, silently disabling strict mode可能重新可做 @suhailopensource 于 75 天前认领,目前没有进行中的 PR。 未关闭
难度 2/5 1-3 小时 新手友好度 35/100
testing-library/react-testing-library#1466 · 1 条评论 ·
-
难度 4/5 3-5 天 新手友好度 30/100
testing-library/react-testing-library#1459 · 2 条评论 ·
-
perf: optimize container lookup with early exit可能重新可做 @Ch-Abhinav-Chowdary 于 301 天前认领,目前没有进行中的 PR。 未关闭
难度 1/5 1 小时以内 新手友好度 35/100
testing-library/react-testing-library#1430 · 1 条评论 ·
-
`fireEvent.mouseEnter` does not forward `relatedTarget` (relatedTarget is the window instead)可能已有人在做 @swarnim02 于 318 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 55/100
查看 testing-library/react-testing-library 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
jaegertracing/jaeger-ui#4596 ·
维护者通常 1 天内回复
-
channels:add check:passed
难度 1/5 1 小时以内 新手友好度 72/100
维护者通常 9 天内回复
-
module: core
难度 2/5 1-3 小时 新手友好度 77/100
bigbluebutton/bigbluebutton#26028 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 91/100
twbs/bootstrap#43050 · 1 条评论 ·
维护者通常 1 天内回复
-
status:untriaged
难度 1/5 1 小时以内 新手友好度 78/100
midnightntwrk/servicedesk#248 ·
维护者通常 1 天内回复