Skip to content

Commit a383a34

Browse files
committed
refactor: code review changes
1 parent 3d3b1b0 commit a383a34

File tree

2 files changed

+30
-44
lines changed

2 files changed

+30
-44
lines changed

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

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test('toContainElement() supports basic case', () => {
1717

1818
expect(() => expect(parent).not.toContainElement(child))
1919
.toThrowErrorMatchingInlineSnapshot(`
20-
"expect(element).not.toContainElement(element)
20+
"expect(container).not.toContainElement(element)
2121
2222
<View
2323
testID="parent"
@@ -44,10 +44,11 @@ test('toContainElement() supports negative case', () => {
4444
const view2 = screen.getByTestId('view2');
4545

4646
expect(view1).not.toContainElement(view2);
47+
expect(view2).not.toContainElement(view1);
4748

4849
expect(() => expect(view1).toContainElement(view2))
4950
.toThrowErrorMatchingInlineSnapshot(`
50-
"expect(element).toContainElement(element)
51+
"expect(container).toContainElement(element)
5152
5253
<View
5354
testID="view1"
@@ -62,7 +63,21 @@ test('toContainElement() supports negative case', () => {
6263
`);
6364
});
6465

65-
test('toContainElement() passing null', () => {
66+
test('toContainElement() handles null container', () => {
67+
render(<View testID="view" />);
68+
69+
const view = screen.getByTestId('view');
70+
71+
expect(() => expect(null).toContainElement(view))
72+
.toThrowErrorMatchingInlineSnapshot(`
73+
"expect(received).toContainElement()
74+
75+
received value must be a host element.
76+
Received has value: null"
77+
`);
78+
});
79+
80+
test('toContainElement() handles null element', () => {
6681
render(<View testID="view" />);
6782

6883
const view = screen.getByTestId('view');
@@ -71,7 +86,7 @@ test('toContainElement() passing null', () => {
7186

7287
expect(() => expect(view).toContainElement(null))
7388
.toThrowErrorMatchingInlineSnapshot(`
74-
"expect(element).toContainElement(element)
89+
"expect(container).toContainElement(element)
7590
7691
<View
7792
testID="view"
@@ -84,32 +99,18 @@ test('toContainElement() passing null', () => {
8499
`);
85100
});
86101

87-
test('toContainElement() on null elements', () => {
88-
render(<View testID="view" />);
89-
90-
const view = screen.getByTestId('view');
91-
92-
expect(() => expect(null).toContainElement(view))
93-
.toThrowErrorMatchingInlineSnapshot(`
94-
"expect(received).toContainElement()
95-
96-
received value must be a host element.
97-
Received has value: null"
98-
`);
99-
});
100-
101-
test('toContainElement() on non-React elements', () => {
102+
test('toContainElement() handles non-element container', () => {
102103
render(<View testID="view" />);
103104

104105
const view = screen.getByTestId('view');
105106

106-
expect(() => expect({ name: 'Non-React element' }).not.toContainElement(view))
107+
expect(() => expect({ name: 'non-element' }).not.toContainElement(view))
107108
.toThrowErrorMatchingInlineSnapshot(`
108109
"expect(received).not.toContainElement()
109110
110111
received value must be a host element.
111112
Received has type: object
112-
Received has value: {"name": "Non-React element"}"
113+
Received has value: {"name": "non-element"}"
113114
`);
114115

115116
expect(() => expect(true).not.toContainElement(view))
@@ -122,30 +123,19 @@ test('toContainElement() on non-React elements', () => {
122123
`);
123124
});
124125

125-
test('toContainElement() passing non-React element', () => {
126+
test('toContainElement() handles non-element element', () => {
126127
render(<View testID="view" />);
127128

128129
const view = screen.getByTestId('view');
129130

130131
expect(() =>
131132
// @ts-expect-error
132-
expect(view).not.toContainElement(true)
133+
expect(view).not.toContainElement({ name: 'non-element' })
133134
).toThrowErrorMatchingInlineSnapshot(`
134135
"expect(received).not.toContainElement()
135136
136137
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"
138+
Received has type: object
139+
Received has value: {"name": "non-element"}"
150140
`);
151141
});

src/matchers/to-contain-element.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { ReactTestInstance } from 'react-test-renderer';
2-
import {
3-
matcherHint,
4-
RECEIVED_COLOR as receivedColor,
5-
} from 'jest-matcher-utils';
2+
import { matcherHint, RECEIVED_COLOR } from 'jest-matcher-utils';
63
import { checkHostElement, formatElement } from './utils';
74

85
export function toContainElement(
@@ -17,22 +14,21 @@ export function toContainElement(
1714
}
1815

1916
let matches: ReactTestInstance[] = [];
20-
2117
if (element) {
2218
matches = container.findAll((node) => node === element);
2319
}
2420

2521
return {
26-
pass: Boolean(matches.length),
22+
pass: matches.length > 0,
2723
message: () => {
2824
return [
2925
matcherHint(
3026
`${this.isNot ? '.not' : ''}.toContainElement`,
31-
'element',
27+
'container',
3228
'element'
3329
),
3430
'',
35-
receivedColor(`${formatElement(container)} ${
31+
RECEIVED_COLOR(`${formatElement(container)} ${
3632
this.isNot ? '\n\ncontains:\n\n' : '\n\ndoes not contain:\n\n'
3733
} ${formatElement(element)}
3834
`),

0 commit comments

Comments
 (0)