-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathutils.ts
73 lines (65 loc) · 2.02 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
import type { ReactTestInstance } from 'react-test-renderer';
import {
EXPECTED_COLOR,
matcherHint,
printReceived,
printWithType,
RECEIVED_COLOR,
stringify,
} from 'jest-matcher-utils';
import redent from 'redent';
import { isHostElement } from '../helpers/component-tree';
class HostElementTypeError 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);
/* istanbul ignore next */
} catch {
// Deliberately empty.
}
this.message = [
matcherHint(`${context.isNot ? '.not' : ''}.${matcherFn.name}`, 'received', ''),
'',
`${RECEIVED_COLOR('received')} value must be a host element.`,
withType,
].join('\n');
}
}
/**
* Throws HostElementTypeError if passed element is not a host element.
*
* @param element ReactTestInstance to check.
* @param matcherFn Matcher function calling the check used for formatting error.
* @param context Jest matcher context used for formatting error.
*/
export function checkHostElement(
element: ReactTestInstance | null | undefined,
matcherFn: jest.CustomMatcher,
context: jest.MatcherContext,
): asserts element is ReactTestInstance {
if (!isHostElement(element)) {
throw new HostElementTypeError(element, matcherFn, context);
}
}
export function formatMessage(
matcher: string,
expectedLabel: string,
expectedValue: string | RegExp | null | undefined,
receivedLabel: string,
receivedValue: string | null | undefined,
) {
return [
`${matcher}\n`,
`${expectedLabel}:\n${EXPECTED_COLOR(redent(formatValue(expectedValue), 2))}`,
`${receivedLabel}:\n${RECEIVED_COLOR(redent(formatValue(receivedValue), 2))}`,
].join('\n');
}
function formatValue(value: unknown) {
return typeof value === 'string' ? value : stringify(value);
}