blob: af304ab3787b1b9ff36c54f3cd3a39175a6a8eec (
plain)
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
|
import { expect } from 'chai';
import React from 'react';
type ReactElementWithChildren = React.ReactElement<{ children: React.ReactNode }>;
function isReactElementWithChildren(element: unknown): element is ReactElementWithChildren {
if (React.isValidElement(element)) {
if (element.props && element.props instanceof Object) {
return 'children' in element.props;
}
}
return false;
}
export function expectChildrenToMatch(
element: React.ReactElement<unknown>,
expectedParts: string[],
) {
if (!isReactElementWithChildren(element)) {
throw new Error('React element does not have children on it');
}
const elementChildren = React.Children.toArray(element.props.children);
expect(elementChildren).to.have.lengthOf(expectedParts.length);
elementChildren.forEach((elementChild, index) => {
if (!isReactElementWithChildren(elementChild)) {
throw new Error('React element child does not have children on it');
}
expect(elementChild.props.children).to.equal(expectedParts[index]);
});
}
export function expectChildrenToMatchElements(
element: React.ReactElement<unknown>,
expectedElements: React.ReactElement[],
) {
if (!isReactElementWithChildren(element)) {
throw new Error('React element does not have children on it');
}
const elementChildren = React.Children.toArray(element.props.children);
expect(elementChildren).to.have.lengthOf(expectedElements.length);
elementChildren.forEach((elementChild, index) => {
if (!isReactElementWithChildren(elementChild)) {
throw new Error('React element child does not have children on it');
}
const expectedElement = expectedElements[index];
if (!isReactElementWithChildren(expectedElement)) {
throw new Error('Expected React element does not have children on it');
}
expect(elementChild.type).to.equal(expectedElement.type);
expect(elementChild.props.children).to.equal(expectedElement.props.children);
});
}
export function createFragment<T extends React.ReactNode>(value: T) {
return React.createElement(React.Fragment, null, value);
}
|