Skip to content

Commit 3d3b1b0

Browse files
siepramdjastrzebski
authored andcommitted
fix: validating host container and element
1 parent 48002c3 commit 3d3b1b0

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/matchers/__tests__/to-contain-element.test.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,31 @@ test('toContainElement() on non-React elements', () => {
121121
Received has value: true"
122122
`);
123123
});
124+
125+
test('toContainElement() passing non-React element', () => {
126+
render(<View testID="view" />);
127+
128+
const view = screen.getByTestId('view');
129+
130+
expect(() =>
131+
// @ts-expect-error
132+
expect(view).not.toContainElement(true)
133+
).toThrowErrorMatchingInlineSnapshot(`
134+
"expect(received).not.toContainElement()
135+
136+
received value must be a host element.
137+
Received has type: boolean
138+
Received has value: true"
139+
`);
140+
});
141+
142+
test('toContainElement() passing null on non-React element', () => {
143+
expect(() => expect(true).not.toContainElement(null))
144+
.toThrowErrorMatchingInlineSnapshot(`
145+
"expect(received).not.toContainElement()
146+
147+
received value must be a host element.
148+
Received has type: boolean
149+
Received has value: true"
150+
`);
151+
});

src/matchers/to-contain-element.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ export function toContainElement(
1010
container: ReactTestInstance,
1111
element: ReactTestInstance | null
1212
) {
13-
if (element !== null || !this.isNot) {
14-
checkHostElement(container, toContainElement, this);
13+
checkHostElement(container, toContainElement, this);
14+
15+
if (element !== null) {
16+
checkHostElement(element, toContainElement, this);
1517
}
1618

1719
let matches: ReactTestInstance[] = [];

0 commit comments

Comments
 (0)