Intial props not working when rendering a hook (or I've done something wrong.)
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 3/5
- Tempo estimado
- 1-2 dias
- Facilidade para iniciantes
- 45/100
- Tipo de issue
- Bug
- Clareza
- Razoavelmente clara
- Status de atividade
- Estagnada
- Stack de tecnologia
- react, typescript
- Domínio
- testing
Direção de pesquisa
Comece com a chamada de renderHook e o wrapper na reprodução fornecida e, em seguida, inspecione como initialProps é passado para o wrapper. Execute novamente o teste custom-step e confirme que o wrapper recebe step 2 inicialmente, recebe step 8 após rerender e que as asserções de count passam.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
"@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:
- Linguagem predominante
- JavaScript
- Estrelas
- 19.7k
- Forks
- 1.2k
- Métricas de merge de PRs
- Nenhum PR com merge em 30d
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Tem um modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de testing-library/react-testing-library
-
fireEvent.select does not wrap its automatic native focus in actTalvez já em andamento @sergioperezcheco assumiu há 10 dias. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 88/100
-
bug: calling configure() without reactStrictMode resets it to undefined, silently disabling strict modeTalvez livre de novo @suhailopensource assumiu há 80 dias e não há nenhum pull request aberto. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 35/100
testing-library/react-testing-library#1466 · 1 comentário ·
-
Dificuldade 4/5 3-5 dias Facilidade para iniciantes 30/100
testing-library/react-testing-library#1459 · 2 comentários ·
-
perf: optimize container lookup with early exitTalvez livre de novo @Ch-Abhinav-Chowdary assumiu há 306 dias e não há nenhum pull request aberto. Aberta
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 35/100
testing-library/react-testing-library#1430 · 1 comentário ·
-
`fireEvent.mouseEnter` does not forward `relatedTarget` (relatedTarget is the window instead)Talvez já em andamento @swarnim02 assumiu há 324 dias. Aberta
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 55/100
Todas as issues de testing-library/react-testing-library
Issues semelhantes
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 68/100
Mantenedores costumam responder em até 2 dias
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 65/100
bigskysoftware/htmx#4147 ·
Mantenedores costumam responder em até 5 dias
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 62/100
vshymanskyy/StandWithUkraine#316 ·
-
README: the green and red points are swapped (green = .fset tracking, red = .fset3 detection)Aberta
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 90/100
-
bug/1-unconfirmed kind/bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
Mantenedores costumam responder em até 1 dia