@testing-library/react-native 7.1.0 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/README.md +24 -4
  2. package/build/act.js.flow +2 -1
  3. package/build/fireEvent.js +24 -17
  4. package/build/fireEvent.js.flow +36 -28
  5. package/build/flushMicroTasks.js +3 -1
  6. package/build/flushMicroTasks.js.flow +1 -0
  7. package/build/helpers/a11yAPI.js +9 -10
  8. package/build/helpers/a11yAPI.js.flow +10 -12
  9. package/build/helpers/byDisplayValue.js +48 -0
  10. package/build/helpers/byDisplayValue.js.flow +56 -0
  11. package/build/helpers/byPlaceholderText.js +47 -0
  12. package/build/helpers/byPlaceholderText.js.flow +54 -0
  13. package/build/helpers/byTestId.js +36 -0
  14. package/build/helpers/byTestId.js.flow +46 -0
  15. package/build/helpers/byText.js +94 -0
  16. package/build/helpers/byText.js.flow +88 -0
  17. package/build/helpers/debugDeep.js +1 -1
  18. package/build/helpers/debugDeep.js.flow +1 -1
  19. package/build/helpers/debugShallow.js.flow +1 -1
  20. package/build/helpers/errors.js +6 -0
  21. package/build/helpers/errors.js.flow +10 -3
  22. package/build/helpers/filterNodeByType.js +10 -0
  23. package/build/helpers/filterNodeByType.js.flow +1 -0
  24. package/build/helpers/findByAPI.js +14 -46
  25. package/build/helpers/findByAPI.js.flow +43 -58
  26. package/build/helpers/format.js.flow +1 -1
  27. package/build/helpers/getByAPI.js +16 -184
  28. package/build/helpers/getByAPI.js.flow +52 -204
  29. package/build/helpers/{makeQuery.js → makeA11yQuery.js} +2 -2
  30. package/build/helpers/{makeQuery.js.flow → makeA11yQuery.js.flow} +5 -3
  31. package/build/helpers/makeQueries.js +78 -0
  32. package/build/helpers/makeQueries.js.flow +114 -0
  33. package/build/helpers/queryByAPI.js +16 -88
  34. package/build/helpers/queryByAPI.js.flow +57 -90
  35. package/build/helpers/timers.js +77 -0
  36. package/build/helpers/timers.js.flow +88 -0
  37. package/build/index.js +1 -0
  38. package/build/render.js +11 -4
  39. package/build/render.js.flow +32 -8
  40. package/build/shallow.js.flow +1 -1
  41. package/build/types.flow.js.flow +4 -0
  42. package/build/waitFor.js +148 -19
  43. package/build/waitFor.js.flow +159 -18
  44. package/build/waitForElementToBeRemoved.js +3 -1
  45. package/build/waitForElementToBeRemoved.js.flow +3 -2
  46. package/build/within.js +2 -4
  47. package/build/within.js.flow +7 -5
  48. package/jest-preset/index.js +10 -0
  49. package/jest-preset/restore-promise.js +1 -0
  50. package/jest-preset/save-promise.js +1 -0
  51. package/package.json +20 -15
  52. package/typings/index.d.ts +13 -4
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.getByAPI = exports.UNSAFE_getAllByProps = exports.UNSAFE_getAllByType = exports.UNSAFE_getByProps = exports.UNSAFE_getByType = exports.getAllByTestId = exports.getAllByDisplayValue = exports.getAllByPlaceholderText = exports.getAllByText = exports.getByTestId = exports.getByDisplayValue = exports.getByPlaceholderText = exports.getByText = void 0;
6
+ exports.getByAPI = exports.UNSAFE_getAllByProps = exports.UNSAFE_getAllByType = exports.UNSAFE_getByProps = exports.UNSAFE_getByType = void 0;
7
7
 
8
8
  var React = _interopRequireWildcard(require("react"));
9
9
 
@@ -11,187 +11,19 @@ var _prettyFormat = _interopRequireDefault(require("pretty-format"));
11
11
 
12
12
  var _errors = require("./errors");
