Add cavy types

This commit is contained in:
Tyler Hoffman
2019-02-23 18:08:33 -05:00
parent 8816e2e88c
commit 46dc879fb3
4 changed files with 105 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
import * as React from 'react';
import { TextInput, View, Text } from 'react-native';
import { hook, TestScope, WithTestHook, Tester } from 'cavy';
type Props = WithTestHook<{
foo: string;
}>;
class SampleComponent extends React.Component<Props> {
render() {
return (
<View ref={this.props.generateTestHook('View.Sample')}>
<Text>{this.props.foo}</Text>
<TextInput ref={this.props.generateTestHook("Input.Sample")} />
</View>
);
}
}
const HookedSampleComponent = hook(SampleComponent); // $ExpectType ComponentClass<{ foo: string; }, any>
function sampleSpec(spec: TestScope) {
spec.describe('it has a name and callback', () => {
spec.it('it has a name and callback', async () => {
spec.component.reRender(); // $ExpectType void
spec.component.clearAsync(); // $ExpectType Promise<void>
spec.findComponent('View.Sample'); // $ExpectType Promise<Component<{}, {}, any>>
spec.press('View.Sample'); // $ExpectType Promise<void>
spec.fillIn('Input.Sample', "hello world"); // $ExpectType Promise<void>
spec.pause(1000); // $ExpectType Promise<void>
spec.exists('View.Sample'); // $ExpectType Promise<true>
spec.notExists('View.MissingSample'); // $ExpectType Promise<true>
});
});
}
<Tester specs={[sampleSpec]} waitTime={42}>
<HookedSampleComponent foo="test" />
</Tester>;
+40
View File
@@ -0,0 +1,40 @@
// Type definitions for cavy 0.6
// Project: https://github.com/pixielabs/cavy
// Definitions by: Tyler Hoffman <https://github.com/tyler-hoffman>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0
import * as React from 'react';
export {};
type RefCallback = (element: React.ReactNode | null) => void;
export type TestHookGenerator = (label: string, callback?: RefCallback) => RefCallback;
export type WithTestHook<T extends {}> = T & { generateTestHook: TestHookGenerator };
export function hook<T extends {}>(component: React.ComponentClass<WithTestHook<T>>): React.ComponentClass<T>;
export interface TesterProps {
specs: Array<(spec: TestScope) => void>;
waitTime: number;
sendReport?: boolean;
}
export class Tester extends React.Component<TesterProps> {
reRender(): void;
clearAsync(): Promise<void>;
}
export class TestScope {
component: Tester;
findComponent(identifier: string): Promise<React.Component>;
describe(label: string, fn: () => void): void;
it(label: string, fn: () => void): void;
press(identifier: string): Promise<void>;
fillIn(identifier: string, str: string): Promise<void>;
pause(time: number): Promise<void>;
exists(identifier: string): Promise<true>;
notExists(identifier: string): Promise<true>;
}
+24
View File
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"jsx": "react",
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"cavy-tests.tsx"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }