@testing-library/react-native 9.0.0-alpha.0 → 9.2.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 (121) hide show
  1. package/README.md +2 -2
  2. package/build/act.d.ts +3 -0
  3. package/build/act.js +2 -1
  4. package/build/act.js.map +1 -0
  5. package/build/cleanup.d.ts +5 -0
  6. package/build/cleanup.js +3 -2
  7. package/build/cleanup.js.map +1 -0
  8. package/build/fireEvent.d.ts +8 -0
  9. package/build/fireEvent.js +2 -1
  10. package/build/fireEvent.js.map +1 -0
  11. package/build/flushMicroTasks.d.ts +5 -0
  12. package/build/flushMicroTasks.js +2 -12
  13. package/build/flushMicroTasks.js.map +1 -0
  14. package/build/helpers/a11yAPI.d.ts +66 -0
  15. package/build/helpers/a11yAPI.js +16 -15
  16. package/build/helpers/a11yAPI.js.map +1 -0
  17. package/build/helpers/byDisplayValue.d.ts +6 -0
  18. package/build/helpers/byDisplayValue.js +2 -1
  19. package/build/helpers/byDisplayValue.js.map +1 -0
  20. package/build/helpers/byPlaceholderText.d.ts +6 -0
  21. package/build/helpers/byPlaceholderText.js +2 -1
  22. package/build/helpers/byPlaceholderText.js.map +1 -0
  23. package/build/helpers/byTestId.d.ts +6 -0
  24. package/build/helpers/byTestId.js +2 -1
  25. package/build/helpers/byTestId.js.map +1 -0
  26. package/build/helpers/byText.d.ts +10 -0
  27. package/build/helpers/byText.js +8 -5
  28. package/build/helpers/byText.js.map +1 -0
  29. package/build/helpers/debugDeep.d.ts +5 -0
  30. package/build/helpers/debugDeep.js +4 -1
  31. package/build/helpers/debugDeep.js.map +1 -0
  32. package/build/helpers/debugShallow.d.ts +6 -0
  33. package/build/helpers/debugShallow.js +4 -7
  34. package/build/helpers/debugShallow.js.map +1 -0
  35. package/build/helpers/errors.d.ts +10 -0
  36. package/build/helpers/errors.js +29 -9
  37. package/build/helpers/errors.js.map +1 -0
  38. package/build/helpers/filterNodeByType.d.ts +3 -0
  39. package/build/helpers/filterNodeByType.js +2 -1
  40. package/build/helpers/filterNodeByType.js.map +1 -0
  41. package/build/helpers/findByAPI.d.ts +17 -0
  42. package/build/helpers/findByAPI.js +2 -1
  43. package/build/helpers/findByAPI.js.map +1 -0
  44. package/build/helpers/format.d.ts +3 -0
  45. package/build/helpers/format.js +4 -3
  46. package/build/helpers/format.js.map +1 -0
  47. package/build/helpers/getByAPI.d.ts +31 -0
  48. package/build/helpers/getByAPI.js +4 -19
  49. package/build/helpers/getByAPI.js.map +1 -0
  50. package/build/helpers/makeA11yQuery.d.ts +13 -0
  51. package/build/helpers/makeA11yQuery.js +2 -1
  52. package/build/helpers/makeA11yQuery.js.map +1 -0
  53. package/build/helpers/makeQueries.d.ts +20 -0
  54. package/build/helpers/makeQueries.js +6 -3
  55. package/build/helpers/makeQueries.js.map +1 -0
  56. package/build/helpers/queryByAPI.d.ts +39 -0
  57. package/build/helpers/queryByAPI.js +3 -8
  58. package/build/helpers/queryByAPI.js.map +1 -0
  59. package/build/helpers/timers.d.ts +6 -0
  60. package/build/helpers/timers.js +9 -6
  61. package/build/helpers/timers.js.map +1 -0
  62. package/build/index.d.ts +1 -0
  63. package/build/index.flow.js +384 -0
  64. package/build/index.js +2 -1
  65. package/build/index.js.map +1 -0
  66. package/build/matches.d.ts +9 -0
  67. package/build/matches.js +3 -2
  68. package/build/matches.js.map +1 -0
  69. package/build/{pure.js.flow → pure.d.ts} +4 -8
  70. package/build/pure.js +14 -31
  71. package/build/pure.js.map +1 -0
  72. package/build/render.d.ts +145 -0
  73. package/build/render.js +14 -10
  74. package/build/render.js.map +1 -0
  75. package/build/renderHook.d.ts +17 -0
  76. package/build/renderHook.js +57 -0
  77. package/build/renderHook.js.map +1 -0
  78. package/build/shallow.d.ts +8 -0
  79. package/build/shallow.js +3 -9
  80. package/build/shallow.js.map +1 -0
  81. package/build/waitFor.d.ts +8 -0
  82. package/build/waitFor.js +32 -23
  83. package/build/waitFor.js.map +1 -0
  84. package/build/waitForElementToBeRemoved.d.ts +2 -0
  85. package/build/waitForElementToBeRemoved.js +2 -1
  86. package/build/waitForElementToBeRemoved.js.map +1 -0
  87. package/build/within.d.ts +127 -0
  88. package/build/within.js +3 -2
  89. package/build/within.js.map +1 -0
  90. package/package.json +25 -15
  91. package/typings/index.flow.js +384 -0
  92. package/build/act.js.flow +0 -9
  93. package/build/cleanup.js.flow +0 -13
  94. package/build/fireEvent.js.flow +0 -127
  95. package/build/flushMicroTasks.js.flow +0 -23
  96. package/build/helpers/a11yAPI.js.flow +0 -241
  97. package/build/helpers/byDisplayValue.js.flow +0 -66
  98. package/build/helpers/byPlaceholderText.js.flow +0 -62
  99. package/build/helpers/byTestId.js.flow +0 -50
  100. package/build/helpers/byText.js.flow +0 -104
  101. package/build/helpers/debugDeep.js.flow +0 -16
  102. package/build/helpers/debugShallow.js.flow +0 -20
  103. package/build/helpers/errors.js.flow +0 -82
  104. package/build/helpers/filterNodeByType.js.flow +0 -1
  105. package/build/helpers/findByAPI.js.flow +0 -76
  106. package/build/helpers/format.js.flow +0 -10
  107. package/build/helpers/getByAPI.js.flow +0 -157
  108. package/build/helpers/makeA11yQuery.js.flow +0 -98
  109. package/build/helpers/makeQueries.js.flow +0 -165
  110. package/build/helpers/queryByAPI.js.flow +0 -166
  111. package/build/helpers/timers.js.flow +0 -88
  112. package/build/index.js.flow +0 -18
  113. package/build/matches.js.flow +0 -41
  114. package/build/render.js.flow +0 -109
  115. package/build/shallow.js.flow +0 -26
  116. package/build/types.flow.js +0 -1
  117. package/build/types.flow.js.flow +0 -59
  118. package/build/waitFor.js.flow +0 -221
  119. package/build/waitForElementToBeRemoved.js.flow +0 -42
  120. package/build/within.js.flow +0 -18
  121. package/typings/index.d.ts +0 -432
@@ -1,20 +0,0 @@
1
- // @flow
2
- import * as React from 'react';
3
- import { shallowInternal } from '../shallow';
4
- import format from './format';
5
-
6
- /**
7
- * Log pretty-printed shallow test component instance
8
- */
9
- export default function debugShallow(
10
- instance: ReactTestInstance | React.Element<any>,
11
- message?: string
12
- ) {
13
- const { output } = shallowInternal(instance);
14
-
15
- if (message) {
16
- console.log(`${message}\n\n`, format(output));
17
- } else {
18
- console.log(format(output));
19
- }
20
- }
@@ -1,82 +0,0 @@
1
- // @flow
2
- import prettyFormat from 'pretty-format';
3
-
4
- export class ErrorWithStack extends Error {
5
- constructor(message: ?string, callsite: Function) {
6
- super(message);
7
- if (Error.captureStackTrace) {
8
- Error.captureStackTrace(this, callsite);
9
- }
10
- }
11
- }
12
-
13
- export const createLibraryNotSupportedError = (error: Error): Error =>
14
- new Error(
15
- `Currently the only supported library to search by text is "react-native".\n\n${error.message}`
16
- );
17
-
18
- export const prepareErrorMessage = (
19
- error: Error,
20
- name: ?string,
21
- value: ?mixed
22
- ): string => {
23
- // Strip info about custom predicate
24
- let errorMessage = error.message.replace(
25
- / matching custom predicate[^]*/gm,
26
- ''
27
- );
28
-
29
- if (name && value) {
30
- errorMessage += ` with ${name} ${prettyFormat(value, { min: true })}`;
31
- }
32
- return errorMessage;
33
- };
34
-
35
- export const createQueryByError = (error: Error, callsite: Function): null => {
36
- if (error.message.includes('No instances found')) {
37
- return null;
38
- }
39
- throw new ErrorWithStack(error.message, callsite);
40
- };
41
-
42
- export function copyStackTrace(target: Error, stackTraceSource: Error) {
43
- target.stack = stackTraceSource.stack.replace(
44
- stackTraceSource.message,
45
- target.message
46
- );
47
- }
48
-
49
- const warned = {};
50
-
51
- export function printDeprecationWarning(functionName: string) {
52
- if (warned[functionName]) {
53
- return;
54
- }
55
-
56
- // eslint-disable-next-line no-console
57
- console.warn(`
58
- Deprecation Warning:
59
- Use of ${functionName} is not recommended and will be deleted in future versions of @testing-library/react-native.
60
- `);
61
-
62
- warned[functionName] = true;
63
- }
64
-
65
- export function throwRemovedFunctionError(
66
- functionName: string,
67
- docsRef: string
68
- ) {
69
- throw new Error(
70
- `"${functionName}" has been removed.\n\nPlease consult: https://callstack.github.io/react-native-testing-library/docs/${docsRef}`
71
- );
72
- }
73
-
74
- export function throwRenamedFunctionError(
75
- functionName: string,
76
- newFunctionName: string
77
- ) {
78
- throw new ErrorWithStack(
79
- `The "${functionName}" function has been renamed to "${newFunctionName}". Please replace all occurrences.`,
80
- throwRenamedFunctionError
81
- );
82
- }
@@ -1 +0,0 @@
1
- export const filterNodeByType = (node, type) => node.type === type;
@@ -1,76 +0,0 @@
1
- // @flow
2
- import type { WaitForOptions } from '../waitFor';
3
- import type { TextMatchOptions } from './byText';
4
- import { findAllByTestId, findByTestId } from './byTestId';
5
- import { findAllByText, findByText } from './byText';
6
- import {
7
- findAllByPlaceholderText,
8
- findByPlaceholderText,
9
- } from './byPlaceholderText';
10
- import { findAllByDisplayValue, findByDisplayValue } from './byDisplayValue';
11
- import { throwRenamedFunctionError } from './errors';
12
-
13
- export type FindByAPI = {|
14
- findAllByDisplayValue: (
15
- value: string | RegExp,
16
- queryOptions?: TextMatchOptions & WaitForOptions,
17
- waitForOptions?: WaitForOptions
18
- ) => Promise<Array<ReactTestInstance>>,
19
- findAllByPlaceholder: () => void,
20
- findAllByPlaceholderText: (
21
- placeholder: string | RegExp,
22
- queryOptions?: TextMatchOptions & WaitForOptions,
23
- waitForOptions?: WaitForOptions
24
- ) => Promise<Array<ReactTestInstance>>,
25
- findAllByTestId: (
26
- testId: string | RegExp,
27
- queryOptions?: TextMatchOptions & WaitForOptions,
28
- waitForOptions?: WaitForOptions
29
- ) => Promise<Array<ReactTestInstance>>,
30
- findAllByText: (
31
- text: string | RegExp,
32
- queryOptions?: TextMatchOptions & WaitForOptions,
33
- waitForOptions?: WaitForOptions
34
- ) => Promise<Array<ReactTestInstance>>,
35
- findByDisplayValue: (
36
- value: string | RegExp,
37
- queryOptions?: TextMatchOptions & WaitForOptions,
38
- waitForOptions?: WaitForOptions
39
- ) => Promise<ReactTestInstance>,
40
- findByPlaceholder: () => void,
41
- findByPlaceholderText: (
42
- placeholder: string | RegExp,
43
- queryOptions?: TextMatchOptions & WaitForOptions,
44
- waitForOptions?: WaitForOptions
45
- ) => Promise<ReactTestInstance>,
46
- findByTestId: (
47
- testId: string | RegExp,
48
- queryOptions?: TextMatchOptions & WaitForOptions,
49
- waitForOptions?: WaitForOptions
50
- ) => Promise<ReactTestInstance>,
51
- findByText: (
52
- text: string | RegExp,
53
- queryOptions?: TextMatchOptions & WaitForOptions,
54
- waitForOptions?: WaitForOptions
55
- ) => Promise<ReactTestInstance>,
56
- |};
57
-
58
- export const findByAPI = (instance: ReactTestInstance): FindByAPI => ({
59
- findByTestId: findByTestId(instance),
60
- findByText: findByText(instance),
61
- findByPlaceholderText: findByPlaceholderText(instance),
62
- findByDisplayValue: findByDisplayValue(instance),
63
- findAllByTestId: findAllByTestId(instance),
64
- findAllByText: findAllByText(instance),
65
- findAllByPlaceholderText: findAllByPlaceholderText(instance),
66
- findAllByDisplayValue: findAllByDisplayValue(instance),
67
-
68
- // Renamed
69
- findByPlaceholder: () =>
70
- throwRenamedFunctionError('findByPlaceholder', 'findByPlaceholderText'),
71
- findAllByPlaceholder: () =>
72
- throwRenamedFunctionError(
73
- 'findAllByPlaceholder',
74
- 'findAllByPlaceholderText'
75
- ),
76
- });
@@ -1,10 +0,0 @@
1
- // @flow
2
- import prettyFormat, { plugins } from 'pretty-format';
3
-
4
- const format = (input: ?ReactTestRendererJSON): typeof prettyFormat =>
5
- prettyFormat(input, {
6
- plugins: [plugins.ReactTestComponent, plugins.ReactElement],
7
- highlight: true,
8
- });
9
-
10
- export default format;
@@ -1,157 +0,0 @@
1
- // @flow
2
- import * as React from 'react';
3
- import prettyFormat from 'pretty-format';
4
- import {
5
- ErrorWithStack,
6
- prepareErrorMessage,
7
- throwRemovedFunctionError,
8
- throwRenamedFunctionError,
9
- } from './errors';
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
- import type { TextMatchOptions } from './byText';
18
-
19
- export type GetByAPI = {|
20
- getByText: (
21
- text: string | RegExp,
22
- queryOptions?: TextMatchOptions
23
- ) => ReactTestInstance,
24
- getByPlaceholderText: (
25
- placeholder: string | RegExp,
26
- queryOptions?: TextMatchOptions
27
- ) => ReactTestInstance,
28
- getByDisplayValue: (
29
- value: string | RegExp,
30
- queryOptions?: TextMatchOptions
31
- ) => ReactTestInstance,
32
- getByTestId: (
33
- testID: string | RegExp,
34
- queryOptions?: TextMatchOptions
35
- ) => ReactTestInstance,
36
- getAllByTestId: (
37
- testID: string | RegExp,
38
- queryOptions?: TextMatchOptions
39
- ) => Array<ReactTestInstance>,
40
- getAllByText: (
41
- text: string | RegExp,
42
- queryOptions?: TextMatchOptions
43
- ) => Array<ReactTestInstance>,
44
- getAllByPlaceholderText: (
45
- placeholder: string | RegExp,
46
- queryOptions?: TextMatchOptions
47
- ) => Array<ReactTestInstance>,
48
- getAllByDisplayValue: (
49
- value: string | RegExp,
50
- queryOptions?: TextMatchOptions
51
- ) => Array<ReactTestInstance>,
52
-
53
- // Unsafe aliases
54
- UNSAFE_getByType: <P>(type: React.ComponentType<P>) => ReactTestInstance,
55
- UNSAFE_getAllByType: <P>(
56
- type: React.ComponentType<P>
57
- ) => Array<ReactTestInstance>,
58
- UNSAFE_getByProps: (props: { [string]: any }) => ReactTestInstance,
59
- UNSAFE_getAllByProps: (props: { [string]: any }) => Array<ReactTestInstance>,
60
-
61
- getByName: () => void,
62
- getByType: () => void,
63
- getByProps: () => void,
64
- getAllByName: () => void,
65
- getAllByType: () => void,
66
- getAllByProps: () => void,
67
-
68
- getByPlaceholder: () => void,
69
- getAllByPlaceholder: () => void,
70
- |};
71
-
72
- export const UNSAFE_getByType = (
73
- instance: ReactTestInstance
74
- ): ((type: React.ComponentType<any>) => ReactTestInstance) =>
75
- function getByTypeFn(type: React.ComponentType<any>) {
76
- try {
77
- return instance.findByType(type);
78
- } catch (error) {
79
- throw new ErrorWithStack(prepareErrorMessage(error), getByTypeFn);
80
- }
81
- };
82
-
83
- export const UNSAFE_getByProps = (
84
- instance: ReactTestInstance
85
- ): ((props: { [propName: string]: any }) => ReactTestInstance) =>
86
- function getByPropsFn(props: { [propName: string]: any }) {
87
- try {
88
- return instance.findByProps(props);
89
- } catch (error) {
90
- throw new ErrorWithStack(prepareErrorMessage(error), getByPropsFn);
91
- }
92
- };
93
-
94
- export const UNSAFE_getAllByType = (
95
- instance: ReactTestInstance
96
- ): ((type: React.ComponentType<any>) => Array<ReactTestInstance>) =>
97
- function getAllByTypeFn(type: React.ComponentType<any>) {
98
- const results = instance.findAllByType(type);
99
- if (results.length === 0) {
100
- throw new ErrorWithStack('No instances found', getAllByTypeFn);
101
- }
102
- return results;
103
- };
104
-
105
- export const UNSAFE_getAllByProps = (
106
- instance: ReactTestInstance
107
- ): ((props: { [propName: string]: any }) => Array<ReactTestInstance>) =>
108
- function getAllByPropsFn(props: { [propName: string]: any }) {
109
- const results = instance.findAllByProps(props);
110
- if (results.length === 0) {
111
- throw new ErrorWithStack(
112
- `No instances found with props:\n${prettyFormat(props)}`,
113
- getAllByPropsFn
114
- );
115
- }
116
- return results;
117
- };
118
-
119
- export const getByAPI = (instance: ReactTestInstance): GetByAPI => ({
120
- getByText: getByText(instance),
121
- getByPlaceholderText: getByPlaceholderText(instance),
122
- getByDisplayValue: getByDisplayValue(instance),
123
- getByTestId: getByTestId(instance),
124
- getAllByText: getAllByText(instance),
125
- getAllByPlaceholderText: getAllByPlaceholderText(instance),
126
- getAllByDisplayValue: getAllByDisplayValue(instance),
127
- getAllByTestId: getAllByTestId(instance),
128
-
129
- // Unsafe
130
- UNSAFE_getByType: UNSAFE_getByType(instance),
131
- UNSAFE_getAllByType: UNSAFE_getAllByType(instance),
132
- UNSAFE_getByProps: UNSAFE_getByProps(instance),
133
- UNSAFE_getAllByProps: UNSAFE_getAllByProps(instance),
134
-
135
- // Removed
136
- getByName: () =>
137
- throwRemovedFunctionError('getByName', 'migration-v2#removed-functions'),
138
- getAllByName: () =>
139
- throwRemovedFunctionError('getAllByName', 'migration-v2#removed-functions'),
140
- getByType: () =>
141
- throwRemovedFunctionError('getByType', 'migration-v2#removed-functions'),
142
- getAllByType: () =>
143
- throwRemovedFunctionError('getAllByType', 'migration-v2#removed-functions'),
144
- getByProps: () =>
145
- throwRemovedFunctionError('getByProps', 'migration-v2#removed-functions'),
146
- getAllByProps: () =>
147
- throwRemovedFunctionError(
148
- 'getAllByProps',
149
- 'migration-v2#removed-functions'
150
- ),
151
-
152
- // Renamed
153
- getByPlaceholder: () =>
154
- throwRenamedFunctionError('getByPlaceholder', 'getByPlaceholderText'),
155
- getAllByPlaceholder: () =>
156
- throwRenamedFunctionError('getAllByPlaceholder', 'getByPlaceholderText'),
157
- });
@@ -1,98 +0,0 @@
1
- // @flow
2
- import waitFor from '../waitFor';
3
- import type { WaitForOptions } from '../waitFor';
4
- import {
5
- ErrorWithStack,
6
- prepareErrorMessage,
7
- createQueryByError,
8
- } from './errors';
9
-
10
- function isNodeValid(node: ReactTestInstance) {
11
- return typeof node.type === 'string';
12
- }
13
-
14
- function makeAliases(aliases: Array<string>, query: Function) {
15
- return aliases
16
- .map((alias) => ({ [alias]: query }))
17
- .reduce((acc, query) => ({ ...acc, ...query }), {});
18
- }
19
-
20
- type QueryNames = {
21
- getBy: Array<string>,
22
- getAllBy: Array<string>,
23
- queryBy: Array<string>,
24
- queryAllBy: Array<string>,
25
- findBy: Array<string>,
26
- findAllBy: Array<string>,
27
- };
28
-
29
- const makeA11yQuery = <P: mixed, M: mixed>(
30
- name: string,
31
- queryNames: QueryNames,
32
- matcherFn: (prop: P, value: M) => boolean
33
- ): ((instance: ReactTestInstance) => { ... }) => (
34
- instance: ReactTestInstance
35
- ) => {
36
- const getBy = (matcher: M) => {
37
- try {
38
- return instance.find(
39
- (node) => isNodeValid(node) && matcherFn(node.props[name], matcher)
40
- );
41
- } catch (error) {
42
- throw new ErrorWithStack(
43
- prepareErrorMessage(error, name, matcher),
44
- getBy
45
- );
46
- }
47
- };
48
-
49
- const getAllBy = (matcher: M) => {
50
- const results = instance.findAll(
51
- (node) => isNodeValid(node) && matcherFn(node.props[name], matcher)
52
- );
53
-
54
- if (results.length === 0) {
55
- throw new ErrorWithStack(
56
- prepareErrorMessage(new Error('No instances found'), name, matcher),
57
- getAllBy
58
- );
59
- }
60
-
61
- return results;
62
- };
63
-
64
- const queryBy = (matcher: M) => {
65
- try {
66
- return getBy(matcher);
67
- } catch (error) {
68
- return createQueryByError(error, queryBy);
69
- }
70
- };
71
-
72
- const queryAllBy = (matcher: M) => {
73
- try {
74
- return getAllBy(matcher);
75
- } catch (error) {
76
- return [];
77
- }
78
- };
79
-
80
- const findBy = (matcher: M, waitForOptions?: WaitForOptions) => {
81
- return waitFor(() => getBy(matcher), waitForOptions);
82
- };
83
-
84
- const findAllBy = (matcher: M, waitForOptions?: WaitForOptions) => {
85
- return waitFor(() => getAllBy(matcher), waitForOptions);
86
- };
87
-
88
- return {
89
- ...makeAliases(queryNames.getBy, getBy),
90
- ...makeAliases(queryNames.getAllBy, getAllBy),
91
- ...makeAliases(queryNames.queryBy, queryBy),
92
- ...makeAliases(queryNames.queryAllBy, queryAllBy),
93
- ...makeAliases(queryNames.findBy, findBy),
94
- ...makeAliases(queryNames.findAllBy, findAllBy),
95
- };
96
- };
97
-
98
- export default makeA11yQuery;
@@ -1,165 +0,0 @@
1
- // @flow
2
- import waitFor from '../waitFor';
3
- import type { WaitForOptions } from '../waitFor';
4
- import { ErrorWithStack } from './errors';
5
- import type { TextMatchOptions } from './byText';
6
-
7
- type QueryFunction<ArgType, ReturnType> = (
8
- instance: ReactTestInstance
9
- ) => (args: ArgType, queryOptions?: TextMatchOptions) => ReturnType;
10
-
11
- type FindQueryFunction<ArgType, ReturnType> = (
12
- instance: ReactTestInstance
13
- ) => (
14
- args: ArgType,
15
- queryOptions?: TextMatchOptions & WaitForOptions,
16
- waitForOptions?: WaitForOptions
17
- ) => Promise<ReturnType>;
18
-
19
- type QueryAllByQuery<QueryArg> = QueryFunction<
20
- QueryArg,
21
- Array<ReactTestInstance>
22
- >;
23
- type QueryByQuery<QueryArg> = QueryFunction<QueryArg, null | ReactTestInstance>;
24
-
25
- type GetAllByQuery<QueryArg> = QueryFunction<
26
- QueryArg,
27
- Array<ReactTestInstance>
28
- >;
29
- type GetByQuery<QueryArg> = QueryFunction<QueryArg, ReactTestInstance>;
30
-
31
- type FindAllByQuery<QueryArg> = FindQueryFunction<
32
- QueryArg,
33
- Array<ReactTestInstance>
34
- >;
35
- type FindByQuery<QueryArg> = FindQueryFunction<QueryArg, ReactTestInstance>;
36
-
37
- export type Queries<QueryArg> = {
38
- getBy: GetByQuery<QueryArg>,
39
- getAllBy: GetAllByQuery<QueryArg>,
40
- queryBy: QueryByQuery<QueryArg>,
41
- findBy: FindByQuery<QueryArg>,
42
- findAllBy: FindAllByQuery<QueryArg>,
43
- };
44
-
45
- // The WaitForOptions has been moved to the second option param of findBy* methods with the adding of TextMatchOptions
46
- // To make the migration easier and avoid a breaking change, keep reading this options from the first param but warn
47
- const deprecatedKeys: $Keys<WaitForOptions>[] = [
48
- 'timeout',
49
- 'interval',
50
- 'stackTraceError',
51
- ];
52
- const extractDeprecatedWaitForOptionUsage = (queryOptions?: WaitForOptions) => {
53
- if (queryOptions) {
54
- const waitForOptions: WaitForOptions = {
55
- timeout: queryOptions.timeout,
56
- interval: queryOptions.interval,
57
- stackTraceError: queryOptions.stackTraceError,
58
- };
59
- deprecatedKeys.forEach((key) => {
60
- if (queryOptions[key]) {
61
- // eslint-disable-next-line no-console
62
- console.warn(
63
- `Use of option "${key}" in a findBy* query's second parameter, TextMatchOptions, is deprecated. Please pass this option in the third, WaitForOptions, parameter.
64
- Example:
65
-
66
- findByText(text, {}, { ${key}: ${queryOptions[key].toString()} })`
67
- );
68
- }
69
- });
70
- return waitForOptions;
71
- }
72
- };
73
-
74
- export function makeQueries<QueryArg>(
75
- queryAllByQuery: QueryAllByQuery<QueryArg>,
76
- getMissingError: (args: QueryArg) => string,
77
- getMultipleError: (args: QueryArg) => string
78
- ): Queries<QueryArg> {
79
- function getAllByQuery(instance: ReactTestInstance) {
80
- return function getAllFn(args: QueryArg, queryOptions?: TextMatchOptions) {
81
- const results = queryAllByQuery(instance)(args, queryOptions);
82
-
83
- if (results.length === 0) {
84
- throw new ErrorWithStack(getMissingError(args), getAllFn);
85
- }
86
-
87
- return results;
88
- };
89
- }
90
-
91
- function queryByQuery(instance: ReactTestInstance) {
92
- return function singleQueryFn(
93
- args: QueryArg,
94
- queryOptions?: TextMatchOptions
95
- ) {
96
- const results = queryAllByQuery(instance)(args, queryOptions);
97
-
98
- if (results.length > 1) {
99
- throw new ErrorWithStack(getMultipleError(args), singleQueryFn);
100
- }
101
-
102
- if (results.length === 0) {
103
- return null;
104
- }
105
-
106
- return results[0];
107
- };
108
- }
109
-
110
- function getByQuery(instance: ReactTestInstance) {
111
- return function getFn(args: QueryArg, queryOptions?: TextMatchOptions) {
112
- const results = queryAllByQuery(instance)(args, queryOptions);
113
-
114
- if (results.length > 1) {
115
- throw new ErrorWithStack(getMultipleError(args), getFn);
116
- }
117
-
118
- if (results.length === 0) {
119
- throw new ErrorWithStack(getMissingError(args), getFn);
120
- }
121
-
122
- return results[0];
123
- };
124
- }
125
-
126
- function findAllByQuery(instance: ReactTestInstance) {
127
- return function findAllFn(
128
- args: QueryArg,
129
- queryOptions?: TextMatchOptions & WaitForOptions,
130
- waitForOptions?: WaitForOptions = {}
131
- ) {
132
- const deprecatedWaitForOptions = extractDeprecatedWaitForOptionUsage(
133
- queryOptions
134
- );
135
- return waitFor(() => getAllByQuery(instance)(args, queryOptions), {
136
- ...deprecatedWaitForOptions,
137
- ...waitForOptions,
138
- });
139
- };
140
- }
141
-
142
- function findByQuery(instance: ReactTestInstance) {
143
- return function findFn(
144
- args: QueryArg,
145
- queryOptions?: TextMatchOptions & WaitForOptions,
146
- waitForOptions?: WaitForOptions = {}
147
- ) {
148
- const deprecatedWaitForOptions = extractDeprecatedWaitForOptionUsage(
149
- queryOptions
150
- );
151
- return waitFor(() => getByQuery(instance)(args, queryOptions), {
152
- ...deprecatedWaitForOptions,
153
- ...waitForOptions,
154
- });
155
- };
156
- }
157
-
158
- return {
159
- getBy: getByQuery,
160
- getAllBy: getAllByQuery,
161
- queryBy: queryByQuery,
162
- findBy: findByQuery,
163
- findAllBy: findAllByQuery,
164
- };
165
- }