Skip to content

Commit 9bd4527

Browse files
committed
refactor: cleanup
1 parent 9caa54a commit 9bd4527

File tree

3 files changed

+17
-35
lines changed

3 files changed

+17
-35
lines changed

src/matchers/__tests__/to-be-on-the-screen.test.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test('toBeOnTheScreen() on attached element', () => {
2121
.toThrowErrorMatchingInlineSnapshot(`
2222
"expect(element).not.toBeOnTheScreen()
2323
24-
expected element tree not to contain element but found:
24+
expected element tree not to contain element, but found
2525
<View
2626
testID="test"
2727
/>"
@@ -47,9 +47,10 @@ test('toBeOnTheScreen() on null element', () => {
4747
expect(null).not.toBeOnTheScreen();
4848
expect(() => expect(null).toBeOnTheScreen())
4949
.toThrowErrorMatchingInlineSnapshot(`
50-
"expect(element).toBeOnTheScreen()
50+
"expect(received).toBeOnTheScreen()
5151
52-
element could not be found in the element tree"
52+
received value must be a host element.
53+
Received has value: null"
5354
`);
5455
});
5556

src/matchers/to-be-on-the-screen.tsx

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import type { ReactTestInstance } from 'react-test-renderer';
22
import { matcherHint, RECEIVED_COLOR } from 'jest-matcher-utils';
3-
import { checkReactElement, printElement } from './utils';
3+
import { screen } from '../screen';
4+
import { checkHostElement, printElement } from './utils';
45

56
export function toBeOnTheScreen(
67
this: jest.MatcherContext,
78
element: ReactTestInstance
89
) {
9-
if (element !== null) {
10-
checkReactElement(element, toBeOnTheScreen, this);
10+
if (element !== null || !this.isNot) {
11+
checkHostElement(element, toBeOnTheScreen, this);
1112
}
1213

1314
const pass =
14-
element === null ? false : getScreenRoot() === getRootElement(element);
15+
element === null ? false : screen.UNSAFE_root === getRootElement(element);
1516

1617
const errorFound = () => {
17-
return `expected element tree not to contain element but found:\n${printElement(
18+
return `expected element tree not to contain element, but found\n${printElement(
1819
element
1920
)}`;
2021
};
@@ -46,20 +47,3 @@ function getRootElement(element: ReactTestInstance) {
4647
}
4748
return root;
4849
}
49-
50-
function getScreenRoot() {
51-
try {
52-
// eslint-disable-next-line import/no-extraneous-dependencies
53-
const { screen } = require('@testing-library/react-native');
54-
if (!screen) {
55-
throw new Error('screen is undefined');
56-
}
57-
58-
return screen.UNSAFE_root ?? screen.container;
59-
} catch (error) {
60-
throw new Error(
61-
'Could not import `screen` object from @testing-library/react-native.\n\n' +
62-
'Using toBeOnTheScreen() matcher requires @testing-library/react-native v10.1.0 or later to be added to your devDependencies.'
63-
);
64-
}
65-
}

src/matchers/utils.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import {
77
} from 'jest-matcher-utils';
88
import prettyFormat, { plugins } from 'pretty-format';
99
import redent from 'redent';
10+
import { isHostElement } from '../helpers/component-tree';
1011

11-
class ReactElementTypeError extends Error {
12+
class HostElementTypeError extends Error {
1213
constructor(
1314
received: unknown,
1415
matcherFn: jest.CustomMatcher,
@@ -20,6 +21,7 @@ class ReactElementTypeError extends Error {
2021
if (Error.captureStackTrace) {
2122
Error.captureStackTrace(this, matcherFn);
2223
}
24+
2325
let withType = '';
2426
try {
2527
withType = printWithType('Received', received, printReceived);
@@ -35,7 +37,7 @@ class ReactElementTypeError extends Error {
3537
''
3638
),
3739
'',
38-
`${RECEIVED_COLOR('received')} value must be a React Element.`,
40+
`${RECEIVED_COLOR('received')} value must be a host element.`,
3941
withType,
4042
].join('\n');
4143
}
@@ -66,17 +68,12 @@ export function printElement(element: ReactTestInstance | null) {
6668
);
6769
}
6870

69-
export function checkReactElement(
71+
export function checkHostElement(
7072
element: ReactTestInstance | null | undefined,
7173
matcherFn: jest.CustomMatcher,
7274
context: jest.MatcherContext
7375
): asserts element is ReactTestInstance {
74-
if (!element) {
75-
throw new ReactElementTypeError(element, matcherFn, context);
76-
}
77-
78-
// @ts-expect-error internal _fiber property of ReactTestInstance
79-
if (!element._fiber && !VALID_ELEMENTS.includes(element.type.toString())) {
80-
throw new ReactElementTypeError(element, matcherFn, context);
76+
if (!isHostElement(element)) {
77+
throw new HostElementTypeError(element, matcherFn, context);
8178
}
8279
}

0 commit comments

Comments
 (0)