This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 222
/
utilities.ts
164 lines (147 loc) · 4.36 KB
/
utilities.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
156
157
158
159
160
161
162
163
164
import {
diff,
matcherErrorMessage,
matcherHint,
RECEIVED_COLOR as receivedColor,
printWithType,
printReceived,
printExpected,
} from 'jest-matcher-utils';
import {Root} from '../root';
import {Element} from '../element';
import type {Node} from '../types';
export function assertIsNode(
node: unknown,
{expectation, isNot}: {expectation: string; isNot: boolean},
) {
if (node == null) {
throw new Error(
matcherErrorMessage(
matcherHint(`.${expectation}`, undefined, undefined, {isNot}),
`${receivedColor(
'received',
)} value must be an @shopify/react-testing Root or Element object`,
`Received ${receivedColor(
'null',
)}.\nThis usually means that your \`.findX\` method failed to find any matching elements.`,
),
);
}
if (
Array.isArray(node) &&
node.length > 1 &&
(node[0] instanceof Root || node[0] instanceof Element)
) {
throw new Error(
matcherErrorMessage(
matcherHint(`.${expectation}`, undefined, undefined, {isNot}),
`${receivedColor(
'received',
)} value must be an @shopify/react-testing Root or Element object`,
`Received an ${receivedColor(
'array of Root or Element objects',
)}.\nThis usually means that you passed in the result of \`.findAllX\`. Pass the result of \`.findX\` instead.`,
),
);
}
if (!(node instanceof Root) && !(node instanceof Element)) {
throw new Error(
matcherErrorMessage(
matcherHint(`.${expectation}`, undefined, undefined, {isNot}),
`${receivedColor(
'received',
)} value must be an @shopify/react-testing Root or Element object`,
printWithType('Received', node, printReceived),
),
);
}
}
export function assertIsType(
type: unknown,
{expectation, isNot}: {expectation: string; isNot: boolean},
) {
if (type == null) {
throw new Error(
matcherErrorMessage(
matcherHint(`.${expectation}`, undefined, undefined, {isNot}),
`${receivedColor(
'expected',
)} value must be a string or a valid React component.`,
printWithType('Expected', type, printExpected),
),
);
}
}
export function diffs(element: Node<any>[], props: object, expand?: boolean) {
return element.reduce<string>((diffs, element, index) => {
const separator = index === 0 ? '' : '\n\n';
return `${diffs}${separator}${normalizedDiff(element, props, {
expand,
showLegend: index === 0,
})}`;
}, '');
}
function normalizedDiff(
element: Node<any>,
props: object,
{expand = false, showLegend = false},
) {
const result =
diffPropsForNode(element, props, {
expand,
}) || '';
return showLegend ? result : result.split('\n\n')[1];
}
export function printType(type: string | React.ComponentType<any>) {
if (typeof type === 'object' && '_context' in type) {
const context = (type as any)._context;
const componentName = type === context.Provider ? 'Provider' : 'Consumer';
const displayName = context.displayName || 'Context';
return `<${displayName}.${componentName} />`;
}
const displayName =
typeof type === 'string'
? type
: type.displayName || type.name || 'Component';
return `<${displayName} />`;
}
export function diffPropsForNode(
node: Node<any>,
props: object,
{expand = false},
) {
return diff(props, getObjectSubset(node.props, props), {
expand,
});
}
// Original from https://github.com/facebook/jest/blob/master/packages/expect/src/utils.ts#L107
function getObjectSubset(object: any, subset: any): any {
if (Array.isArray(object)) {
if (Array.isArray(subset) && subset.length === object.length) {
return subset.map((sub: any, i: number) =>
getObjectSubset(object[i], sub),
);
}
} else if (object instanceof Date) {
return object;
} else if (
typeof object === 'object' &&
object !== null &&
typeof subset === 'object' &&
subset !== null
) {
const trimmed: any = {};
Object.keys(subset)
.filter((key) => Reflect.has(object, key))
.forEach(
(key) => (trimmed[key] = getObjectSubset(object[key], subset[key])),
);
if (Object.keys(trimmed).length > 0) {
return trimmed;
}
}
return object;
}
export function pluralize(word: string, count: number) {
return count === 1 ? word : `${word}s`;
}