reactjsreact-nativeunit-testingreact-testing-libraryreact-native-testing-library

test prop that's receiving number with testing-library/react-native


i tried to read the documentation of at testing-library/react-native and i searched on stackoverflow but i didn't found a good result.

I have this component

import React from 'react';
import { CardContainer, Title, Icon, Item, ClickableItem, StyledRow, StyledRowImage } from './style';

interface Props {
  title?: string;
  imageURL?: any;
  onPress?: () => {};
  children?: JSX.Element;
  iconWidth?: number;
  iconHeight?: number;
  padding?: number;
  withouIcon?: boolean;
  setBorder?: {};
  fontSize?: number;
  margin?: number;
  withoutIcon?: boolean;
}
const ClickableCard: React.FC<Props> = ({
  title,
  imageURL,
  onPress,
  children,
  iconWidth,
  iconHeight,
  padding,
  setBorder,
  fontSize,
  setMargin,
  withoutIcon,
  expand = false,
}) => {
  return (
    <CardContainer
      padding={padding}
      style={expand !== true ? { display: 'flex', flexShrink: 0.5, flexGrow: 0.5 } : null}
    >
      {children || (
        <ClickableItem onPress={onPress} setBorder={setBorder} setMargin={setMargin}>
          <>
            {!withoutIcon && (
              <StyledRowImage>
                {imageURL && <Icon source={imageURL} iconWidth={iconWidth} iconHeight={iconHeight} />}
              </StyledRowImage>
            )}
            <StyledRow>
              <Title fontSize={fontSize}>{title}</Title>
            </StyledRow>
          </>
        </ClickableItem>
      )}
    </CardContainer>
  );
}

and i need to test the first props padding, that receive a number. I tried getByText too but, obv, it doesn't work.

i tried to


import React from 'react';
import { fireEvent, render, wait } from '@testing-library/react-native';
import ClickableCard from '@components/ClickableCard';

describe('ClickableCard component', () => {
  it('renders correctly', () => {
    render(<ClickableCard />);
  });

  it('renders with the specified padding', () => {
    const { queryBy } = render(<ClickableCard padding={10} />);

    const padding = queryBy(10);
    expect(padding).toBeDefined();
    expect(padding.children.length).toBe(10);
    expect(padding.children[0]).toBe('ClickableCard');
  });
});

I was asking to my self if it exist a sort of getByNumber or something like that.

The question is:

how can i correctly test the const { queryBy } = render(<ClickableCard padding={10} />);


Solution

  • I had the same problem and I solved it by setting an testID to my element and then using .props to access. For you it would be something like:

    return (
      <CardContainer
        {/*Add testID to the component you wanna get the prop from*/}
        testID="container-test-id"
        padding={padding}
        style={expand !== true ? { display: 'flex', flexShrink: 0.5, flexGrow: 0.5 } : null}
      >
        {/*...*/}
      </CardContainer>
    );
    

    Then on your test you get it with getByTestId

    it('renders with the specified padding', () => {
      const { getByTestId } = render(<ClickableCard padding={10} />);
    
      expect(getByTestId('container-test-id').props.padding).toBe(10)
    });
    

    Hope it helps.