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

164
Visualizações
React Native check context value on click test case

I have following context

Home.tsx

export const ThemeContext = React.createContext(null)

const Home = () => {
  const { width } = Dimensions.get("window")
  const [theme, setTheme] = React.useState({
    active: 0,
    heightOfScrollView: 0,
    profileWidth: width * 0.2,
    scrolledByTouchingProfile: false
  })
  
  const horizontalScrollRef = React.useRef<ScrollView>()
  const verticalScrollRef = React.useRef<ScrollView>()

  return (
    <>
      <SafeAreaView style={styles.safeAreaContainer} />
      <Header title="Contacts" />
      <ThemeContext.Provider value={{ theme, setTheme }}>

In component A, I have a button which changes in the context

const onProfileTouched = (index: number) => {
  setTheme({ ...theme, active: index });
};

This leads to an image being active

const ImageCircle = ({ active, uri }: Props) => {
  return (
    <View
      style={
        active
          ? { ...styles.parentView, ...styles.active }
          : { ...styles.parentView }
      }>
      <Image source={uri} width={30} height={30} />
    </View>
  );
};

Now, I want to write a test case (I haven't written a test case before) that confirms that the state has actually changed or perhaps an active border is added to the image

I added a testId to my button which I used to fire an event

it('changes active on profile clicked', () => {
  const { getByTestId } = render(<Home />);
  fireEvent.press(getByTestId('HScroll3.button'));
});

Now, I am unsure, how to grab the value of context or change in style so as I can confirm that indeed the component for which the button is pressed is active

I am using import {render, fireEvent} from '@testing-library/react-native' but open to change.

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

0

With testing-library you want to check the visual output, not the internal state. This way your tests are much more valuable, because the end user doesn't care if you're using context, state or anything else, they care if the button has the "active" state. So if at some point you'll decide to change your mind and refactor theming completely, your test will still give you value and confidence.

I would suggest to install @testing-library/jest-native, you just have to add this setupFilesAfterEnv": ["@testing-library/jest-native/extend-expect"] to your Jest config or import that extend-expect file in your test setup file.

Once you have it set up, you're ready to go.

I don't know what's in your styles.active style, but let's assume it's e.g. { borderColor: '#00ffff' }. Add a testID prop to the View in ImageCircle component, e.g. testID="imageCircleView". Then to test if everything works as you'd expect, you just have to add this to your test:

expect(getByTestId('imageCircleView')).toHaveStyle({ borderColor: '#00ffff' });

And that's it.

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