react-native-confirmation-code-input icon indicating copy to clipboard operation
react-native-confirmation-code-input copied to clipboard

How to test with detox?

Open oliviayizhang opened this issue 5 years ago • 6 comments

Is there a way to do detox test with this library? I have no luck finding the element/type text because the code input component doesn't expose individual input and doesn't support testID too.

oliviayizhang avatar Aug 19 '19 15:08 oliviayizhang

This might not be your blocker, but for me I just needed to input the code and get to the next screen to keep my test flow going. I just wound up targeting the parent view and sending the keystrokes there. Worked to get me past the screen but it doesn't give you any deep control with the CodeInput itself. Just a stop-gap.

//  component
import React from 'react'
import { View } from 'react-native'
const Screen = () => (
  <View testID={`parent-view-id`}>
    <CodeInput />
  </View>
)
//  spec
describe('component with code input', () => {
  it('should type in code', () => {
    await element(by.id(`parent-view-id`)).typeText('123456')
  })
})

dillonchr avatar Sep 11 '19 22:09 dillonchr

@dillonchr this approach doesn't work for me. which version of library and detox are you trying?

maxkomarychev avatar Oct 08 '19 10:10 maxkomarychev

@dillonchr this approach doesn't work for me. which version of library and detox are you trying?

@maxkomarychev we're using detox@^14.3.4 and [email protected] oh and it turns out we're using a branch of [email protected]. https://github.com/marketforces/react-native-confirmation-code-input so maybe in the changes we made to width being able to copy and paste helped us out along the way. Not sure on what would cause the difference.

dillonchr avatar Oct 08 '19 13:10 dillonchr

@oliviayizhang You can use a fork without this problem https://github.com/retyui/react-native-confirmation-code-field

retyui avatar Feb 19 '20 08:02 retyui

Hey @dillonchr @oliviayizhang Firstly, set testID={CODE_INPUT} in the code input component (the matcher will return n TextFields, being n your code length. Then use forEach to write one code character or number in each TextField individually per iteration.

const code = '123456';
const codeSplit = code.split('');
codeSplit.forEach(async (number, index) => {
  await element(by.id(CODE_INPUT)).atIndex(index).typeText(number);
 });
});

sebastianpaz avatar Apr 16 '20 17:04 sebastianpaz

pass value one to one

 await element(by.id('CodeInput')).atIndex(0).typeText("1");
 await element(by.id('CodeInput')).atIndex(1).typeText("1");
 await element(by.id('CodeInput')).atIndex(2).typeText("1");
 await element(by.id('CodeInput')).atIndex(3).typeText("1");

Moustafa-mahmaed avatar Jul 12 '21 11:07 Moustafa-mahmaed