jest-native icon indicating copy to clipboard operation
jest-native copied to clipboard

feat: toBeFocused

Open chris-chapin opened this issue 3 years ago • 3 comments

Describe the feature you'd like:

A new matcher for testing if an element is focused.

Similar to what Detox has: https://wix.github.io/Detox/docs/api/expect#tobefocused

chris-chapin avatar Jan 31 '22 06:01 chris-chapin

this would be really useful!

Bigrev avatar Mar 02 '22 16:03 Bigrev

have no idea why react-testing-library has a focus feature but not react-native-testing-library

HudiDev avatar Apr 14 '22 22:04 HudiDev

Interesting idea. I'm not sure if feasible, as RNTL does it's renders using React Test Renderer and not React Native renderer, and hence it's runtime environment is quite limited. In contract RTL uses real React DOM renderer working with Js DOM to have full-blown browser environment.

@chris-chapin would you be willing to build a PR for this?

mdjastrzebski avatar May 01 '22 21:05 mdjastrzebski

Closing as stale.

@chris-chapin if you find any suitable solution to the native/JS issue I've described above feel free to submit a PR for this.

mdjastrzebski avatar Sep 30 '22 10:09 mdjastrzebski

Hello,

In one of our test, the onFocus is setting the focus in a state and then display a list of clickable choice. I am not able to test it because this feature is not supported.

Can we reopen this please?

kopax-polyconseil avatar Oct 13 '22 14:10 kopax-polyconseil

@kopax-polyconseil afaik supporting toBeFocused() in Jest Native/React Native Testing Library/React Test Renderer is not possible due to lack of runtime environment that would manage the focus. Original (web) React Testing Library has jsdom to simulate browser env, while Detox runs using real React Native environment on device/emulator.

In our case someone would have to create such non-standard environment, moreover it would have to be maintained to match changes in React Native.

mdjastrzebski avatar Oct 16 '22 08:10 mdjastrzebski

not ideal but its something at least, I ended up wrapping my input with a view with a conditional test_id to search for in my tests

<View testID={`${testID}_${isFocus ? 'focused' : 'not_focused' }>...

export-mike avatar Mar 20 '23 01:03 export-mike

not ideal but its something at least, I ended up wrapping my input with a view with a conditional test_id to search for in my tests

<View testID={`${testID}_${isFocus ? 'focused' : 'not_focused' }>...

This makes me so sad that we have to do this.

EGurney avatar Aug 14 '23 17:08 EGurney