[@types/cavy] Update generateTestHook function types (#37204)

* Update generateTestHook function types

* Empty commit for Travis
This commit is contained in:
AbigailMcP 2019-07-30 23:26:15 +01:00 committed by Jesse Trinity
parent 82f8b9305e
commit e0a244294d
3 changed files with 20 additions and 7 deletions

View File

@ -8,11 +8,22 @@ type Props = WithTestHook<{
}>;
class SampleComponent extends React.Component<Props> {
textInputRef: React.ReactNode | null;
constructor(props: Props) {
super(props);
this.textInputRef = null;
}
setTextInputRef = (el?: React.ReactNode) => {
this.textInputRef = el;
}
render() {
return (
<View ref={this.props.generateTestHook('View.Sample')}>
<Text>{this.props.foo}</Text>
<TextInput ref={this.props.generateTestHook("Input.Sample")} />
<TextInput ref={this.props.generateTestHook("Input.Sample", this.setTextInputRef)} />
</View>
);
}
@ -22,9 +33,8 @@ const HookedSampleComponent = hook(SampleComponent); // $ExpectType ComponentCla
const SampleFunctionComponent: React.FunctionComponent = () => {
const generateTestHook = useCavy();
return (
<View ref={generateTestHook(`FunctionView.Sample`)}></View>
);
const ref = React.createRef();
return <View ref={generateTestHook(`FunctionView.Sample`, ref)}></View>;
};
function sampleSpec(spec: TestScope) {

View File

@ -1,4 +1,4 @@
// Type definitions for cavy 2.0
// Type definitions for cavy 2.2
// Project: https://github.com/pixielabs/cavy
// Definitions by: Tyler Hoffman <https://github.com/tyler-hoffman>
// Abigail McPhillips <https://github.com/AbigailMcP>
@ -11,7 +11,10 @@ export {};
type RefCallback = (element: React.ReactNode | null) => void;
export type TestHookGenerator = (label: string, callback?: RefCallback) => RefCallback;
type TestHookGeneratorWithRefCallback = (label: string, ref?: RefCallback) => RefCallback;
type TestHookGeneratorWithRefObject = (label: string, ref?: React.RefObject<any>) => React.RefObject<any>;
export type TestHookGenerator = TestHookGeneratorWithRefCallback & TestHookGeneratorWithRefObject;
export type WithTestHook<T extends {}> = T & { generateTestHook: TestHookGenerator };

View File

@ -21,4 +21,4 @@
"index.d.ts",
"cavy-tests.tsx"
]
}
}