Skip to content

toHaveProp not working on Button #27

@mdjastrzebski

Description

@mdjastrzebski
  • react-native or expo: react-native (ver. 4.8.0)
  • @testing-library/react-native version: 5.0.3
  • jest-preset: react-native
  • react-native version: 0.62.2
  • node version: v12.16.3

Relevant code or config:

import React from 'react';
import {View, Text, Button} from 'react-native';
import {render} from '@testing-library/react-native';
import '@testing-library/jest-native/extend-expect';

test('toHaveProp works correctly', () => {
  const {queryByTestId} = render(
    <View>
      <Text allowFontScaling={false} testID="text">
        text
      </Text>
      <Button disabled testID="button" title="ok" />
    </View>,
  );

  expect(queryByTestId('button')).toHaveProp('accessibilityStates', [
    'disabled',
  ]);
  expect(queryByTestId('button')).toHaveProp('accessible');
  expect(queryByTestId('button')).not.toHaveProp('disabled');
  expect(queryByTestId('button')).not.toHaveProp('title', 'ok');
});

What you did:

Running toHaveProp sample code for jest-native website.

What happened:

Test fails with following error:

 FAIL  __tests__/jestNative.test.tsx
  ● toHaveProp works correctly

    expect(element).toHaveProp("accessibilityStates", ["disabled"]) // element.getAttribute("accessibilityStates") === ["disabled"]

    Expected the element to have prop:
      accessibilityStates=["disabled"]
    Received:
      null

      14 |   );
      15 | 
    > 16 |   expect(queryByTestId('button')).toHaveProp('accessibilityStates', [
         |                                   ^
      17 |     'disabled',
      18 |   ]);
      19 |   expect(queryByTestId('button')).toHaveProp('accessible');

      at Object.<anonymous> (__tests__/jestNative.test.tsx:16:35)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        2.456s, estimated 5s

Reproduction:

I've setup a repro repository on github: https://github.com/mdjastrzebski/repro-jest-native-to-have-prop

Basically it's fresh React Native CLI projects, with current version of @testing-library/react-native and @testing-library/jest-native. I have also seen the same problem when using react-native-testing-library instead of @testing-library/react-native.

Problem description:

toHaveProp does not seem to recognize some props on the Button elements. Sample code from jest-native docs fails.

Additional question: why is disabled and title from sample docs supposed to not be recognized by toHaveProp? Is it some issue related to Button or toHaveProp?

Suggested solution:

Fix 'toHaveProp' to work correctly on docs sample code OR fix the docs.
Document and explain non-trival behavior when checking Button props.

Can you help us fix this issue by submitting a pull request?

Not really, as I do not know what should be the expected behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions