-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathutils.ts
155 lines (133 loc) · 3.73 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { Text, TextInput } from 'react-native';
import type { ReactTestInstance } from 'react-test-renderer';
import redent from 'redent';
import {
RECEIVED_COLOR as receivedColor,
EXPECTED_COLOR as expectedColor,
matcherHint,
printWithType,
printReceived,
stringify,
} from 'jest-matcher-utils';
import prettyFormat, { plugins } from 'pretty-format';
import { isHostElement } from './component-tree';
const { ReactTestComponent, ReactElement } = plugins;
class ReactElementTypeError extends Error {
constructor(received: unknown, matcherFn: jest.CustomMatcher, context: jest.MatcherContext) {
super();
/* istanbul ignore next */
if (Error.captureStackTrace) {
Error.captureStackTrace(this, matcherFn);
}
let withType = '';
try {
withType = printWithType('Received', received, printReceived);
} catch (e) {
// Deliberately empty.
}
/* istanbul ignore next */
this.message = [
matcherHint(`${context.isNot ? '.not' : ''}.${matcherFn.name}`, 'received', ''),
'',
`${receivedColor(
'received',
)} value must be a host element or composite Text/TextInput element`,
withType,
].join('\n');
}
}
function checkReactElement(
element: ReactTestInstance | null | undefined,
matcherFn: jest.CustomMatcher,
context: jest.MatcherContext,
): asserts element is ReactTestInstance {
if (!element) {
throw new ReactElementTypeError(element, matcherFn, context);
}
if (!isHostElement(element) && element.type !== Text && element.type !== TextInput) {
throw new ReactElementTypeError(element, matcherFn, context);
}
}
function getType({ type }: ReactTestInstance) {
// @ts-expect-error: ReactTestInstance contains too loose typing
return type.displayName || type.name || type;
}
function printElement(element: ReactTestInstance | null) {
if (element == null) {
return 'null';
}
return redent(
prettyFormat(
{
// This prop is needed persuade the prettyFormat that the element is
// a ReactTestRendererJSON instance, so it is formatted as JSX.
$$typeof: Symbol.for('react.test.json'),
type: element.type,
props: element.props,
},
{
plugins: [ReactTestComponent, ReactElement],
printFunctionName: false,
printBasicPrototype: false,
highlight: true,
},
),
2,
);
}
function display(value: unknown) {
return typeof value === 'string' ? value : stringify(value);
}
function getMessage(
matcher: string,
expectedLabel: string,
expectedValue: string | RegExp,
receivedLabel: string,
receivedValue: string | null,
) {
return [
`${matcher}\n`,
`${expectedLabel}:\n${expectedColor(redent(display(expectedValue), 2))}`,
`${receivedLabel}:\n${receivedColor(redent(display(receivedValue), 2))}`,
].join('\n');
}
function matches(textToMatch: string, matcher: string | RegExp) {
if (matcher instanceof RegExp) {
return matcher.test(textToMatch);
}
return textToMatch.includes(matcher);
}
function normalize(text: string) {
return text.replace(/\s+/g, ' ').trim();
}
function isEmpty(value: unknown) {
if (!value) {
return true;
}
if (Array.isArray(value)) {
return value.length === 0;
}
if (typeof value === 'object') {
return Object.keys(value).length === 0;
}
return false;
}
const warned: Record<string, boolean> = {};
export function printDeprecationWarning(functionName: string, message: string) {
if (warned[functionName]) {
return;
}
// eslint-disable-next-line no-console
console.warn(`Deprecation Warning:\n${message}`);
warned[functionName] = true;
}
export {
ReactElementTypeError,
checkReactElement,
getType,
getMessage,
matches,
normalize,
isEmpty,
printElement,
};