react-native-testing-library icon indicating copy to clipboard operation
react-native-testing-library copied to clipboard

[TypeScript] Typings for get/queryByType and get/queryAllByType breaks with components that has required props

Open kdbcinco opened this issue 8 years ago • 11 comments

I'm using React Native Elements and testing my components using react-native-testing-library. However, when I try to use the getByType/getAllByType/queryByType/queryAllByType APIs, TypeScript complains about props that are marked by the component as required. One example of a component with a required attribute is the Icon component from React Native Elements.

Consider this example:

// Icon has a required prop `name`
import { Icon, Input } from 'react-native-elements';

// Current signature used by getByType/getAllByType/queryByType/queryAllByType
// getByType: (type: React.ComponentType) => ReactTestInstance;
// getAllByType: (type: React.ComponentType) => Array<ReactTestInstance>
// queryByType: (type: React.ComponentType) => ReactTestInstance | null;
// queryAllByType: (type: React.ComponentType) => Array<ReactTestInstance> | [];

const inst = render(<Input leftIcon={{ name: 'visibility-off' }} />);
// TypeScript fails!
const icon = inst.getByType(Icon);

Error shown

[ts]
Argument of type 'typeof Icon' is not assignable to parameter of type 'ComponentType<{}>'.
  Type 'typeof Icon' is not assignable to type 'ComponentClass<{}, any>'.
    Types of parameters 'props' and 'props' are incompatible.
      Type '{}' is not assignable to type 'Readonly<IconProps>'.
        Property 'name' is missing in type '{}'. [2345]

kdbcinco avatar Nov 23 '18 08:11 kdbcinco

Do you know any better type for this other than React.ComponentType? cc @Esemesek

thymikee avatar Nov 23 '18 08:11 thymikee

@thymikee I tried creating a function with (type: React.ComponentType<any>) and it's working. I'm not sure though if it'll affect anything internally with how react-native-testing-library works. I think the issue with just using plain React.ComponentType is that it sets the Props template parameter with an empty object by default.

// from @types/react
type ComponentType<P = {}> = ComponentClass<P> | FunctionComponent<P>;

kdbcinco avatar Nov 23 '18 09:11 kdbcinco

But isn't it right? By default props is an empty object. Sounds like a bug in react-native-elements typings to me.

thymikee avatar Nov 23 '18 10:11 thymikee

Can you give more detailed example with more example code?

henrikra avatar Nov 23 '18 13:11 henrikra

The Icon component from RNE has this type definition:

class Icon extends React.Component<IconProps, any> {}

With IconProps defined as:

interface IconProps {
    // required
    name: string;
    // other props defined as optional (prop?: type;)
}

Now when using getByType(type: React.ComponentType) (which really means this getByType(type: React.ComponentType<{}>)), IconProps will not be compatible anymore because {} does not have the required name prop.

For now, I'm using the *ByName APIs as a workaround, though they're now deprecated.

kdbcinco avatar Nov 23 '18 13:11 kdbcinco

So, will changing React.ComponentType to React.ComponentClass<any, any> | React.StatelessComponent<any> do the trick? Maybe there's more permissive type provided by TS itself?

thymikee avatar Nov 23 '18 13:11 thymikee

I'm thinking of function getByType<P>(type: React.ComponentType<P>). That way, the function will follow the Prop type defined for the given type.

kdbcinco avatar Nov 23 '18 13:11 kdbcinco

Mind sending a PR plus adding a typing test in typings/__tests__?

thymikee avatar Nov 23 '18 13:11 thymikee

Sure, tomorrow as I'm already heading out of the office. 😄

kdbcinco avatar Nov 23 '18 13:11 kdbcinco

No rush, enjoy your weekend!

thymikee avatar Nov 23 '18 13:11 thymikee

@thymikee Done creating a PR. Might've messed it up a bit. 😅

kdbcinco avatar Nov 26 '18 06:11 kdbcinco