Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
Testing a custom hook with useSelector

I have a custom hook I wrote and I am trying to test just the hook itself but I need to wrap it inside of a Provider. see error below

Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider>

Below is my custom hook.

import _ from 'lodash';
import { useSelector } from 'react-redux';

const useSelectedTable = () => {
  const { tables, currentTableId } = useSelector((state: any) => {
    return {
      tables: state.tables,
      currentTableId: state.currentTableId
    };
  });

  const table = _.find(tables, tab => tab._id === currentTableId);

  return table;
};

export default useSelectedTable;

The test that I am workign on looks like this right now.

describe('useSelectedTable', () => {
  test('Should get current selected table', () => {
    const { result } = renderHook(() => useSelectedTable());

    console.log(result.all);
  });
});

any thoughts?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since you hook uses redux you will need to add a wrapper that has the store on it, something like this:

  const store = {} // whatever you to do test your mock store today
  const wrapper = ({ children }) => <Provider store={store}>{children}</Provider>

  renderHook(() => useSelectedTable(), { wrapper });

here is the docs that talk about adding the wrapper with react hooks https://react-hooks-testing-library.com/usage/advanced-hooks#context

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda