Skip to content

Commit e6efd07

Browse files
gnapseKent C. Dodds
authored and
Kent C. Dodds
committed
chore: refactor custom matchers (#1)
* Remove unneeded if/else logic in custom matchers * Use common message format for all custom matchers * Check html element in toBeInTheDOM
1 parent f19e276 commit e6efd07

File tree

2 files changed

+59
-56
lines changed

2 files changed

+59
-56
lines changed

src/__tests__/element-queries.js

+4
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ test('using jest helpers to assert element states', () => {
114114
expect(() =>
115115
expect(queryByTestId('count-value')).not.toHaveTextContent('2'),
116116
).toThrowError()
117+
118+
expect(() =>
119+
expect({thisIsNot: 'an html element'}).toBeInTheDOM(),
120+
).toThrowError()
117121
})
118122

119123
test('using jest helpers to check element attributes', () => {

src/jest-extensions.js

+55-56
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
matcherHint,
3-
printReceived,
43
printExpected,
54
stringify,
65
RECEIVED_COLOR as receivedColor,
@@ -26,9 +25,19 @@ function checkHtmlElement(htmlElement) {
2625
}
2726
}
2827

29-
const assertMessage = (assertionName, message, received, expected) =>
30-
`${matcherHint(`${assertionName}`, 'received', '')} \n${message}: ` +
31-
`${printExpected(expected)} \nReceived: ${printReceived(received)}`
28+
function getMessage(
29+
matcher,
30+
expectedLabel,
31+
expectedValue,
32+
receivedLabel,
33+
receivedValue,
34+
) {
35+
return [
36+
`${matcher}\n`,
37+
`${expectedLabel}:\n ${expectedColor(expectedValue)}`,
38+
`${receivedLabel}:\n ${receivedColor(receivedValue)}`,
39+
].join('\n')
40+
}
3241

3342
function printAttribute(name, value) {
3443
return value === undefined ? name : `${name}=${stringify(value)}`
@@ -42,58 +51,47 @@ function getAttributeComment(name, value) {
4251

4352
const extensions = {
4453
toBeInTheDOM(received) {
45-
getDisplayName(received)
4654
if (received) {
47-
return {
48-
message: () =>
49-
`${matcherHint(
50-
'.not.toBeInTheDOM',
51-
'received',
52-
'',
53-
)} Expected the element not to be present` +
54-
`\nReceived : ${printReceived(received)}`,
55-
pass: true,
56-
}
57-
} else {
58-
return {
59-
message: () =>
60-
`${matcherHint(
61-
'.toBeInTheDOM',
62-
'received',
55+
checkHtmlElement(received)
56+
}
57+
return {
58+
pass: !!received,
59+
message: () => {
60+
const to = this.isNot ? 'not to' : 'to'
61+
return getMessage(
62+
matcherHint(
63+
`${this.isNot ? '.not' : ''}.toBeInTheDOM`,
64+
'element',
6365
'',
64-
)} Expected the element to be present` +
65-
`\nReceived : ${printReceived(received)}`,
66-
pass: false,
67-
}
66+
),
67+
'Expected',
68+
`element ${to} be present`,
69+
'Received',
70+
received,
71+
)
72+
},
6873
}
6974
},
7075

7176
toHaveTextContent(htmlElement, checkWith) {
7277
checkHtmlElement(htmlElement)
7378
const textContent = htmlElement.textContent
74-
const pass = matches(textContent, htmlElement, checkWith)
75-
if (pass) {
76-
return {
77-
message: () =>
78-
assertMessage(
79-
'.not.toHaveTextContent',
80-
'Expected value not equals to',
81-
htmlElement,
82-
checkWith,
83-
),
84-
pass: true,
85-
}
86-
} else {
87-
return {
88-
message: () =>
89-
assertMessage(
90-
'.toHaveTextContent',
91-
'Expected value equals to',
92-
htmlElement,
93-
checkWith,
79+
return {
80+
pass: matches(textContent, htmlElement, checkWith),
81+
message: () => {
82+
const to = this.isNot ? 'not to' : 'to'
83+
return getMessage(
84+
matcherHint(
85+
`${this.isNot ? '.not' : ''}.toHaveTextContent`,
86+
'element',
87+
'',
9488
),
95-
pass: false,
96-
}
89+
`Expected element ${to} have text content`,
90+
checkWith,
91+
'Received',
92+
textContent,
93+
)
94+
},
9795
}
9896
},
9997

@@ -108,14 +106,9 @@ const extensions = {
108106
: hasAttribute,
109107
message: () => {
110108
const to = this.isNot ? 'not to' : 'to'
111-
const receivedAttribute = receivedColor(
112-
hasAttribute
113-
? printAttribute(name, receivedValue)
114-
: 'attribute was not found',
115-
)
116-
const expectedMsg = `Expected the element ${to} have attribute:\n ${expectedColor(
117-
printAttribute(name, expectedValue),
118-
)}`
109+
const receivedAttribute = hasAttribute
110+
? printAttribute(name, receivedValue)
111+
: null
119112
const matcher = matcherHint(
120113
`${this.isNot ? '.not' : ''}.toHaveAttribute`,
121114
'element',
@@ -127,7 +120,13 @@ const extensions = {
127120
comment: getAttributeComment(name, expectedValue),
128121
},
129122
)
130-
return `${matcher}\n\n${expectedMsg}\nReceived:\n ${receivedAttribute}`
123+
return getMessage(
124+
matcher,
125+
`Expected the element ${to} have attribute`,
126+
printAttribute(name, expectedValue),
127+
'Received',
128+
receivedAttribute,
129+
)
131130
},
132131
}
133132
},

0 commit comments

Comments
 (0)