Skip to content

feat: support @testing-library/react-native 7.0 #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

brunohkbx
Copy link
Collaborator

@brunohkbx brunohkbx commented Nov 12, 2020

Rewrite toBeDisabled asserting the logic RN does around onStartShouldSetResponder as discussed in #23

  • Documentation added to the
    docs
  • Typescript definitions updated
  • Tests
  • Ready to be merged

Resolves #43

@brunohkbx brunohkbx changed the title BREAKING CHANGE: support @testing-library/react-native 7.0 feat: support @testing-library/react-native 7.0 Nov 12, 2020
@brunohkbx brunohkbx force-pushed the feat/support-@testing-library/react-native-7.0 branch 2 times, most recently from 0accbc4 to 6978fcb Compare November 12, 2020 15:57
@codecov
Copy link

codecov bot commented Nov 12, 2020

Codecov Report

Merging #50 (f63a819) into master (3cfd279) will decrease coverage by 3.65%.
The diff coverage is 100.00%.

❗ Current head f63a819 differs from pull request most recent head 1f1bda8. Consider uploading reports for the commit 1f1bda8 to get more accurate results
Impacted file tree graph

@@             Coverage Diff             @@
##            master      #50      +/-   ##
===========================================
- Coverage   100.00%   96.34%   -3.66%     
===========================================
  Files            8        8              
  Lines           96       82      -14     
  Branches        28       26       -2     
===========================================
- Hits            96       79      -17     
- Misses           0        1       +1     
- Partials         0        2       +2     
Impacted Files Coverage Δ
src/to-be-disabled.js 60.00% <100.00%> (-40.00%) ⬇️
src/utils.js 93.75% <0.00%> (-6.25%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3cfd279...1f1bda8. Read the comment docs.

@thymikee
Copy link
Collaborator

Um, I don't see why would we remove the feature to add it once again? Maybe we could extract some helpers from RNTL that could be used by toBeDisabled? I'm in favor of removing toBeEnabled though, as it should be inverse of disabled.

@brunohkbx
Copy link
Collaborator Author

@thymikee sorry for taking too long to answer.
I thought exactly the same. I'll try to reimplement toBeDisabled in this PR

@deriegle
Copy link

deriegle commented Dec 18, 2020

I'll try to reimplement toBeDisabled in this PR

Is there still work being done on this? We'd really like to be able to use toBeDisabled in our codebase again, but it doesn't work as expected after upgrading to v7 of RNTL.

@brunohkbx brunohkbx force-pushed the feat/support-@testing-library/react-native-7.0 branch from 6978fcb to 2835556 Compare April 6, 2021 19:39
@brunohkbx brunohkbx force-pushed the feat/support-@testing-library/react-native-7.0 branch from 2835556 to 1f1bda8 Compare April 6, 2021 19:42
@brunohkbx
Copy link
Collaborator Author

brunohkbx commented Apr 6, 2021

@thymikee @mdjastrzebski after some months I'm finally back to this PR. sorry for that.

I have some questions:

  1. Do you guys think this PR should be a breaking change? Or is it fine to release in the current 4.0?
  2. Looks like RNTL master branch is quite different than this PR #460 from @mdjastrzebski. Should we still rely only on onStartShouldSetResponder? Or we could also check pointerEvents and accessibilityState?

Copy link
Collaborator

@mdjastrzebski mdjastrzebski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added some thoughts about the tests and vague idea about the implementation, these might be relevant but might be not because I haven't worked with RNTL internals lately.


export function toBeDisabled(element) {
checkReactElement(element, toBeDisabled, this);

const isDisabled = isElementDisabled(element) || isAncestorDisabled(element);
const isDisabled = !(element.props?.onStartShouldSetResponder?.() ?? true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion we should mimic the current RNTL 7.2 logic here: isEventEnabled function & it's deps. Potentially the check might have to be recursive as fireEvent implementation in RNTL is performing recursive checks.

Comment on lines +23 to +37
test.each([
['Button', Button, { title: 'some button' }],
['TouchableOpacity', TouchableOpacity, {}],
['TouchableHighlight', TouchableHighlight, {}],
['TouchableWithoutFeedback', TouchableWithoutFeedback, {}],
['TouchableNativeFeedback', TouchableNativeFeedback, {}],
['Pressable', Pressable, {}],
])('handles disabled prop for %s', (_, Component, props) => {
const { queryByTestId } = render(
<Component disabled testID="touchable" {...props}>
<View />
</Component>,
);

expect(queryByTestId('touchable')).toBeDisabled();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test.each([
['Button', Button, { title: 'some button' }],
['TouchableOpacity', TouchableOpacity, {}],
['TouchableHighlight', TouchableHighlight, {}],
['TouchableWithoutFeedback', TouchableWithoutFeedback, {}],
['TouchableNativeFeedback', TouchableNativeFeedback, {}],
['Pressable', Pressable, {}],
])('handles disabled prop for %s', (_, Component, props) => {
const { queryByTestId } = render(
<Component disabled testID="touchable" {...props}>
<View />
</Component>,
);
expect(queryByTestId('touchable')).toBeDisabled();
test.each([
['TouchableOpacity', TouchableOpacity, {}],
['TouchableHighlight', TouchableHighlight, {}],
['TouchableWithoutFeedback', TouchableWithoutFeedback, {}],
['TouchableNativeFeedback', TouchableNativeFeedback, {}],
['Pressable', Pressable, {}],
])('handles disabled prop for %s', (_, Component, props) => {
const { getByText, getByTestId } = render(
<Component disabled testID="touchable" {...props}>
<Text>Trigger</Text>
</Component>,
);
expect(getByText('Trigger')).toBeDisabled();
expect(getByTestId('touchable')).toBeDisabled();

I think that we should assume that there is a certain duality between RNTL fireEvent and toBeDisabled, if fireEvent calls and event handler then toBeDisabled should be false, and vice-versa (obviously except the case when there is no event handler defined). So if RNTL users frequently use things like getByText to find button with given text, we should also support calling toBeDisabled() on that getByText result.

@brunohkbx @thymikee Wdyt? Does it make sense? (my RNTL expertise got little rusty lately)

NB: Button is excluded because it uses title prop and didn't want to mix it with child element. We can test it separately if needed.

expect(queryByTestId(name)).toBeDisabled();
expect(() => expect(queryByTestId(name)).not.toBeDisabled()).toThrowError();
});
test.each([
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want to test the opposite case as well (not.toBeDisabled() when there is no disabled prop).

@mdjastrzebski
Copy link
Collaborator

This PR is out of sync with main branch.

@brunohkbx would you have time and will to re-examine the PR and rebase/reimplement the relevant parts with the current codebase?

@mdjastrzebski
Copy link
Collaborator

Closing as stale. Things have move a lot since this PR has been create. @brunohkbx We might restart this effort in a fresh PR, based on current codebase, if you still see value in it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support @testing-library/react-native 7.0
4 participants