inital commit

This commit is contained in:
Michał Pierzchała
2018-10-01 18:15:56 +02:00
parent 191057ede1
commit ac77a55068
9 changed files with 4690 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
flow-typed/
+6
View File
@@ -0,0 +1,6 @@
{
"extends": "@callstack",
"rules": {
"flowtype/no-weak-types": 0
}
}
+2
View File
@@ -0,0 +1,2 @@
[libs]
flow-typed
+2
View File
@@ -0,0 +1,2 @@
node_modules
*.log
+2
View File
@@ -0,0 +1,2 @@
# react-native-testing-library
+75
View File
@@ -0,0 +1,75 @@
// flow-typed signature: 9b9f4128694a7f68659d945b81fb78ff
// flow-typed version: 46dfe79a54/react-test-renderer_v16.x.x/flow_>=v0.47.x
// Type definitions for react-test-renderer 16.x.x
// Ported from: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-test-renderer
type ReactComponentInstance = React$Component<any>;
type ReactTestRendererJSON = {
type: string,
props: { [propName: string]: any },
children: null | ReactTestRendererJSON[]
};
type ReactTestRendererTree = ReactTestRendererJSON & {
nodeType: "component" | "host",
instance: ?ReactComponentInstance,
rendered: null | ReactTestRendererTree
};
type ReactTestInstance = {
instance: ?ReactComponentInstance,
type: string | Function,
props: { [propName: string]: any },
parent: null | ReactTestInstance,
children: Array<ReactTestInstance | string>,
find(predicate: (node: ReactTestInstance) => boolean): ReactTestInstance,
findByType(type: React$ElementType): ReactTestInstance,
findByProps(props: { [propName: string]: any }): ReactTestInstance,
findAll(
predicate: (node: ReactTestInstance) => boolean,
options?: { deep: boolean }
): ReactTestInstance[],
findAllByType(
type: React$ElementType,
options?: { deep: boolean }
): ReactTestInstance[],
findAllByProps(
props: { [propName: string]: any },
options?: { deep: boolean }
): ReactTestInstance[]
};
type TestRendererOptions = {
createNodeMock(element: React$Element<any>): any
};
declare module "react-test-renderer" {
declare export type ReactTestRenderer = {
toJSON(): null | ReactTestRendererJSON,
toTree(): null | ReactTestRendererTree,
unmount(nextElement?: React$Element<any>): void,
update(nextElement: React$Element<any>): void,
getInstance(): ?ReactComponentInstance,
root: ReactTestInstance
};
declare function create(
nextElement: React$Element<any>,
options?: TestRendererOptions
): ReactTestRenderer;
}
declare module "react-test-renderer/shallow" {
declare export default class ShallowRenderer {
static createRenderer(): ShallowRenderer;
getMountedInstance(): ReactTestInstance;
getRenderOutput<E: React$Element<any>>(): E;
getRenderOutput(): React$Element<any>;
render(element: React$Element<any>, context?: any): void;
unmount(): void;
}
}
+28
View File
@@ -0,0 +1,28 @@
{
"name": "react-native-testing-library",
"version": "0.0.0",
"description": "Simple React Native testing utilities helping you write better tests with less effort",
"main": "src/index.js",
"repository": "git@github.com:callstack/react-native-testing-library.git",
"author": "Michał Pierzchała <thymikee@gmail.com>",
"license": "MIT",
"private": false,
"devDependencies": {
"@callstack/eslint-config": "^2.0.0",
"babel-jest": "^23.6.0",
"eslint": "^5.6.1",
"flow-bin": "^0.81.0",
"jest": "^23.6.0",
"pretty-format": "^23.6.0",
"react": "^16.5.2",
"react-test-renderer": "^16.5.2"
},
"peerDependencies": {
"pretty-format": ">=20.0.0",
"react": ">=16.0.0",
"react-test-renderer": ">= 16.0.0"
},
"dependencies": {
"react-is": "^16.5.2"
}
}
+82
View File
@@ -0,0 +1,82 @@
// @flow
import * as React from 'react';
import { isValidElementType } from 'react-is';
import TestRenderer from 'react-test-renderer'; // eslint-disable-line import/no-extraneous-dependencies
import ShallowRenderer from 'react-test-renderer/shallow'; // eslint-disable-line import/no-extraneous-dependencies
import prettyFormat, { plugins } from 'pretty-format'; // eslint-disable-line import/no-extraneous-dependencies
const getNodeByName = (node, name) =>
node.type.name === name ||
node.type.displayName === name ||
node.type === name;
const getNodeByText = (node, text) =>
(getNodeByName(node, 'Text') || getNodeByName(node, 'TextInput')) &&
(typeof text === 'string'
? text === node.props.children
: text.test(node.props.children));
/**
* Wait for microtask queue to flush
*/
export const flushMicrotasksQueue = (): Promise<any> =>
new Promise(resolve => setImmediate(resolve));
/**
* Renders test component deeply using react-test-renderer and exposes helpers
* to assert on the output.
*/
export const render = (component: React.Element<*>) => {
const renderer = TestRenderer.create(component);
const instance = renderer.root;
return {
instance,
renderer,
getByTestId: (testID: string) => instance.findByProps({ testID }),
getByName: (name: string) =>
instance.find(node => getNodeByName(node, name)),
getAllByName: (name: string) =>
instance.findAll(node => getNodeByName(node, name)),
getByText: (text: string | RegExp) =>
instance.find(node => getNodeByText(node, text)),
getAllByText: (text: string | RegExp) =>
instance.findAll(node => getNodeByText(node, text)),
getByProps: (props: { [propName: string]: any }) =>
instance.findByProps(props),
getAllByProps: (props: { [propName: string]: any }) =>
instance.findAllByProps(props),
};
};
/**
* Renders test component shallowly using react-test-renderer/shallow
*/
export const shallow = (instance: ReactTestInstance | React.Element<*>) => {
const renderer = new ShallowRenderer();
if (isValidElementType(instance)) {
// $FlowFixMe - instance is React.Element<*> in this branch
renderer.render(instance);
} else {
renderer.render(React.createElement(instance.type, instance.props));
}
const result = renderer.getRenderOutput();
return result;
};
/**
* Log pretty-printed shallow test component instance
*/
export const debug = (
instance: ReactTestInstance | React.Element<*>,
message?: any
) => {
// eslint-disable-next-line no-console
console.log(
prettyFormat(shallow(instance), {
plugins: [plugins.ReactTestComponent, plugins.ReactElement],
}),
message
);
};
+4492
View File
File diff suppressed because it is too large Load Diff