13
13
 
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
- function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
17
-
18
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
19
-
20
- const filterNodeByType = (node, type) => node.type === type;
14
+ var _byTestId = require("./byTestId");
21
15
 
22
- const getNodeByText = (node, text) => {
23
- try {
24
- const {
25
- Text
26
- } = require('react-native');
27
-
28
- const isTextComponent = filterNodeByType(node, Text);
29
-
30
- if (isTextComponent) {
31
- const textChildren = getChildrenAsText(node.props.children, Text);
16
+ var _byText = require("./byText");
32
17
 
33
- if (textChildren) {
34
- const textToTest = textChildren.join('');
35
- return typeof text === 'string' ? text === textToTest : text.test(textToTest);
36
- }
37
- }
18
+ var _byPlaceholderText = require("./byPlaceholderText");
38
19
 
39
- return false;
40
- } catch (error) {
41
- throw (0, _errors.createLibraryNotSupportedError)(error);
42
- }
43
- };
20
+ var _byDisplayValue = require("./byDisplayValue");
44
21
 
45
- const getChildrenAsText = (children, TextComponent, textContent = []) => {
46
- React.Children.forEach(children, child => {
47
- var _child$props;
48
-
49
- if (typeof child === 'string') {
50
- textContent.push(child);
51
- return;
52
- }
53
-
54
- if (typeof child === 'number') {
55
- textContent.push(child.toString());
56
- return;
57
- }
58
-
59
- if (child === null || child === void 0 ? void 0 : (_child$props = child.props) === null || _child$props === void 0 ? void 0 : _child$props.children) {
60
- // Bail on traversing text children down the tree if current node (child)
61
- // has no text. In such situations, react-test-renderer will traverse down
62
- // this tree in a separate call and run this query again. As a result, the
63
- // query will match the deepest text node that matches requested text.
64
- if (filterNodeByType(child, TextComponent) && textContent.length === 0) {
65
- return;
66
- }
67
-
68
- getChildrenAsText(child.props.children, TextComponent, textContent);
69
- }
70
- });
71
- return textContent;
72
- };
73
-
74
- const getTextInputNodeByPlaceholderText = (node, placeholder) => {
75
- try {
76
- const {
77
- TextInput
78
- } = require('react-native');
79
-
80
- return filterNodeByType(node, TextInput) && (typeof placeholder === 'string' ? placeholder === node.props.placeholder : placeholder.test(node.props.placeholder));
81
- } catch (error) {
82
- throw (0, _errors.createLibraryNotSupportedError)(error);
83
- }
84
- };
85
-
86
- const getTextInputNodeByDisplayValue = (node, value) => {
87
- try {
88
- const {
89
- TextInput
90
- } = require('react-native');
91
-
92
- return filterNodeByType(node, TextInput) && (typeof value === 'string' ? value === node.props.value : value.test(node.props.value));
93
- } catch (error) {
94
- throw (0, _errors.createLibraryNotSupportedError)(error);
95
- }
96
- };
97
-
98
- const getNodeByTestId = (node, testID) => {
99
- return typeof testID === 'string' ? testID === node.props.testID : testID.test(node.props.testID);
100
- };
101
-
102
- const getByText = instance => function getByTextFn(text) {
103
- try {
104
- return instance.find(node => getNodeByText(node, text));
105
- } catch (error) {
106
- throw new _errors.ErrorWithStack((0, _errors.prepareErrorMessage)(error, 'text', text), getByTextFn);
107
- }
108
- };
109
-
110
- exports.getByText = getByText;
111
-
112
- const getByPlaceholderText = instance => function getByPlaceholderTextFn(placeholder) {
113
- try {
114
- return instance.find(node => getTextInputNodeByPlaceholderText(node, placeholder));
115
- } catch (error) {
116
- throw new _errors.ErrorWithStack((0, _errors.prepareErrorMessage)(error, 'placeholder', placeholder), getByPlaceholderTextFn);
117
- }
118
- };
119
-
120
- exports.getByPlaceholderText = getByPlaceholderText;
121
-
122
- const getByDisplayValue = instance => function getByDisplayValueFn(displayValue) {
123
- try {
124
- return instance.find(node => getTextInputNodeByDisplayValue(node, displayValue));
125
- } catch (error) {
126
- throw new _errors.ErrorWithStack((0, _errors.prepareErrorMessage)(error, 'display value', displayValue), getByDisplayValueFn);
127
- }
128
- };
129
-
130
- exports.getByDisplayValue = getByDisplayValue;
131
-
132
- const getByTestId = instance => function getByTestIdFn(testID) {
133
- try {
134
- const results = getAllByTestId(instance)(testID);
135
-
136
- if (results.length === 1) {
137
- return results[0];
138
- } else {
139
- throw new _errors.ErrorWithStack(` Expected 1 but found ${results.length} instances with testID: ${String(testID)}`, getByTestIdFn);
140
- }
141
- } catch (error) {
142
- throw new _errors.ErrorWithStack((0, _errors.prepareErrorMessage)(error), getByTestIdFn);
143
- }
144
- };
145
-
146
- exports.getByTestId = getByTestId;
147
-
148
- const getAllByText = instance => function getAllByTextFn(text) {
149
- const results = instance.findAll(node => getNodeByText(node, text));
150
-
151
- if (results.length === 0) {
152
- throw new _errors.ErrorWithStack(`No instances found with text: ${String(text)}`, getAllByTextFn);
153
- }
154
-
155
- return results;
156
- };
157
-
158
- exports.getAllByText = getAllByText;
159
-
160
- const getAllByPlaceholderText = instance => function getAllByPlaceholderTextFn(placeholder) {
161
- const results = instance.findAll(node => getTextInputNodeByPlaceholderText(node, placeholder));
162
-
163
- if (results.length === 0) {
164
- throw new _errors.ErrorWithStack(`No instances found with placeholder: ${String(placeholder)}`, getAllByPlaceholderTextFn);
165
- }
166
-
167
- return results;
168
- };
169
-
170
- exports.getAllByPlaceholderText = getAllByPlaceholderText;
171
-
172
- const getAllByDisplayValue = instance => function getAllByDisplayValueFn(value) {
173
- const results = instance.findAll(node => getTextInputNodeByDisplayValue(node, value));
174
-
175
- if (results.length === 0) {
176
- throw new _errors.ErrorWithStack(`No instances found with display value: ${String(value)}`, getAllByDisplayValueFn);
177
- }
178
-
179
- return results;
180
- };
181
-
182
- exports.getAllByDisplayValue = getAllByDisplayValue;
183
-
184
- const getAllByTestId = instance => function getAllByTestIdFn(testID) {
185
- const results = instance.findAll(node => getNodeByTestId(node, testID)).filter(element => typeof element.type === 'string');
186
-
187
- if (results.length === 0) {
188
- throw new _errors.ErrorWithStack(`No instances found with testID: ${String(testID)}`, getAllByTestIdFn);
189
- }
22
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
190
23
 
191
- return results;
192
- };
24
+ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
193
25
 
