wrapper context is not shared with `renderHook`
@Lucatonello がすでに取り組んでいます。
2024年12月2日 から。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 35/100
- issue の種類
- 機能追加
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- javascript, react
調査の方向性
Issue に示されている renderHook ラッパーの動作と waitFor アサーションから始めます。個別の renderHook 呼び出しがそれぞれのラッパーをどのように mount するかを追跡し、その後、複数の hook 関数を含む単一の renderHook 呼び出しと比較します。完了時には、関連する hook インスタンスを個別にテストするためのサポートされた方法を確立するか、制限事項と推奨されるテスト方法を文書化する必要があります。
索引モデルが issue の本文から書いたものです。
説明
I have a hook that does some complicated things with state[^1]. Specifically, it works like this:
const [pizza, setPizza] = usePizza()
console.log(pizza)
// { my: { slice: "slice22" }, other: { slice: "slice19" } }
const [slice] = usePizza(pizza => pizza.my.slice)
console.log(slice)
// "slice22"
When the user calls setPizza, a new pizza object gets set globally. But the second instance of the hook usePizza(pizza => pizza.my.slice) does not re-render unless that "slice" of the global pizza object specifically changed. (In other words, if pizza.my.slice returns the same value as before ("slice22"), any component using that hook will not re-render.)
This property of not re-rendering unless necessary is a critical part of the hook. I want to test it. To do that, I need to do something like this:
const wrapper = (
... common context here (from Tanstack Query)
)
// for the moment, let's pretend I have methods `.toHaveRenderedOnce` and `.toHaveRenderedTwice`
test('it only re-renders a slice when it changes', () => {
const {result: fullPizza} = renderHook(() => usePizza(), {wrapper})
const {result: mySlice} = renderHook(() => usePizza(pizza => pizza.my.slice), {wrapper})
const {result: otherSlice} = renderHook(() => usePizza(pizza => pizza.other.slice), {wrapper})
const updatedPizza = { my: { slice: "slice22" }, other: { slice: "slice55" }}
const [pizza, setPizza] = fullPizza.current
setPizza(updatedPizza)
await waitFor(() => {
expect(fullPizza.current[0]).toEqual(updatedPizza)
})
// my slice did not change; it should render once
await waitFor(() => {
expect(mySlice.current[0]).toEqual("slice22")
expect(mySlice).toHaveRenderedOnce() // notional
})
// other slice changed; it should render twice
await waitFor(() => {
expect(otherSlice.current[0]).toEqual("slice55")
expect(mySlice).toHaveRenderedTwice() // notional
})
})
There are (at least) two problems here. The first is that there's no way to check how many times the hook rendered, but that's a different problem (for which I may have a rudimentary solution).
The second problem, and the subject of this issue, is that it seems that the wrapper context is not shared between my two hooks. This line does not work:
await waitFor(() => {
expect(otherSlice.current[0]).toEqual("slice55")
})
// this value is never picked up when I change it in the other hook
So, even though the wrapper itself is the same instance, the two hooks don't seem to be sharing the same React context. I can do this instead:
const {result} = renderHook(() => [
() => usePizza(),
() => usePizza(pizza => pizza.my.slice),
() => usePizza(pizza => pizza.other.slice)
], {wrapper})
This solves the wrapper issue; changes in usePizza() are reflected in the other slice (usePizza(pizza => pizza.other.slice)). But unfortunately now all three instances of the hook are rendered in tandem, so they always all have the same number of renders. I can't verify that usePizza(pizza => pizza.my.slice) doesn't re-render.
I suppose it makes sense that the context is isolated between different calls to renderHook, or we might leak state in between tests. But then how can I test this functionality? Is there a way using this library to test that a change inside one hook instance either does or does not trigger an update/re-render in another related hook instance?
[^1]: Specifically, I'm using Tanstack Query's select data transformation, similar to the idea of useContextSelector.
- 主要言語
- JavaScript
- スター
- 19.7k
- フォーク
- 1.2k
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
testing-library/react-testing-library のほかの issue
-
fireEvent.select does not wrap its automatic native focus in act対応中かも @sergioperezcheco が 6 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
-
bug: calling configure() without reactStrictMode resets it to undefined, silently disabling strict mode再び着手できるかも @suhailopensource が 76 日前に担当しましたが、オープン中のプルリクエストはありません。 オープン
難易度 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 が 302 日前に担当しましたが、オープン中のプルリクエストはありません。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 35/100
testing-library/react-testing-library#1430 · コメント 1 件 ·
-
`fireEvent.mouseEnter` does not forward `relatedTarget` (relatedTarget is the window instead)対応中かも @swarnim02 が 319 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 55/100
testing-library/react-testing-library の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
-
[quality] Nothing keeps docs/skills/manifest.md in sync with docs/skills/*.md対応中かも @hivecommons-hive が今日担当しました。 オープンagent/quality hive/covered-by-pr hive/hosted-available-lke648397-260827-5n31 quality testing
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
needs verification
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 6 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
apache/rocketmq-dashboard#6610 ·
メンテナーはふだん 3 日以内に返信