-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathto-be-on-the-screen.ts
38 lines (32 loc) · 1.14 KB
/
to-be-on-the-screen.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
import type { ReactTestInstance } from 'react-test-renderer';
import { matcherHint, RECEIVED_COLOR } from 'jest-matcher-utils';
import redent from 'redent';
import { getUnsafeRootElement } from '../helpers/component-tree';
import { formatElement } from '../helpers/format-element';
import { screen } from '../screen';
import { checkHostElement } from './utils';
export function toBeOnTheScreen(this: jest.MatcherContext, element: ReactTestInstance) {
if (element !== null || !this.isNot) {
checkHostElement(element, toBeOnTheScreen, this);
}
const pass = element === null ? false : screen.UNSAFE_root === getUnsafeRootElement(element);
const errorFound = () => {
return `expected element tree not to contain element, but found\n${redent(
formatElement(element),
2,
)}`;
};
const errorNotFound = () => {
return `element could not be found in the element tree`;
};
return {
pass,
message: () => {
return [
matcherHint(`${this.isNot ? '.not' : ''}.toBeOnTheScreen`, 'element', ''),
'',
RECEIVED_COLOR(this.isNot ? errorFound() : errorNotFound()),
].join('\n');
},
};
}