forked from testing-library/jest-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
133 lines (114 loc) 路 2.8 KB
/
utils.js
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
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';
const { ReactTestComponent, ReactElement } = plugins;
const VALID_ELEMENTS = [
'Image',
'Text',
'TextInput',
'Modal',
'View',
'RefreshControl',
'ScrollView',
'ActivityIndicator',
'ListView',
'ListViewDataSource',
];
class ReactElementTypeError extends Error {
constructor(received, matcherFn, context) {
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 React Element.`,
withType,
].join('\n');
}
}
function checkReactElement(element, ...args) {
if (!VALID_ELEMENTS.includes(element.type) && !element._fiber) {
throw new ReactElementTypeError(element, ...args);
}
}
function getType({ type }) {
return type.displayName || type.name || type;
}
function printElement({ props }) {
return ` ${prettyFormat(
{ props },
{
plugins: [ReactTestComponent, ReactElement],
printFunctionName: false,
highlight: true,
},
)}`;
}
function display(value) {
return typeof value === 'string' ? value : stringify(value);
}
function getMessage(matcher, expectedLabel, expectedValue, receivedLabel, receivedValue) {
return [
`${matcher}\n`,
`${expectedLabel}:\n${expectedColor(redent(display(expectedValue), 2))}`,
`${receivedLabel}:\n${receivedColor(redent(display(receivedValue), 2))}`,
].join('\n');
}
function matches(textToMatch, matcher) {
if (matcher instanceof RegExp) {
return matcher.test(textToMatch);
} else {
return textToMatch.includes(String(matcher));
}
}
function normalize(text) {
return text.replace(/\s+/g, ' ').trim();
}
function isEmpty(value) {
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 = {};
export function printDeprecationWarning(functionName, message) {
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,
};