194
- exports.getAllByTestId = getAllByTestId;
26
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
195
27
 
196
28
  const UNSAFE_getByType = instance => function getByTypeFn(type) {
197
29
  try {
@@ -238,14 +70,14 @@ const UNSAFE_getAllByProps = instance => function getAllByPropsFn(props) {
238
70
  exports.UNSAFE_getAllByProps = UNSAFE_getAllByProps;
239
71
 
240
72
  const getByAPI = instance => ({
241
- getByText: getByText(instance),
242
- getByPlaceholderText: getByPlaceholderText(instance),
243
- getByDisplayValue: getByDisplayValue(instance),
244
- getByTestId: getByTestId(instance),
245
- getAllByText: getAllByText(instance),
246
- getAllByPlaceholderText: getAllByPlaceholderText(instance),
247
- getAllByDisplayValue: getAllByDisplayValue(instance),
248
- getAllByTestId: getAllByTestId(instance),
73
+ getByText: (0, _byText.getByText)(instance),
74
+ getByPlaceholderText: (0, _byPlaceholderText.getByPlaceholderText)(instance),
75
+ getByDisplayValue: (0, _byDisplayValue.getByDisplayValue)(instance),
76
+ getByTestId: (0, _byTestId.getByTestId)(instance),
77
+ getAllByText: (0, _byText.getAllByText)(instance),
78
+ getAllByPlaceholderText: (0, _byPlaceholderText.getAllByPlaceholderText)(instance),
79
+ getAllByDisplayValue: (0, _byDisplayValue.getAllByDisplayValue)(instance),
80
+ getAllByTestId: (0, _byTestId.getAllByTestId)(instance),
249
81
  // Unsafe
250
82
  UNSAFE_getByType: UNSAFE_getByType(instance),
251
83
  UNSAFE_getAllByType: UNSAFE_getAllByType(instance),
@@ -3,210 +3,52 @@ import * as React from 'react';
3
3
  import prettyFormat from 'pretty-format';
4
4
  import {
5
5
  ErrorWithStack,
6
- createLibraryNotSupportedError,
7
6
  prepareErrorMessage,
8
7
  throwRemovedFunctionError,
9
8
  throwRenamedFunctionError,
10
9
  } from './errors';
11
-
12
- const filterNodeByType = (node, type) => node.type === type;
13
-
14
- const getNodeByText = (node, text) => {
15
- try {
16
- const { Text } = require('react-native');
17
- const isTextComponent = filterNodeByType(node, Text);
18
- if (isTextComponent) {
19
- const textChildren = getChildrenAsText(node.props.children, Text);
20
- if (textChildren) {
21
- const textToTest = textChildren.join('');
22
- return typeof text === 'string'
23
- ? text === textToTest
24
- : text.test(textToTest);
25
- }
26
- }
27
- return false;
28
- } catch (error) {
29
- throw createLibraryNotSupportedError(error);
30
- }
31
- };
32
-
33
- const getChildrenAsText = (children, TextComponent, textContent = []) => {
34
- React.Children.forEach(children, (child) => {
35
- if (typeof child === 'string') {
36
- textContent.push(child);
37
- return;
38
- }
39
-
40
- if (typeof child === 'number') {
41
- textContent.push(child.toString());
42
- return;
43
- }
44
-
45
- if (child?.props?.children) {
46
- // Bail on traversing text children down the tree if current node (child)
47
- // has no text. In such situations, react-test-renderer will traverse down
48
- // this tree in a separate call and run this query again. As a result, the
49
- // query will match the deepest text node that matches requested text.
50
- if (filterNodeByType(child, TextComponent) && textContent.length === 0) {
51
- return;
52
- }
53
-
54
- getChildrenAsText(child.props.children, TextComponent, textContent);
55
- }
56
- });
57
-
58
- return textContent;
59
- };
60
-
61
- const getTextInputNodeByPlaceholderText = (node, placeholder) => {
62
- try {
63
- const { TextInput } = require('react-native');
64
- return (
65
- filterNodeByType(node, TextInput) &&
66
- (typeof placeholder === 'string'
67
- ? placeholder === node.props.placeholder
68
- : placeholder.test(node.props.placeholder))
69
- );
70
- } catch (error) {
71
- throw createLibraryNotSupportedError(error);
72
- }
73
- };
74
-
75
- const getTextInputNodeByDisplayValue = (node, value) => {
76
- try {
77
- const { TextInput } = require('react-native');
78
- return (
79
- filterNodeByType(node, TextInput) &&
80
- (typeof value === 'string'
81
- ? value === node.props.value
82
- : value.test(node.props.value))
83
- );
84
- } catch (error) {
85
- throw createLibraryNotSupportedError(error);
86
- }
87
- };
88
-
89
- const getNodeByTestId = (node, testID) => {
90
- return typeof testID === 'string'
91
- ? testID === node.props.testID
92
- : testID.test(node.props.testID);
93
- };
94
-
95
- export const getByText = (instance: ReactTestInstance) =>
96
- function getByTextFn(text: string | RegExp) {
97
- try {
98
- return instance.find((node) => getNodeByText(node, text));
99
- } catch (error) {
100
- throw new ErrorWithStack(
101
- prepareErrorMessage(error, 'text', text),
102
- getByTextFn
103
- );
104
- }
105
- };
106
-
107
- export const getByPlaceholderText = (instance: ReactTestInstance) =>
108
- function getByPlaceholderTextFn(placeholder: string | RegExp) {
109
- try {
110
- return instance.find((node) =>
111
- getTextInputNodeByPlaceholderText(node, placeholder)
112
- );
113
- } catch (error) {
114
- throw new ErrorWithStack(
115
- prepareErrorMessage(error, 'placeholder', placeholder),
116
- getByPlaceholderTextFn
117
- );
118
- }
119
- };
120
-
121
- export const getByDisplayValue = (instance: ReactTestInstance) =>
122
- function getByDisplayValueFn(displayValue: string | RegExp) {
123
- try {
124
- return instance.find((node) =>
125
- getTextInputNodeByDisplayValue(node, displayValue)
126
- );
127
- } catch (error) {
128
- throw new ErrorWithStack(
129
- prepareErrorMessage(error, 'display value', displayValue),
130
- getByDisplayValueFn
131
- );
132
- }
133
- };
134
-
135
- export const getByTestId = (instance: ReactTestInstance) =>
136
- function getByTestIdFn(testID: string | RegExp) {
137
- try {
138
- const results = getAllByTestId(instance)(testID);
139
- if (results.length === 1) {
140
- return results[0];
141
- } else {
142
- throw new ErrorWithStack(
143
- ` Expected 1 but found ${
144
- results.length
145
- } instances with testID: ${String(testID)}`,
146
- getByTestIdFn
147
- );
148
- }
149
- } catch (error) {
150
- throw new ErrorWithStack(prepareErrorMessage(error), getByTestIdFn);
151
- }
152
- };
153
-
154
- export const getAllByText = (instance: ReactTestInstance) =>
155
- function getAllByTextFn(text: string | RegExp) {
156
- const results = instance.findAll((node) => getNodeByText(node, text));
157
- if (results.length === 0) {
158
- throw new ErrorWithStack(
159
- `No instances found with text: ${String(text)}`,
160
- getAllByTextFn
161
- );
162
- }
163
- return results;
164
- };
165
-
166
- export const getAllByPlaceholderText = (instance: ReactTestInstance) =>
167
- function getAllByPlaceholderTextFn(placeholder: string | RegExp) {
168
- const results = instance.findAll((node) =>
169
- getTextInputNodeByPlaceholderText(node, placeholder)
170
- );
171
- if (results.length === 0) {
172
- throw new ErrorWithStack(
173
- `No instances found with placeholder: ${String(placeholder)}`,
174
- getAllByPlaceholderTextFn
175
- );
176
- }
177
- return results;
178
- };
179
-
180
- export const getAllByDisplayValue = (instance: ReactTestInstance) =>
181
- function getAllByDisplayValueFn(value: string | RegExp) {
182
- const results = instance.findAll((node) =>
183
- getTextInputNodeByDisplayValue(node, value)
184
- );
185
- if (results.length === 0) {
186
- throw new ErrorWithStack(
187
- `No instances found with display value: ${String(value)}`,
188
- getAllByDisplayValueFn
189
- );
190
- }
191
- return results;
192
- };
193
-
194
- export const getAllByTestId = (instance: ReactTestInstance) =>
195
- function getAllByTestIdFn(testID: string | RegExp): ReactTestInstance[] {
196
- const results = instance
197
- .findAll((node) => getNodeByTestId(node, testID))
198
- .filter((element) => typeof element.type === 'string');
199
-
200
- if (results.length === 0) {
201
- throw new ErrorWithStack(
202
- `No instances found with testID: ${String(testID)}`,
203
- getAllByTestIdFn
204
- );
205
- }
206
- return results;
207
- };
208
-
209
- export const UNSAFE_getByType = (instance: ReactTestInstance) =>
10
+ import { getAllByTestId, getByTestId } from './byTestId';
11
+ import { getAllByText, getByText } from './byText';
12
+ import {
13
+ getAllByPlaceholderText,
14
+ getByPlaceholderText,
15
+ } from './byPlaceholderText';
16
+ import { getAllByDisplayValue, getByDisplayValue } from './byDisplayValue';
17
+
18
+ export type GetByAPI = {|
19
+ getByText: (text: string | RegExp) => ReactTestInstance,
20
+ getByPlaceholderText: (placeholder: string | RegExp) => ReactTestInstance,
21
+ getByDisplayValue: (value: string | RegExp) => ReactTestInstance,
22
+ getByTestId: (testID: string | RegExp) => ReactTestInstance,
23
+ getAllByTestId: (testID: string | RegExp) => Array<ReactTestInstance>,
24
+ getAllByText: (text: string | RegExp) => Array<ReactTestInstance>,
25
+ getAllByPlaceholderText: (
26
+ placeholder: string | RegExp
27
+ ) => Array<ReactTestInstance>,
28
+ getAllByDisplayValue: (value: string | RegExp) => Array<ReactTestInstance>,
29
+
30
+ // Unsafe aliases
31
+ UNSAFE_getByType: <P>(type: React.ComponentType<P>) => ReactTestInstance,
32
+ UNSAFE_getAllByType: <P>(
33
+ type: React.ComponentType<P>
34
+ ) => Array<ReactTestInstance>,
35
+ UNSAFE_getByProps: (props: { [string]: any }) => ReactTestInstance,
36
+ UNSAFE_getAllByProps: (props: { [string]: any }) => Array<ReactTestInstance>,
37
+
38
+ getByName: () => void,
39
+ getByType: () => void,
40
+ getByProps: () => void,
41
+ getAllByName: () => void,
42
+ getAllByType: () => void,
43
+ getAllByProps: () => void,
44
+
45
+ getByPlaceholder: () => void,
46
+ getAllByPlaceholder: () => void,
47
+ |};
48
+
49
+ export const UNSAFE_getByType = (
50
+ instance: ReactTestInstance
51
+ ): ((type: React.ComponentType<any>) => ReactTestInstance) =>
210
52
  function getByTypeFn(type: React.ComponentType<any>) {
211
53
  try {
212
54
  return instance.findByType(type);
@@ -215,7 +57,9 @@ export const UNSAFE_getByType = (instance: ReactTestInstance) =>
215
57
  }
216
58
  };
217
59
 
218
- export const UNSAFE_getByProps = (instance: ReactTestInstance) =>
60
+ export const UNSAFE_getByProps = (
61
+ instance: ReactTestInstance
62
+ ): ((props: { [propName: string]: any }) => ReactTestInstance) =>
219
63
  function getByPropsFn(props: { [propName: string]: any }) {
220
64
  try {
221
65
  return instance.findByProps(props);
@@ -224,7 +68,9 @@ export const UNSAFE_getByProps = (instance: ReactTestInstance) =>
224
68
  }
225
69
  };
226
70
 
227
- export const UNSAFE_getAllByType = (instance: ReactTestInstance) =>
71
+ export const UNSAFE_getAllByType = (
72
+ instance: ReactTestInstance
73
+ ): ((type: React.ComponentType<any>) => Array<ReactTestInstance>) =>
228
74
  function getAllByTypeFn(type: React.ComponentType<any>) {
229
75
  const results = instance.findAllByType(type);
230
76
  if (results.length === 0) {
@@ -233,7 +79,9 @@ export const UNSAFE_getAllByType = (instance: ReactTestInstance) =>
233
79
  return results;
234
80
  };
235
81
 
236
- export const UNSAFE_getAllByProps = (instance: ReactTestInstance) =>
82
+ export const UNSAFE_getAllByProps = (
83
+ instance: ReactTestInstance
84
+ ): ((props: { [propName: string]: any }) => Array<ReactTestInstance>) =>
237
85
  function getAllByPropsFn(props: { [propName: string]: any }) {
238
86
  const results = instance.findAllByProps(props);
239
87
  if (results.length === 0) {
@@ -245,7 +93,7 @@ export const UNSAFE_getAllByProps = (instance: ReactTestInstance) =>
245
93
  return results;
246
94
  };
247
95
 
248
- export const getByAPI = (instance: ReactTestInstance) => ({
96
+ export const getByAPI = (instance: ReactTestInstance): GetByAPI => ({
249
97
  getByText: getByText(instance),
250
98
  getByPlaceholderText: getByPlaceholderText(instance),
251
99
  getByDisplayValue: getByDisplayValue(instance),
@@ -23,7 +23,7 @@ function makeAliases(aliases, query) {
23
23
  }), {});
24
24
  }
25
25
 
26
- const makeQuery = (name, queryNames, matcherFn) => instance => {
26
+ const makeA11yQuery = (name, queryNames, matcherFn) => instance => {
27
27
  const getBy = matcher => {
28
28
  try {
29
29
  return instance.find(node => isNodeValid(node) && matcherFn(node.props[name], matcher));
@@ -75,5 +75,5 @@ const makeQuery = (name, queryNames, matcherFn) => instance => {
75
75
  };
76
76
  };
77
77
 
78
- var _default = makeQuery;
78
+ var _default = makeA11yQuery;
79
79
  exports.default = _default;
@@ -26,11 +26,13 @@ type QueryNames = {
26
26
  findAllBy: Array<string>,
27
27
  };
28
28
 
29
- const makeQuery = <P: mixed, M: mixed>(
29
+ const makeA11yQuery = <P: mixed, M: mixed>(
30
30
  name: string,
31
31
  queryNames: QueryNames,
32
32
  matcherFn: (prop: P, value: M) => boolean
33
- ) => (instance: ReactTestInstance) => {
33
+ ): ((instance: ReactTestInstance) => { ... }) => (
34
+ instance: ReactTestInstance
35
+ ) => {
34
36
  const getBy = (matcher: M) => {
35
37
  try {
36
38
  return instance.find(
@@ -93,4 +95,4 @@ const makeQuery = <P: mixed, M: mixed>(
93
95
  };
94
96
  };
95
97
 
96
- export default makeQuery;
98
+ export default makeA11yQuery;
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.makeQueries = makeQueries;
7
+
8
+ var _waitFor = _interopRequireDefault(require("../waitFor"));
9
+
10
+ var _errors = require("./errors");
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ function makeQueries(queryAllByQuery, getMissingError, getMultipleError) {
15
+ function getAllByQuery(instance) {
16
+ return function getAllFn(args) {
17
+ const results = queryAllByQuery(instance)(args);
18
+
19
+ if (results.length === 0) {
20
+ throw new _errors.ErrorWithStack(getMissingError(args), getAllFn);
21
+ }
22
+
23
+ return results;
24
+ };
25
+ }
26
+
27
+ function queryByQuery(instance) {
28
+ return function singleQueryFn(args) {
29
+ const results = queryAllByQuery(instance)(args);
30
+
31
+ if (results.length > 1) {
32
+ throw new _errors.ErrorWithStack(getMultipleError(args), singleQueryFn);
33
+ }
34
+
35
+ if (results.length === 0) {
36
+ return null;
37
+ }
38
+
39
+ return results[0];
40
+ };
41
+ }
42
+
43
+ function getByQuery(instance) {
44
+ return function getFn(args) {
45
+ const results = queryAllByQuery(instance)(args);
46
+
47
+ if (results.length > 1) {
48
+ throw new _errors.ErrorWithStack(getMultipleError(args), getFn);
49
+ }
50
+
51
+ if (results.length === 0) {
52
+ throw new _errors.ErrorWithStack(getMissingError(args), getFn);
53
+ }
54
+
55
+ return results[0];
56
+ };
57
+ }
58
+
59
+ function findAllByQuery(instance) {
60
+ return function findAllFn(args, waitForOptions = {}) {
61
+ return (0, _waitFor.default)(() => getAllByQuery(instance)(args), waitForOptions);
62
+ };
63
+ }
64
+
65
+ function findByQuery(instance) {
66
+ return function findFn(args, waitForOptions = {}) {
67
+ return (0, _waitFor.default)(() => getByQuery(instance)(args), waitForOptions);
68
+ };
69
+ }
70
+
71
+ return {
72
+ getBy: getByQuery,
73
+ getAllBy: getAllByQuery,
74
+ queryBy: queryByQuery,
75
+ findBy: findByQuery,
76
+ findAllBy: findAllByQuery
77
+ };
78
+ }