From 9871677b8749e817f38b21bf7839f2e71279e171 Mon Sep 17 00:00:00 2001 From: MattAgn Date: Sat, 28 Nov 2020 16:31:49 +0100 Subject: [PATCH 1/9] refactor: add makeQueries functions --- src/helpers/makeQueries.js | 78 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/helpers/makeQueries.js diff --git a/src/helpers/makeQueries.js b/src/helpers/makeQueries.js new file mode 100644 index 000000000..11b94cbd0 --- /dev/null +++ b/src/helpers/makeQueries.js @@ -0,0 +1,78 @@ +// @flow +import waitFor from '../waitFor'; +import type { WaitForOptions } from '../waitFor'; +import { ErrorWithStack } from './errors'; + +type AllQuery = ( + instance: ReactTestInstance +) => (args: any) => Array; + +export const makeGetAllQuery = ( + allQuery: AllQuery, + instance: ReactTestInstance, + getMissingElementError: (args: any) => string +) => + function getAllFn(...args: Array) { + const results = allQuery(instance)(...args); + if (!results.length) { + throw new ErrorWithStack(getMissingElementError(...args), getAllFn); + } + + return results; + }; + +export const makeSingleQuery = ( + allQuery: AllQuery, + instance: ReactTestInstance, + getMissingElementError: (args: any, nbResults: number) => string +) => + function singleQueryFn(...args: Array) { + const results = allQuery(instance)(...args); + + if (results.length > 1) { + throw new ErrorWithStack( + getMissingElementError(...args, results.length), + singleQueryFn + ); + } + + if (results.length === 0) { + return null; + } + + return results[0]; + }; + +export const makeGetQuery = ( + allQuery: AllQuery, + instance: ReactTestInstance, + getMissingElementError: (args: any, nbResults: number) => string +) => + function getFn(...args: Array) { + const results = allQuery(instance)(...args); + + if (results.length !== 1) { + throw new ErrorWithStack( + getMissingElementError(...args, results.length), + getFn + ); + } + + return results[0]; + }; + +export const makeFindAllQuery = ( + getAllQuery: AllQuery, + instance: ReactTestInstance +) => + function findAllFn(args: any, waitForOptions: WaitForOptions = {}) { + return waitFor(() => getAllQuery(instance)(args), waitForOptions); + }; + +export const makeFindQuery = ( + getQuery: (instance: ReactTestInstance) => (args: any) => ReactTestInstance, + instance: ReactTestInstance +) => + function findFn(args: any, waitForOptions: WaitForOptions = {}) { + return waitFor(() => getQuery(instance)(args), waitForOptions); + }; From 791c5d5a3bd0522bf9fcc50cb1d6b60878b452a0 Mon Sep 17 00:00:00 2001 From: MattAgn Date: Sat, 28 Nov 2020 16:32:27 +0100 Subject: [PATCH 2/9] refactor: use new query builders on byTestId --- src/__tests__/byTestId.test.js | 171 ++++++++++++++++++++++++++++++++ src/__tests__/findByApi.test.js | 6 -- src/__tests__/render.test.js | 30 ------ src/helpers/byTestId.js | 58 +++++++++++ src/helpers/findByAPI.js | 23 +---- src/helpers/getByAPI.js | 45 +-------- src/helpers/queryByAPI.js | 26 +---- 7 files changed, 232 insertions(+), 127 deletions(-) create mode 100644 src/__tests__/byTestId.test.js create mode 100644 src/helpers/byTestId.js diff --git a/src/__tests__/byTestId.test.js b/src/__tests__/byTestId.test.js new file mode 100644 index 000000000..7c95a97cd --- /dev/null +++ b/src/__tests__/byTestId.test.js @@ -0,0 +1,171 @@ +// @flow +import React from 'react'; +import { View, Text, TextInput, TouchableOpacity, Button } from 'react-native'; +import { render } from '..'; + +const PLACEHOLDER_FRESHNESS = 'Add custom freshness'; +const PLACEHOLDER_CHEF = 'Who inspected freshness?'; +const INPUT_FRESHNESS = 'Custom Freshie'; +const INPUT_CHEF = 'I inspected freshie'; + +class MyButton extends React.Component { + render() { + return ( + + {this.props.children} + + ); + } +} + +class Banana extends React.Component { + state = { + fresh: false, + }; + + componentDidUpdate() { + if (this.props.onUpdate) { + this.props.onUpdate(); + } + } + + componentWillUnmount() { + if (this.props.onUnmount) { + this.props.onUnmount(); + } + } + + changeFresh = () => { + this.setState((state) => ({ + fresh: !state.fresh, + })); + }; + + render() { + const test = 0; + return ( + + Is the banana fresh? + + {this.state.fresh ? 'fresh' : 'not fresh'} + + + + + Change freshness! + + First Text + Second Text + {test} + + ); + } +} + +const MyComponent = () => { + return My Component; +}; + +test('getByTestId returns only native elements', () => { + const { getByTestId, getAllByTestId } = render( + + Text + + +