-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathto-contain-element.ts
37 lines (32 loc) · 1.05 KB
/
to-contain-element.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
import type { ReactTestInstance } from 'react-test-renderer';
import { matcherHint, RECEIVED_COLOR } from 'jest-matcher-utils';
import redent from 'redent';
import { formatElement } from '../helpers/format-element';
import { checkHostElement } from './utils';
export function toContainElement(
this: jest.MatcherContext,
container: ReactTestInstance,
element: ReactTestInstance | null,
) {
checkHostElement(container, toContainElement, this);
if (element !== null) {
checkHostElement(element, toContainElement, this);
}
let matches: ReactTestInstance[] = [];
if (element) {
matches = container.findAll((node) => node === element);
}
return {
pass: matches.length > 0,
message: () => {
return [
matcherHint(`${this.isNot ? '.not' : ''}.toContainElement`, 'container', 'element'),
'',
RECEIVED_COLOR(`${redent(formatElement(container), 2)} ${
this.isNot ? '\n\ncontains:\n\n' : '\n\ndoes not contain:\n\n'
} ${redent(formatElement(element), 2)}
`),
].join('\n');
},
};
}